KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openlaszlo > compiler > Canvas


1 /* *****************************************************************************
2  * Canvas.java
3 * ****************************************************************************/

4
5 /* J_LZ_COPYRIGHT_BEGIN *******************************************************
6 * Copyright 2001-2004 Laszlo Systems, Inc. All Rights Reserved. *
7 * Use is subject to license terms. *
8 * J_LZ_COPYRIGHT_END *********************************************************/

9
10 /**
11  * A <code>Canvas</code> represents the underlying
12  * area in which a compiled app will run
13  */

14
15 package org.openlaszlo.compiler;
16 import org.openlaszlo.utils.StringUtils;
17 import org.openlaszlo.xml.internal.XMLUtils;
18 import org.openlaszlo.server.Configuration;
19 import org.openlaszlo.server.LPS;
20 import java.util.*;
21 import java.io.Serializable JavaDoc;
22 import java.io.File JavaDoc;
23 import org.jdom.Element;
24 import org.jdom.Namespace;
25 import org.apache.log4j.Logger;
26
27 public class Canvas implements java.io.Serializable JavaDoc {
28
29     // TODO: [old ebloch] change these to properties
30
// TODO: [2003-10-25 bloch] or better yet derrive them from the schema
31

32     private static Logger mLogger = Logger.getLogger(Canvas.class);
33
34     /** Default canvas width for compilation */
35     private static final int DEFAULT_WIDTH = 500;
36
37     /** Default canvas height for compilation */
38     private static final int DEFAULT_HEIGHT = 400;
39
40     /** Default canvas backgorund color */
41     private static final int DEFAULT_BGCOLOR = 0xFFFFFF;
42
43     /** Default canvas backgorund color */
44     private static final String JavaDoc DEFAULT_TITLE = "Laszlo Application";
45
46     /** Default canvas font info */
47     // TODO: [2003-10-25 bloch] this should come from the build system
48
public static final String JavaDoc DEFAULT_VERSION = "1.1";
49
50     // TODO: [2003-10-25 bloch] these should come from a properties file
51
public static final String JavaDoc DEFAULT_SWF6_FONT = "Verdana,Vera,sans-serif";
52
53     public static final String JavaDoc DEFAULT_FONT = "default";
54     public static final String JavaDoc DEFAULT_FONT_FILENAME = "verity" + File.separator + "verity11.ttf";
55     public static final String JavaDoc DEFAULT_BOLD_FONT_FILENAME = "verity" + File.separator + "verity11bold.ttf";
56     public static final String JavaDoc DEFAULT_ITALIC_FONT_FILENAME = "verity" + File.separator + "verity11italic.ttf";
57     public static final String JavaDoc DEFAULT_BOLD_ITALIC_FONT_FILENAME = "verity" + File.separator + "verity11bolditalic.ttf";
58     
59     public String JavaDoc defaultFont () {
60         if (mSWFVersion.equals( "swf5" )) {
61             return DEFAULT_FONT;
62         } else {
63             return DEFAULT_SWF6_FONT;
64         }
65     }
66
67     public String JavaDoc defaultFontsize () {
68         if (mSWFVersion.equals( "swf5" )) {
69             return DEFAULT_FONTSIZE;
70         } else {
71             return DEFAULT_SWF6_FONTSIZE;
72         }
73     }
74
75
76     public static final String JavaDoc DEFAULT_FONTSIZE = "8";
77     public static final String JavaDoc DEFAULT_SWF6_FONTSIZE = "11";
78     public static final String JavaDoc DEFAULT_FONTSTYLE = "";
79     
80     /** Default persistent connection parameters */
81     private static final long DEFAULT_HEARTBEAT = 5000; // 5 seconds
82
private static final boolean DEFAULT_SENDUSERDISCONNECT = false;
83     
84     public String JavaDoc defaultFont = DEFAULT_FONT;
85     public String JavaDoc defaultFontFilename = DEFAULT_FONT_FILENAME;
86     public String JavaDoc defaultBoldFontFilename = DEFAULT_BOLD_FONT_FILENAME;
87     public String JavaDoc defaultItalicFontFilename = DEFAULT_ITALIC_FONT_FILENAME;
88     public String JavaDoc defaultBoldItalicFontFilename = DEFAULT_BOLD_ITALIC_FONT_FILENAME;
89     
90     /** File path relative to webapp. */
91     private String JavaDoc mFilePath = null;
92     /** Width of the canvas. */
93     private int mWidth = DEFAULT_WIDTH;
94     /** Height of the canvas. */
95     private int mHeight = DEFAULT_HEIGHT;
96     /** Background color of the canvas. */
97     private int mBGColor = DEFAULT_BGCOLOR;
98
99     // Dimension strings
100
private String JavaDoc mWidthString = null;
101     private String JavaDoc mHeightString = null;
102
103     // Default to proxied deployment
104
private boolean mProxied = true;
105
106     /** Width of the root output text object (Flash 5 limits us to
107      * setting this at compile time) */

108     private int mMaxTextWidth = 0;
109
110     /** Height of the root output text object (Flash 5 limits us to
111      * setting this at compile time) */

112     private int mMaxTextHeight = 0;
113     
114     /** FontInfo for the canvas. */
115     private FontInfo mFontInfo = null;
116     
117     /** Title for the canvas. */
118     private String JavaDoc mTitle = DEFAULT_TITLE;
119     
120     /** Version of the flash player file format which we compile to **/
121     private String JavaDoc mSWFVersion = LPS.getProperty("compiler.runtime.default", "swf6");
122     
123     /** Persistent connection parameters. */
124     private boolean mIsConnected = false;
125     private boolean mSendUserDisconnect = false;
126     private long mHeartbeat = 0;
127     private String JavaDoc mAuthenticator = null;
128     private String JavaDoc mGroup = null;
129     private Set mAgents = null;
130
131     private final Map mSecurityOptions = new Hashtable();
132
133     private String JavaDoc mCompilationWarningText = null;
134     private String JavaDoc mCompilationWarningXML = null;
135     private Element mInfo = new org.jdom.Element("stats");
136     
137     public void setCompilationWarningText(String JavaDoc text) {
138         mCompilationWarningText = text;
139     }
140
141     public void setCompilationWarningXML(String JavaDoc xml) {
142         mCompilationWarningXML = xml;
143     }
144     
145     public void setSWFVersion(String JavaDoc text) {
146         mSWFVersion = text;
147     }
148
149     public String JavaDoc getSWFVersion() {
150         return mSWFVersion;
151     }
152
153     public void addInfo(Element info) {
154         mInfo.addContent(info);
155     }
156
157     public String JavaDoc getCompilationWarningText() {
158         return mCompilationWarningText;
159     }
160
161     public void setFontInfo(FontInfo info) {
162         mFontInfo = info;
163     }
164     
165     public String JavaDoc getInfoAsString() {
166         org.jdom.output.XMLOutputter outputter =
167             new org.jdom.output.XMLOutputter();
168         outputter.setTextNormalize(true);
169         return outputter.outputString(mInfo);
170     }
171
172     /** @return file path */
173     public String JavaDoc getFilePath() {
174         return mFilePath;
175     }
176
177     /** @return file path */
178     public void setFilePath(String JavaDoc filePath) {
179         mFilePath = filePath;
180     }
181
182     /** @return width */
183     public int getWidth() {
184         return mWidth;
185     }
186
187     /** @return width */
188     public String JavaDoc getWidthXML() {
189         if (mWidthString == null)
190             return "" + mWidth;
191         else
192             return mWidthString;
193     }
194
195     /** @param width */
196     public void setWidth(int w) {
197         mWidth = w;
198     }
199
200     /** @param width */
201     public void setWidthString(String JavaDoc w) {
202         mWidthString = w;
203     }
204
205
206     /** @return height */
207     public int getHeight() {
208         return mHeight;
209     }
210
211     /** @return width */
212     public String JavaDoc getHeightXML() {
213         if (mHeightString == null)
214             return "" + mHeight;
215         else
216             return mHeightString;
217     }
218
219     /** @param height */
220     public void setHeight(int h) {
221         mHeight = h;
222     }
223
224     /** @param height */
225     public void setHeightString(String JavaDoc h) {
226         mHeightString = h;
227     }
228
229
230     /** @return maxTextWidth */
231     public int getMaxTextWidth() {
232         return mMaxTextWidth;
233     }
234
235     /** @param maxTextWidth */
236     public void setMaxTextWidth(int h) {
237         mMaxTextWidth = h;
238     }
239
240     /** @return maxTextHeight */
241     public int getMaxTextHeight() {
242         return mMaxTextHeight;
243     }
244
245     /** @param maxTextHeight */
246     public void setMaxTextHeight(int h) {
247         mMaxTextHeight = h;
248     }
249
250
251     /** @return Background color */
252     public int getBGColor() {
253         return mBGColor;
254     }
255
256     /** @return Returns bgcolor as a hexadecimal string */
257     // TODO: [12-21-2002 ows] This belongs in a utility library.
258
public String JavaDoc getBGColorString() {
259         String JavaDoc red = Integer.toHexString((mBGColor >> 16) & 0xff);
260         String JavaDoc green = Integer.toHexString((mBGColor >> 8) & 0xff);
261         String JavaDoc blue = Integer.toHexString(mBGColor & 0xff);
262         if (red.length() == 1)
263             red = "0" + red;
264         if (green.length() == 1)
265             green = "0" + green;
266         if (blue.length() == 1)
267             blue = "0" + blue;
268         return "#" + red + green + blue;
269     }
270
271     /** @param BGColor Background color */
272     public void setBGColor(int BGColor) {
273         mBGColor = BGColor;
274     }
275
276     /** @return Title */
277     public String JavaDoc getTitle() {
278         return mTitle;
279
280     }
281     
282     public String JavaDoc getISBN() {
283         return "192975213X";
284     }
285     
286     /** @param title */
287     public void setTitle(String JavaDoc t) {
288         mTitle = t;
289     }
290
291     /** @return Heartbeat */
292     public long getHeartbeat() {
293         return mHeartbeat;
294
295     }
296
297     /** @param heartbeat */
298     public void setHeartbeat(long heartbeat) {
299         mHeartbeat = heartbeat;
300     }
301
302     /** @return Group */
303     public String JavaDoc getGroup() {
304         return mGroup;
305
306     }
307
308     /** @param group */
309     public void setGroup(String JavaDoc g) {
310         mGroup = g;
311     }
312
313     /** @return Authenticator */
314     public String JavaDoc getAuthenticator() {
315         return mAuthenticator;
316
317     }
318
319     /** @param authenticator */
320     public void setAuthenticator(String JavaDoc a) {
321         mAuthenticator = a;
322     }
323
324     /** @return send user disconnect */
325     public boolean doSendUserDisconnect() {
326         return mSendUserDisconnect;
327     }
328
329     /** @param sud */
330     public void setSendUserDisconnect(boolean sud) {
331         mSendUserDisconnect = sud;
332     }
333
334     /** @param serverless */
335     public void setProxied(boolean val) {
336         mProxied = val;
337     }
338
339     /** @return is this app compiling for serverless deployment */
340     public boolean isProxied() {
341         return mProxied;
342     }
343
344     /** @return is connected */
345     public boolean isConnected() {
346         return mIsConnected;
347     }
348
349     /** @param isConnected */
350     public void setIsConnected(boolean isConnected) {
351         mIsConnected = isConnected;
352     }
353
354     /** @return agents */
355     public Set getAgents() {
356         return mAgents;
357     }
358
359     /** @param agent agent's URL */
360     public void addAgent(String JavaDoc agent) {
361         if (mAgents == null)
362             mAgents = new HashSet();
363         mAgents.add(agent);
364     }
365
366
367     /** @return font info */
368     public FontInfo getFontInfo() {
369         return mFontInfo;
370     }
371
372     /** @return security options */
373     public Map getSecurityOptions() {
374         return mSecurityOptions;
375     }
376
377     /** @param element */
378     public void setSecurityOptions(Element element) {
379         Configuration.addOption(mSecurityOptions, element);
380     }
381
382     public String JavaDoc getXML(String JavaDoc content) {
383         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
384         buffer.append(
385             "<canvas " +
386             "title=\"" + XMLUtils.escapeXml(getTitle()) + "\" " +
387             "bgcolor=\"" + getBGColorString() + "\" " +
388             "width=\"" + getWidthXML() + "\" " +
389             "height=\"" + getHeightXML() + "\" " +
390             "runtime=\"" + getSWFVersion() +"\" " +
391             ">");
392         buffer.append(content);
393         buffer.append(getInfoAsString());
394         if (mCompilationWarningXML != null)
395             buffer.append("<warnings>" + mCompilationWarningXML + "</warnings>");
396         buffer.append("</canvas>");
397         return buffer.toString();
398     }
399     
400     /**
401      * Initialize persistent connection values.
402      */

403     protected void initializeConnection(Element elt) {
404         // TODO: [2003-10-16 pkang] Create and move this function into
405
// ConnectionCompiler.java
406
Element eltConnection = elt.getChild("connection", elt.getNamespace());
407         if (eltConnection!=null) {
408
409             setIsConnected(true);
410             setSendUserDisconnect(DEFAULT_SENDUSERDISCONNECT);
411             setHeartbeat(DEFAULT_HEARTBEAT);
412
413             String JavaDoc heartbeat = eltConnection.getAttributeValue("heartbeat");
414             if (heartbeat != null) {
415                 try {
416                     setHeartbeat(Long.parseLong(heartbeat));
417                 } catch (NumberFormatException JavaDoc e) {
418                     throw new CompilationError(elt, "heartbeat", e);
419                 }
420             }
421
422             String JavaDoc sendUserDisconnect =
423                 eltConnection.getAttributeValue("receiveuserdisconnect");
424             if (sendUserDisconnect != null) {
425                 setSendUserDisconnect(Boolean.valueOf(sendUserDisconnect).booleanValue());
426             }
427
428             String JavaDoc group = eltConnection.getAttributeValue("group");
429             if (group != null) {
430                 setGroup(group);
431             }
432
433             // Don't set a default authenticator in canvas. We want to be able
434
// to override this through lps.properties. Return null if one does
435
// not exist.
436
String JavaDoc authenticator = eltConnection.getAttributeValue("authenticator");
437             if (authenticator != null) {
438                 setAuthenticator(authenticator);
439             }
440
441             List agents = eltConnection.getChildren("agent", elt.getNamespace());
442             for (int i=0; i < agents.size(); i++) {
443                 Element eltAgent = (Element)agents.get(i);
444                 String JavaDoc url = eltAgent.getAttributeValue("url");
445                 if (url != null || ! url.equals(""))
446                     addAgent(url);
447             }
448         }
449     }
450 }
451
Popular Tags