From 9eba948b02be9ab7bedbff83b63fda1580ab0d46 Mon Sep 17 00:00:00 2001 From: rusefi Date: Tue, 21 Apr 2020 01:18:07 -0400 Subject: [PATCH] it mostly works! --- .../screen/src/ScreenGenerator.java | 176 +++++++++++++++++- 1 file changed, 174 insertions(+), 2 deletions(-) diff --git a/java_tools/ts_screenshots/screen/src/ScreenGenerator.java b/java_tools/ts_screenshots/screen/src/ScreenGenerator.java index b19bde11e9..7425490902 100644 --- a/java_tools/ts_screenshots/screen/src/ScreenGenerator.java +++ b/java_tools/ts_screenshots/screen/src/ScreenGenerator.java @@ -1,5 +1,177 @@ + +import javax.imageio.ImageIO; +import javax.swing.*; +import java.awt.*; +import java.awt.image.BufferedImage; +import java.io.File; +import java.io.IOException; +import java.lang.reflect.InvocationTargetException; +import java.util.ArrayList; + public class ScreenGenerator { - public static void main(String[] args) { - TunerStudio.main(args); + public static final String TS_DIALOG = "com.efiAnalytics.ui.dg"; + public static final String PNG = "png"; + private static ArrayList topLevelButtons = new ArrayList<>(); + + + private static final String DESTINATION = "images" + File.separator; + + public static void main(String[] args) throws InterruptedException, InvocationTargetException, IOException, AWTException { + new File(DESTINATION).mkdirs(); + + Thread t = new Thread(() -> TunerStudio.main(args)); + + t.setDaemon(false); + t.start(); + + + Frame mainFrame = findMainFrame(); + + while (topLevelButtons.isEmpty()) { + visitComponent(mainFrame, null, "", (parent, component) -> { + if (component instanceof AbstractButton) { + AbstractButton ab = (AbstractButton) component; + System.out.println("topLevelButton " + ab.getText()); + + if (isTopLevelMenuButton(component)) { + topLevelButtons.add(ab); + } + } + }); + Thread.sleep(1000); + } + + System.out.println("Done discovering buttons, " + topLevelButtons.size()); + + doJob(mainFrame); + } + + private static Frame findMainFrame() throws InterruptedException { + while (true) { + Frame[] all = JFrame.getFrames(); + + for (Frame frame : all) { + System.out.println("I see " + frame.getTitle()); + + if (frame.getTitle().contains("TunerStudio")) { + return frame; + } + } + Thread.sleep(1000); + } + } + + interface Callback { + void onComponent(Component parent, Component component); + } + + public static void visitComponent(Component cmp, Component parent, String prefix, Callback callback) { + System.out.println(prefix + " I see " + cmp.getClass()); + callback.onComponent(parent, cmp); + Container container = (Container) cmp; + if (container == null) { + // Not a container, return + return; + } + // Go visit and add all children + for (Component subComponent : container.getComponents()) { + visitComponent(subComponent, cmp, prefix + " " + subComponent.getClass().getSimpleName(), callback); + } + } + + private static boolean isTopLevelMenuButton(Component cmp) { + return cmp instanceof bi.b; + } + + private static void doJob(Frame frame) throws InterruptedException, InvocationTargetException, IOException, AWTException { + printAllDialogs("Dialogs before clicking ", JDialog.getWindows()); + + + for (AbstractButton topLevel : topLevelButtons) { + SwingUtilities.invokeAndWait(topLevel::doClick); + Thread.sleep(500); + + ImageIO.write( + getScreenShot(frame), + "png", + new File(DESTINATION + topLevel.getText() + ".png")); + + printAllDialogs("dialogs after clicking ", JDialog.getWindows()); + java.util.List menuItems = new ArrayList<>(); + visitComponent(frame, null, "just clicked ", (parent, component) -> { + if (component instanceof JMenuItem && component.getClass().getName().endsWith("aH.gc")) { + JMenuItem menuItem = (JMenuItem) component; + System.out.println("Menu item " + menuItem.getText()); + menuItems.add(menuItem); + } + }); + + + for (JMenuItem m : menuItems) { + + //d.getLocation() + + SwingUtilities.invokeAndWait(new Runnable() { + @Override + public void run() { + try { + + m.doClick(); + Thread.sleep(500); + + JDialog d = findDialog(); + + Robot robot = new Robot(); + Rectangle captureRect = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()); + BufferedImage screenFullImage = robot.createScreenCapture(captureRect); + ImageIO.write(screenFullImage, PNG, new File(DESTINATION + "full_" + d.getTitle() + ".png")); + + ImageIO.write( + getScreenShot(d), + PNG, + new File(DESTINATION + d.getTitle() + ".png")); + Thread.sleep(100); + d.setVisible(false); + d.dispose(); + } catch (IOException | InterruptedException | AWTException e) { + throw new IllegalStateException(e); + } + } + }); + + } + + printAllDialogs("total frames now = ", JFrame.getFrames()); + + Thread.sleep(1000); + } + } + + private static JDialog findDialog() { + for (Window d : Dialog.getWindows()) { + if (d.getClass().getName().equals(TS_DIALOG) && d.isVisible()) { + return (JDialog) d; + } + } + throw new IllegalStateException("Not found"); + } + + private static void printAllDialogs(String message, Window[] windows) { + System.out.println(message + windows.length); + for (Window window : windows) + System.out.println("type " + window.getClass()); + } + + public static BufferedImage getScreenShot(Component component) { + + BufferedImage image = new BufferedImage( + component.getWidth(), + component.getHeight(), + BufferedImage.TYPE_INT_RGB + ); + // call the Component's paint method, using + // the Graphics object of the image. + component.paint(image.getGraphics()); // alternately use .printAll(..) + return image; } }