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.vector;
017:
018: import org.directwebremoting.ScriptBuffer;
019: import org.directwebremoting.proxy.ScriptProxy;
020: import org.directwebremoting.proxy.io.Context;
021:
022: /**
023: * Defines a base class for GUI controls that implement both the cross-platform box profile painting introduced in
024: 3.2 and the cross-platform (VML/SVG) vector painting, also introduced in 3.2.
025:
026: This class should be extended by custom GUI classes that will display vector elements.
027: * @author Joe Walker [joe at getahead dot org]
028: * @author DRAPGEN - Dwr Reverse Ajax Proxy GENerator
029: */
030: public class Block extends jsx3.gui.Block {
031: /**
032: * All reverse ajax proxies need context to work from
033: * @param scriptProxy The place we are writing scripts to
034: * @param context The script that got us to where we are now
035: */
036: public Block(Context context, String extension,
037: ScriptProxy scriptProxy) {
038: super (context, extension, scriptProxy);
039: }
040:
041: /**
042: * Returns the vector canvas on which this control paints itself. If no canvas has already been created, then
043: createVector() is called to create it.
044: */
045: @SuppressWarnings("unchecked")
046: public jsx3.vector.Tag getCanvas() {
047: String extension = "getCanvas().";
048: try {
049: java.lang.reflect.Constructor<jsx3.vector.Tag> ctor = jsx3.vector.Tag.class
050: .getConstructor(Context.class, String.class,
051: ScriptProxy.class);
052: return ctor.newInstance(this , extension, getScriptProxy());
053: } catch (Exception ex) {
054: throw new IllegalArgumentException("Unsupported type: "
055: + jsx3.vector.Tag.class.getName());
056: }
057: }
058:
059: /**
060: * Returns the vector canvas on which this control paints itself. If no canvas has already been created, then
061: createVector() is called to create it.
062: * @param returnType The expected return type
063: */
064: @SuppressWarnings("unchecked")
065: public <T> T getCanvas(Class<T> returnType) {
066: String extension = "getCanvas().";
067: try {
068: java.lang.reflect.Constructor<T> ctor = returnType
069: .getConstructor(Context.class, String.class,
070: ScriptProxy.class);
071: return ctor.newInstance(this , extension, getScriptProxy());
072: } catch (Exception ex) {
073: throw new IllegalArgumentException(
074: "Unsupported return type: " + returnType.getName());
075: }
076: }
077:
078: /**
079: * Creates the vector tag tree that will render this GUI control. Subclasses of this class should override this
080: method to specify the manner in which they render.
081:
082: The basic template for a method overriding this method is:
083:
084: CustomVector.prototype.createVector = function() {
085: var objCanvas = this.jsxsuper();
086: // modify objCanvas, add children, etc.
087: return objCanvas;
088: };
089:
090: This method should do the work of creating and updating the vector tree to the state when it is ready to be
091: rendered on screen, but without calling updateVector() directly.
092: */
093: @SuppressWarnings("unchecked")
094: public jsx3.vector.Tag createVector() {
095: String extension = "createVector().";
096: try {
097: java.lang.reflect.Constructor<jsx3.vector.Tag> ctor = jsx3.vector.Tag.class
098: .getConstructor(Context.class, String.class,
099: ScriptProxy.class);
100: return ctor.newInstance(this , extension, getScriptProxy());
101: } catch (Exception ex) {
102: throw new IllegalArgumentException("Unsupported type: "
103: + jsx3.vector.Tag.class.getName());
104: }
105: }
106:
107: /**
108: * Creates the vector tag tree that will render this GUI control. Subclasses of this class should override this
109: method to specify the manner in which they render.
110:
111: The basic template for a method overriding this method is:
112:
113: CustomVector.prototype.createVector = function() {
114: var objCanvas = this.jsxsuper();
115: // modify objCanvas, add children, etc.
116: return objCanvas;
117: };
118:
119: This method should do the work of creating and updating the vector tree to the state when it is ready to be
120: rendered on screen, but without calling updateVector() directly.
121: * @param returnType The expected return type
122: */
123: @SuppressWarnings("unchecked")
124: public <T> T createVector(Class<T> returnType) {
125: String extension = "createVector().";
126: try {
127: java.lang.reflect.Constructor<T> ctor = returnType
128: .getConstructor(Context.class, String.class,
129: ScriptProxy.class);
130: return ctor.newInstance(this , extension, getScriptProxy());
131: } catch (Exception ex) {
132: throw new IllegalArgumentException(
133: "Unsupported return type: " + returnType.getName());
134: }
135: }
136:
137: /**
138: * Updates the pre-existing vector tree of this control on, for example, a resize or repaint event. Methods
139: overriding this method should return true if the update is successful or false to
140: force the vector tree to be completely recreated with createVector().
141:
142: The basic template for a method overriding this method is:
143:
144: CustomVector.prototype.updateVector = function(objVector) {
145: this.jsxsuper(objVector);
146: // modify objCanvas, modify children, etc.
147: return true;
148: };
149: * @param objVector the root of the vector render tree.
150: * @param callback <code>true</code> if the tree could be updated inline or <code>false</code> if it must be
151: recreated by calling <code>createVector()</code>.
152: */
153: @SuppressWarnings("unchecked")
154: public void updateVector(jsx3.vector.Tag objVector,
155: org.directwebremoting.proxy.Callback<Boolean> callback) {
156: ScriptBuffer script = new ScriptBuffer();
157: String callbackPrefix = "";
158:
159: if (callback != null) {
160: callbackPrefix = "var reply = ";
161: }
162:
163: script.appendCall(callbackPrefix + getContextPath()
164: + "updateVector", objVector);
165:
166: if (callback != null) {
167: String key = org.directwebremoting.extend.CallbackHelper
168: .saveCallback(callback, Boolean.class);
169: script
170: .appendCall("__System.activateCallback", key,
171: "reply");
172: }
173:
174: getScriptProxy().addScript(script);
175: }
176:
177: /**
178: * Instantiates and returns a new instance of jsx3.vector.Canvas. The implementation of
179: createVector() in this class calls this method to create the base vector tag. This method may be
180: overridden to provide a base tag of another type that Canvas.
181: */
182: @SuppressWarnings("unchecked")
183: public jsx3.vector.Tag createCanvas() {
184: String extension = "createCanvas().";
185: try {
186: java.lang.reflect.Constructor<jsx3.vector.Tag> ctor = jsx3.vector.Tag.class
187: .getConstructor(Context.class, String.class,
188: ScriptProxy.class);
189: return ctor.newInstance(this , extension, getScriptProxy());
190: } catch (Exception ex) {
191: throw new IllegalArgumentException("Unsupported type: "
192: + jsx3.vector.Tag.class.getName());
193: }
194: }
195:
196: /**
197: * Instantiates and returns a new instance of jsx3.vector.Canvas. The implementation of
198: createVector() in this class calls this method to create the base vector tag. This method may be
199: overridden to provide a base tag of another type that Canvas.
200: * @param returnType The expected return type
201: */
202: @SuppressWarnings("unchecked")
203: public <T> T createCanvas(Class<T> returnType) {
204: String extension = "createCanvas().";
205: try {
206: java.lang.reflect.Constructor<T> ctor = returnType
207: .getConstructor(Context.class, String.class,
208: ScriptProxy.class);
209: return ctor.newInstance(this , extension, getScriptProxy());
210: } catch (Exception ex) {
211: throw new IllegalArgumentException(
212: "Unsupported return type: " + returnType.getName());
213: }
214: }
215:
216: /**
217: * Renders a cross-platform vector event handler. When an event of type strEvtType bubbles up to the
218: HTML element rendered by objElm, the instance method of this object whose name is
219: strMethod will be called with two parameters: the browser event wrapped in an instance of
220: jsx3.gui.Event, and the native HTMLElement that defined the event handler.
221: * @param strEvtType the event type, one of <code>jsx3.gui.Event.CLICK</code>, etc.
222: * @param strMethod the instance method to call on this object when the event is received.
223: * @param objElm the HTML element to which to add the event handler.
224: */
225: public void paintEventHandler(String strEvtType, String strMethod,
226: jsx3.vector.Tag objElm) {
227: ScriptBuffer script = new ScriptBuffer();
228: script.appendCall(getContextPath() + "paintEventHandler",
229: strEvtType, strMethod, objElm);
230: getScriptProxy().addScript(script);
231: }
232:
233: }
|