Search in sources :

Example 6 with AWTEventListener

use of java.awt.event.AWTEventListener in project intellij-community by JetBrains.

the class DarculaTest method main.

public static void main(String[] args) {
    try {
        UIManager.setLookAndFeel(new DarculaLaf());
    } catch (UnsupportedLookAndFeelException ignored) {
    }
    final JFrame frame = new JFrame("Darcula Demo");
    frame.setSize(900, 500);
    final DarculaTest form = new DarculaTest();
    final JPanel root = form.myRoot;
    frame.setContentPane(root);
    frame.getRootPane().setDefaultButton(form.myDefaultButton);
    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {

        @Override
        public void eventDispatched(AWTEvent event) {
            if (event instanceof KeyEvent && event.getID() == KeyEvent.KEY_PRESSED && ((KeyEvent) event).getKeyCode() == KeyEvent.VK_F1) {
                new ShowUIDefaultsAction().actionPerformed(null);
            }
        }
    }, AWTEvent.KEY_EVENT_MASK);
    SwingUtilities.invokeLater(() -> frame.setVisible(true));
}
Also used : DarculaLaf(com.intellij.ide.ui.laf.darcula.DarculaLaf) KeyEvent(java.awt.event.KeyEvent) ShowUIDefaultsAction(com.intellij.ui.ShowUIDefaultsAction) AWTEventListener(java.awt.event.AWTEventListener)

Example 7 with AWTEventListener

use of java.awt.event.AWTEventListener in project intellij-community by JetBrains.

the class WindowSystemPlaybackCall method waitForDialog.

public static AsyncResult<String> waitForDialog(final PlaybackContext context, final String title) {
    final AsyncResult<String> result = new AsyncResult<>();
    final Ref<AWTEventListener> listener = new Ref<>();
    listener.set(new AWTEventListener() {

        @Override
        public void eventDispatched(AWTEvent event) {
            if (event.getID() == WindowEvent.WINDOW_ACTIVATED) {
                final Window wnd = ((WindowEvent) event).getWindow();
                if (wnd instanceof JDialog) {
                    if (title.equals(((JDialog) wnd).getTitle())) {
                        Toolkit.getDefaultToolkit().removeAWTEventListener(listener.get());
                        SwingUtilities.invokeLater(() -> getUiReady(context).notify(result));
                    }
                }
            }
        }
    });
    Toolkit.getDefaultToolkit().addAWTEventListener(listener.get(), WindowEvent.WINDOW_EVENT_MASK);
    SimpleTimer.getInstance().setUp(() -> {
        Toolkit.getDefaultToolkit().removeAWTEventListener(listener.get());
        if (!result.isProcessed()) {
            result.setRejected("Timed out waiting for window: " + title);
        }
    }, Registry.intValue("actionSystem.commandProcessingTimeout"));
    return result;
}
Also used : ToolWindow(com.intellij.openapi.wm.ToolWindow) Ref(com.intellij.openapi.util.Ref) AWTEventListener(java.awt.event.AWTEventListener) AsyncResult(com.intellij.openapi.util.AsyncResult)

Example 8 with AWTEventListener

use of java.awt.event.AWTEventListener in project ats-framework by Axway.

the class SwingElementLocator method useComponentInspector.

/**
     * Log the clicked element (its type, properties and index in the component hierarchy from the current container)
     *
     * @param driver {@link SwingDriverInternal} instance
     */
public static void useComponentInspector(final SwingDriverInternal driver) {
    // long eventMask = AWTEvent.MOUSE_MOTION_EVENT_MASK + AWTEvent.MOUSE_EVENT_MASK;
    long eventMask = AWTEvent.MOUSE_EVENT_MASK;
    Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {

        public void eventDispatched(AWTEvent e) {
            if (e.getID() == MouseEvent.MOUSE_PRESSED && e.getSource() instanceof Component) {
                Component component = (Component) e.getSource();
                Class<?> swingClass = findSwingClass(component.getClass());
                if (swingClass == null) {
                    swingClass = component.getClass();
                    log.warn("Can't find swing class of type \"" + swingClass.getName() + "\"");
                }
                String logEntry = "\t[INSPECTOR] " + getProperties(component).replaceFirst("\\[", " [") + " index=" + calculateIndex(component, swingClass);
                if (!component.getClass().getName().equals(swingClass.getName())) {
                    logEntry += "\t(extends " + swingClass.getName() + ")";
                }
                System.out.println(logEntry);
            }
        }

        private String getProperties(Component c) {
            String properties = Formatting.inEdtFormat(c);
            if (c instanceof JButton) {
                String tooltip = ((JButton) c).getToolTipText();
                if (!StringUtils.isNullOrEmpty(tooltip)) {
                    int lastBrIndex = properties.lastIndexOf(']');
                    if (lastBrIndex > 0) {
                        properties = properties.substring(0, lastBrIndex) + ", tooltip='" + tooltip + "'" + properties.substring(lastBrIndex);
                    } else {
                        return c.getClass().getName() + " [tooltip='" + tooltip + "']";
                    }
                }
            }
            return properties;
        }

        private Class<?> findSwingClass(Class<?> clazz) {
            if (clazz == null) {
                return null;
            }
            if (clazz.getName().startsWith("javax.swing")) {
                return clazz;
            }
            return findSwingClass(clazz.getSuperclass());
        }

        @SuppressWarnings("unchecked")
        private int calculateIndex(final Component component, Class<?> swingClass) {
            ContainerFixture<?> containerFixture = driver.getActiveContainerFixture();
            Robot robot = null;
            if (containerFixture != null) {
                // use the current robot instance
                robot = containerFixture.robot;
            } else {
                robot = BasicRobot.robotWithCurrentAwtHierarchy();
            }
            List<Component> found = SwingElementFinder.find(robot.hierarchy(), ((containerFixture != null) ? containerFixture.component() : null), new GenericTypeMatcher<Component>((Class<Component>) swingClass, true) {

                @Override
                protected boolean isMatching(Component c) {
                    return true;
                }
            });
            return found.indexOf(component);
        }
    }, eventMask);
}
Also used : AWTEventListener(java.awt.event.AWTEventListener) JButton(javax.swing.JButton) GenericTypeMatcher(org.fest.swing.core.GenericTypeMatcher) ContainerFixture(org.fest.swing.fixture.ContainerFixture) AWTEvent(java.awt.AWTEvent) List(java.util.List) JList(javax.swing.JList) Component(java.awt.Component) JTextComponent(javax.swing.text.JTextComponent) Robot(org.fest.swing.core.Robot) BasicRobot(org.fest.swing.core.BasicRobot)

Example 9 with AWTEventListener

use of java.awt.event.AWTEventListener in project intellij-community by JetBrains.

the class FocusTracesAction method actionPerformed.

@Override
public void actionPerformed(AnActionEvent e) {
    final Project project = e.getData(CommonDataKeys.PROJECT);
    final IdeFocusManager manager = IdeFocusManager.getGlobalInstance();
    if (!(manager instanceof FocusManagerImpl))
        return;
    final FocusManagerImpl focusManager = (FocusManagerImpl) manager;
    myActive = !myActive;
    if (myActive) {
        myFocusTracker = new AWTEventListener() {

            @Override
            public void eventDispatched(AWTEvent event) {
                if (event instanceof FocusEvent && event.getID() == FocusEvent.FOCUS_GAINED) {
                    focusManager.recordFocusRequest(((FocusEvent) event).getComponent(), false);
                }
            }
        };
        Toolkit.getDefaultToolkit().addAWTEventListener(myFocusTracker, AWTEvent.FOCUS_EVENT_MASK);
    }
    if (!myActive) {
        final List<FocusRequestInfo> requests = focusManager.getRequests();
        new FocusTracesDialog(project, new ArrayList<>(requests)).show();
        Toolkit.getDefaultToolkit().removeAWTEventListener(myFocusTracker);
        myFocusTracker = null;
        requests.clear();
    }
}
Also used : FocusManagerImpl(com.intellij.openapi.wm.impl.FocusManagerImpl) Project(com.intellij.openapi.project.Project) AWTEventListener(java.awt.event.AWTEventListener) IdeFocusManager(com.intellij.openapi.wm.IdeFocusManager) ArrayList(java.util.ArrayList) FocusEvent(java.awt.event.FocusEvent) FocusRequestInfo(com.intellij.openapi.wm.impl.FocusRequestInfo)

Example 10 with AWTEventListener

use of java.awt.event.AWTEventListener in project intellij-plugins by JetBrains.

the class UserActivityMonitor method initComponent.

@Override
public void initComponent() {
    if (ApplicationManager.getApplication().isUnitTestMode())
        return;
    KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(e -> {
        activity();
        return false;
    });
    AWTEventListener listener = new AWTEventListener() {

        @Override
        public void eventDispatched(AWTEvent event) {
            activity();
        }
    };
    Toolkit.getDefaultToolkit().addAWTEventListener(listener, AWTEvent.MOUSE_EVENT_MASK | AWTEvent.MOUSE_MOTION_EVENT_MASK);
    activity();
    ProjectUtil.runWhenProjectOpened(project -> {
        Thread t = new Thread(this, getComponentName() + " thread");
        t.setDaemon(true);
        t.start();
    });
}
Also used : AWTEventListener(java.awt.event.AWTEventListener)

Aggregations

AWTEventListener (java.awt.event.AWTEventListener)13 AWTEvent (java.awt.AWTEvent)4 KeyEvent (java.awt.event.KeyEvent)3 AWTEventListenerProxy (java.awt.event.AWTEventListenerProxy)2 JButton (javax.swing.JButton)2 JTextComponent (javax.swing.text.JTextComponent)2 SplashWindow (apps.SplashWindow)1 DnDAware (com.intellij.ide.dnd.DnDAware)1 DarculaLaf (com.intellij.ide.ui.laf.darcula.DarculaLaf)1 Disposable (com.intellij.openapi.Disposable)1 Project (com.intellij.openapi.project.Project)1 AsyncResult (com.intellij.openapi.util.AsyncResult)1 Ref (com.intellij.openapi.util.Ref)1 IdeFocusManager (com.intellij.openapi.wm.IdeFocusManager)1 IdeGlassPane (com.intellij.openapi.wm.IdeGlassPane)1 ToolWindow (com.intellij.openapi.wm.ToolWindow)1 FocusManagerImpl (com.intellij.openapi.wm.impl.FocusManagerImpl)1 FocusRequestInfo (com.intellij.openapi.wm.impl.FocusRequestInfo)1 ShowUIDefaultsAction (com.intellij.ui.ShowUIDefaultsAction)1 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)1