KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openlaszlo > iv > flash > api > text > TextField


1 /*
2  * $Id: TextField.java,v 1.4 2002/07/15 22:39:32 skavish Exp $
3  *
4  * ==========================================================================
5  *
6  * The JGenerator Software License, Version 1.0
7  *
8  * Copyright (c) 2000 Dmitry Skavish (skavish@usa.net). All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution, if
22  * any, must include the following acknowlegement:
23  * "This product includes software developed by Dmitry Skavish
24  * (skavish@usa.net, http://www.flashgap.com/)."
25  * Alternately, this acknowlegement may appear in the software itself,
26  * if and wherever such third-party acknowlegements normally appear.
27  *
28  * 4. The name "The JGenerator" must not be used to endorse or promote
29  * products derived from this software without prior written permission.
30  * For written permission, please contact skavish@usa.net.
31  *
32  * 5. Products derived from this software may not be called "The JGenerator"
33  * nor may "The JGenerator" appear in their names without prior written
34  * permission of Dmitry Skavish.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL DMITRY SKAVISH OR THE OTHER
40  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  *
49  */

50
51 package org.openlaszlo.iv.flash.api.text;
52
53 import java.io.*;
54 import java.awt.geom.*;
55
56 import org.openlaszlo.iv.flash.parser.Parser;
57 import org.openlaszlo.iv.flash.util.*;
58 import org.openlaszlo.iv.flash.api.*;
59 import org.openlaszlo.iv.flash.context.Context;
60
61 /**
62  * Text field.
63  *
64  * @author Dmitry Skavish
65  */

66 public final class TextField extends FlashDef {
67
68     public static final int HASLAYOUT = 0x2000;
69     public static final int NOSELECT = 0x1000;
70     public static final int BORDER = 0x0800;
71     public static final int HTML = 0x0200;
72     public static final int USEOUTLINES = 0x0100;
73     public static final int HASTEXT = 0x0080;
74     public static final int WORDWRAP = 0x0040;
75     public static final int MULTILINE = 0x0020;
76     public static final int PASSWORD = 0x0010;
77     public static final int READONLY = 0x0008;
78     public static final int HASTEXTCOLOR = 0x0004;
79     public static final int HASMAXLENGTH = 0x0002;
80     public static final int HASFONT = 0x0001;
81
82     private Rectangle2D bounds; // text field's bounds
83
private Font font; // font
84
private int height; // font's height in twixels
85
private AlphaColor color; // text color
86
private int maxlength; // maximum length of the text
87
private int align; // 0-left,1-right,2-center,3-justify
88
private int leftmargin; // left margin in twixels
89
private int rightmargin; // right margin in twixels
90
private int indent; // indentation in twixels
91
private int leading; // leading in twixels
92
private String JavaDoc varName; // variable name
93
private String JavaDoc initText; // initial text
94
private int flags; // text field's flags
95

96     public TextField() {}
97
98     /**
99      * Creates TextField
100      *
101      * @param initText initial text (optional)
102      * @param varName variable name
103      * @param font font of the field (optional)
104      * @param height height of the font in twixels
105      * @param color color of the text field (optional)
106      */

107     public TextField( String JavaDoc initText, String JavaDoc varName, Font font, int height, AlphaColor color ) {
108         this.initText = initText;
109         this.varName = varName;
110         this.font = font;
111         this.height = height;
112         this.color = color;
113         if( font != null ) flags = HASFONT;
114         if( color != null ) flags |= HASTEXTCOLOR;
115         if( initText != null ) flags |= HASTEXT;
116     }
117
118     /**
119      * Returns maximum length of this text field
120      *
121      * @return maximum length
122      */

123     public int getMaxLength() {
124         return maxlength;
125     }
126
127     /**
128      * Sets new maximum length to this text field
129      *
130      * @param maxLenth new maximum length
131      */

132     public void setMaxLength( int maxlength ) {
133         this.maxlength = maxlength;
134         if( maxlength >= 0 ) flags |= HASMAXLENGTH;
135         else flags &= ~HASMAXLENGTH;
136     }
137
138     /**
139      * Sets new font and height
140      *
141      * @param font new font to be set
142      * @param height fonts height in twixels
143      */

144     public void setFont( Font font, int height ) {
145         this.font = font;
146         this.height = height;
147         if( font != null ) flags |= HASFONT;
148         else flags &= ~HASFONT;
149     }
150
151     /**
152      * Returns font
153      *
154      * @return this textfield font
155      */

156     public Font getFont() {
157         return font;
158     }
159
160     /**
161      * Returns font height
162      *
163      * @return font height
164      */

165     public int getHeight() {
166         return height;
167     }
168
169     /**
170      * Returns variable name
171      *
172      * @return variable name
173      */

174     public String JavaDoc getVarName() {
175         return varName;
176     }
177
178     /**
179      * Sets new variable name
180      *
181      * @param varName new variable name
182      */

183     public void setVarName( String JavaDoc varName ) {
184         this.varName = varName;
185     }
186
187     /**
188      * Returns initial text
189      *
190      * @return inittial text
191      */

192     public String JavaDoc getInitText() {
193         return initText;
194     }
195
196     /**
197      * Sets new initial text to this text field
198      *
199      * @param initText new initial text
200      */

201     public void setInitText( String JavaDoc initText ) {
202         this.initText = initText;
203         if( initText != null ) flags |= HASTEXT;
204         else flags &= ~HASTEXT;
205     }
206
207     /**
208      * Sets new flags to this text field
209      *
210      * @param flags new flags to be set
211      */

212     public void setFlags( int flags ) {
213         this.flags = flags;
214     }
215
216     /**
217      * Returns current flags of this text field
218      *
219      * @return current flags
220      */

221     public int getFlags() {
222         return flags;
223     }
224
225     /**
226      * Adds specified flags to this text field flags
227      *
228      * @param flags additional flags
229      * @return new flags
230      */

231     public int addFlags( int flags ) {
232         this.flags |= flags;
233         return flags;
234     }
235
236     /**
237      * Removes specified flags
238      *
239      * @param flags flags to be removed
240      * @return new flags
241      */

242     public int removeFlags( int flags ) {
243         this.flags &= ~flags;
244         return flags;
245     }
246
247     /**
248      * Sets bounds to this text field
249      *
250      * @param bounds text field bounds
251      */

252     public void setBounds( Rectangle2D bounds ) {
253         this.bounds = bounds;
254     }
255
256     /**
257      * Returns bounds of this text field
258      *
259      * @return bounds
260      */

261     public Rectangle2D getBounds() {
262         return bounds;
263     }
264
265     /**
266      * Sets layout of the text in this text field
267      *
268      * @param align alignment (0-left,1-right,2-center,3-justify)
269      * @param leftmargin left margin in twixels
270      * @param rightmargin right margin in twixels
271      * @param indent indentation in twixels
272      * @param leading leading in twixels
273      */

274     public void setLayout( int align, int leftmargin, int rightmargin, int indent, int leading ) {
275         this.align = align;
276         this.leftmargin = leftmargin;
277         this.rightmargin = rightmargin;
278         this.indent = indent;
279         this.leading = leading;
280         flags |= HASLAYOUT;
281     }
282
283     public int getAlign() { return align; }
284     public int getLeftMargin() { return leftmargin; }
285     public int getRightMargin() { return rightmargin; }
286     public int getIndent() { return indent; }
287     public int getLeading() { return leading; }
288
289     /**
290      * Sets color
291      *
292      * @param color text color
293      */

294     public void setColor( AlphaColor color ) {
295         this.color = color;
296         if( color != null ) flags |= HASTEXTCOLOR;
297         else flags &= ~HASTEXTCOLOR;
298     }
299
300     /**
301      * Returns text color
302      *
303      * @return text color
304      */

305     public AlphaColor getColor() {
306         return color;
307     }
308
309     public int getTag() {
310         return Tag.DEFINEEDITTEXT;
311     }
312
313     public static TextField parse( Parser p ) {
314         TextField o = new TextField();
315         // get id
316
o.setID( p.getUWord() );
317         // get bounds
318
o.bounds = p.getRect();
319
320         // get flags
321
int flags = o.flags = p.getUWord();
322
323         if( (flags&HASFONT) != 0 ) {
324             o.font = ((FontDef) p.getDef( p.getUWord() )).getFont();
325             o.height = p.getUWord();
326         }
327
328         if( (flags&HASTEXTCOLOR) != 0 ) {
329             o.color = Color.parseRGBA(p);
330         }
331
332         if( (flags&HASMAXLENGTH) != 0 ) {
333             o.maxlength = p.getUWord();
334         }
335
336         if( (flags&HASLAYOUT) != 0 ) {
337             o.align = p.getUByte();
338             o.leftmargin = p.getUWord();
339             o.rightmargin = p.getUWord();
340             o.indent = p.getUWord();
341             o.leading = p.getUWord();
342         }
343
344         o.varName = p.getString();
345
346         if( (flags&HASTEXT) != 0 ) {
347             o.initText = p.getString();
348         }
349
350         return o;
351     }
352
353     public void collectDeps( DepsCollector dc ) {
354         if( (flags&HASFONT) != 0 ) {
355             dc.addDep( font );
356         }
357     }
358
359     public void collectFonts( FontsCollector fc ) {
360         if( (flags&HASFONT) != 0 ) {
361             FontDef fdef = fc.addFont( font, null );
362             if( (flags&USEOUTLINES) != 0 ) {
363                 if( !PropertyManager.mxLibraryFontID.equalsIgnoreCase(initText) ) {
364                     fdef.setWriteLayout(true);
365                     fdef.setWriteAllChars(true); // we don't know what characters will be needed, so we direct to write all
366
}
367             }
368         }
369     }
370
371     public void write( FlashOutput fob ) {
372         if( (flags&USEOUTLINES) != 0 && PropertyManager.mxLibraryFontID.equalsIgnoreCase(initText) ) {
373             int start = fob.getPos(); // save for tag
374
fob.skip(2);
375             fob.writeDefID(this);
376             fob.write(GeomHelper.newRectangle(0, 0, 1, 1));
377             fob.writeWord(0);
378             fob.writeStringZ("");
379             int size = fob.getPos()-start-2; // 2 - short tag
380
fob.writeShortTagAt(getTag(), size, start);
381         } else {
382             // if varName.length + initText.length > 22, then generate long tag
383
int l = varName.length() + ((flags&HASTEXT)!=0?initText.length():0);
384             int start = fob.getPos(); // save for tag
385

386             if( l > 22 ) {
387                 fob.skip(6); // 6 - long tag
388
} else {
389                 fob.skip(2);
390             }
391
392             fob.writeDefID(this);
393             fob.write(bounds);
394             fob.writeWord(flags);
395
396             if( (flags&HASFONT) != 0 ) {
397                 fob.writeFontID( font );
398                 fob.writeWord(height);
399             }
400
401             if( (flags&HASTEXTCOLOR) != 0 ) {
402                 color.write(fob);
403             }
404
405             if( (flags&HASMAXLENGTH) != 0 ) {
406                 fob.writeWord(maxlength);
407             }
408
409             if( (flags&HASLAYOUT) != 0 ) {
410                 fob.writeByte(align);
411                 fob.writeWord(leftmargin);
412                 fob.writeWord(rightmargin);
413                 fob.writeWord(indent);
414                 fob.writeWord(leading);
415             }
416
417             fob.writeStringZ(varName);
418
419             if( (flags&HASTEXT) != 0 ) {
420                 fob.writeStringZ(initText);
421             }
422
423             if( l > 22 ) {
424                 int size = fob.getPos()-start-6; // 6 - long tag
425
fob.writeLongTagAt( getTag(), size, start );
426             } else {
427                 int size = fob.getPos()-start-2; // 2 - short tag
428
fob.writeShortTagAt( getTag(), size, start );
429             }
430         }
431     }
432
433     public void printContent( PrintStream out, String JavaDoc indent ) {
434         out.println( indent+"TextField: id="+getID()+", flags="+Util.w2h(flags) );
435         out.println( indent+" "+bounds );
436
437         if( (flags&HASFONT) != 0 ) {
438             out.println( indent+" font="+font.getFontName()+" height="+height );
439         }
440
441         if( (flags&HASTEXTCOLOR) != 0 ) {
442             color.printContent(out, indent+" ");
443         }
444
445         if( (flags&HASMAXLENGTH) != 0 ) {
446             out.println( indent+" maxlength="+maxlength );
447         }
448
449         if( (flags&HASLAYOUT) != 0 ) {
450             out.println( indent+" align="+align );
451             out.println( indent+" leftmargin="+leftmargin );
452             out.println( indent+" rightmargin="+rightmargin );
453             out.println( indent+" indent="+indent );
454             out.println( indent+" leading="+leading );
455         }
456
457         out.println( indent+" varName='"+varName+"'" );
458
459         if( (flags&HASTEXT) != 0 ) {
460             out.println( indent+" initText='"+initText+"'" );
461         }
462     }
463
464     public void apply( Context context ) {
465         super.apply( context );
466         varName = context.apply( varName );
467         initText = context.apply( initText );
468     }
469
470     protected boolean _isConstant() {
471         return !Util.hasVar(varName) && !Util.hasVar(initText);
472     }
473
474     protected FlashItem copyInto( FlashItem item, ScriptCopier copier ) {
475         super.copyInto( item, copier );
476         ((TextField)item).bounds = (Rectangle2D) bounds.clone();
477         ((TextField)item).font = font;
478         ((TextField)item).height = height;
479         ((TextField)item).color = (AlphaColor) color.getCopy(copier);
480         ((TextField)item).maxlength = maxlength;
481         ((TextField)item).align = align;
482         ((TextField)item).leftmargin = leftmargin;
483         ((TextField)item).rightmargin = rightmargin;
484         ((TextField)item).indent = indent;
485         ((TextField)item).leading = leading;
486         ((TextField)item).varName = varName;
487         ((TextField)item).initText = initText;
488         ((TextField)item).flags = flags;
489         return item;
490     }
491
492     public FlashItem getCopy( ScriptCopier copier ) {
493         return copyInto( new TextField(), copier );
494     }
495
496 }
497
Popular Tags