KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: MultipageListCommand.java,v 1.1 2002/04/19 06:10:34 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.commands;
52
53 import java.awt.geom.*;
54 import java.io.*;
55 import java.util.*;
56 import org.openlaszlo.iv.flash.api.*;
57 import org.openlaszlo.iv.flash.api.action.*;
58 import org.openlaszlo.iv.flash.api.button.*;
59 import org.openlaszlo.iv.flash.api.shape.*;
60 import org.openlaszlo.iv.flash.context.*;
61 import org.openlaszlo.iv.flash.util.*;
62
63 /**
64  * Multipage List JGenerator Object
65  *
66  * @author William L. Thomson Jr.
67  * @company Obsidian-Studios Inc.
68  * @version 1.0
69  * @date April 17, 2002
70  */

71 public class MultipageListCommand extends GenericCommand {
72
73     protected int textColumn,clipColumn,urlColumn,windowColumn = -1;
74     protected Script externalMediaScript,prevSymbol,homeSymbol,nextSymbol,textSymbol,templateScript = null;
75     protected String JavaDoc dataSource,prevSymbolName,homeSymbolName,nextSymbolName,textSymbolName,instanceName,externalMediaName;
76
77     protected void initColumnIndexes(String JavaDoc[][] data) {
78         textColumn = findColumn("Text",data);
79         urlColumn = findColumn("Url",data);
80         windowColumn = findColumn("Window",data);
81         if (textColumn==-1) Log.error("MultipageListCommand Error\nA text column was not specified in the data source.");
82         if (urlColumn!=-1 && windowColumn==-1) Log.warn("MultipageListCommand Warning\nA url was specified but a window column was not specified in the data source. Will end up using default window, _blank");
83     }
84     protected void initParams(Context context) {
85         dataSource = getParameter(context,"datasource","");
86         prevSymbolName = getParameter(context,"prevsym","");
87         homeSymbolName = getParameter(context,"homesym","");
88     nextSymbolName = getParameter(context,"nextsym","");
89     textSymbolName = getParameter(context,"textsym","");
90         instanceName = getParameter(context,"instancename","");
91         if (dataSource.equals("")) Log.error("MultipageListCommand Error\nA data source was not specified in authoring environment.");
92         if (prevSymbolName.equals("")) Log.error("MultipageListCommand Error\nA previous symbol was not specified in authoring environment.");
93         if (homeSymbolName.equals("")) Log.error("MultipageListCommand Error\nA home symbol was not specified in authoring environment.");
94         if (nextSymbolName.equals("")) Log.error("MultipageListCommand Error\nA next symbol was not specified in authoring environment.");
95         if (textSymbolName.equals("")) Log.error("MultipageListCommand Error\nA text symbol was not specified in authoring environment.");
96         if (instanceName.equals("")) Log.error("MultipageListCommand Error\nA instance name was not specified in authoring environment.");
97     }
98     protected void initSymbols(FlashFile flashFile) {
99         prevSymbol = flashFile.getScript(prevSymbolName);
100         homeSymbol = flashFile.getScript(homeSymbolName);
101         nextSymbol = flashFile.getScript(nextSymbolName);
102         textSymbol = flashFile.getScript(textSymbolName);
103         if (prevSymbol==null) Log.error("MultipageListCommand Warning\nCould not get previous symbol script specified.");
104         if (homeSymbol==null) Log.error("MultipageListCommand Warning\nCould not get home symbol script specified.");
105         if (nextSymbol==null) Log.error("MultipageListCommand Warning\nCould not get next symbol script specified.");
106     if (textSymbol==null) Log.error("MultipageListCommand Error\nCould not get text symbol script specified.");
107     }
108     public void doCommand(FlashFile flashFile,Context context,Script parentScript, int frames) {
109         initParams(context);
110         initSymbols(flashFile);
111         String JavaDoc[][] data = null;
112         try {
113             UrlDataSource urlDataSource = new UrlDataSource(dataSource,flashFile);
114             data = urlDataSource.getData();
115         }
116         catch (IVException ive) {
117             Log.error("MultipageListCommand Error\nCould not Parse the datasource into a multi-dimensional array because :\n"+ive);
118         }
119         catch (IOException ioe) {
120             Log.error("MultipageListCommand Error\nCould not Parse the datasource into a multi-dimensional array because :\n"+ioe);
121         }
122         if (data!=null) {
123             initColumnIndexes(data);
124             makeLists(flashFile,data); // make the lists
125
} else {
126             Log.error("MultipageListCommand Error\nThe datasource it empty.");
127         }
128     }
129     protected void makeLists(FlashFile flashFile,String JavaDoc[][] data) {
130         Instance instance = getInstance(); // get an instance of the template
131
instance.name = instanceName; // set the name of the instance
132
double width = instance.matrix.getScaleX()*2048; // determine the bounding box width
133
double height = instance.matrix.getScaleY()*2048; // determine the bounding box height
134
GeomHelper.deScaleMatrix(instance.matrix); // scale the instance, otherwise every clip will be distorted
135
Script script = instance.copyScript();
136         Frame frame = script.getFrameAt(0);
137         frame.addStopAction();
138         double prevHeight = prevSymbol.getBounds().getHeight();
139         double prevWidth = prevSymbol.getBounds().getWidth();
140         Button2 prevButton = createButton(prevSymbol);
141         prevButton.addActionCondition(onReleasePrev());
142         double homeHeight = homeSymbol.getBounds().getHeight();
143         double homeWidth = homeSymbol.getBounds().getWidth();
144         Button2 homeButton = createButton(homeSymbol);
145         homeButton.addActionCondition(onReleaseHome());
146         double nextHeight = nextSymbol.getBounds().getHeight();
147         double nextWidth = nextSymbol.getBounds().getWidth();
148         Button2 nextButton = createButton(nextSymbol);
149         nextButton.addActionCondition(onReleaseNext());
150         double symbolHeight = (prevHeight > nextHeight) ? prevHeight : nextHeight;
151         double y = -height/2+symbolHeight;
152         double x = -width/2;
153         double clipHeight = 0;
154         double newPageHeight = 0; // this will be a cumlative total of clip heights on a page
155
double pageHeight = height-symbolHeight; // this will be used to determine the total height we have on a page
156
int level = 1;
157         for (int row =1;row<data.length;row++) {
158             Script clipScript = textSymbol.copyScript();
159             Context clipContext = null;
160             try {
161                 clipContext = ContextFactory.createContext(data,row); // create a new context
162
}
163             catch (IVException ive) {
164                 Log.error("MultipageListCommand Error\nCould not make new button context because :\n"+ive);
165             }
166             try {
167                 flashFile.processScript(clipScript,clipContext);
168             }
169             catch (IVException ive) {
170                 Log.error("MultipageListCommand Error\nCould not process new button context because :\n"+ive);
171             }
172             clipHeight = clipScript.getBounds().getHeight(); // get the height before creating and adding a sub menu
173
newPageHeight += clipHeight; // add the clip height to our new page's height
174
if(newPageHeight>pageHeight) { // check to see if there is still room on the page
175
frame = script.newFrame(); // since there is no more room, let's make another page on a new frame
176
frame.addStopAction(); // add a stop action, unless you want ticking pages :)
177
script.removeAllInstances(frame); // remove all the previous instances from this frame
178
y = -height/2+symbolHeight; // reset the y coordinate to start were the nav symbols stop
179
newPageHeight = clipHeight; // reset the page height to just the clip height that will be added
180
level = 1; // start over again at the bottom level
181
}
182             if (urlColumn!=-1) {
183                 Button2 button2 = createButton(data,row,clipScript);
184                 frame.addInstance(button2,level,AffineTransform.getTranslateInstance(x,y),null);
185             } else {
186                 frame.addInstance(clipScript,level,AffineTransform.getTranslateInstance(x,y),null);
187             }
188             y += clipHeight; // add the clip height to the current y position
189
level++;
190         }
191         int frameCount = script.getFrameCount();
192         if(frameCount>1) { // if we have more than one frame add the navigation buttons
193
Frame tempFrame = script.getFrameAt(1);
194             double navY = -height/2;
195             tempFrame.addInstance(prevButton,100,AffineTransform.getTranslateInstance(x,navY),null); // add the previous button to all frames except the first
196
x += prevWidth+40; // add 40 to space out nav buttons
197
tempFrame.addInstance(homeButton,101,AffineTransform.getTranslateInstance(x,navY),null); // add the home button to all frames except the first
198
x += homeWidth+40; // add 40 to space out nav buttons
199
script.getFrameAt(0).addInstance(nextButton,102,AffineTransform.getTranslateInstance(x,navY),null); // add the next button to all frames including the first
200
script.getFrameAt(frameCount-1).removeInstance(102); // remove the next button from the last frame
201
}
202     }
203     protected ActionCondition onReleasePrev() {
204         Program program = new Program();
205         program.prevFrame();
206         return(ActionCondition.onRelease(program));
207     }
208     protected ActionCondition onReleaseHome() {
209         Program homeProgram = new Program();
210         homeProgram.gotoFrame(0);
211         return(ActionCondition.onRelease(homeProgram));
212     }
213     protected ActionCondition onReleaseNext() {
214         Program nextProgram = new Program();
215         nextProgram.nextFrame();
216         return(ActionCondition.onRelease(nextProgram));
217     }
218     protected Button2 createButton(Script symbol) {
219         Button2 button2 = new Button2();
220         int states = ButtonRecord.Up|ButtonRecord.Over|ButtonRecord.Down; // add the symbol to define the rest of the states
221
button2.addButtonRecord(new ButtonRecord(states,symbol,1,AffineTransform.getTranslateInstance(0,0),CXForm.newIdentity(true)));
222         Shape shape = new Shape();
223         shape.setBounds(symbol.getBounds());
224         shape.setFillStyle0(FillStyle.newSolid(new Color(0,0,0)));
225         shape.drawRectangle(symbol.getBounds());
226         button2.addButtonRecord(new ButtonRecord(ButtonRecord.HitTest,shape,2,AffineTransform.getTranslateInstance(0,0),CXForm.newIdentity(true)));
227         button2.setTrackAs(Button2.TrackAsButton);
228         return (button2);
229     }
230     protected Button2 createButton(String JavaDoc[][] data,int row,Script clipScript) {
231         Button2 button2 = createButton(clipScript);
232         Program program = new Program();
233         if (windowColumn==-1) program.getURL(data[row][urlColumn],"_blank");
234         else program.getURL(data[row][urlColumn],data[row][windowColumn]);
235         button2.addActionCondition(ActionCondition.onPress(program));
236         return(button2);
237     }
238 }
239
Popular Tags