KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openlaszlo > iv > flash > commands > GeneralXMLListCommand


1 /*
2  * $Id: GeneralXMLListCommand.java,v 1.5 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 /*
52  * 12/11/2001 fixed alignment problems, bounds now taken into account
53  *
54  */

55
56 package org.openlaszlo.iv.flash.commands;
57
58 import org.openlaszlo.iv.flash.parser.*;
59 import org.openlaszlo.iv.flash.api.*;
60 import org.openlaszlo.iv.flash.util.*;
61
62 import org.openlaszlo.iv.flash.context.*;
63
64 import java.io.*;
65 import java.util.*;
66 import java.awt.geom.*;
67
68 public class GeneralXMLListCommand extends GenericXMLCommand {
69
70     public static final int STARTX = 0; // 20
71
public static final int STARTY = 0; // 20
72

73     protected boolean mask;
74     protected String JavaDoc instancename;
75     protected boolean isFixed;
76
77     protected int orient;
78
79     protected static final int ORIENT_HORIZ = 0;
80     protected static final int ORIENT_VERT = 1;
81     protected static final int ORIENT_TEMPORAL = 2;
82
83     protected int order;
84
85     protected static final int ASCENDING_ORDER = 1;
86     protected static final int DESCENDING_ORDER = -1;
87     protected static final int NO_ORDER = 0;
88
89     protected int listSize; // in twips
90
protected int winWidth;
91     protected int winHeight;
92
93     protected String JavaDoc halign;
94     protected String JavaDoc valign;
95     protected String JavaDoc itemspace;
96     protected String JavaDoc clip;
97     protected String JavaDoc sortby;
98     protected String JavaDoc itemname;
99
100     protected String JavaDoc scrollPaneName;
101
102     protected boolean isSetFileSize;
103     protected int fileWidthAdd;
104     protected int fileHeightAdd;
105
106     protected void initParms( Context context ) throws IVException
107     {
108         super.initParms( context );
109
110         mask = getBoolParameter( context, "mask", true );
111         instancename = getParameter( context, "instancename" );
112
113         // mx stuff
114
scrollPaneName = getParameter( context, "scrollpanename" );
115         if( scrollPaneName != null && scrollPaneName.length() == 0 ) scrollPaneName = null;
116
117         String JavaDoc sorient = getParameter( context, "orient", "horizontal" );
118
119         if ( sorient.equalsIgnoreCase( "vertical" ) )
120         {
121             orient = ORIENT_VERT;
122         }
123         else if ( sorient.equalsIgnoreCase( "temporal" ) )
124         {
125             orient = ORIENT_TEMPORAL;
126         }
127         else // default to horizontal
128
{
129             orient = ORIENT_HORIZ;
130         }
131
132         String JavaDoc spacing = getParameter( context, "spacing", "auto" );
133
134         isFixed = spacing.equalsIgnoreCase("fixed");
135
136         String JavaDoc sorder = getParameter( context, "order", "none" );
137
138         if ( sorder.equalsIgnoreCase("descending") )
139         {
140             order = DESCENDING_ORDER;
141         }
142         else if ( sorder.equalsIgnoreCase("ascending") )
143         {
144             order = ASCENDING_ORDER;
145         }
146         else
147         {
148             order = NO_ORDER;
149         }
150
151         halign = getParameter( context, "halign", "'left'" );
152         valign = getParameter( context, "valign", "'top'" );
153         itemspace = getParameter( context, "itemspace", "0" );
154         clip = getParameter( context, "clip", "name()" );
155         sortby = getParameter( context, "sortby", "'none'" );
156         itemname = getParameter( context, "itemname", null );
157
158         if( (isSetFileSize=getBoolParameter(context, "setfilesize", false)) ) {
159             fileWidthAdd = getIntParameter(context, "filewidthadd", 0)*20;
160             fileHeightAdd = getIntParameter(context, "fileheightadd", 0)*20;
161         }
162     }
163
164     protected Script makeList( FlashFile file, Context context, Script parent, int frameNum ) throws IVException
165     {
166         GraphContext gc = getGraphContext( file, context );
167
168         if ( gc == null )
169         {
170             throw new IVException( Resource.NOGRAPHCONTEXT );
171         }
172
173         List contextList = gc.getValueList( select );
174
175         if ( contextList == null )
176         {
177             throw new IVException( Resource.NOMATCHINGCONTEXTS, new Object JavaDoc[] {select} );
178         }
179
180         Instance mainInst = getInstance();
181         Rectangle2D winBounds = GeomHelper.getTransformedSize( mainInst.matrix,
182             GeomHelper.newRectangle(-1024, -1024, 2048, 2048) ); // mask of the list
183
winWidth = (int) winBounds.getWidth();
184         winHeight = (int) winBounds.getHeight();
185
186         Script listScript = new Script(1);
187         Frame frame = listScript.newFrame();
188
189         if ( orient == ORIENT_TEMPORAL )
190         {
191             frame.addStopAction();
192         }
193
194         int x = STARTX, y = STARTY;
195
196         // sort items
197

198         if ( order != NO_ORDER )
199         {
200             contextList = GraphContext.sortValueList( contextList, sortby, order == ASCENDING_ORDER );
201         }
202
203         // process datasource
204

205         Context myContext;
206         ListIterator iter = contextList.listIterator();
207         int i = 1;
208
209         while( iter.hasNext() )
210         {
211             myContext = ( Context ) iter.next();
212
213             String JavaDoc clipName = evalStringParameter( myContext, clip, "" );
214
215             Script template = file.getScript( clipName );
216
217             if( template == null )
218             {
219                 Log.logRB( Resource.CMDSCRIPTNOTFOUND, new Object JavaDoc[] {clipName, getCommandName()} );
220             }
221             else
222             {
223                 Script script = template.copyScript();
224
225                 file.processScript( script, myContext );
226
227                 int delta = 0;
228
229                 if( itemspace != null )
230                 {
231                     delta = getIntParameter( myContext, "itemspace", 0 ) * 20;
232                 }
233
234                 // we will set matrix later
235
Instance scInst = frame.addInstance( script, i, null, null );
236
237                 if( itemname != null )
238                 {
239                     scInst.name = evalStringParameter( myContext, itemname, "item"+i );
240                 }
241
242                 Rectangle2D bounds = script.getBounds();
243                 int myX = (int) bounds.getX();
244                 int myY = (int) bounds.getY();
245                 int myWidth = (int) bounds.getWidth();
246                 int myHeight = (int) bounds.getHeight();
247
248                 int dx = x;
249                 int dy = y;
250
251                 if ( orient == ORIENT_VERT )
252                 {
253                     if ( ! isFixed ) { delta += myHeight; }
254                     y += delta;
255                 }
256                 else if ( orient == ORIENT_HORIZ )
257                 {
258                     if ( ! isFixed ) { delta += myWidth; }
259                     x += delta;
260                 }
261                 else if ( orient == ORIENT_TEMPORAL )
262                 {
263                     frame = listScript.newFrame();
264                 }
265
266                 String JavaDoc s_halign = getParameter( myContext, halign, "left" );
267                 String JavaDoc s_valign = getParameter( myContext, valign, "top" );
268
269                 // alignment
270

271                 double shiftX, shiftY;
272
273                 if ( orient == ORIENT_VERT )
274                 {
275                     shiftX = winWidth;
276                     shiftY = myHeight;
277                 }
278                 else if ( orient == ORIENT_HORIZ )
279                 {
280                     shiftX = myWidth;
281                     shiftY = winHeight;
282                 }
283                 else
284                 {
285                     shiftX = 0;
286                     shiftY = 0;
287                 }
288
289                 if ( s_halign.equalsIgnoreCase( "right" ) )
290                 {
291                     dx += shiftX;
292                 }
293                 else if ( s_halign.equalsIgnoreCase("center") )
294                 {
295                     dx += shiftX / 2;
296                 }
297
298                 if ( s_valign.equalsIgnoreCase("bottom") )
299                 {
300                     dy += shiftY;
301                 }
302                 else if ( s_valign.equalsIgnoreCase("center") )
303                 {
304                     dy += shiftY / 2;
305                 }
306
307                 // set matrix
308
AffineTransform matrix = AffineTransform.getTranslateInstance(dx-myX,dy-myY);
309                 scInst.matrix = matrix;
310             }
311
312             i++;
313         }
314
315         // calculate list size in twips
316

317         if( orient == ORIENT_VERT )
318         {
319             listSize = y - STARTY;
320         }
321         else if ( orient == ORIENT_HORIZ )
322         {
323             listSize = x - STARTX;
324         }
325
326         GeomHelper.deScaleMatrix( mainInst.matrix );
327         mainInst.matrix.translate( -winWidth/2, -winHeight/2 );
328
329         if ( instancename != null )
330         {
331             mainInst.name = instancename;
332         }
333
334         if( isSetFileSize ) {
335             int width = fileWidthAdd, height = fileHeightAdd;
336             if( orient == ORIENT_VERT ) {
337                 height += listSize;
338                 width += winWidth;
339             } else {
340                 width += listSize;
341                 height += winHeight;
342             }
343             file.setFrameSize(GeomHelper.newRectangle(0, 0, width, height));
344         }
345
346         return listScript;
347     }
348
349     protected void addMask( Script parent, int frameNum ) {
350         addMask(parent, frameNum, getInstance());
351     }
352
353     protected void addMask( Script parent, int frameNum, Instance inst ) {
354         if( mask ) {
355             addMask(parent, frameNum, inst, winWidth, winHeight);
356         }
357     }
358 }
359
Popular Tags