/** * Returns the ancestor {@link Window} of this {@code Panel} or * {@code null} if the component is not (yet) placed in a * {@code Window}. */ public @Nullable Window getAncestorWindow() { return getAncestorWindow(this); }
@Before public void setUp() { instance = new EnhancedPanel(); instance.add(new JLabel("Hello world!")); }
keyFilePanel.setBorder(javax.swing.BorderFactory.createEmptyBorder(10, 10, 10, 10)); keyFilePanel.addPanelListener(new de.schlichtherle.truezip.swing.PanelListener() { public void ancestorWindowShown(de.schlichtherle.truezip.swing.PanelEvent evt) { keyFilePanelAncestorWindowShown(evt); keyFilePanel.setLayout(new java.awt.GridBagLayout()); gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; gridBagConstraints.insets = new java.awt.Insets(0, 0, 5, 0); keyFilePanel.add(keyFileLabel, gridBagConstraints); gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; gridBagConstraints.weightx = 1.0; keyFilePanel.add(keyFile, gridBagConstraints); gridBagConstraints.gridy = 1; gridBagConstraints.insets = new java.awt.Insets(0, 10, 0, 0); keyFilePanel.add(keyFileChooser, gridBagConstraints);
/** * Calls {@link #fireAncestorWindowShown} or * {@link #fireAncestorWindowHidden}, depending on the ID of the given * {@code event}. */ protected void processPanelEvent(final PanelEvent event) { switch (event.getID()) { case PanelEvent.ANCESTOR_WINDOW_SHOWN: fireAncestorWindowShown(event); break; case PanelEvent.ANCESTOR_WINDOW_HIDDEN: fireAncestorWindowHidden(event); break; default: throw new AssertionError(); } }
@Override public void hierarchyChanged(final HierarchyEvent e) { if ((e.getChangeFlags() & HierarchyEvent.SHOWING_CHANGED) != HierarchyEvent.SHOWING_CHANGED) return; final Window window = getAncestorWindow(); assert null != window : "A showing panel must have a containing window!"; final boolean windowShown = window.isShowing(); if (windowShown != isShowing()) return; processPanelEvent(new PanelEvent(EnhancedPanel.this, windowShown ? PanelEvent.ANCESTOR_WINDOW_SHOWN : PanelEvent.ANCESTOR_WINDOW_HIDDEN)); } } // class EnhancedPanelHierarchyListener
java.awt.GridBagConstraints gridBagConstraints; passwdPanel = new de.schlichtherle.truezip.swing.EnhancedPanel(); newPasswd1Label = new javax.swing.JLabel(); newPasswd1Field = new javax.swing.JPasswordField(); passwdPanel.addPanelListener(new de.schlichtherle.truezip.swing.PanelListener() { public void ancestorWindowShown(de.schlichtherle.truezip.swing.PanelEvent evt) { passwdPanelAncestorWindowShown(evt); passwdPanel.setLayout(new java.awt.GridBagLayout()); gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; gridBagConstraints.insets = new java.awt.Insets(0, 0, 5, 5); passwdPanel.add(newPasswd1Label, gridBagConstraints); gridBagConstraints.weightx = 1.0; gridBagConstraints.insets = new java.awt.Insets(0, 0, 5, 0); passwdPanel.add(newPasswd1Field, gridBagConstraints); gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 5); passwdPanel.add(newPasswd2Label, gridBagConstraints); gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; gridBagConstraints.weightx = 1.0; passwdPanel.add(newPasswd2Field, gridBagConstraints);
listeners = instance.getListeners(PanelListener.class); assertEquals(0, listeners.length); instance.addPanelListener(listener); fail(); } catch (NullPointerException expected) { listeners = instance.getListeners(PanelListener.class); assertEquals(0, listeners.length); instance.removePanelListener(listener); fail(); } catch (NullPointerException expected) { listeners = instance.getListeners(PanelListener.class); assertEquals(0, listeners.length); instance.addPanelListener(listener); listeners = instance.getListeners(PanelListener.class); assertEquals(1, listeners.length); assertSame(listener, listeners[0]); instance.addPanelListener(listener); listeners = instance.getListeners(PanelListener.class); assertEquals(2, listeners.length); assertSame(listener, listeners[0]); assertSame(listener, listeners[1]); instance.removePanelListener(listener);
@Test public void testFireAncestorWindowShown() { final CountingPanelListener l = new CountingPanelListener(); instance.addPanelListener(l); instance.addPanelListener(l); // add again to receive same event twice! assertEquals(0, l.shown); PanelEvent event = null; instance.fireAncestorWindowShown(event); assertEquals(2, l.shown); }
@Test public void testFireAncestorWindowHidden() { final CountingPanelListener l = new CountingPanelListener(); instance.addPanelListener(l); instance.addPanelListener(l); // add again to receive same event twice! assertEquals(0, l.hidden); PanelEvent event = null; instance.fireAncestorWindowHidden(event); assertEquals(2, l.hidden); }
listeners = instance.getPanelListeners(); assertEquals(0, listeners.length); instance.addPanelListener(listener); fail(); } catch (NullPointerException expected) { listeners = instance.getPanelListeners(); assertEquals(0, listeners.length); instance.removePanelListener(listener); fail(); } catch (NullPointerException expected) { listeners = instance.getPanelListeners(); assertEquals(0, listeners.length); instance.addPanelListener(listener); listeners = instance.getPanelListeners(); assertEquals(1, listeners.length); assertSame(listener, listeners[0]); instance.addPanelListener(listener); listeners = instance.getPanelListeners(); assertEquals(2, listeners.length); assertSame(listener, listeners[0]); assertSame(listener, listeners[1]); instance.removePanelListener(listener);
public void testEvents4JFrame() throws Exception { final CountingPanelListener l = new CountingPanelListener(); instance.addPanelListener(l);
/** * Returns an array of all the panel listeners * registered on this component. * * @return All of this panel's {@code PanelListener}s or an empty * array if no panel listeners are currently registered. * * @see #addPanelListener * @see #removePanelListener */ public PanelListener[] getPanelListeners() { return getListeners(PanelListener.class); }
java.awt.GridBagConstraints gridBagConstraints; passwdPanel = new de.schlichtherle.truezip.swing.EnhancedPanel(); passwdLabel = new javax.swing.JLabel(); passwdField = new javax.swing.JPasswordField(); error = new javax.swing.JLabel(); passwdPanel.addPanelListener(new de.schlichtherle.truezip.swing.PanelListener() { public void ancestorWindowShown(de.schlichtherle.truezip.swing.PanelEvent evt) { passwdPanelAncestorWindowShown(evt); passwdPanel.setLayout(new java.awt.GridBagLayout()); gridBagConstraints.gridy = 0; gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 5); passwdPanel.add(passwdLabel, gridBagConstraints); gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; gridBagConstraints.weightx = 1.0; passwdPanel.add(passwdField, gridBagConstraints);
/** * Shows and hides the EnhancedPanel instance several times in a * JOptionPane and counts these events. */ @Test public void testEvents4JOptionPane() throws Exception { final CountingPanelListener l = new CountingPanelListener(); instance.addPanelListener(l); final String title = EnhancedPanelIT.class.getSimpleName(); final Runnable makeVisible = new Runnable() { @Override public void run() { JOptionPane.showMessageDialog( null, instance, title, JOptionPane.INFORMATION_MESSAGE); } }; for (int i = 1; i <= 3; i++) { runOnEdt(makeVisible); final JDialogOperator dialog = new JDialogOperator(title); // wait for JOptionPane assertEquals(i, l.shown); assertEquals(i - 1, l.hidden); final JButtonOperator button = new JButtonOperator(dialog); button.push(); button.getQueueTool().waitEmpty(WAIT_EMPTY); assertEquals(i, l.shown); assertEquals(i, l.hidden); } }
final Window window = evt.getSource().getAncestorWindow(); window.addWindowFocusListener(new WindowFocusListener() { @Override
final Window window = evt.getSource().getAncestorWindow(); window.addWindowFocusListener(new WindowFocusListener() { @Override
final Window window = evt.getSource().getAncestorWindow(); window.addWindowFocusListener(new WindowFocusListener() { @Override
@Test public void testGetAncestorWindow() throws InterruptedException { assertNull(instance.getAncestorWindow()); JFrameOperator frame = showFrameWith(instance); Window window1 = instance.getAncestorWindow(); assertSame(frame.getSource(), window1); frame.dispose(); frame = showFrameWith(instance); // change enclosing frame Window window2 = instance.getAncestorWindow(); assertSame(frame.getSource(), window2); frame.dispose(); assertNotSame(window1, window2); }