KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: GeneralListCommand.java,v 1.7 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 java.io.*;
59 import java.awt.geom.*;
60
61 import org.openlaszlo.iv.flash.parser.*;
62 import org.openlaszlo.iv.flash.api.*;
63 import org.openlaszlo.iv.flash.util.*;
64
65 import org.openlaszlo.iv.flash.context.Context;
66
67 public class GeneralListCommand extends GenericCommand {
68
69     protected String JavaDoc datasource;
70     protected String JavaDoc halign;
71     protected String JavaDoc valign;
72     protected boolean mask;
73     protected int itemspace;
74     protected String JavaDoc instancename;
75     protected boolean isVertical;
76     protected boolean isFixed;
77
78     protected int listSize; // in twips
79
protected int winWidth;
80     protected int winHeight;
81
82     protected boolean isSetFileSize;
83     protected int fileWidthAdd;
84     protected int fileHeightAdd;
85
86     protected String JavaDoc scrollPaneName;
87
88     protected void initParms( Context context ) throws IVException {
89         datasource = getParameter( context, "datasource", "" );
90         halign = getParameter( context, "halign" );
91         valign = getParameter( context, "valign" );
92         mask = getBoolParameter( context, "mask", true );
93         itemspace = getIntParameter( context, "itemspace", 0 ) * 20;
94         instancename = getParameter( context, "instancename" );
95
96         // mx stuff
97
scrollPaneName = getParameter( context, "scrollpanename" );
98         if( scrollPaneName != null && scrollPaneName.length() == 0 ) scrollPaneName = null;
99
100         String JavaDoc orient = getParameter( context, "orient", "horizontal" );
101         String JavaDoc spacing = getParameter( context, "spacing", "auto" );
102         isVertical = orient.equalsIgnoreCase("vertical");
103         isFixed = spacing.equalsIgnoreCase("fixed");
104
105         if( (isSetFileSize=getBoolParameter(context, "setfilesize", false)) ) {
106             fileWidthAdd = getIntParameter(context, "filewidthadd", 0)*20;
107             fileHeightAdd = getIntParameter(context, "fileheightadd", 0)*20;
108         }
109     }
110
111     protected Script makeList( FlashFile file, Context context, Script parent, int frameNum ) throws IVException {
112         String JavaDoc[][] data;
113         try {
114             UrlDataSource ds = new UrlDataSource(datasource,file);
115             data = ds.getData();
116         } catch( IOException e ) {
117             throw new IVException(Resource.ERRDATAREAD, new Object JavaDoc[] {datasource, getCommandName()}, e);
118         }
119
120         if( data.length < 1 ) {
121             throw new IVException(Resource.INVALDATASOURCE, new Object JavaDoc[] {datasource, getCommandName()});
122         }
123
124         Instance mainInst = getInstance();
125         Rectangle2D winBounds = GeomHelper.getTransformedSize( mainInst.matrix,
126             GeomHelper.newRectangle(-1024, -1024, 2048, 2048) ); // mask of the list
127
winWidth = (int) winBounds.getWidth();
128         winHeight = (int) winBounds.getHeight();
129
130         int clipIdx = findColumn( "clip", data );
131         int spaceIdx = findColumn( "space", data );
132         int instancenameIdx = findColumn( "instancename", data );
133         if( clipIdx == -1 ) {
134             throw new IVException(Resource.COLNOTFOUNDCMD, new Object JavaDoc[] {getCommandName(), "Clip", datasource});
135         }
136
137         Script listScript = new Script(1);
138         Frame frame = listScript.newFrame();
139         int x = 0, y = 0;
140
141         // process datasource
142
for( int row=1; row<data.length; row++ ) {
143             Context myContext = makeContext( context, data, row );
144             String JavaDoc clipName = data[row][clipIdx];
145             Script template = file.getScript(clipName);
146             if( template == null ) {
147                 Log.logRB( Resource.CMDSCRIPTNOTFOUND, new Object JavaDoc[] {clipName, "List"} );
148             } else {
149                 Script myScript = template.copyScript();
150                 file.processScript( myScript, myContext );
151
152                 int delta = 0;
153                 if( spaceIdx != -1 ) {
154                     delta = Util.toInt( data[row][spaceIdx], 0 ) * 20;
155                 }
156                 Instance scInst = frame.addInstance(myScript, row, null, null);
157                 if( instancenameIdx != -1 ) {
158                     scInst.name = data[row][instancenameIdx];
159                 }
160
161                 Rectangle2D bounds = myScript.getBounds();
162                 // it turned out that we need only width and height (because of alignment parameters)
163
int myX = 0; // (int) bounds.getX();
164
int myY = 0; // (int) bounds.getY();
165
int myWidth = (int) bounds.getWidth();
166                 int myHeight = (int) bounds.getHeight();
167
168                 int dx = x;
169                 int dy = y;
170
171                 // calculate next item's coordinates
172
if( isVertical ) {
173                     if( !isFixed ) {
174                         delta += myHeight;
175                     }
176                     delta += itemspace;
177                     y += delta;
178                 } else {
179                     if( !isFixed ) {
180                         delta += myWidth;
181                     }
182                     delta += itemspace;
183                     x += delta;
184                 }
185
186                 // align current item
187
double shiftX, shiftY;
188                 if( isVertical ) {
189                     shiftX = winWidth;
190                     shiftY = myHeight;
191                 } else {
192                     shiftX = myWidth;
193                     shiftY = winHeight;
194                 }
195                 if( halign.equalsIgnoreCase("right") ) {
196                     dx += shiftX;
197                 } else if( halign.equalsIgnoreCase("center") ) {
198                     dx += shiftX/2;
199                 }
200
201                 if( valign.equalsIgnoreCase("bottom") ) {
202                     dy += shiftY;
203                 } else if( valign.equalsIgnoreCase("center") ) {
204                     dy += shiftY/2;
205                 }
206
207                 // set matrix
208
AffineTransform matrix = AffineTransform.getTranslateInstance(dx-myX,dy-myY);
209                 scInst.matrix = matrix;
210             }
211         }
212
213         // calculate list size in twips
214
if( isVertical ) {
215             listSize = y;
216         } else {
217             listSize = x;
218         }
219
220         GeomHelper.deScaleMatrix( mainInst.matrix );
221         mainInst.matrix.translate( -winWidth/2, -winHeight/2 );
222
223         if( instancename != null ) {
224             mainInst.name = instancename;
225         }
226
227         if( isSetFileSize ) {
228             int width = fileWidthAdd, height = fileHeightAdd;
229             if( isVertical ) {
230                 height += listSize;
231                 width += winWidth;
232             } else {
233                 width += listSize;
234                 height += winHeight;
235             }
236             width = ((width + 19)/20)*20;
237             height = ((height + 19)/20)*20;
238             file.setFrameSize(GeomHelper.newRectangle(0, 0, width, height));
239         }
240
241         return listScript;
242     }
243
244     protected void addMask( Script parent, int frameNum ) {
245         addMask(parent, frameNum, getInstance());
246     }
247
248     protected void addMask( Script parent, int frameNum, Instance inst ) {
249         if( mask ) {
250             addMask(parent, frameNum, inst, winWidth, winHeight);
251         }
252     }
253
254 }
255
Popular Tags