Example usage for java.awt AWTEvent getSource

List of usage examples for java.awt AWTEvent getSource

Introduction

In this page you can find the example usage for java.awt AWTEvent getSource.

Prototype

public Object getSource() 

Source Link

Document

The object on which the Event initially occurred.

Usage

From source file:Main.java

/**
 * Tries to find the owning window for the AWT-event's source.
 * //from   w w  w . j  ava2  s.  com
 * @param aEvent
 *          the AWT event to find the owning window for, may be
 *          <code>null</code>.
 * @return the owning window, or <code>null</code> if no such window could be
 *         found, or a <code>null</code> event was given.
 */
public static final Window getOwningWindow(final AWTEvent aEvent) {
    Window owner = null;
    if (aEvent != null) {
        final Object source = aEvent.getSource();
        if (source instanceof Component) {
            owner = getOwningWindow((Component) source);
        }
    }
    return owner;
}

From source file:Main.java

private static void dispatchEventsUntilNow() {
    if (java.awt.EventQueue.isDispatchThread() && !isDispatching) {
        isDispatching = true;//from  w  ww. j a va2s .co  m
        try {
            int handled = 0;
            java.awt.EventQueue eventQ = java.awt.Toolkit.getDefaultToolkit().getSystemEventQueue();
            class StopEvent extends AWTEvent {
                StopEvent() {
                    super(new Object(), 0);
                }
            }
            eventQ.postEvent(new StopEvent());
            while (true) {
                try {
                    AWTEvent event = eventQ.getNextEvent();
                    if (event instanceof StopEvent) {
                        break;
                    }
                    if (event instanceof ActiveEvent) {
                        ((ActiveEvent) event).dispatch();
                    } else {
                        Object source = event.getSource();
                        if (source instanceof Component) {
                            Component comp = (Component) source;
                            comp.dispatchEvent(event);
                        }
                    }
                    handled++;
                } catch (InterruptedException e) {
                    //hmm
                }
            }
            //            Debug.trace("####dispatchEventsUntilNow handeld "+(handled)+" events");
        } finally {
            isDispatching = false;
        }
    }
}

From source file:Main.java

public Main() {
    label = new JLabel("Waiting...");
    frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(label);//  w  ww .  j a va  2s  .c  o m
    frame.setSize(200, 200);
    frame.setVisible(true);

    Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {
        int count;

        @Override
        public void eventDispatched(AWTEvent event) {
            Object source = event.getSource();
            if (source instanceof Component) {
                Component comp = (Component) source;
                Window win = null;
                if (comp instanceof Window) {
                    win = (Window) comp;
                } else {
                    win = SwingUtilities.windowForComponent(comp);
                }
                if (win == frame) {
                    timer.restart();
                    label.setText("Interrupted..." + (++count));
                }
            }
        }
    }, AWTEvent.KEY_EVENT_MASK | AWTEvent.MOUSE_EVENT_MASK | AWTEvent.MOUSE_MOTION_EVENT_MASK
            | AWTEvent.MOUSE_WHEEL_EVENT_MASK);

    timer = new Timer(5000, new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            frame.dispose();
        }
    });
    timer.start();
}

From source file:gdsc.smlm.ij.plugins.PSFCalculator.java

public boolean dialogItemChanged(GenericDialog gd, AWTEvent e) {
    if (e == null)
        return true;
    Object o = e.getSource();
    if (o == sdNmText || o == sdPixelsText || o == fwhmPixelsText)
        return true;
    if (widthNmText != null) {
        if (o == pixelPitchLabel || o == widthNmText || o == widthPixelsText)
            return true;
    }/*  w w  w  . ja v  a  2s.  c o m*/

    if (aquireLock()) {
        try {
            // Continue while the parameter is changing
            boolean parametersChanged = true;
            while (parametersChanged) {
                if (!readDialog())
                    return false;

                // Store the parameters to be processed
                double pixelPitch = settings.pixelPitch;
                double magnification = settings.magnification;
                double beamExpander = settings.beamExpander;
                double wavelength = settings.wavelength;
                double numericalAperture = settings.numericalAperture;
                boolean adjustForSquarePixels = settings.adjustForSquarePixels;
                double proportionalityFactor = settings.proportionalityFactor;

                // Do something with parameters
                if (widthNmText != null) {
                    pixelPitchLabel.setText(getPixelPitchLabel());
                    widthNmText.setText(IJ.d2s(calculateAiryWidth(wavelength, numericalAperture), 3));
                    widthPixelsText.setText(IJ.d2s(calculateAiryWidth(pixelPitch, magnification * beamExpander,
                            wavelength, numericalAperture), 3));
                }
                sdNmText.setText(
                        IJ.d2s(calculateStdDev(wavelength, numericalAperture, proportionalityFactor), 3));
                double sd = calculateStdDev(pixelPitch, magnification * beamExpander, wavelength,
                        numericalAperture, proportionalityFactor, adjustForSquarePixels);
                sdPixelsText.setText(IJ.d2s(sd, 3));
                fwhmPixelsText.setText(IJ.d2s(sd * SD_TO_HWHM_FACTOR, 3));

                double s = calculateStdDev(pixelPitch, magnification * beamExpander, wavelength,
                        numericalAperture, 1, false);
                plotProfile(calculateAiryWidth(pixelPitch, magnification * beamExpander, wavelength,
                        numericalAperture), sd / s);

                // Check if the parameters have changed again
                parametersChanged = (pixelPitch != settings.pixelPitch)
                        || (magnification != settings.magnification) || (beamExpander != settings.beamExpander)
                        || (wavelength != settings.wavelength)
                        || (numericalAperture != settings.numericalAperture)
                        || (proportionalityFactor != settings.proportionalityFactor);
            }
        } finally {
            // Ensure the running flag is reset
            lock = false;
        }
    }

    return true;
}

From source file:edu.ku.brc.ui.dnd.SimpleGlassPane.java

/**
 * Receives all key events in the AWT and processes the ones that originated from the current
 * window with the glass pane./*  w w w.  j av a  2  s  .  c om*/
 *
 * @param event the AWTEvent that was fired
 */
public void eventDispatched(AWTEvent event) {
    //Object source = event.getSource();

    boolean srcIsComp = (event.getSource() instanceof Component);

    if ((event instanceof KeyEvent) && srcIsComp) {
        if (frame == null) {
            Component p = getParent();
            while (frame == null && p != null) {
                if (p instanceof JFrame) {
                    frame = (JFrame) p;
                }
                p = p.getParent();
            }
        }

        // If the event originated from the window w/glass pane, consume the event
        //if ((SwingUtilities.windowForComponent((Component) source) == frame))
        {
            ((KeyEvent) event).consume();
            //Toolkit.getDefaultToolkit().beep();
        }
    }
}

From source file:net.sourceforge.squirrel_sql.fw.gui.debug.DebugEventListener.java

public void eventDispatched(AWTEvent event) {
    Object o = event.getSource();
    if (o instanceof JComponent && o != null) {
        JComponent source = (JComponent) o;
        switch (event.getID()) {
        case MouseEvent.MOUSE_DRAGGED:
            printDebugInfo(source, event);
            break;
        case MouseEvent.MOUSE_ENTERED:
            printDebugInfo(source, event);
            setToolTipText(source, event);
            setBorder(source, event);//from   w w w.ja  v a  2  s .c o m
            break;
        case MouseEvent.MOUSE_EXITED:
            printDebugInfo(source, event);
            setBorder(source, event);
            break;
        }
    }
}

From source file:org.tinymediamanager.ui.MainWindow.java

/**
 * Initialize the contents of the frame.
 *//*  w  ww. ja  v a  2 s .  c o m*/
private void initialize() {
    // set the logo
    setIconImages(LOGOS);
    setBounds(5, 5, 1100, 727);
    // do nothing, we have our own windowClosing() listener
    // setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
    getContentPane().setLayout(new BorderLayout(0, 0));

    JLayeredPane content = new JLayeredPane();
    content.setLayout(new FormLayout(
            new ColumnSpec[] { ColumnSpec.decode("default:grow"), FormFactory.RELATED_GAP_COLSPEC,
                    ColumnSpec.decode("right:270px"), },
            new RowSpec[] { RowSpec.decode("fill:max(500px;default):grow"), }));
    getContentPane().add(content, BorderLayout.CENTER);

    JPanel mainPanel = new JPanel();
    mainPanel.setLayout(new FormLayout(new ColumnSpec[] { ColumnSpec.decode("default:grow") },
            new RowSpec[] { RowSpec.decode("fill:max(500px;default):grow") }));
    content.add(mainPanel, "1, 1, 3, 1, fill, fill");
    content.setLayer(mainPanel, 1);

    JTabbedPane tabbedPane = VerticalTextIcon.createTabbedPane(JTabbedPane.LEFT);
    tabbedPane.setTabPlacement(JTabbedPane.LEFT);
    mainPanel.add(tabbedPane, "1, 1, fill, fill");
    // getContentPane().add(tabbedPane, "1, 2, fill, fill");

    panelStatusBar = new StatusBar();
    getContentPane().add(panelStatusBar, BorderLayout.SOUTH);

    panelMovies = new MoviePanel();
    VerticalTextIcon.addTab(tabbedPane, BUNDLE.getString("tmm.movies"), panelMovies); //$NON-NLS-1$

    panelMovieSets = new MovieSetPanel();
    VerticalTextIcon.addTab(tabbedPane, BUNDLE.getString("tmm.moviesets"), panelMovieSets); //$NON-NLS-1$

    panelTvShows = new TvShowPanel();
    VerticalTextIcon.addTab(tabbedPane, BUNDLE.getString("tmm.tvshows"), panelTvShows); //$NON-NLS-1$

    // shutdown listener - to clean database connections safely
    addWindowListener(new WindowAdapter() {
        @Override
        public void windowClosing(WindowEvent e) {
            closeTmm();
        }
    });

    MessageManager.instance.addListener(TmmUIMessageCollector.instance);

    // mouse event listener for context menu
    Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {
        @Override
        public void eventDispatched(AWTEvent arg0) {
            if (arg0 instanceof MouseEvent && MouseEvent.MOUSE_RELEASED == arg0.getID()
                    && arg0.getSource() instanceof JTextComponent) {
                MouseEvent me = (MouseEvent) arg0;
                JTextComponent tc = (JTextComponent) arg0.getSource();
                if (me.isPopupTrigger() && tc.getComponentPopupMenu() == null) {
                    TextFieldPopupMenu.buildCutCopyPaste().show(tc, me.getX(), me.getY());
                }
            }
        }
    }, AWTEvent.MOUSE_EVENT_MASK);

    // temp info for users using Java 6
    if (SystemUtils.IS_JAVA_1_6) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                JOptionPane.showMessageDialog(MainWindow.this, BUNDLE.getString("tmm.java6")); //$NON-NLS-1$
            }
        });
    }

    // inform user is MI could not be loaded
    if (Platform.isLinux() && StringUtils.isBlank(MediaInfo.version())) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                JOptionPane.showMessageDialog(MainWindow.this, BUNDLE.getString("mediainfo.failed.linux")); //$NON-NLS-1$
            }
        });
    }
}