001: // WebOnSwing - Web Application Framework
002: //Copyright (C) 2003 Fernando Damian Petrola
003: //
004: //This library is free software; you can redistribute it and/or
005: //modify it under the terms of the GNU Lesser General Public
006: //License as published by the Free Software Foundation; either
007: //version 2.1 of the License, or (at your option) any later version.
008: //
009: //This library is distributed in the hope that it will be useful,
010: //but WITHOUT ANY WARRANTY; without even the implied warranty of
011: //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: //Lesser General Public License for more details.
013: //
014: //You should have received a copy of the GNU Lesser General Public
015: //License along with this library; if not, write to the Free Software
016: //Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017:
018: package net.ar.webonswing.remote;
019:
020: import java.awt.*;
021: import java.awt.event.*;
022: import java.lang.reflect.*;
023: import java.util.*;
024: import java.util.List;
025:
026: import javax.swing.*;
027: import javax.swing.event.*;
028:
029: import net.ar.webonswing.*;
030: import net.ar.webonswing.helpers.*;
031: import net.ar.webonswing.managers.script.*;
032: import net.ar.webonswing.render.markup.*;
033: import net.ar.webonswing.wrapping.*;
034: import net.ar.webonswing.wrapping.swing.*;
035:
036: import org.apache.commons.lang.*;
037: import org.apache.commons.logging.*;
038:
039: public class RemoteHelper {
040: public static String str2web(String aString) {
041: return StringUtils.replace(aString, "\n", "<br>");
042: }
043:
044: public static String escapeSingleQuotes(String aString) {
045: return StringUtils.replace(StringUtils.replace(StringUtils
046: .replace(aString, "'", "\\'"), "\n", " "), "\r", " ");
047: }
048:
049: public static String createJsInstance(String instanceName,
050: String className, Object[] parameters) {
051: Vector result = new Vector();
052:
053: for (int i = 0; i < parameters.length; i++) {
054: String between = "";
055: Object value = parameters[i];
056:
057: if (value instanceof Object[])
058: value = createJsInstance("", "Array", (Object[]) value);
059: else if (value instanceof String)
060: between = "'";
061:
062: result.add(between + value.toString() + between);
063: }
064:
065: String var = instanceName.length() > 0 ? "var " : "";
066: String assign = instanceName.length() > 0 ? "= " : "";
067: String semicolon = instanceName.length() > 0 ? ";" : "";
068:
069: return var + instanceName + assign + "new " + className + "("
070: + StringUtils.join(result.iterator(), ", ") + ")"
071: + semicolon;
072: }
073:
074: public static void fireClickEvent(JComponent aComponent) {
075: try {
076: MouseEvent theClickEvent = new MouseEvent(aComponent, 500,
077: System.currentTimeMillis(), 16, 1, 1, 1, false);
078: Method theMethod = Component.class.getDeclaredMethod(
079: "processEvent", new Class[] { AWTEvent.class });
080: theMethod.setAccessible(true);
081: theMethod
082: .invoke(aComponent, new Object[] { theClickEvent });
083: } catch (Exception e) {
084: throw new WebOnSwingException(e);
085: }
086: }
087:
088: public static void addMouseListeners(JComponent aComponent,
089: String theComponentName, Tag theTag) {
090: try {
091: EventListener[] theListeners = aComponent
092: .getListeners(MouseListener.class);
093:
094: if (theListeners.length > 0) {
095: if (!theListeners[0].getClass().getMethod(
096: "mouseClicked",
097: new Class[] { MouseEvent.class })
098: .getDeclaringClass().equals(MouseAdapter.class))
099: theTag.addAttribute(new TagAttribute("onclick",
100: "ed.dispatch(new MouseClickedEvent('"
101: + theComponentName + "'));"));
102:
103: if (!theListeners[0].getClass().getMethod(
104: "mousePressed",
105: new Class[] { MouseEvent.class })
106: .getDeclaringClass().equals(MouseAdapter.class))
107: theTag.addAttribute(new TagAttribute("onmousedown",
108: "ed.dispatch(new MousePressedEvent('"
109: + theComponentName + "'));"));
110:
111: if (!theListeners[0].getClass().getMethod(
112: "mouseReleased",
113: new Class[] { MouseEvent.class })
114: .getDeclaringClass().equals(MouseAdapter.class))
115: theTag.addAttribute(new TagAttribute("onmouseup",
116: "ed.dispatch(new MouseReleasedEvent('"
117: + theComponentName + "'));"));
118: }
119:
120: if (aComponent.getListeners(MouseMotionListener.class).length > 0
121: && theTag != null)
122: theTag.addAttribute(new TagAttribute("onmouseover",
123: "ed.dispatch(new MouseMovedEvent('"
124: + theComponentName + "'));"));
125: } catch (Exception e) {
126: LogFactory.getLog(RemoteHelper.class).error(
127: "Cannot do reflection", e);
128: }
129: }
130:
131: public static String getComponentInitScript(
132: VisualComponent aComponent) {
133: return getListenersAdds(aComponent).toString();
134: }
135:
136: public static String getListenersAdds(VisualComponent aComponent) {
137:
138: StringBuffer theContainer = new StringBuffer();
139:
140: theContainer.append(addListeners(aComponent,
141: ActionListener.class));
142: theContainer.append(addListeners(aComponent,
143: MouseListener.class));
144: theContainer.append(addListeners(aComponent,
145: MouseMotionListener.class));
146: theContainer
147: .append(addListeners(aComponent, ItemListener.class));
148: theContainer.append(addListeners(aComponent,
149: ListSelectionListener.class));
150: theContainer.append(addListeners(aComponent,
151: TreeSelectionListener.class));
152: theContainer.append(addListeners(aComponent,
153: ChangeListener.class));
154: theContainer.append(addListeners(aComponent,
155: PopupMenuListener.class));
156:
157: return theContainer.toString();
158: }
159:
160: public static String addListeners(VisualComponent aComponent,
161: Class theListenerClass) {
162: EventListener[] theListeners = ((SwingComponentWrapper) aComponent)
163: .getJComponent().getListeners(theListenerClass);
164:
165: StringBuffer theContainer = new StringBuffer();
166:
167: for (int i = 0; i < theListeners.length; i++) {
168: StringBuffer theCreationScript = new StringBuffer();
169:
170: if (theListeners[i] instanceof RemoteListener)
171: theCreationScript
172: .append(getRemoteListenerCreation((RemoteListener) theListeners[i]));
173: else
174: theCreationScript
175: .append("new "
176: + WosHelper
177: .getNoPackageClassName(theListenerClass)
178: + "()");
179:
180: theContainer.append("getComponent('" + aComponent.getName()
181: + "').addListener(" + theCreationScript + ");");
182: }
183:
184: return theContainer.toString();
185: }
186:
187: public static String getRemoteListenerCreation(
188: RemoteListener theRemoteListener) {
189: List theParametersNames = new Vector();
190:
191: for (int i = 0; i < theRemoteListener.getRemoteParameters().length; i++) {
192: Object theParameter = theRemoteListener
193: .getRemoteParameters()[i];
194: String theStringParameter = "";
195:
196: if (theParameter instanceof JComponent) {
197: VisualComponent theComponent = WosFramework
198: .getInstance().getHierarchyWrapper()
199: .getComponentWrapper(theParameter);
200: theStringParameter = theComponent.getName();
201: } else
202: theStringParameter = theParameter.toString();
203:
204: theParametersNames.add("'" + theStringParameter + "'");
205: }
206:
207: return "new " + theRemoteListener.getRemoteName() + " ("
208: + StringUtils.join(theParametersNames.iterator(), ", ")
209: + ")";
210: }
211:
212: public static boolean hasRemoteListeners(Container aContainer,
213: Class aListenerClass) {
214: boolean hasRemote = false;
215: EventListener[] theListeners = aContainer
216: .getListeners(aListenerClass);
217: for (int i = 0; i < theListeners.length; i++)
218: hasRemote |= theListeners[i] instanceof RemoteListener;
219:
220: return hasRemote;
221: }
222:
223: public static void removeToolTipListeners(JComponent aComponent) {
224: MouseListener[] theMouseListeners = (MouseListener[]) aComponent
225: .getListeners(MouseListener.class);
226: for (int i = 0; i < theMouseListeners.length; i++)
227: if (theMouseListeners[i] instanceof ToolTipManager)
228: aComponent.removeMouseListener(theMouseListeners[i]);
229:
230: MouseMotionListener[] theMouseMotionListeners = (MouseMotionListener[]) aComponent
231: .getListeners(MouseMotionListener.class);
232: for (int i = 0; i < theMouseMotionListeners.length; i++)
233: if (theMouseMotionListeners[i] instanceof MouseMotionAdapter)
234: aComponent
235: .removeMouseMotionListener(theMouseMotionListeners[i]);
236: }
237:
238: public static String wosunescape(String aString) {
239: return StringUtils.replace(aString, "__", "_");
240: }
241:
242: public static void addDynApiInitCode(
243: ScriptContributionContainer aContributionManager) {
244: aContributionManager.addInclude(WosFramework.getInstance()
245: .getCompleteResourcePath()
246: + "/dynapi/dynapi.js");
247: aContributionManager.addCodeOnce("dynapi.library.setPath('"
248: + WosFramework.getInstance().getCompleteResourcePath()
249: + "/dynapi/');");
250: aContributionManager
251: .addCodeOnce("dynapi.library.addPackage('dynapi.widgets', dynapi.library.path + 'widgets/');");
252: aContributionManager
253: .addCodeOnce("dynapi.library.include('dynapi.library');");
254: aContributionManager
255: .addCodeOnce("dynapi.library.include('dynapi.api');");
256: aContributionManager
257: .addCodeOnce("dynapi.library.include('DynLayerInline');");
258: aContributionManager
259: .addCodeOnce("dynapi.library.include('DragEvent');");
260: }
261: }
|