001: /*
002: * Copyright (c) 2004 JETA Software, Inc. All rights reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without modification,
005: * are permitted provided that the following conditions are met:
006: *
007: * o Redistributions of source code must retain the above copyright notice,
008: * this list of conditions and the following disclaimer.
009: *
010: * o Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * o Neither the name of JETA Software nor the names of its contributors may
015: * be used to endorse or promote products derived from this software without
016: * specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
020: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
021: * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
022: * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
023: * INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
024: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
025: * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
026: * INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
027: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
028: */
029:
030: package com.jeta.open.gui.framework;
031:
032: import java.awt.Component;
033: import java.awt.Container;
034: import java.awt.event.ActionListener;
035: import java.awt.event.ComponentListener;
036: import java.awt.event.ContainerListener;
037: import java.awt.event.FocusListener;
038: import java.awt.event.HierarchyBoundsListener;
039: import java.awt.event.HierarchyListener;
040: import java.awt.event.InputMethodListener;
041: import java.awt.event.ItemListener;
042: import java.awt.event.KeyListener;
043: import java.awt.event.MouseListener;
044: import java.awt.event.MouseMotionListener;
045: import java.awt.event.MouseWheelListener;
046: import java.lang.reflect.Method;
047: import java.util.EventListener;
048:
049: import javax.swing.AbstractButton;
050: import javax.swing.JTextField;
051: import javax.swing.event.ChangeListener;
052:
053: /**
054: * This class recursively searches a container for child components. It searches
055: * for all registered listeners on those components that are part of the
056: * com.jeta package and removes those listeners. This is used to assist in
057: * garbage collection. Hopefully, it ensures that a frame or container does not
058: * linger because of registered listeners that are never removed.
059: *
060: * @author Jeff Tassin
061: */
062: public class JETAComponentCleanser {
063: Object[] m_params = new Object[1];
064: Class[] m_types = new Class[1];
065:
066: /**
067: * Recursively searches all Components owned by this container. If the
068: * Component has a name, we store it in the m_components hash table
069: *
070: * @param container
071: * the container to search
072: */
073: public void cleanse(Container container) {
074: int count = container.getComponentCount();
075: for (int index = 0; index < count; index++) {
076: Component comp = container.getComponent(index);
077:
078: if (comp instanceof Container)
079: cleanse((Container) comp);
080:
081: removeJETAListeners(comp, ActionListener.class,
082: "removeActionListener");
083: removeJETAListeners(comp, ContainerListener.class,
084: "removeContainerListener");
085: removeJETAListeners(comp, ChangeListener.class,
086: "removeChangeListener");
087: removeJETAListeners(comp, ItemListener.class,
088: "removeItemListener");
089: removeJETAListeners(comp, ComponentListener.class,
090: "removeComponentListener");
091: removeJETAListeners(comp, FocusListener.class,
092: "removeFocusListener");
093: removeJETAListeners(comp, HierarchyBoundsListener.class,
094: "removeHierarchyBoundsListener");
095: removeJETAListeners(comp, HierarchyListener.class,
096: "removeHierarchyListener");
097: removeJETAListeners(comp, InputMethodListener.class,
098: "removeInputMethodListener");
099: removeJETAListeners(comp, KeyListener.class,
100: "removeKeyListener");
101: removeJETAListeners(comp, MouseListener.class,
102: "removeMouseListener");
103: removeJETAListeners(comp, MouseMotionListener.class,
104: "removeMouseMotionListener");
105: removeJETAListeners(comp, MouseWheelListener.class,
106: "removeMouseWheelListener");
107: }
108: }
109:
110: public void removeJETAListeners(Component comp,
111: Class listenerClass, String methodName) {
112: m_types[0] = listenerClass;
113: Method method = null;
114:
115: EventListener[] listeners = comp.getListeners(listenerClass);
116: for (int index = 0; index < listeners.length; index++) {
117: EventListener listener = listeners[index];
118: String lclass = listener.getClass().getName();
119: if (lclass.indexOf("com.jeta") >= 0) {
120: if (method == null) {
121: try {
122: if (listenerClass == ActionListener.class
123: || listenerClass == ChangeListener.class
124: || listenerClass == ItemListener.class) {
125: if (comp instanceof AbstractButton
126: || comp instanceof JTextField) {
127: method = comp.getClass().getMethod(
128: methodName, m_types);
129: }
130: } else {
131: method = comp.getClass().getMethod(
132: methodName, m_types);
133: }
134: } catch (Exception e) {
135: e.printStackTrace();
136: }
137: }
138:
139: if (method != null) {
140: try {
141: m_params[0] = listener;
142: method.invoke(comp, m_params);
143: } catch (Exception e) {
144: e.printStackTrace();
145: }
146: }
147: }
148: }
149: }
150:
151: }
|