001: /*
002: * Copyright Javelin Software, All rights reserved.
003: */
004:
005: package com.javelin.swinglets.plaf.javascript;
006:
007: import java.awt.event.*;
008: import java.io.*;
009: import java.util.*;
010:
011: import com.javelin.swinglets.*;
012: import com.javelin.swinglets.event.*;
013: import com.javelin.swinglets.plaf.*;
014:
015: /**
016: * JSUtility defines a utility class for Java Script.
017: *
018: * @author Robin Sharp
019: */
020:
021: public class JSUtility {
022: /**
023: * Render the script code for a text the PrintWriter.
024: */
025: public static void updateScript(PrintWriter out,
026: SContainer container) {
027: JSUtility.updateScript(out, (SComponent) container);
028:
029: //Loop through all the children updating the script
030: SComponentUI ui = null;
031: for (int index = 0; index < container.getComponentCount(); index++) {
032: ui = container.getComponent(index).getUI();
033: if (ui != null)
034: ui.updateScript(out, container.getComponent(index));
035: }
036:
037: }
038:
039: /**
040: * Render the script code for a text the PrintWriter.
041: */
042: public static void updateScript(PrintWriter out, SComponent c) {
043: if (c.getScriptCount() == 0)
044: return;
045:
046: for (Enumeration scripts = c.getScripts(); scripts
047: .hasMoreElements();) {
048: SScript script = (SScript) scripts.nextElement();
049:
050: if (!script.getContentType().equals(SScript.JAVASCRIPT))
051: continue;
052:
053: if (script.getScript() != null || script.getUrl() != null) {
054: //Write out the script
055: out.print("<SCRIPT");
056:
057: if (script.getContentType() != null) {
058: out.print(" TYPE=\"");
059: out.print(script.getContentType());
060: out.print("\"");
061: }
062:
063: if (script.getUrl() != null) {
064: out.print(" SRC=\"");
065: out.print(script.getUrl());
066: out.print("\"");
067: }
068:
069: if (script.isDeferred()) {
070: out.print(" DEFER=true");
071: }
072:
073: out.println(" >");
074:
075: if (script.getUrl() == null
076: && script.getScript() != null) {
077: out.println(script.getScript());
078: }
079:
080: out.println("</SCRIPT>");
081: }
082:
083: if (script.getNoScript() != null) {
084: out.println("<NOSCRIPT>");
085: out.println(script.getNoScript());
086: out.println("</NOSCRIPT>");
087: }
088: }
089: }
090:
091: /**
092: * Render the event handling script for a text the PrintWriter.
093: */
094: public static void updateEvents(PrintWriter out, SComponent c,
095: Vector listeners) {
096: if (!c.isVisible())
097: return;
098:
099: if (listeners == null)
100: return;
101:
102: boolean focusEventFound = false;
103:
104: //If there are any events write them out
105: if (!listeners.isEmpty()) {
106: EventListener eventListener = null;
107:
108: //Loop though all the listeners
109: for (int index = 0; index < listeners.size(); index++) {
110: eventListener = (EventListener) listeners
111: .elementAt(index);
112:
113: if (!(eventListener instanceof ScriptedListener))
114: return;
115:
116: //keep track of whether these events were found
117: if (eventListener instanceof FocusListener)
118: focusEventFound = true;
119:
120: Enumeration events = ((ScriptedListener) eventListener)
121: .getEvents();
122: Enumeration scripts = ((ScriptedListener) eventListener)
123: .getScripts();
124:
125: //For each listener write out the event handlers
126: while (events.hasMoreElements()) {
127: out.print(" ");
128: out.print(events.nextElement());
129: out.print("=\"");
130: out.print(scripts.nextElement());
131: out.print("\" ");
132: }
133: }
134: }
135:
136: if (!focusEventFound && c.getToolTipText() != null) {
137: out.print(" onFocus=\"window.status='");
138: out.print(c.getToolTipText());
139: out.print("';return true\"");
140: out
141: .print(" onBlur=\"window.status=window.defaultStatus;return true\"");
142: }
143:
144: }
145:
146: public static String encodeURL(SComponent component, String url) {
147: SwingletManager sm = component.getSwingletManager();
148:
149: if (sm instanceof ServletManager
150: && ((ServletManager) sm).getResponse() != null) {
151: return ((ServletManager) sm).getResponse().encodeUrl(url);
152: }
153:
154: return url;
155: }
156:
157: }
|