KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openlaszlo > sc > AddInputText


1 /* *****************************************************************************
2  * addinputtext.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 package org.openlaszlo.sc;
11
12 import org.openlaszlo.utils.FileUtils;
13 import org.openlaszlo.utils.StringUtils;
14 import org.openlaszlo.compiler.FontInfo;
15 import org.openlaszlo.iv.flash.api.*;
16 import org.openlaszlo.iv.flash.api.action.*;
17 import org.openlaszlo.iv.flash.api.image.*;
18 import org.openlaszlo.iv.flash.api.sound.*;
19 import org.openlaszlo.iv.flash.api.text.*;
20 import org.openlaszlo.iv.flash.util.*;
21 import org.openlaszlo.iv.flash.cache.*;
22 import org.apache.log4j.*;
23
24 import java.io.*;
25 import java.util.*;
26
27 // jgen 1.4
28
import java.awt.geom.Rectangle2D JavaDoc;
29
30
31 /** Add in fixed-size input text resource with specified dimensions.
32  *
33  * We get a list of resource names like this:
34  *
35  * 'lzinputtext/lztahoe8/8/plain/398/157'
36  *
37  * We need to parse out font name, size ,style, and width/height, and
38  * create a new textfield for them with the WORDWRAP flag asserted.
39  */

40
41 public class AddInputText {
42     private static Logger mLogger = Logger.getLogger(AddInputText.class);
43
44     /** Constant */
45     private static final int TWIP = 20;
46
47     /** Leading for text and input text */
48     private static int mTextLeading = 2;
49
50     static public void main (String JavaDoc args[]) {
51         addtext(args[0], args[1], new String JavaDoc[] {args[2], args[3]} );
52     }
53
54     static public void addtext (String JavaDoc infile, String JavaDoc outfile, String JavaDoc resourceNames[]) {
55         try {
56             FlashFile f = FlashFile.parse(infile);
57             OutputStream out = new FileOutputStream(outfile);
58
59             java.util.Enumeration JavaDoc defs = f.definitions();
60             FontsCollector fc = new FontsCollector();
61
62             HashMap fontsTable = new HashMap();
63
64             while(defs.hasMoreElements()) {
65                 FlashDef def = (FlashDef)defs.nextElement();
66                 if (def instanceof FontDef) {
67                     FontDef fontDef = (FontDef)def;
68                     Font font = fontDef.getFont();
69                     String JavaDoc bold = ((font.BOLD & font.flags) > 0 ? "bold" : "");
70                     String JavaDoc italic = ((font.ITALIC & font.flags) > 0 ?
71                                      "italic" : "");
72                     //System.out.println("Copying font " + font.getFontName()
73
//+ bold + italic);
74
FontDef fdef = fc.addFont(font, null);
75                     fdef.setWriteAllChars(true);
76                     fdef.setWriteLayout(true);
77                     fontsTable.put(font.getFontName() + "::"+bold+italic, font);
78                 }
79             }
80
81
82             // Look up fonts for resources, add custom input text fields
83
for (int i = 0; i < resourceNames.length; i++) {
84                 String JavaDoc rsrc = resourceNames[i];
85                 System.out.println("addinputtext: processing "+rsrc);
86                 // parse string of form 'lzinputtext/lztahoe8/8/plain/398/157[/html][/passwd]/(m|s)' into FontInfo, w/h
87
String JavaDoc path[] = StringUtils.split(rsrc, "/");
88                 if (path.length < 7) {
89                     Exception JavaDoc e = new RuntimeException JavaDoc("inputtext resource name '"+rsrc+"' couldn't be parsed into a form like lzinputtext/lztahoe8/8/plain/398/157[/html][/passwd]/(m|s)");
90                     e.printStackTrace();
91                     throw e;
92                 }
93                 String JavaDoc fname = path[1];
94                 String JavaDoc fsize = path[2];
95                 String JavaDoc fstyle = path[3];
96                 int width = (int) Double.parseDouble(path[4]);
97                 int height = (int) Double.parseDouble(path[5]);
98                 Font font = (Font) fontsTable.get(fname + "::" + (fstyle.equals("plain") ? "" : fstyle));
99                 if (font == null) {
100                     Exception JavaDoc e = new RuntimeException JavaDoc("could not find font "+fname+" " +fstyle +" for inputtext resource named '"+rsrc);
101                     e.printStackTrace();
102                     throw e;
103                 }
104                 boolean multiline = false;
105                 boolean password = false;
106
107                 // look for "passwd" or "m" tokens
108
for (int j = 6; j < path.length; j++) {
109                     if (path[j].equals("passwd")) {
110                         password = true;
111                     } else if (path[j].equals("m")) {
112                         multiline = true;
113                     }
114                 }
115
116                 FontInfo finfo = new FontInfo(fname, fsize, fstyle);
117                 addCustomInputText(f, font, finfo, width, height, multiline, password);
118             }
119
120             writeFlashFile(f, fc, out);
121
122         } catch (Exception JavaDoc e) {
123             mLogger.error("exception in AddInputText.addtext", e);
124         }
125     }
126
127     static void writeFlashFile(FlashFile f, FontsCollector fc, OutputStream out) {
128         try {
129             InputStream input;
130             input = f.generate(fc, null, false).getInputStream();
131             FileUtils.send(input, out);
132             input.close();
133             out.close();
134         } catch (Exception JavaDoc e) {
135             mLogger.error("exception in AddInputText.writeFlashFile", e);
136         }
137     }
138
139
140     /** Create a custom text input field that will correspond to a specific lzx input text view.
141      */

142     static public void addCustomInputText(FlashFile f, Font font, FontInfo fontInfo, int width, int height,
143                                           boolean multiline, boolean password)
144     {
145         String JavaDoc fontName = fontInfo.getName();
146         String JavaDoc mstring;
147         mstring = multiline ? "/m" : "/s";
148
149         String JavaDoc name =
150             "lzinputtext/" +
151             fontName + "/" +
152             fontInfo.getSize() + "/" +
153             fontInfo.getStyle() + "/" + width + "/" + height +
154             (password ? "/passwd" : "") + mstring;
155
156         // Create a movieclip with a single frame with
157
// a text field of the correct size.
158
Script movieClip = new Script(1);
159         Frame frame = movieClip.newFrame();
160
161         TextField input = new TextField("", "text",
162                                          font, fontInfo.getSize()*TWIP, AlphaColor.black);
163
164         int flags = input.getFlags();
165
166         if (password) {
167             flags |= TextField.PASSWORD;
168         }
169
170         input.setFlags(flags
171                        | TextField.USEOUTLINES
172                        | TextField.HASFONT
173                        | (multiline ? TextField.MULTILINE : 0)
174                        | (multiline ? TextField.WORDWRAP : 0)
175                        );
176
177         // left, right, indent, and align don't make sense
178
// when we do all input text wrapping ourselves.
179
// Leading still matters though!
180
input.setLayout(0, 0, 0, 0, mTextLeading*TWIP);
181
182         input.setBounds(new Rectangle2D.Double JavaDoc(0, 0, width*TWIP, height*TWIP));
183
184         frame.addInstance(input, 1, null, null);
185         frame.addStopAction();
186
187         // Add movieClip to library
188
f.addDefToLibrary(name, movieClip);
189         // Export it.
190
ExportAssets ea = new ExportAssets();
191         ea.addAsset(name, movieClip);
192         System.out.println("addCustomInputText: added "+name);
193         Timeline timeline = f.getMainScript().getTimeline();
194         frame = timeline.getFrameAt(timeline.getFrameCount() - 1);
195         frame.addFlashObject(ea);
196     }
197  
198 }
199
Popular Tags