001: /*
002: * SwingML Copyright (C) 2002 Robert Morris.
003: *
004: * This library is free software; you can redistribute it and/or modify it under
005: * the terms of the GNU Lesser General Public License as published by the Free
006: * Software Foundation; either version 2 of the License, or (at your option) any
007: * later version.
008: *
009: * This library is distributed in the hope that it will be useful, but WITHOUT
010: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011: * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012: * details.
013: *
014: * You should have received a copy of the GNU Lesser General Public License
015: * along with this library; if not, write to the Free Software Foundation, Inc.,
016: * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
017: *
018: * Authors: Robert Morris <robertj@morris.net>
019: *
020: */
021:
022: package org.swingml.event;
023:
024: import java.awt.*;
025: import java.util.*;
026:
027: import javax.swing.*;
028:
029: import org.swingml.*;
030: import org.swingml.model.*;
031: import org.swingml.system.*;
032:
033: /**
034: * This class provides utility functions for implementers of the InvokableEvent
035: * class. Generally, this class is the super class of most InvokableEvent
036: * implementations
037: *
038: * @author Robert J. Morris
039: *
040: * @see org.swingml.event.InvokableEventHandler
041: * @see org.swingml.model.ActionParamModel
042: */
043: public class EventUtil {
044:
045: /**
046: * This method expects the objects in the params argument to be instances of
047: * the ActionParamModel object.
048: *
049: * @param params
050: * A reference to an array of Object instances.
051: *
052: * @return A valid reference to a Map object. null if an error occurred.
053: *
054: * @see org.swingml.model.ActionParamModel
055: */
056: protected Map buildActionParametersMap(Object[] aParams) {
057: Map theReturnMap = null;
058: ActionParamModel theParam = null;
059: if (aParams != null && aParams.length > 0) {
060: theReturnMap = new HashMap(aParams.length);
061: for (int i = 0; i < aParams.length; i++) {
062: if (aParams[i] instanceof ActionParamModel) {
063: theParam = (ActionParamModel) aParams[i];
064: theReturnMap.put(theParam.getName(), theParam);
065: }
066: }
067: }
068: return theReturnMap;
069: }
070:
071: private Component findComponent(Component[] aComponents,
072: String aComponentName) {
073: Component theRetComp = null;
074: Component theFoundComp = null;
075: if (aComponents != null && aComponents.length > 0) {
076: for (int i = 0; i < aComponents.length; i++) {
077: if (aComponents[i].getName() != null
078: && aComponents[i].getName().equalsIgnoreCase(
079: aComponentName)) {
080: theRetComp = aComponents[i];
081: break;
082: }
083: if (aComponents[i] instanceof Container) {
084: theFoundComp = this .findComponent(
085: ((Container) aComponents[i])
086: .getComponents(), aComponentName);
087: if (theFoundComp != null) {
088: theRetComp = theFoundComp;
089: break;
090: }
091: }
092: }
093: }
094: return theRetComp;
095: }
096:
097: private SwingMLRenderer findRenderer(Component[] aComponents) {
098: SwingMLRenderer theRetComp = null;
099: SwingMLRenderer theFoundComp = null;
100: if (aComponents != null && aComponents.length > 0) {
101: for (int i = 0; i < aComponents.length; i++) {
102: if (aComponents[i] instanceof SwingMLRenderer) {
103: theRetComp = (SwingMLRenderer) aComponents[i];
104: break;
105: }
106: if (aComponents[i] instanceof Container) {
107: theFoundComp = this
108: .findRenderer(((Container) aComponents[i])
109: .getComponents());
110: if (theFoundComp != null) {
111: theRetComp = theFoundComp;
112: break;
113: }
114: }
115: }
116: }
117: return theRetComp;
118: }
119:
120: /**
121: * This function returns a reference to the component specified by the
122: * componentName argument. The component argument does not need to contain
123: * the requested object, it only needs to be in the same document as the
124: * component argument.
125: *
126: * @param component
127: * A reference to the Component instance specified in the
128: * COMPONENT attribute of the <EXTERNAL-ACTION> tag.
129: *
130: * @param componentName
131: * A String representing the name of the component to return.
132: *
133: * @return A reference to the named component, or null if the name does not
134: * exist within the document hierarchy.
135: */
136: public Component getComponent(Component aComponent,
137: String aComponentName) {
138: Component theRet = null;
139: Container theRootComponent = this .getRootComponent(aComponent);
140: theRet = this .findComponent(theRootComponent.getComponents(),
141: aComponentName);
142: return theRet;
143: }
144:
145: /**
146: * This method returns a reference to the Renderer instance within which the
147: * Component referenced by the component argument resides.
148: *
149: * @param component
150: * A refernce to the component specified by the COMPONENT
151: * attribute of the <EXTERNAL-ACTION> tag.
152: *
153: * @return A reference to the SwingMLRender within which the InvokableEvent
154: * instance resides.
155: */
156: protected SwingMLRenderer getRenderer(Component aComponent) {
157: SwingMLRenderer theRet = null;
158: try {
159: Container theRootComponent = this
160: .getRootComponent(aComponent);
161: if (!(theRootComponent instanceof SwingMLRenderer)) {
162: theRet = this .findRenderer(theRootComponent
163: .getComponents());
164: } else {
165: theRet = (SwingMLRenderer) theRootComponent;
166: }
167: } catch (Exception e) {
168: SwingMLLogger
169: .getInstance()
170: .log(
171: ILogCapable.ERROR,
172: "Syntax Error: There is a problem resolving the SwingMLRenderer for the referenced component.");
173: }
174: return theRet;
175: }
176:
177: private Container getRootComponent(Component aComponent) {
178: Container theRetComp = null;
179: if (aComponent instanceof JComponent) {
180: theRetComp = ((JComponent) aComponent)
181: .getTopLevelAncestor();
182: } else if (aComponent.getParent() == null
183: && aComponent instanceof Container) {
184: theRetComp = (Container) aComponent;
185: } else if (aComponent != null) {
186: theRetComp = getRootComponent(aComponent.getParent());
187: }
188: return theRetComp;
189: }
190: }
|