001: /*
002: * Copyright 2005 Joe Walker
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package jsx3.gui;
017:
018: import org.directwebremoting.ScriptBuffer;
019: import org.directwebremoting.proxy.ScriptProxy;
020: import org.directwebremoting.proxy.io.Context;
021:
022: /**
023: * Abstract superclass of model objects that are painted to screen.
024: * @author Joe Walker [joe at getahead dot org]
025: * @author DRAPGEN - Dwr Reverse Ajax Proxy GENerator
026: */
027: public class Painted extends jsx3.app.Model {
028: /**
029: * All reverse ajax proxies need context to work from
030: * @param scriptProxy The place we are writing scripts to
031: * @param context The script that got us to where we are now
032: */
033: public Painted(Context context, String extension,
034: ScriptProxy scriptProxy) {
035: super (context, extension, scriptProxy);
036: }
037:
038: /**
039: * instance initializer
040: * @param strName unique name distinguishing this object from all other JSX GUI objects in the JSX application
041: */
042: public Painted(String strName) {
043: super ((Context) null, (String) null, (ScriptProxy) null);
044: ScriptBuffer script = new ScriptBuffer();
045: script.appendCall("new Painted", strName);
046: setInitScript(script);
047: }
048:
049: /**
050: * Returns the absolute positioning of the object's on-screen view in relation to JSXROOT (whose left/top is 0/0).
051: Returns information as a JavaScript object with properties, L, T, W, H
052: of @objRoot is null, the on-screen view for JSXROOT is used as the object reference
053: * @param objRoot object reference to IE DOM object (i.e., div, span, etc); if null is passed, the first div child of JSXROOT's on-screen representation will be used
054: * @param objGUI object reference to item to get absolute position for—as opposed to this instance (useful for determining placement of html objects contained by JSX objects, but not part of the actual JSX DOM)
055: * @return JScript object with properties: L, T, W, H (corresponding to left, top width, height)
056: */
057: @SuppressWarnings("unchecked")
058: public jsx3.lang.Object getAbsolutePosition(String objRoot,
059: String objGUI) {
060: String extension = "getAbsolutePosition(\"" + objRoot
061: + "\", \"" + objGUI + "\").";
062: try {
063: java.lang.reflect.Constructor<jsx3.lang.Object> ctor = jsx3.lang.Object.class
064: .getConstructor(Context.class, String.class,
065: ScriptProxy.class);
066: return ctor.newInstance(this , extension, getScriptProxy());
067: } catch (Exception ex) {
068: throw new IllegalArgumentException("Unsupported type: "
069: + jsx3.lang.Object.class.getName());
070: }
071: }
072:
073: /**
074: * Returns the absolute positioning of the object's on-screen view in relation to JSXROOT (whose left/top is 0/0).
075: Returns information as a JavaScript object with properties, L, T, W, H
076: of @objRoot is null, the on-screen view for JSXROOT is used as the object reference
077: * @param objRoot object reference to IE DOM object (i.e., div, span, etc); if null is passed, the first div child of JSXROOT's on-screen representation will be used
078: * @param objGUI object reference to item to get absolute position for—as opposed to this instance (useful for determining placement of html objects contained by JSX objects, but not part of the actual JSX DOM)
079: * @param returnType The expected return type
080: * @return JScript object with properties: L, T, W, H (corresponding to left, top width, height)
081: */
082: @SuppressWarnings("unchecked")
083: public <T> T getAbsolutePosition(String objRoot, String objGUI,
084: Class<T> returnType) {
085: String extension = "getAbsolutePosition(\"" + objRoot
086: + "\", \"" + objGUI + "\").";
087: try {
088: java.lang.reflect.Constructor<T> ctor = returnType
089: .getConstructor(Context.class, String.class,
090: ScriptProxy.class);
091: return ctor.newInstance(this , extension, getScriptProxy());
092: } catch (Exception ex) {
093: throw new IllegalArgumentException(
094: "Unsupported return type: " + returnType.getName());
095: }
096: }
097:
098: /**
099: * assigns a dynamic property to one of this object's properties
100: returns reference to self to facilitate method chaining;
101: * @param strName property on this GUI object that will now use a dynamic property (e.g., 'jsxleft','jsxtop','jsxheight',etc.);
102: * @param strValue name of a dynamic style, whose value will be used
103: * @return this object
104: */
105: public jsx3.gui.Painted setDynamicProperty(String strName,
106: String strValue) {
107: ScriptBuffer script = new ScriptBuffer();
108: script.appendCall(getContextPath() + "setDynamicProperty",
109: strName, strValue);
110: getScriptProxy().addScript(script);
111: return this ;
112: }
113:
114: /**
115: * Returns the value of the dynamic property @strPropName; if not found, returns null
116: * @param strName property on this GUI object that will now use a dynamic property (e.g., 'jsxleft','jsxtop','jsxheight',etc.);
117: * @param callback value of the property
118: */
119: @SuppressWarnings("unchecked")
120: public void getDynamicProperty(String strName,
121: org.directwebremoting.proxy.Callback<String> callback) {
122: ScriptBuffer script = new ScriptBuffer();
123: String callbackPrefix = "";
124:
125: if (callback != null) {
126: callbackPrefix = "var reply = ";
127: }
128:
129: script.appendCall(callbackPrefix + getContextPath()
130: + "getDynamicProperty", strName);
131:
132: if (callback != null) {
133: String key = org.directwebremoting.extend.CallbackHelper
134: .saveCallback(callback, String.class);
135: script
136: .appendCall("__System.activateCallback", key,
137: "reply");
138: }
139:
140: getScriptProxy().addScript(script);
141: }
142:
143: /**
144: * Sets a property on the object that when the object is rendered on-screen, the HTML tag will be assigned the given name/value pair as a tag attribute
145: * @param strName the name of the property/attribute
146: * @param strValue the value for the property; may not contain double-quotes; escape via jsx3.util.strEscapeHTML if necessary or use combinations of single-quotes and escaped single-quotes
147: * @return this object (this)
148: */
149: public jsx3.gui.Painted setAttribute(String strName, String strValue) {
150: ScriptBuffer script = new ScriptBuffer();
151: script.appendCall(getContextPath() + "setAttribute", strName,
152: strValue);
153: getScriptProxy().addScript(script);
154: return this ;
155: }
156:
157: /**
158: * Returns value for the custom attribute with the given name; returns null if no attribute found
159: * @param strName the name of the property/attribute
160: */
161: @SuppressWarnings("unchecked")
162: public void getAttribute(String strName,
163: org.directwebremoting.proxy.Callback<String> callback) {
164: ScriptBuffer script = new ScriptBuffer();
165: String callbackPrefix = "";
166:
167: if (callback != null) {
168: callbackPrefix = "var reply = ";
169: }
170:
171: script.appendCall(callbackPrefix + getContextPath()
172: + "getAttribute", strName);
173:
174: if (callback != null) {
175: String key = org.directwebremoting.extend.CallbackHelper
176: .saveCallback(callback, String.class);
177: script
178: .appendCall("__System.activateCallback", key,
179: "reply");
180: }
181:
182: getScriptProxy().addScript(script);
183: }
184:
185: /**
186: * Returns handle to the JavaScript Object Array containing all events for the JSX GUI object;
187: NOTE: This object will contain zero or more JavaScript Objects with the following Properties: script, type, system
188: */
189: @SuppressWarnings("unchecked")
190: public jsx3.lang.Object getAttributes() {
191: String extension = "getAttributes().";
192: try {
193: java.lang.reflect.Constructor<jsx3.lang.Object> ctor = jsx3.lang.Object.class
194: .getConstructor(Context.class, String.class,
195: ScriptProxy.class);
196: return ctor.newInstance(this , extension, getScriptProxy());
197: } catch (Exception ex) {
198: throw new IllegalArgumentException("Unsupported type: "
199: + jsx3.lang.Object.class.getName());
200: }
201: }
202:
203: /**
204: * Returns handle to the JavaScript Object Array containing all events for the JSX GUI object;
205: NOTE: This object will contain zero or more JavaScript Objects with the following Properties: script, type, system
206: * @param returnType The expected return type
207: */
208: @SuppressWarnings("unchecked")
209: public <T> T getAttributes(Class<T> returnType) {
210: String extension = "getAttributes().";
211: try {
212: java.lang.reflect.Constructor<T> ctor = returnType
213: .getConstructor(Context.class, String.class,
214: ScriptProxy.class);
215: return ctor.newInstance(this , extension, getScriptProxy());
216: } catch (Exception ex) {
217: throw new IllegalArgumentException(
218: "Unsupported return type: " + returnType.getName());
219: }
220: }
221:
222: /**
223: * removes the specific custom property bound to this object; returns a reference to self (this) to facilitate method chaining
224: * @param strName the name of the custom property to remove
225: * @return this object
226: */
227: @SuppressWarnings("unchecked")
228: public jsx3.gui.Painted removeAttribute(String strName) {
229: String extension = "removeAttribute(\"" + strName + "\").";
230: try {
231: java.lang.reflect.Constructor<jsx3.gui.Painted> ctor = jsx3.gui.Painted.class
232: .getConstructor(Context.class, String.class,
233: ScriptProxy.class);
234: return ctor.newInstance(this , extension, getScriptProxy());
235: } catch (Exception ex) {
236: throw new IllegalArgumentException("Unsupported type: "
237: + jsx3.gui.Painted.class.getName());
238: }
239: }
240:
241: /**
242: * removes the specific custom property bound to this object; returns a reference to self (this) to facilitate method chaining
243: * @param strName the name of the custom property to remove
244: * @param returnType The expected return type
245: * @return this object
246: */
247: @SuppressWarnings("unchecked")
248: public <T> T removeAttribute(String strName, Class<T> returnType) {
249: String extension = "removeAttribute(\"" + strName + "\").";
250: try {
251: java.lang.reflect.Constructor<T> ctor = returnType
252: .getConstructor(Context.class, String.class,
253: ScriptProxy.class);
254: return ctor.newInstance(this , extension, getScriptProxy());
255: } catch (Exception ex) {
256: throw new IllegalArgumentException(
257: "Unsupported return type: " + returnType.getName());
258: }
259: }
260:
261: /**
262: * removes all events bound to this object; NOTE: The object must still be painted/repainted for its corresponding on-screen view to be likewise updated; returns a reference to self (this) to facilitate method chaining
263: * @return this object
264: */
265: @SuppressWarnings("unchecked")
266: public jsx3.gui.Painted removeAttributes() {
267: String extension = "removeAttributes().";
268: try {
269: java.lang.reflect.Constructor<jsx3.gui.Painted> ctor = jsx3.gui.Painted.class
270: .getConstructor(Context.class, String.class,
271: ScriptProxy.class);
272: return ctor.newInstance(this , extension, getScriptProxy());
273: } catch (Exception ex) {
274: throw new IllegalArgumentException("Unsupported type: "
275: + jsx3.gui.Painted.class.getName());
276: }
277: }
278:
279: /**
280: * removes all events bound to this object; NOTE: The object must still be painted/repainted for its corresponding on-screen view to be likewise updated; returns a reference to self (this) to facilitate method chaining
281: * @param returnType The expected return type
282: * @return this object
283: */
284: @SuppressWarnings("unchecked")
285: public <T> T removeAttributes(Class<T> returnType) {
286: String extension = "removeAttributes().";
287: try {
288: java.lang.reflect.Constructor<T> ctor = returnType
289: .getConstructor(Context.class, String.class,
290: ScriptProxy.class);
291: return ctor.newInstance(this , extension, getScriptProxy());
292: } catch (Exception ex) {
293: throw new IllegalArgumentException(
294: "Unsupported return type: " + returnType.getName());
295: }
296: }
297:
298: /**
299: * gives focus to the on-screen VIEW for the element; returns a handle to the html/dhtml element as exposed by the native browser
300: */
301: @SuppressWarnings("unchecked")
302: public void focus(
303: org.directwebremoting.proxy.Callback<String> callback) {
304: ScriptBuffer script = new ScriptBuffer();
305: String callbackPrefix = "";
306:
307: if (callback != null) {
308: callbackPrefix = "var reply = ";
309: }
310:
311: script.appendCall(callbackPrefix + getContextPath() + "focus");
312:
313: if (callback != null) {
314: String key = org.directwebremoting.extend.CallbackHelper
315: .saveCallback(callback, String.class);
316: script
317: .appendCall("__System.activateCallback", key,
318: "reply");
319: }
320:
321: getScriptProxy().addScript(script);
322: }
323:
324: /**
325: * Returns handle/reference to the JSX GUI Object's on-screen counterpartÑbasically a handle to a DHTML object such as a DIV, SPAN, etc
326: * @param objGUI either the HTML document containing the rendered object or an HTML element in that document.
327: This argument is optional but improves the efficiency of this method if provided.
328: * @param callback IE DHTML object
329: */
330: @SuppressWarnings("unchecked")
331: public void getRendered(jsx3.gui.Event objGUI,
332: org.directwebremoting.proxy.Callback<String> callback) {
333: ScriptBuffer script = new ScriptBuffer();
334: String callbackPrefix = "";
335:
336: if (callback != null) {
337: callbackPrefix = "var reply = ";
338: }
339:
340: script.appendCall(callbackPrefix + getContextPath()
341: + "getRendered", objGUI);
342:
343: if (callback != null) {
344: String key = org.directwebremoting.extend.CallbackHelper
345: .saveCallback(callback, String.class);
346: script
347: .appendCall("__System.activateCallback", key,
348: "reply");
349: }
350:
351: getScriptProxy().addScript(script);
352: }
353:
354: /**
355: * Returns handle/reference to the JSX GUI Object's on-screen counterpartÑbasically a handle to a DHTML object such as a DIV, SPAN, etc
356: * @param objGUI either the HTML document containing the rendered object or an HTML element in that document.
357: This argument is optional but improves the efficiency of this method if provided.
358: * @param callback IE DHTML object
359: */
360: @SuppressWarnings("unchecked")
361: public void getRendered(jsx3.lang.Object objGUI,
362: org.directwebremoting.proxy.Callback<String> callback) {
363: ScriptBuffer script = new ScriptBuffer();
364: String callbackPrefix = "";
365:
366: if (callback != null) {
367: callbackPrefix = "var reply = ";
368: }
369:
370: script.appendCall(callbackPrefix + getContextPath()
371: + "getRendered", objGUI);
372:
373: if (callback != null) {
374: String key = org.directwebremoting.extend.CallbackHelper
375: .saveCallback(callback, String.class);
376: script
377: .appendCall("__System.activateCallback", key,
378: "reply");
379: }
380:
381: getScriptProxy().addScript(script);
382: }
383:
384: /**
385: * Updates the view of this object by calling paint() and replacing the current view with the
386: returned HTML. This method has no effect if this object is not currently displayed.
387: * @param callback the result of calling <code>paint()</code> or <code>null</code> if this object is not displayed.
388: */
389: @SuppressWarnings("unchecked")
390: public void repaint(
391: org.directwebremoting.proxy.Callback<String> callback) {
392: ScriptBuffer script = new ScriptBuffer();
393: String callbackPrefix = "";
394:
395: if (callback != null) {
396: callbackPrefix = "var reply = ";
397: }
398:
399: script
400: .appendCall(callbackPrefix + getContextPath()
401: + "repaint");
402:
403: if (callback != null) {
404: String key = org.directwebremoting.extend.CallbackHelper
405: .saveCallback(callback, String.class);
406: script
407: .appendCall("__System.activateCallback", key,
408: "reply");
409: }
410:
411: getScriptProxy().addScript(script);
412: }
413:
414: /**
415: * Returns the DHTML, used for this object's on-screen VIEW
416: * @param callback DHTML
417: */
418: @SuppressWarnings("unchecked")
419: public void paint(
420: org.directwebremoting.proxy.Callback<String> callback) {
421: ScriptBuffer script = new ScriptBuffer();
422: String callbackPrefix = "";
423:
424: if (callback != null) {
425: callbackPrefix = "var reply = ";
426: }
427:
428: script.appendCall(callbackPrefix + getContextPath() + "paint");
429:
430: if (callback != null) {
431: String key = org.directwebremoting.extend.CallbackHelper
432: .saveCallback(callback, String.class);
433: script
434: .appendCall("__System.activateCallback", key,
435: "reply");
436: }
437:
438: getScriptProxy().addScript(script);
439: }
440:
441: /**
442: * A hook that subclasses of Painted may override in order to perform additional manipulation of the HTML DOM
443: created by the paint method. The order of steps follows. All steps occur in a single browser thread so that
444: the screen does not update between steps 2 and 3.
445:
446: The paint() method of this object is called.
447: The result of the paint() method is inserted into the HTML DOM.
448: The onAfterPaint() method of this object is called, passing in the newly inserted root HTML element.
449: * @param objGUI the rendered HTML element representing this object.
450: */
451: public void onAfterPaint(String objGUI) {
452: ScriptBuffer script = new ScriptBuffer();
453: script.appendCall(getContextPath() + "onAfterPaint", objGUI);
454: getScriptProxy().addScript(script);
455: }
456:
457: /**
458: * Paints a child of this object without repainting this entire object. The child is inserted into the view of
459: this object as the last child object, regardless of its actual position relative to other children. This method
460: has no effect if this object is not currently painted.
461: * @param objChild the child object to paint.
462: * @param bGroup <code>true</code> if this method is being called iteratively over a collection of
463: children. This parameter will only be <code>false</code> on the final call in the iteration.
464: * @param objGUI
465: * @param bCascadeOnly
466: */
467: public void paintChild(jsx3.gui.Painted objChild, boolean bGroup,
468: String objGUI, boolean bCascadeOnly) {
469: ScriptBuffer script = new ScriptBuffer();
470: script.appendCall(getContextPath() + "paintChild", objChild,
471: bGroup, objGUI, bCascadeOnly);
472: getScriptProxy().addScript(script);
473: }
474:
475: /**
476: * Iterates through children and returns concatenation of paint() method for all children.
477: * @param c the children to paint. If not provided <code>this.getChildren()</code> is used.
478: * @param callback DHTML
479: */
480: @SuppressWarnings("unchecked")
481: public void paintChildren(Object[] c,
482: org.directwebremoting.proxy.Callback<String> callback) {
483: ScriptBuffer script = new ScriptBuffer();
484: String callbackPrefix = "";
485:
486: if (callback != null) {
487: callbackPrefix = "var reply = ";
488: }
489:
490: script.appendCall(callbackPrefix + getContextPath()
491: + "paintChildren", c);
492:
493: if (callback != null) {
494: String key = org.directwebremoting.extend.CallbackHelper
495: .saveCallback(callback, String.class);
496: script
497: .appendCall("__System.activateCallback", key,
498: "reply");
499: }
500:
501: getScriptProxy().addScript(script);
502: }
503:
504: /**
505: * Removes the box model abstraction for a given object and its descendants. This effectively resets the box profiler, so dimensions can be recalculated as if the object was just broought into the visual DOM.
506: * @param properties Will designate by name, those properties that should be updated on the object's VIEW (without requiring the MODEL to repaint), including one or more of the following: padding, margin, border
507: */
508: public void recalcBox(Object[] properties) {
509: ScriptBuffer script = new ScriptBuffer();
510: script.appendCall(getContextPath() + "recalcBox", properties);
511: getScriptProxy().addScript(script);
512: }
513:
514: }
|