001: /*
002: * $Id: TextField.java,v 1.4 2002/07/15 22:39:32 skavish Exp $
003: *
004: * ==========================================================================
005: *
006: * The JGenerator Software License, Version 1.0
007: *
008: * Copyright (c) 2000 Dmitry Skavish (skavish@usa.net). All rights reserved.
009: *
010: * Redistribution and use in source and binary forms, with or without
011: * modification, are permitted provided that the following conditions are met:
012: *
013: * 1. Redistributions of source code must retain the above copyright
014: * notice, this list of conditions and the following disclaimer.
015: *
016: * 2. Redistributions in binary form must reproduce the above copyright
017: * notice, this list of conditions and the following disclaimer in
018: * the documentation and/or other materials provided with the
019: * distribution.
020: *
021: * 3. The end-user documentation included with the redistribution, if
022: * any, must include the following acknowlegement:
023: * "This product includes software developed by Dmitry Skavish
024: * (skavish@usa.net, http://www.flashgap.com/)."
025: * Alternately, this acknowlegement may appear in the software itself,
026: * if and wherever such third-party acknowlegements normally appear.
027: *
028: * 4. The name "The JGenerator" must not be used to endorse or promote
029: * products derived from this software without prior written permission.
030: * For written permission, please contact skavish@usa.net.
031: *
032: * 5. Products derived from this software may not be called "The JGenerator"
033: * nor may "The JGenerator" appear in their names without prior written
034: * permission of Dmitry Skavish.
035: *
036: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
037: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
038: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
039: * DISCLAIMED. IN NO EVENT SHALL DMITRY SKAVISH OR THE OTHER
040: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
041: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
042: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
043: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
044: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
045: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
046: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
047: * SUCH DAMAGE.
048: *
049: */
050:
051: package org.openlaszlo.iv.flash.api.text;
052:
053: import java.io.*;
054: import java.awt.geom.*;
055:
056: import org.openlaszlo.iv.flash.parser.Parser;
057: import org.openlaszlo.iv.flash.util.*;
058: import org.openlaszlo.iv.flash.api.*;
059: import org.openlaszlo.iv.flash.context.Context;
060:
061: /**
062: * Text field.
063: *
064: * @author Dmitry Skavish
065: */
066: public final class TextField extends FlashDef {
067:
068: public static final int HASLAYOUT = 0x2000;
069: public static final int NOSELECT = 0x1000;
070: public static final int BORDER = 0x0800;
071: public static final int HTML = 0x0200;
072: public static final int USEOUTLINES = 0x0100;
073: public static final int HASTEXT = 0x0080;
074: public static final int WORDWRAP = 0x0040;
075: public static final int MULTILINE = 0x0020;
076: public static final int PASSWORD = 0x0010;
077: public static final int READONLY = 0x0008;
078: public static final int HASTEXTCOLOR = 0x0004;
079: public static final int HASMAXLENGTH = 0x0002;
080: public static final int HASFONT = 0x0001;
081:
082: private Rectangle2D bounds; // text field's bounds
083: private Font font; // font
084: private int height; // font's height in twixels
085: private AlphaColor color; // text color
086: private int maxlength; // maximum length of the text
087: private int align; // 0-left,1-right,2-center,3-justify
088: private int leftmargin; // left margin in twixels
089: private int rightmargin; // right margin in twixels
090: private int indent; // indentation in twixels
091: private int leading; // leading in twixels
092: private String varName; // variable name
093: private String initText; // initial text
094: private int flags; // text field's flags
095:
096: public TextField() {
097: }
098:
099: /**
100: * Creates TextField
101: *
102: * @param initText initial text (optional)
103: * @param varName variable name
104: * @param font font of the field (optional)
105: * @param height height of the font in twixels
106: * @param color color of the text field (optional)
107: */
108: public TextField(String initText, String varName, Font font,
109: int height, AlphaColor color) {
110: this .initText = initText;
111: this .varName = varName;
112: this .font = font;
113: this .height = height;
114: this .color = color;
115: if (font != null)
116: flags = HASFONT;
117: if (color != null)
118: flags |= HASTEXTCOLOR;
119: if (initText != null)
120: flags |= HASTEXT;
121: }
122:
123: /**
124: * Returns maximum length of this text field
125: *
126: * @return maximum length
127: */
128: public int getMaxLength() {
129: return maxlength;
130: }
131:
132: /**
133: * Sets new maximum length to this text field
134: *
135: * @param maxLenth new maximum length
136: */
137: public void setMaxLength(int maxlength) {
138: this .maxlength = maxlength;
139: if (maxlength >= 0)
140: flags |= HASMAXLENGTH;
141: else
142: flags &= ~HASMAXLENGTH;
143: }
144:
145: /**
146: * Sets new font and height
147: *
148: * @param font new font to be set
149: * @param height fonts height in twixels
150: */
151: public void setFont(Font font, int height) {
152: this .font = font;
153: this .height = height;
154: if (font != null)
155: flags |= HASFONT;
156: else
157: flags &= ~HASFONT;
158: }
159:
160: /**
161: * Returns font
162: *
163: * @return this textfield font
164: */
165: public Font getFont() {
166: return font;
167: }
168:
169: /**
170: * Returns font height
171: *
172: * @return font height
173: */
174: public int getHeight() {
175: return height;
176: }
177:
178: /**
179: * Returns variable name
180: *
181: * @return variable name
182: */
183: public String getVarName() {
184: return varName;
185: }
186:
187: /**
188: * Sets new variable name
189: *
190: * @param varName new variable name
191: */
192: public void setVarName(String varName) {
193: this .varName = varName;
194: }
195:
196: /**
197: * Returns initial text
198: *
199: * @return inittial text
200: */
201: public String getInitText() {
202: return initText;
203: }
204:
205: /**
206: * Sets new initial text to this text field
207: *
208: * @param initText new initial text
209: */
210: public void setInitText(String initText) {
211: this .initText = initText;
212: if (initText != null)
213: flags |= HASTEXT;
214: else
215: flags &= ~HASTEXT;
216: }
217:
218: /**
219: * Sets new flags to this text field
220: *
221: * @param flags new flags to be set
222: */
223: public void setFlags(int flags) {
224: this .flags = flags;
225: }
226:
227: /**
228: * Returns current flags of this text field
229: *
230: * @return current flags
231: */
232: public int getFlags() {
233: return flags;
234: }
235:
236: /**
237: * Adds specified flags to this text field flags
238: *
239: * @param flags additional flags
240: * @return new flags
241: */
242: public int addFlags(int flags) {
243: this .flags |= flags;
244: return flags;
245: }
246:
247: /**
248: * Removes specified flags
249: *
250: * @param flags flags to be removed
251: * @return new flags
252: */
253: public int removeFlags(int flags) {
254: this .flags &= ~flags;
255: return flags;
256: }
257:
258: /**
259: * Sets bounds to this text field
260: *
261: * @param bounds text field bounds
262: */
263: public void setBounds(Rectangle2D bounds) {
264: this .bounds = bounds;
265: }
266:
267: /**
268: * Returns bounds of this text field
269: *
270: * @return bounds
271: */
272: public Rectangle2D getBounds() {
273: return bounds;
274: }
275:
276: /**
277: * Sets layout of the text in this text field
278: *
279: * @param align alignment (0-left,1-right,2-center,3-justify)
280: * @param leftmargin left margin in twixels
281: * @param rightmargin right margin in twixels
282: * @param indent indentation in twixels
283: * @param leading leading in twixels
284: */
285: public void setLayout(int align, int leftmargin, int rightmargin,
286: int indent, int leading) {
287: this .align = align;
288: this .leftmargin = leftmargin;
289: this .rightmargin = rightmargin;
290: this .indent = indent;
291: this .leading = leading;
292: flags |= HASLAYOUT;
293: }
294:
295: public int getAlign() {
296: return align;
297: }
298:
299: public int getLeftMargin() {
300: return leftmargin;
301: }
302:
303: public int getRightMargin() {
304: return rightmargin;
305: }
306:
307: public int getIndent() {
308: return indent;
309: }
310:
311: public int getLeading() {
312: return leading;
313: }
314:
315: /**
316: * Sets color
317: *
318: * @param color text color
319: */
320: public void setColor(AlphaColor color) {
321: this .color = color;
322: if (color != null)
323: flags |= HASTEXTCOLOR;
324: else
325: flags &= ~HASTEXTCOLOR;
326: }
327:
328: /**
329: * Returns text color
330: *
331: * @return text color
332: */
333: public AlphaColor getColor() {
334: return color;
335: }
336:
337: public int getTag() {
338: return Tag.DEFINEEDITTEXT;
339: }
340:
341: public static TextField parse(Parser p) {
342: TextField o = new TextField();
343: // get id
344: o.setID(p.getUWord());
345: // get bounds
346: o.bounds = p.getRect();
347:
348: // get flags
349: int flags = o.flags = p.getUWord();
350:
351: if ((flags & HASFONT) != 0) {
352: o.font = ((FontDef) p.getDef(p.getUWord())).getFont();
353: o.height = p.getUWord();
354: }
355:
356: if ((flags & HASTEXTCOLOR) != 0) {
357: o.color = Color.parseRGBA(p);
358: }
359:
360: if ((flags & HASMAXLENGTH) != 0) {
361: o.maxlength = p.getUWord();
362: }
363:
364: if ((flags & HASLAYOUT) != 0) {
365: o.align = p.getUByte();
366: o.leftmargin = p.getUWord();
367: o.rightmargin = p.getUWord();
368: o.indent = p.getUWord();
369: o.leading = p.getUWord();
370: }
371:
372: o.varName = p.getString();
373:
374: if ((flags & HASTEXT) != 0) {
375: o.initText = p.getString();
376: }
377:
378: return o;
379: }
380:
381: public void collectDeps(DepsCollector dc) {
382: if ((flags & HASFONT) != 0) {
383: dc.addDep(font);
384: }
385: }
386:
387: public void collectFonts(FontsCollector fc) {
388: if ((flags & HASFONT) != 0) {
389: FontDef fdef = fc.addFont(font, null);
390: if ((flags & USEOUTLINES) != 0) {
391: if (!PropertyManager.mxLibraryFontID
392: .equalsIgnoreCase(initText)) {
393: fdef.setWriteLayout(true);
394: fdef.setWriteAllChars(true); // we don't know what characters will be needed, so we direct to write all
395: }
396: }
397: }
398: }
399:
400: public void write(FlashOutput fob) {
401: if ((flags & USEOUTLINES) != 0
402: && PropertyManager.mxLibraryFontID
403: .equalsIgnoreCase(initText)) {
404: int start = fob.getPos(); // save for tag
405: fob.skip(2);
406: fob.writeDefID(this );
407: fob.write(GeomHelper.newRectangle(0, 0, 1, 1));
408: fob.writeWord(0);
409: fob.writeStringZ("");
410: int size = fob.getPos() - start - 2; // 2 - short tag
411: fob.writeShortTagAt(getTag(), size, start);
412: } else {
413: // if varName.length + initText.length > 22, then generate long tag
414: int l = varName.length()
415: + ((flags & HASTEXT) != 0 ? initText.length() : 0);
416: int start = fob.getPos(); // save for tag
417:
418: if (l > 22) {
419: fob.skip(6); // 6 - long tag
420: } else {
421: fob.skip(2);
422: }
423:
424: fob.writeDefID(this );
425: fob.write(bounds);
426: fob.writeWord(flags);
427:
428: if ((flags & HASFONT) != 0) {
429: fob.writeFontID(font);
430: fob.writeWord(height);
431: }
432:
433: if ((flags & HASTEXTCOLOR) != 0) {
434: color.write(fob);
435: }
436:
437: if ((flags & HASMAXLENGTH) != 0) {
438: fob.writeWord(maxlength);
439: }
440:
441: if ((flags & HASLAYOUT) != 0) {
442: fob.writeByte(align);
443: fob.writeWord(leftmargin);
444: fob.writeWord(rightmargin);
445: fob.writeWord(indent);
446: fob.writeWord(leading);
447: }
448:
449: fob.writeStringZ(varName);
450:
451: if ((flags & HASTEXT) != 0) {
452: fob.writeStringZ(initText);
453: }
454:
455: if (l > 22) {
456: int size = fob.getPos() - start - 6; // 6 - long tag
457: fob.writeLongTagAt(getTag(), size, start);
458: } else {
459: int size = fob.getPos() - start - 2; // 2 - short tag
460: fob.writeShortTagAt(getTag(), size, start);
461: }
462: }
463: }
464:
465: public void printContent(PrintStream out, String indent) {
466: out.println(indent + "TextField: id=" + getID() + ", flags="
467: + Util.w2h(flags));
468: out.println(indent + " " + bounds);
469:
470: if ((flags & HASFONT) != 0) {
471: out.println(indent + " font=" + font.getFontName()
472: + " height=" + height);
473: }
474:
475: if ((flags & HASTEXTCOLOR) != 0) {
476: color.printContent(out, indent + " ");
477: }
478:
479: if ((flags & HASMAXLENGTH) != 0) {
480: out.println(indent + " maxlength=" + maxlength);
481: }
482:
483: if ((flags & HASLAYOUT) != 0) {
484: out.println(indent + " align=" + align);
485: out.println(indent + " leftmargin=" + leftmargin);
486: out.println(indent + " rightmargin=" + rightmargin);
487: out.println(indent + " indent=" + indent);
488: out.println(indent + " leading=" + leading);
489: }
490:
491: out.println(indent + " varName='" + varName + "'");
492:
493: if ((flags & HASTEXT) != 0) {
494: out.println(indent + " initText='" + initText + "'");
495: }
496: }
497:
498: public void apply(Context context) {
499: super .apply(context);
500: varName = context.apply(varName);
501: initText = context.apply(initText);
502: }
503:
504: protected boolean _isConstant() {
505: return !Util.hasVar(varName) && !Util.hasVar(initText);
506: }
507:
508: protected FlashItem copyInto(FlashItem item, ScriptCopier copier) {
509: super .copyInto(item, copier);
510: ((TextField) item).bounds = (Rectangle2D) bounds.clone();
511: ((TextField) item).font = font;
512: ((TextField) item).height = height;
513: ((TextField) item).color = (AlphaColor) color.getCopy(copier);
514: ((TextField) item).maxlength = maxlength;
515: ((TextField) item).align = align;
516: ((TextField) item).leftmargin = leftmargin;
517: ((TextField) item).rightmargin = rightmargin;
518: ((TextField) item).indent = indent;
519: ((TextField) item).leading = leading;
520: ((TextField) item).varName = varName;
521: ((TextField) item).initText = initText;
522: ((TextField) item).flags = flags;
523: return item;
524: }
525:
526: public FlashItem getCopy(ScriptCopier copier) {
527: return copyInto(new TextField(), copier);
528: }
529:
530: }
|