001: /*
002: * $Id: GeneralListCommand.java,v 1.7 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: /*
052: * 12/11/2001 fixed alignment problems, bounds now taken into account
053: *
054: */
055:
056: package org.openlaszlo.iv.flash.commands;
057:
058: import java.io.*;
059: import java.awt.geom.*;
060:
061: import org.openlaszlo.iv.flash.parser.*;
062: import org.openlaszlo.iv.flash.api.*;
063: import org.openlaszlo.iv.flash.util.*;
064:
065: import org.openlaszlo.iv.flash.context.Context;
066:
067: public class GeneralListCommand extends GenericCommand {
068:
069: protected String datasource;
070: protected String halign;
071: protected String valign;
072: protected boolean mask;
073: protected int itemspace;
074: protected String instancename;
075: protected boolean isVertical;
076: protected boolean isFixed;
077:
078: protected int listSize; // in twips
079: protected int winWidth;
080: protected int winHeight;
081:
082: protected boolean isSetFileSize;
083: protected int fileWidthAdd;
084: protected int fileHeightAdd;
085:
086: protected String scrollPaneName;
087:
088: protected void initParms(Context context) throws IVException {
089: datasource = getParameter(context, "datasource", "");
090: halign = getParameter(context, "halign");
091: valign = getParameter(context, "valign");
092: mask = getBoolParameter(context, "mask", true);
093: itemspace = getIntParameter(context, "itemspace", 0) * 20;
094: instancename = getParameter(context, "instancename");
095:
096: // mx stuff
097: scrollPaneName = getParameter(context, "scrollpanename");
098: if (scrollPaneName != null && scrollPaneName.length() == 0)
099: scrollPaneName = null;
100:
101: String orient = getParameter(context, "orient", "horizontal");
102: String spacing = getParameter(context, "spacing", "auto");
103: isVertical = orient.equalsIgnoreCase("vertical");
104: isFixed = spacing.equalsIgnoreCase("fixed");
105:
106: if ((isSetFileSize = getBoolParameter(context, "setfilesize",
107: false))) {
108: fileWidthAdd = getIntParameter(context, "filewidthadd", 0) * 20;
109: fileHeightAdd = getIntParameter(context, "fileheightadd", 0) * 20;
110: }
111: }
112:
113: protected Script makeList(FlashFile file, Context context,
114: Script parent, int frameNum) throws IVException {
115: String[][] data;
116: try {
117: UrlDataSource ds = new UrlDataSource(datasource, file);
118: data = ds.getData();
119: } catch (IOException e) {
120: throw new IVException(Resource.ERRDATAREAD, new Object[] {
121: datasource, getCommandName() }, e);
122: }
123:
124: if (data.length < 1) {
125: throw new IVException(Resource.INVALDATASOURCE,
126: new Object[] { datasource, getCommandName() });
127: }
128:
129: Instance mainInst = getInstance();
130: Rectangle2D winBounds = GeomHelper.getTransformedSize(
131: mainInst.matrix, GeomHelper.newRectangle(-1024, -1024,
132: 2048, 2048)); // mask of the list
133: winWidth = (int) winBounds.getWidth();
134: winHeight = (int) winBounds.getHeight();
135:
136: int clipIdx = findColumn("clip", data);
137: int spaceIdx = findColumn("space", data);
138: int instancenameIdx = findColumn("instancename", data);
139: if (clipIdx == -1) {
140: throw new IVException(
141: Resource.COLNOTFOUNDCMD,
142: new Object[] { getCommandName(), "Clip", datasource });
143: }
144:
145: Script listScript = new Script(1);
146: Frame frame = listScript.newFrame();
147: int x = 0, y = 0;
148:
149: // process datasource
150: for (int row = 1; row < data.length; row++) {
151: Context myContext = makeContext(context, data, row);
152: String clipName = data[row][clipIdx];
153: Script template = file.getScript(clipName);
154: if (template == null) {
155: Log.logRB(Resource.CMDSCRIPTNOTFOUND, new Object[] {
156: clipName, "List" });
157: } else {
158: Script myScript = template.copyScript();
159: file.processScript(myScript, myContext);
160:
161: int delta = 0;
162: if (spaceIdx != -1) {
163: delta = Util.toInt(data[row][spaceIdx], 0) * 20;
164: }
165: Instance scInst = frame.addInstance(myScript, row,
166: null, null);
167: if (instancenameIdx != -1) {
168: scInst.name = data[row][instancenameIdx];
169: }
170:
171: Rectangle2D bounds = myScript.getBounds();
172: // it turned out that we need only width and height (because of alignment parameters)
173: int myX = 0; // (int) bounds.getX();
174: int myY = 0; // (int) bounds.getY();
175: int myWidth = (int) bounds.getWidth();
176: int myHeight = (int) bounds.getHeight();
177:
178: int dx = x;
179: int dy = y;
180:
181: // calculate next item's coordinates
182: if (isVertical) {
183: if (!isFixed) {
184: delta += myHeight;
185: }
186: delta += itemspace;
187: y += delta;
188: } else {
189: if (!isFixed) {
190: delta += myWidth;
191: }
192: delta += itemspace;
193: x += delta;
194: }
195:
196: // align current item
197: double shiftX, shiftY;
198: if (isVertical) {
199: shiftX = winWidth;
200: shiftY = myHeight;
201: } else {
202: shiftX = myWidth;
203: shiftY = winHeight;
204: }
205: if (halign.equalsIgnoreCase("right")) {
206: dx += shiftX;
207: } else if (halign.equalsIgnoreCase("center")) {
208: dx += shiftX / 2;
209: }
210:
211: if (valign.equalsIgnoreCase("bottom")) {
212: dy += shiftY;
213: } else if (valign.equalsIgnoreCase("center")) {
214: dy += shiftY / 2;
215: }
216:
217: // set matrix
218: AffineTransform matrix = AffineTransform
219: .getTranslateInstance(dx - myX, dy - myY);
220: scInst.matrix = matrix;
221: }
222: }
223:
224: // calculate list size in twips
225: if (isVertical) {
226: listSize = y;
227: } else {
228: listSize = x;
229: }
230:
231: GeomHelper.deScaleMatrix(mainInst.matrix);
232: mainInst.matrix.translate(-winWidth / 2, -winHeight / 2);
233:
234: if (instancename != null) {
235: mainInst.name = instancename;
236: }
237:
238: if (isSetFileSize) {
239: int width = fileWidthAdd, height = fileHeightAdd;
240: if (isVertical) {
241: height += listSize;
242: width += winWidth;
243: } else {
244: width += listSize;
245: height += winHeight;
246: }
247: width = ((width + 19) / 20) * 20;
248: height = ((height + 19) / 20) * 20;
249: file.setFrameSize(GeomHelper.newRectangle(0, 0, width,
250: height));
251: }
252:
253: return listScript;
254: }
255:
256: protected void addMask(Script parent, int frameNum) {
257: addMask(parent, frameNum, getInstance());
258: }
259:
260: protected void addMask(Script parent, int frameNum, Instance inst) {
261: if (mask) {
262: addMask(parent, frameNum, inst, winWidth, winHeight);
263: }
264: }
265:
266: }
|