1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
|
// $Id: ActionUtilities.java,v 1.1 2004/11/03 18:30:04 bobtarling Exp $
/*
* ActionUtilities.java
*/
package org.tigris.swidgets;
import java.awt.Component;
import java.awt.Container;
import java.awt.event.ActionEvent;
import javax.swing.JPopupMenu;
/**
* A collection of utility methods for Swing Actions.
*
* @author Eugenio Alvarez
* @stereotype utility
*/
public class ActionUtilities {
/**
* Intended for use inside an <code>actionPerformed</code> method eg:
* <pre>
* public void actionPerformed(ActionEvent ae) {
* Container appRoot = ActionUtilities.getActionRoot(ae);
* }
* </pre>
*
* Returns the root object, usually a <code>JFrame, JDialog or
* JApplet</code> that is the owner of the source event source
* object (JMenu, JMenuItem, JPopupMenu etc).
*
* @param ae the given action event
* @return the root object
*/
public static Container getActionRoot(ActionEvent ae) {
return ActionUtilities.getActionRoot(ae.getSource());
} // getActionRoot()
/**
* Intended for use inside an <code>actionPerformed</code> method eg:
* <pre>
* public void actionPerformed(ActionEvent e) {
* Container appRoot = ActionUtilities.getActionRoot(e.getSource());
* }
* </pre>
* @return the root object, usually a JFrame, JDialog or JApplet
* that is the owner of the source event source object
* (JMenu, JMenuItem, JPopupMenu etc).
* null if none is found.
*
* @param source the source of the event
*/
public static Container getActionRoot(Object source) {
Container container = null;
if (source instanceof Component) {
Component component = (Component) source;
container = ActionUtilities.getContainer(component);
if (container == null) {
if (source instanceof Container) {
return (Container) source;
} // end if
return null;
} // end if
while (ActionUtilities.getContainer(container) != null) {
container = ActionUtilities.getContainer(container);
} // end while
} // end if
return container;
} // end getActionRoot()
/**
* Helper method to find the <code>Container</code> of
* <code>Component</code>.
*/
private static Container getContainer(Component source) {
Container container = source.getParent();
if (container != null) {
return container;
}
if (source instanceof JPopupMenu) {
JPopupMenu jPopupMenu = (JPopupMenu) source;
Component component = jPopupMenu.getInvoker();
if (component instanceof Container) {
container = (Container) component;
} // end if
} // end if
return container;
} // end getContainer()
} // end class ActionUtilities
|