import java.applet.Applet; import java.awt.Component; import java.awt.event.ComponentEvent; import java.awt.event.ComponentListener; import java.awt.event.ContainerEvent; import java.awt.event.ContainerListener; import java.awt.event.HierarchyBoundsListener; import java.awt.event.HierarchyEvent; import java.awt.event.HierarchyListener; import java.awt.event.InputMethodEvent; import java.awt.event.InputMethodListener; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.event.MouseMotionListener; /** * Tests listener events on parent. This applet should * be notified of all events on the parent, if it has * a corresponding listener attached. * * @author langel at redhat dot com */ public class TestAppletListeners extends Applet implements ContainerListener, ComponentListener, MouseListener, MouseMotionListener, InputMethodListener, HierarchyListener, HierarchyBoundsListener { boolean compAdded; boolean compRemoved; boolean compResized; boolean compMoved; boolean compShown; boolean compHidden; boolean mouseClicked; boolean mousePressed; boolean mouseReleased; boolean mouseEntered; boolean mouseExited; boolean mouseDragged; boolean mouseMoved; boolean inputMethodTextChanged; boolean caretPositionChanged; boolean hierarchyChanged; boolean ancestorMoved; boolean ancestorResized; public void init() { addContainerListener(this); addComponentListener(this); addMouseListener(this); addMouseMotionListener(this); addInputMethodListener(this); addHierarchyListener(this); addHierarchyBoundsListener(this); triggerEvents(); } void triggerEvents() { Component p = getParent(); if (p != null) { dispatchEvent(new ComponentEvent(p, ComponentEvent.COMPONENT_MOVED)); if (!compMoved) System.err.println("componentMoved not called!"); dispatchEvent(new ComponentEvent(p, ComponentEvent.COMPONENT_RESIZED)); if (!compResized) System.err.println("componentResized not called!"); dispatchEvent(new ComponentEvent(p, ComponentEvent.COMPONENT_SHOWN)); if (!compShown) System.err.println("componentShown not called!"); dispatchEvent(new ComponentEvent(p, ComponentEvent.COMPONENT_HIDDEN)); if (!compHidden) System.err.println("componentHidden not called!"); dispatchEvent(new HierarchyEvent(p, HierarchyEvent.HIERARCHY_CHANGED, null, null)); if (!hierarchyChanged) System.err.println("hierarchyChanged not called!"); dispatchEvent(new HierarchyEvent(p, HierarchyEvent.ANCESTOR_MOVED, null, null)); if (!ancestorMoved) System.err.println("ancestorMoved not called!"); dispatchEvent(new HierarchyEvent(p, HierarchyEvent.ANCESTOR_RESIZED, null, null)); if (!ancestorResized) System.err.println("ancestorResized not called!"); dispatchEvent(new InputMethodEvent(p, InputMethodEvent.INPUT_METHOD_TEXT_CHANGED, 0, null, 0, null, null)); if (!inputMethodTextChanged) System.err.println("inputMethodTextChanged not called!"); dispatchEvent(new InputMethodEvent(p, InputMethodEvent.CARET_POSITION_CHANGED, 0, null, 0, null, null)); if (!caretPositionChanged) System.err.println("caretPositionChanged not called!"); dispatchEvent(new MouseEvent(p, MouseEvent.MOUSE_DRAGGED, 0, 0, 0, 0, 1, false, 0)); if (!mouseDragged) System.err.println("mouseDragged not called!"); dispatchEvent(new MouseEvent(p, MouseEvent.MOUSE_MOVED, 0, 0, 0, 0, 1, false, 0)); if (!mouseMoved) System.err.println("mouseMoved not called!"); dispatchEvent(new MouseEvent(p, MouseEvent.MOUSE_CLICKED, 0, 0, 0, 0, 1, false, 0)); if (!mouseClicked) System.err.println("mouseClicked not called!"); dispatchEvent(new MouseEvent(p, MouseEvent.MOUSE_PRESSED, 0, 0, 0, 0, 1, false, 0)); if (!mousePressed) System.err.println("mousePressed not called!"); dispatchEvent(new MouseEvent(p, MouseEvent.MOUSE_RELEASED, 0, 0, 0, 0, 1, false, 0)); if (!mouseReleased) System.err.println("mouseReleased not called!"); dispatchEvent(new MouseEvent(p, MouseEvent.MOUSE_MOVED, 0, 0, 0, 0, 1, false, 0)); if (!mouseMoved) System.err.println("mouseMoved not called!"); dispatchEvent(new MouseEvent(p, MouseEvent.MOUSE_ENTERED, 0, 0, 0, 0, 1, false, 0)); if (!mouseEntered) System.err.println("mouseEntered not called!"); dispatchEvent(new MouseEvent(p, MouseEvent.MOUSE_EXITED, 0, 0, 0, 0, 1, false, 0)); if (!mouseExited) System.err.println("mouseExited not called!"); dispatchEvent(new ContainerEvent(p, ContainerEvent.COMPONENT_ADDED, null)); if (!compAdded) System.err.println("compAdded not called!"); dispatchEvent(new ContainerEvent(p, ContainerEvent.COMPONENT_REMOVED, null)); if (!compRemoved) System.err.println("compRemoved not called!"); } } // ///////////////////////////////// /// ContainerListener Methods ///// /////////////////////////////////// /** * This method is called when a component is added to the container. * * @param event the ContainerEvent indicating component * addition */ public void componentAdded(ContainerEvent event) { compAdded = true; } /** * This method is called when a component is removed from the container. * * @param event the ContainerEvent indicating component removal */ public void componentRemoved(ContainerEvent event) { compRemoved = true; } /////////////////////////////////// /// ComponentListener Methods ///// /////////////////////////////////// /** * This method is called when the component is resized. * * @param event the ComponentEvent indicating the resize */ public void componentResized(ComponentEvent event) { compResized = true; } /** * This method is called when the component is moved. * * @param event the ComponentEvent indicating the move */ public void componentMoved(ComponentEvent event) { compMoved = true; } /** * This method is called when the component is made visible. * * @param event the ComponentEvent indicating the visibility */ public void componentShown(ComponentEvent event) { compShown = true; } /** * This method is called when the component is hidden. * * @param event the ComponentEvent indicating the visibility */ public void componentHidden(ComponentEvent event) { compHidden = true; } /////////////////////////////////// ////// MouseListener Methods ////// /////////////////////////////////// /** * This method is called when the mouse is clicked (pressed and released * in short succession) on a component. * * @param event the MouseEvent indicating the click */ public void mouseClicked(MouseEvent event) { mouseClicked = true; } /** * This method is called when the mouse is pressed over a component. * * @param event the MouseEvent for the press */ public void mousePressed(MouseEvent event) { mousePressed = true; } /** * This method is called when the mouse is released over a component. * * @param event the MouseEvent for the release */ public void mouseReleased(MouseEvent event) { mouseReleased = true; } /** * This method is called when the mouse enters a component. * * @param event the MouseEvent for the entry */ public void mouseEntered(MouseEvent event) { mouseEntered = true; } /** * This method is called when the mouse exits a component. * * @param event the MouseEvent for the exit */ public void mouseExited(MouseEvent event) { mouseExited = true; } /////////////////////////////////// /// MouseMotionListener Methods /// /////////////////////////////////// /** * This method is called when the mouse is moved over a component * while a button has been pressed. * * @param event the MouseEvent indicating the motion */ public void mouseDragged(MouseEvent event) { mouseDragged = true; } /** * This method is called when the mouse is moved over a component * while no button is pressed. * * @param event the MouseEvent indicating the motion */ public void mouseMoved(MouseEvent event) { mouseMoved = true; } /////////////////////////////////// /// InputMethodListener Methods /// /////////////////////////////////// /** * This method is called when the text is changed. * * @param event the InputMethodEvent indicating the text change */ public void inputMethodTextChanged(InputMethodEvent event) { inputMethodTextChanged = true; } /** * This method is called when the cursor position within the text is changed. * * @param event the InputMethodEvent indicating the change */ public void caretPositionChanged(InputMethodEvent event) { caretPositionChanged = true; } /////////////////////////////////// //// HierarchyListener Methods //// /////////////////////////////////// /** * Called when the hierarchy of this component changes. Use * getChangeFlags() on the event to see what exactly changed. * * @param e the event describing the change */ public void hierarchyChanged(HierarchyEvent event) { hierarchyChanged = true; } ///////////////////////////////////////// //// HierarchyBoundsListener Methods //// ///////////////////////////////////////// /** * Called when an ancestor component of the source is moved. * * @param e the event describing the ancestor's motion */ public void ancestorMoved(HierarchyEvent e) { ancestorMoved = true; } /** * Called when an ancestor component is resized. * * @param e the event describing the ancestor's resizing */ public void ancestorResized(HierarchyEvent e) { ancestorResized = true; } }