001: /* *****************************************************************************
002: * addinputtext.java
003: * ****************************************************************************/
004:
005: /* J_LZ_COPYRIGHT_BEGIN *******************************************************
006: * Copyright 2001-2004 Laszlo Systems, Inc. All Rights Reserved. *
007: * Use is subject to license terms. *
008: * J_LZ_COPYRIGHT_END *********************************************************/
009:
010: package org.openlaszlo.sc;
011:
012: import org.openlaszlo.utils.FileUtils;
013: import org.openlaszlo.utils.StringUtils;
014: import org.openlaszlo.compiler.FontInfo;
015: import org.openlaszlo.iv.flash.api.*;
016: import org.openlaszlo.iv.flash.api.action.*;
017: import org.openlaszlo.iv.flash.api.image.*;
018: import org.openlaszlo.iv.flash.api.sound.*;
019: import org.openlaszlo.iv.flash.api.text.*;
020: import org.openlaszlo.iv.flash.util.*;
021: import org.openlaszlo.iv.flash.cache.*;
022: import org.apache.log4j.*;
023:
024: import java.io.*;
025: import java.util.*;
026:
027: // jgen 1.4
028: import java.awt.geom.Rectangle2D;
029:
030: /** Add in fixed-size input text resource with specified dimensions.
031: *
032: * We get a list of resource names like this:
033: *
034: * 'lzinputtext/lztahoe8/8/plain/398/157'
035: *
036: * We need to parse out font name, size ,style, and width/height, and
037: * create a new textfield for them with the WORDWRAP flag asserted.
038: */
039:
040: public class AddInputText {
041: private static Logger mLogger = Logger
042: .getLogger(AddInputText.class);
043:
044: /** Constant */
045: private static final int TWIP = 20;
046:
047: /** Leading for text and input text */
048: private static int mTextLeading = 2;
049:
050: static public void main(String args[]) {
051: addtext(args[0], args[1], new String[] { args[2], args[3] });
052: }
053:
054: static public void addtext(String infile, String outfile,
055: String resourceNames[]) {
056: try {
057: FlashFile f = FlashFile.parse(infile);
058: OutputStream out = new FileOutputStream(outfile);
059:
060: java.util.Enumeration defs = f.definitions();
061: FontsCollector fc = new FontsCollector();
062:
063: HashMap fontsTable = new HashMap();
064:
065: while (defs.hasMoreElements()) {
066: FlashDef def = (FlashDef) defs.nextElement();
067: if (def instanceof FontDef) {
068: FontDef fontDef = (FontDef) def;
069: Font font = fontDef.getFont();
070: String bold = ((font.BOLD & font.flags) > 0 ? "bold"
071: : "");
072: String italic = ((font.ITALIC & font.flags) > 0 ? "italic"
073: : "");
074: //System.out.println("Copying font " + font.getFontName()
075: //+ bold + italic);
076: FontDef fdef = fc.addFont(font, null);
077: fdef.setWriteAllChars(true);
078: fdef.setWriteLayout(true);
079: fontsTable.put(font.getFontName() + "::" + bold
080: + italic, font);
081: }
082: }
083:
084: // Look up fonts for resources, add custom input text fields
085: for (int i = 0; i < resourceNames.length; i++) {
086: String rsrc = resourceNames[i];
087: System.out.println("addinputtext: processing " + rsrc);
088: // parse string of form 'lzinputtext/lztahoe8/8/plain/398/157[/html][/passwd]/(m|s)' into FontInfo, w/h
089: String path[] = StringUtils.split(rsrc, "/");
090: if (path.length < 7) {
091: Exception e = new RuntimeException(
092: /* (non-Javadoc)
093: * @i18n.test
094: * @org-mes="inputtext resource name '" + p[0] + "' couldn't be parsed into a form like lzinputtext/lztahoe8/8/plain/398/157[/html][/passwd]/(m|s)"
095: */
096: org.openlaszlo.i18n.LaszloMessages.getMessage(
097: AddInputText.class.getName(), "051018-94",
098: new Object[] { rsrc }));
099: e.printStackTrace();
100: throw e;
101: }
102: String fname = path[1];
103: String fsize = path[2];
104: String fstyle = path[3];
105: int width = (int) Double.parseDouble(path[4]);
106: int height = (int) Double.parseDouble(path[5]);
107: Font font = (Font) fontsTable.get(fname + "::"
108: + (fstyle.equals("plain") ? "" : fstyle));
109: if (font == null) {
110: Exception e = new RuntimeException(
111: /* (non-Javadoc)
112: * @i18n.test
113: * @org-mes="could not find font " + p[0] + " " + p[1] + " for inputtext resource named '" + p[2]
114: */
115: org.openlaszlo.i18n.LaszloMessages.getMessage(
116: AddInputText.class.getName(), "051018-112",
117: new Object[] { fname, fstyle, rsrc }));
118: e.printStackTrace();
119: throw e;
120: }
121: boolean multiline = false;
122: boolean password = false;
123:
124: // look for "passwd" or "m" tokens
125: for (int j = 6; j < path.length; j++) {
126: if (path[j].equals("passwd")) {
127: password = true;
128: } else if (path[j].equals("m")) {
129: multiline = true;
130: }
131: }
132:
133: FontInfo finfo = new FontInfo(fname, fsize, fstyle);
134: addCustomInputText(f, font, finfo, width, height,
135: multiline, password);
136: }
137:
138: writeFlashFile(f, fc, out);
139:
140: } catch (Exception e) {
141: mLogger.error(
142: /* (non-Javadoc)
143: * @i18n.test
144: * @org-mes="exception in AddInputText.addtext"
145: */
146: org.openlaszlo.i18n.LaszloMessages.getMessage(
147: AddInputText.class.getName(), "051018-142"), e);
148: }
149: }
150:
151: static void writeFlashFile(FlashFile f, FontsCollector fc,
152: OutputStream out) {
153: try {
154: InputStream input;
155: input = f.generate(fc, null, false).getInputStream();
156: FileUtils.send(input, out);
157: input.close();
158: out.close();
159: } catch (Exception e) {
160: mLogger.error(
161: /* (non-Javadoc)
162: * @i18n.test
163: * @org-mes="exception in AddInputText.writeFlashFile"
164: */
165: org.openlaszlo.i18n.LaszloMessages.getMessage(
166: AddInputText.class.getName(), "051018-161"), e);
167: }
168: }
169:
170: /** Create a custom text input field that will correspond to a specific lzx input text view.
171: */
172: static public void addCustomInputText(FlashFile f, Font font,
173: FontInfo fontInfo, int width, int height,
174: boolean multiline, boolean password) {
175: String fontName = fontInfo.getName();
176: String mstring;
177: mstring = multiline ? "/m" : "/s";
178:
179: String name = "lzinputtext/" + fontName + "/"
180: + fontInfo.getSize() + "/" + fontInfo.getStyle() + "/"
181: + width + "/" + height + (password ? "/passwd" : "")
182: + mstring;
183:
184: // Create a movieclip with a single frame with
185: // a text field of the correct size.
186: Script movieClip = new Script(1);
187: Frame frame = movieClip.newFrame();
188:
189: TextField input = new TextField("", "text", font, fontInfo
190: .getSize()
191: * TWIP, AlphaColor.black);
192:
193: int flags = input.getFlags();
194:
195: if (password) {
196: flags |= TextField.PASSWORD;
197: }
198:
199: input.setFlags(flags | TextField.USEOUTLINES
200: | TextField.HASFONT
201: | (multiline ? TextField.MULTILINE : 0)
202: | (multiline ? TextField.WORDWRAP : 0));
203:
204: // left, right, indent, and align don't make sense
205: // when we do all input text wrapping ourselves.
206: // Leading still matters though!
207: input.setLayout(0, 0, 0, 0, mTextLeading * TWIP);
208:
209: input.setBounds(new Rectangle2D.Double(0, 0, width * TWIP,
210: height * TWIP));
211:
212: frame.addInstance(input, 1, null, null);
213: frame.addStopAction();
214:
215: // Add movieClip to library
216: f.addDefToLibrary(name, movieClip);
217: // Export it.
218: ExportAssets ea = new ExportAssets();
219: ea.addAsset(name, movieClip);
220: System.out.println("addCustomInputText: added " + name);
221: Timeline timeline = f.getMainScript().getTimeline();
222: frame = timeline.getFrameAt(timeline.getFrameCount() - 1);
223: frame.addFlashObject(ea);
224: }
225:
226: }
|