KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openlaszlo > iv > flash > api > Frame


1 /*
2  * $Id: Frame.java,v 1.10 2002/08/08 23:26:54 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;
52
53 import org.openlaszlo.iv.flash.util.*;
54 import org.openlaszlo.iv.flash.commands.*;
55
56 import org.openlaszlo.iv.flash.api.shape.*;
57 import org.openlaszlo.iv.flash.api.text.*;
58 import org.openlaszlo.iv.flash.api.action.*;
59 import org.openlaszlo.iv.flash.context.Context;
60
61 import java.awt.geom.AffineTransform JavaDoc;
62 import java.awt.geom.Rectangle2D JavaDoc;
63 import java.io.*;
64 import java.util.*;
65
66 /**
67  * A movie frame
68  * <P>
69  * A frame contains zero or more FlashObjects.
70  * A frame can have a name (to be referenced in gotoFrame action).
71  *
72  * @author Dmitry Skavish
73  * @see Timeline
74  */

75 public final class Frame extends IVVector {
76
77     private String JavaDoc name;
78     private boolean is_anchor; // flash mx
79

80     /**
81      * Creates empty frame
82      */

83     public Frame() {}
84
85     /**
86      * Creates frame of specified capacity
87      *
88      * @param capacity capacity of created frame
89      */

90     public Frame( int capacity ) {
91         super( capacity );
92     }
93
94     /**
95      * Creates frame from specified vector
96      * <P>
97      * Creates frame with capacity equal to the size of specified vector
98      * and copies all data from the specified vector to this frame
99      *
100      * @param data vector to copy from
101      */

102     public Frame( IVVector data ) {
103         super( data );
104     }
105
106     /**
107      * Returns name of this frame
108      *
109      * @return name of this frame or null
110      */

111     public String JavaDoc getName() {
112         return name;
113     }
114
115     /**
116      * Assings name to this frame
117      *
118      * @param name new name of this frame
119      */

120     public void setName( String JavaDoc name ) {
121         this.name = name;
122     }
123
124     /**
125      * Sets whether this frame is named anchor or not
126      * <P>
127      * Only for Flash MX
128      *
129      * @param is_anchor this frame is named anchor if its true
130      */

131     public void setAnchor( boolean is_anchor ) {
132         this.is_anchor = is_anchor;
133     }
134
135     /**
136      * Returns whether this frame is named anchor or not
137      * <P>
138      * Only for Flash MX
139      *
140      * @return whether this frame is named anchor or not
141      */

142     public boolean isAnchor() {
143         return is_anchor;
144     }
145
146     /**
147      * Adds flash object's instance to this frame
148      * <P>
149      * Creates new instance and adds it to this frame using specified
150      * depth, matrix and cxform.
151      *
152      * @param def flash definition instance of which has to be added
153      * @param depth layer depth of the instance to be added
154      * @param matrix transformation matrix for the instance (optional)
155      * @param cxform transformation color for the instance (optional)
156      * @return added instance
157      * @see Instance
158      */

159     public Instance addInstance( FlashDef def, int depth, AffineTransform JavaDoc matrix, CXForm cxform ) {
160         return addInstance(def, depth, matrix, cxform, null);
161     }
162
163     /**
164      * Adds flash object's instance to this frame
165      * <P>
166      * Creates new instance and adds it to this frame using specified
167      * depth, matrix, cxform and name.
168      *
169      * @param def flash definition instance of which has to be added
170      * @param depth layer depth of the instance to be added
171      * @param matrix transformation matrix for the instance (optional)
172      * @param cxform transformation color for the instance (optional)
173      * @param name name of the instance (optional)
174      * @return added instance
175      * @see Instance
176      */

177     public Instance addInstance( FlashDef def, int depth, AffineTransform JavaDoc matrix, CXForm cxform, String JavaDoc name ) {
178         Instance inst = new Instance();
179         inst.def = def;
180         inst.depth = depth;
181         inst.matrix = matrix;
182         inst.cxform = cxform;
183         inst.name = name;
184         addFlashObject( inst );
185         return inst;
186     }
187
188     /**
189      * Adds new instance of flash object which is already on a timeline with new transformation matrix
190      * <P>
191      * Creates new instance of an existing on a timeline flash object
192      * and assigns specified transformation matrix to it.
193      *
194      * @param depth layer depth of the existing instance
195      * @param matrix transformation matrix for the instance (optional)
196      * @return added instance
197      */

198     public Instance addInstance( int depth, AffineTransform JavaDoc matrix ) {
199         return addInstance(depth, matrix, null);
200     }
201
202     /**
203      * Adds new instance of flash object which is already on a timeline with new transformation matrix
204      * <P>
205      * Creates new instance of an existing on a timeline flash object
206      * and assigns specified transformation and color matrixes to it.
207      *
208      * @param depth layer depth of the existing instance
209      * @param matrix transformation matrix for the instance (optional)
210      * @param cxform color matrix for the instance (optional)
211      * @return added instance
212      */

213     public Instance addInstance( int depth, AffineTransform JavaDoc matrix, CXForm cxform ) {
214         Instance inst = new Instance();
215         inst.depth = depth;
216         inst.matrix = matrix;
217         inst.cxform = cxform;
218         inst.isMove = true;
219         addFlashObject( inst );
220         return inst;
221     }
222
223     /**
224      * Adds specified instance into specified layer
225      *
226      * @param inst specified instance
227      * @param depth layer depth
228      * @return added instance
229      */

230     public Instance addInstance( Instance inst, int depth ) {
231         inst.depth = depth;
232         addFlashObject( inst );
233         return inst;
234     }
235
236     /**
237      * "Removes" flash object's instance from specified layer
238      * <p>
239      * Does not actually remove any instance from this frame, but rather adds
240      * flash tag RemoveObject to this frame.
241      *
242      * @param depth layer depth of the existing instance to be removed
243      * @return RemoveObject tag
244      */

245     public RemoveObject removeInstance( int depth ) {
246         RemoveObject ro = new RemoveObject();
247         ro.depth = depth;
248         addFlashObject( ro );
249         return ro;
250     }
251
252     /**
253      * Adds specified flash object to this frame
254      *
255      * @param o flash object to be added
256      */

257     public void addFlashObject( FlashObject o ) {
258         addElement( o );
259     }
260
261     /**
262      * Replaces flash object at specified index with new one
263      *
264      * @param o new flash object to be replaced with
265      * @param index specified index
266      */

267     public void setFlashObjectAt( FlashObject o, int index ) {
268         setElementAt( o, index );
269     }
270
271     /**
272      * Returns flash object at specified index
273      *
274      * @param index specified index
275      * @return flash object at specified index
276      */

277     public FlashObject getFlashObjectAt( int index ) {
278         return (FlashObject) elementAt( index );
279     }
280
281     /**
282      * Removes flash object at specified index
283      *
284      * @param index specified index
285      * @return removed flash object
286      */

287     public FlashObject removeFlashObjectAt( int index ) {
288         return (FlashObject) removeElementAt( index );
289     }
290
291     /**
292      * Removes specified flash object from this frame
293      *
294      * @param o specified flash object to be removed
295      */

296     public void remove( FlashObject o ) {
297         removeElement( o );
298     }
299
300     /**
301      * Writes content of this frame to flash buffer
302      * <P>
303      * Has to be used only when this frame is NOT from main timeline.
304      *
305      * @param fob flash buffer to write to
306      */

307     public void write( FlashOutput fob ) {
308         if( name != null ) writeFrameLabel(fob);
309         super.write( fob );
310         Tag.SHOWFRAME_TAG.write( fob );
311     }
312
313     /**
314      * Writes content of this frame to flash buffer
315      * <P>
316      * Has to be used only when this frame is from main timeline.
317      *
318      * @param fob flash buffer to write to
319      * @param dc dependencies collector
320      */

321     public void generate( FlashOutput fob, DepsCollector dc ) {
322         if( name != null ) writeFrameLabel(fob);
323         for( int i=0; i<top; i++ ) {
324             FlashObject fo = (FlashObject) objects[i];
325             fo.generate( fob, dc );
326         }
327         Tag.SHOWFRAME_TAG.write( fob );
328     }
329
330     /**
331      * Collects dependencies of all the object from this frame
332      *
333      * @param dc dependencies collector
334      */

335     public void collectDeps( DepsCollector dc ) {
336         for( int i=0; i<top; i++ ) {
337             FlashObject fo = (FlashObject) objects[i];
338             fo.collectDeps(dc);
339         }
340     }
341
342     /**
343      * Processes all the scripts from this frame in the specified context
344      *
345      * @param file flash file to be used for processing scripts
346      * @param context context to be used for processing scripts
347      * @exception IVException
348      */

349     public void process( FlashFile file, Context context ) throws IVException {
350         name = context.apply( name );
351         for( int i=0; i<top; i++ ) {
352             FlashObject fo = (FlashObject) objects[i];
353             file.processObject(fo, context);
354         }
355     }
356
357     /**
358      * Applies specified context to all the object from this frame
359      *
360      * @param context specified context
361      */

362     public void apply( Context context ) {
363         name = context.apply(name);
364         for( int i=0; i<top; i++ ) {
365             FlashObject fo = (FlashObject) objects[i];
366             fo.apply(context);
367         }
368     }
369
370     /**
371      * Performs all generator commands from this frame
372      *
373      * @param file flash file to used for processing commands
374      * @param context context to used for processing commands
375      * @param parent parent script
376      * @param frame this frame's number in the parent script's timelime
377      * @exception IVException thrown if there were some errors during command processing
378      */

379     public void doCommand( FlashFile file, Context context, Script parent, int frame ) throws IVException {
380         for( int i=0; i<top; i++ ) {
381             FlashObject fo = (FlashObject) objects[i];
382             if( !(fo instanceof Instance) ) continue;
383             Instance instance = (Instance) fo;
384             GenericCommand cmd = instance.command;
385
386             if( cmd != null ) {
387                 try {
388                     cmd.doCommand( file, context, parent, frame );
389                 } catch( Throwable JavaDoc t ) {
390                     Log.logRB( Resource.ERRDOCMD, new Object JavaDoc[] {
391                                     file.getFullName(), parent.getName(),
392                                     String.valueOf(frame+1), cmd.getCommandName() }, t );
393
394                     if( PropertyManager.showErrorsInline ) {
395                         // create script with gray rectangle and text in place of the placeholder
396
Script script = new Script(1);
397                         Frame fr = script.newFrame();
398                         Shape shape = new Shape();
399                         shape.setLineStyle( new LineStyle(20, AlphaColor.black) );
400                         shape.setFillStyle0( FillStyle.newSolid( new AlphaColor( 150, 150, 150, 150 ) ) );
401                         Rectangle2D JavaDoc r = GeomHelper.newRectangle(-1024, -1024, 2048, 2048);
402                         shape.drawRectangle(r);
403                         shape.setBounds(r);
404                         fr.addInstance(shape, 1, null, null);
405
406                         FlashFile defFile = file.getDefaultSymbolFile();
407                         if( defFile != null ) {
408                             Font font = defFile.getFont( "Arial" );
409                             if( font != null ) {
410                                 String JavaDoc msg = cmd.getCommandName()+" : "+t.getMessage();
411                                 Text text = Text.newText();
412                                 TextItem item = new TextItem( msg, font, 8*20, AlphaColor.black );
413                                 text.addTextItem( item );
414                                 text.setBounds( (Rectangle2D JavaDoc) r.clone() );
415                                 AffineTransform JavaDoc m = AffineTransform.getTranslateInstance(-1024, -1024);
416                                 fr.addInstance(text, 2, m, null);
417                             }
418                         }
419                         instance.def = script;
420                         instance.command = null;
421                     } else {
422                         // just put empty shape as a definition for the instance
423
instance.def = Shape.newEmptyShape1();
424                         instance.command = null;
425                     }
426                 }
427             }
428         }
429     }
430
431     /**
432      * Adds bounds of all the object from this frame to the specified rectangle
433      *
434      * @param rect specified bounds
435      */

436     public void addBounds( Rectangle2D JavaDoc rect ) {
437         for( int i=0; i<top; i++ ) {
438             FlashObject fo = (FlashObject) objects[i];
439             GeomHelper.add( rect, fo.getBounds() );
440         }
441     }
442
443     /**
444      * Appends all the objects from the specified frame to this one
445      *
446      * @param f frame to be added to this one
447      */

448     public void append( Frame f ) {
449         for( int i=0; i<f.size(); i++ ) {
450             addFlashObject( f.getFlashObjectAt(i) );
451         }
452     }
453
454     /**
455      * Returns true if this frame is constant
456      * <P>
457      * Returns true if all the objects from this frame are constants.
458      *
459      * @return true - if this frame is constant
460      */

461     public boolean isConstant() {
462         if( Util.hasVar(name) ) return false;
463         for( int i=0; i<top; i++ ) {
464             FlashObject fo = (FlashObject) objects[i];
465             if( !fo.isConstant() ) return false;
466         }
467         return true;
468     }
469
470     /**
471      * Creates a copy of this frame
472      * <P>
473      * The copy contains copies of all the objects
474      *
475      * @param copier copier to be used when copying
476      * @return Frame casted to IVVector
477      */

478     public IVVector getCopy( ScriptCopier copier ) {
479         Frame c = new Frame( size() );
480         for( int i=0; i<top; i++ ) {
481             FlashObject fo = (FlashObject) objects[i];
482             c.setElementAt( fo.getCopy(copier), i );
483         }
484         c.setName( name );
485         return c;
486     }
487
488     /**
489      * Add a stop action to this frame. We do this a lot so it's nice to
490      * have this code in one place.
491      */

492      public void addStopAction() {
493         DoAction action = new DoAction();
494
495         action.program = new Program();
496         action.program.stop();
497
498         addFlashObject( action );
499      }
500
501      private void writeFrameLabel( FlashOutput fob ) {
502          fob.writeTag( Tag.FRAMELABEL, name.length()+(is_anchor?2:1) );
503          fob.writeStringZ( name );
504          if( is_anchor ) fob.writeByte(1);
505      }
506
507 }
508
509
Popular Tags