KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: TreeMenuCommand.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.commands.*;
61 import org.openlaszlo.iv.flash.context.*;
62 import org.openlaszlo.iv.flash.util.*;
63
64 /**
65  * TreeMenuCommand JGenerator Object
66  *
67  * @author William L. Thomson Jr.
68  * @company Obsidian-Studios Inc.
69  * @version 1.0
70  * @date April 16, 2002
71  */

72 public class TreeMenuCommand extends GenericCommand {
73
74     protected int itemPad;
75     protected int upClipColumn,overClipColumn,numColumn,parentColumn,directionColumn,titleColumn,urlColumn,windowColumn,goToColumn,goToLabelColumn = -1;
76     protected Script headerSymbol,footerSymbol;
77     protected String JavaDoc dataSource,headerSymbolName,footerSymbolName,instanceName,closeLevel;
78
79     protected void initColumnIndexes(String JavaDoc[][] data) {
80         upClipColumn = findColumn("upClip",data);
81         overClipColumn = findColumn("overClip",data);
82         numColumn = findColumn("Num",data);
83         parentColumn = findColumn("Parent",data);
84         directionColumn = findColumn("Direction",data);
85         titleColumn = findColumn("Title",data);
86         urlColumn = findColumn("Url",data);
87         windowColumn = findColumn("Window",data);
88         goToColumn = findColumn("goto",data);
89         goToLabelColumn = findColumn("gotoLabel",data);
90         if( upClipColumn==-1 ) Log.error("TreeMenuCommand Error\nA upClip column was not specified in the data source.");
91         if( overClipColumn==-1 ) Log.error("TreeMenuCommand Error\nA overClip column was not specified in the data source.");
92         if( numColumn==-1 ) Log.error("TreeMenuCommand Error\nA num column was not specified in the data source.");
93         if( parentColumn==-1 ) Log.error("TreeMenuCommand Error\nA parent column was not specified in the data source.");
94         if( directionColumn==-1 ) Log.error("TreeMenuCommand Error\nA direction column was not specified in the data source.");
95         if( titleColumn==-1 ) Log.error("TreeMenuCommand Error\nA title column was not specified in the data source.");
96         if( urlColumn==-1 && goToColumn==-1 && goToColumn==-1 ) Log.error("TreeMenuCommand Error\nNeither a url or goto or gotoLabel column was not specified in the data source.");
97     }
98     protected void initParams(Context context) {
99         dataSource = getParameter(context,"dataSource", "");
100         headerSymbolName = getParameter(context,"headerSymbol", "");
101         footerSymbolName = getParameter(context,"footerSymbol", "");
102         instanceName = getParameter(context,"instanceName", "");
103         closeLevel = getParameter(context,"closeLevel", "All");
104         itemPad = getIntParameter(context,"itemPad",10);
105         if( dataSource.equals("") ) Log.error("TreeMenuCommand Error\nA data source was not specified in authoring environment.");
106         if( headerSymbolName.equals("") ) Log.error("TreeMenuCommand Error\nA header symbol was not specified in authoring environment.");
107         if( footerSymbolName.equals("") ) Log.error("TreeMenuCommand Error\nA footer symbol was not specified in authoring environment.");
108         if( instanceName.equals("") ) Log.error("TreeMenuCommand Error\nA instance name was not specified in authoring environment.");
109     }
110     protected void initSymbols(FlashFile flashFile) {
111         headerSymbol = flashFile.getScript(headerSymbolName);
112         footerSymbol = flashFile.getScript(footerSymbolName);
113         if( headerSymbol==null ) Log.error("TreeMenuCommand Error\nCould not get the header script from the flash file.");
114         if( footerSymbol==null ) Log.error("TreeMenuCommand Error\nCould not get the footer script from the flash file.");
115     }
116     public void doCommand (FlashFile flashFile,Context context,Script parentScript,int frameNum) {
117         initParams(context);
118         initSymbols(flashFile);
119         String JavaDoc[][] data = null;
120         try {
121             UrlDataSource urlDataSource = new UrlDataSource(dataSource,flashFile);
122             data = urlDataSource.getData();
123         } catch( IVException ive ) {
124             Log.error("TreeMenuCommand Error\nCould not Parse the datasource into a multi-dimensional array because :\n"+ive);
125         } catch( IOException ioe ) {
126             Log.error("TreeMenuCommand Error\nCould not Parse the datasource into a multi-dimensional array because :\n"+ioe);
127         }
128         if( data!=null ) {
129             initColumnIndexes(data);
130             makeMenu(flashFile,data);
131         } else {
132             Log.error("TreeMenuCommand Error\nThe datasource it empty.");
133         }
134     }
135     protected void makeMenu(FlashFile flashFile,String JavaDoc[][] data) {
136         Instance instance = getInstance(); // get an instance of the template
137
instance.name = instanceName; // set the name of the instance
138
Script script = instance.copyScript(); // copy the instance's script
139
Frame frame = null;
140         if( closeLevel.equalsIgnoreCase("All") ) { // Check to see if we should close all menus
141
script.getFrameAt(0).addStopAction(); // add a stop action to frame 1
142
script.newFrame(); // create a new frame so we now have two
143
frame = script.getFrameAt(1); // frame 2 will represent menu open this and all sub menus
144
} else
145             frame = script.getFrameAt(0); // get the first frame and show the first main menu
146
frame.addStopAction(); // and add a stop action to it
147
String JavaDoc direction = "up"; // direction used for footer and header placement
148
double buttonWidth = 0;
149         double buttonHeight = 0;
150         double y = 0; // y (vertical) to space out each button
151
double x = 0; // x for later
152
int row = 0; // row of data represents row in table
153
int menuItem = 0; // number of items, buttons or menus in this menu
154
if( flashFile.getMainScript().getBounds().getY()<flashFile.getMainScript().getBounds().getHeight()/2 ) direction = "down";
155         for( row =1;row<data.length;row++ ) {
156             if( data[row][parentColumn].equals("0") ) { // get the main menu data and create the main buttons and sub menus
157
Script buttonScript = createButton(flashFile,data,row,menuItem);
158                 buttonWidth = buttonScript.getBounds().getWidth(); // get the width before creating and adding a sub menu
159
buttonHeight = buttonScript.getBounds().getHeight(); // get the height before creating and adding a sub menu
160
makeSubMenu(flashFile,data,row,buttonScript,buttonWidth,buttonHeight);
161                 frame.addInstance(buttonScript,row+1,AffineTransform.getTranslateInstance(x,y),null,"item"+menuItem);
162                 if( data[row][directionColumn].equalsIgnoreCase("down") ) {
163                     direction = "down";
164                     y += buttonHeight-itemPad;
165                 } else {
166                     direction = "up";
167                     y -= buttonHeight-itemPad;
168                 }
169                 menuItem++;
170             }
171         }
172         Button2 invisibleButton = createInvisibleButton(script.getBounds());
173         if( direction.equals("down") ) {
174             frame.addInstance(invisibleButton,0,AffineTransform.getTranslateInstance(x,0),null); // add the invisible button that will close the whole menu
175
addHeader(frame,row,x,itemPad);
176             addFooter(frame,row,x,y-itemPad);
177         } else {
178             frame.addInstance(invisibleButton,0,AffineTransform.getTranslateInstance(x,-(y+buttonHeight)),null); // add the invisible button that will close the whole menu
179
addFooter(frame,row,x,buttonHeight-itemPad);
180             addHeader(frame,row,x,y+buttonHeight-itemPad);
181         }
182     }
183     protected void makeSubMenu(FlashFile flashFile,String JavaDoc[][] data,int row,Script buttonScript,double buttonWidth,double buttonHeight) {
184         String JavaDoc direction = "up";
185         double subButtonWidth = 0;
186         double subButtonHeight = 0;
187         double subX = buttonWidth;
188         double subY = 0;
189         int subRow = 0;
190         int subMenuItem = 0;
191         Frame innerFrame = null;
192         for( subRow=1;subRow<data.length;subRow++ ) {
193             if( Integer.parseInt(data[subRow][parentColumn])==row ) {
194                 if( innerFrame==null ) {
195                     buttonScript.getFrameAt(0).addStopAction();
196                     innerFrame = buttonScript.newFrame();
197                     innerFrame.addStopAction();
198                 }
199                 Script subButtonScript = createButton(flashFile,data,subRow,subMenuItem);
200                 subButtonWidth = subButtonScript.getBounds().getWidth(); // get the width before creating and adding a sub menu
201
subButtonHeight = subButtonScript.getBounds().getHeight(); // get the height before creating and adding a sub menu
202
makeSubMenu(flashFile,data,subRow,subButtonScript,subButtonWidth,subButtonHeight);
203                 innerFrame.addInstance(subButtonScript,1,AffineTransform.getTranslateInstance(subX,subY),null,"item"+subMenuItem);
204                 if( data[subRow][directionColumn].equalsIgnoreCase("down") ) {
205                     direction = "down";
206                     subY += subButtonHeight-itemPad;
207                 } else {
208                     direction = "up";
209                     subY -= subButtonHeight-itemPad;
210                 }
211                 subMenuItem++;
212             }
213         }
214         if( innerFrame!=null ) {
215             if( direction.equals("down") ) {
216                 addHeader(innerFrame,subRow,subX,itemPad);
217                 addFooter(innerFrame,subRow,subX,subY-itemPad);
218             } else {
219                 addFooter(innerFrame,subRow,subX,subButtonHeight-itemPad);
220                 addHeader(innerFrame,subRow,subX,subY+subButtonHeight-itemPad);
221             }
222         }
223     }
224     protected void addFooter(Frame frame,int level,double x,double y) {
225         frame.addInstance(footerSymbol,level,AffineTransform.getTranslateInstance(x,y),null);
226     }
227     protected void addHeader(Frame frame,int level,double x,double y) {
228         frame.addInstance(headerSymbol,level,AffineTransform.getTranslateInstance(x,y),null);
229     }
230     protected Script createButton(FlashFile flashFile,String JavaDoc[][] data,int row,int itemNumber) {
231         String JavaDoc name = data[row][titleColumn];
232         Script script = new Script(1);
233         Frame frame = script.getFrameAt(0);
234         Context buttonContext = null;
235         try {
236             buttonContext = ContextFactory.createContext(data,row); // create a new context
237
} catch( IVException ive ) {
238             Log.error("TreeMenuCommand Error\nCould not make new button context because :\n"+ive);
239         }
240         Script upClipScript = flashFile.getScript(data[row][upClipColumn]).copyScript(); // get a copy of the upClip script
241
Script overClipScript = flashFile.getScript(data[row][overClipColumn]).copyScript(); // get a copy of the overClip script
242
try {
243             flashFile.processScript(upClipScript,buttonContext);
244             flashFile.processScript(overClipScript,buttonContext);
245         } catch( IVException ive ) {
246             Log.error("TreeMenuCommand Error\nCould not process new button context because :\n"+ive);
247         }
248         Button2 button2 = new Button2();
249         button2.addActionCondition(onRollOver(itemNumber));
250         button2.addActionCondition(onRelease(data,row));
251         button2.addButtonRecord(new ButtonRecord(ButtonRecord.Up,upClipScript,1,AffineTransform.getTranslateInstance(0,0),CXForm.newIdentity(true)));
252         button2.addButtonRecord(new ButtonRecord(ButtonRecord.Over|ButtonRecord.Down,overClipScript,2,AffineTransform.getTranslateInstance(0,0),CXForm.newIdentity(true)));
253         Shape shape = new Shape();
254         shape.setBounds(upClipScript.getBounds());
255         shape.setFillStyle0(FillStyle.newSolid(new Color(0,0,0)));
256         shape.drawRectangle(upClipScript.getBounds());
257         button2.addButtonRecord(new ButtonRecord(ButtonRecord.HitTest,shape,3,AffineTransform.getTranslateInstance(0,0),CXForm.newIdentity(true)));
258         button2.setTrackAs(Button2.TrackAsMenu);
259         frame.addInstance(button2,1,AffineTransform.getTranslateInstance(0,0),null,name);
260         return(script);
261     }
262     protected Button2 createInvisibleButton(Rectangle2D rectangle2D) {
263         Button2 button2 = new Button2();
264         Program program = new Program();
265         program.gotoFrame(0);
266         button2.addActionCondition(ActionCondition.onRollOver(program));
267         Shape shape = new Shape();
268         shape.setBounds(rectangle2D);
269         shape.setFillStyle0(FillStyle.newSolid(new Color(0,0,0)));
270         shape.drawRectangle(rectangle2D);
271         button2.addButtonRecord(new ButtonRecord(ButtonRecord.HitTest,shape,1,AffineTransform.getTranslateInstance(0,0),CXForm.newIdentity(true)));
272         button2.setTrackAs(Button2.TrackAsButton);
273         return(button2);
274     }
275     protected ActionCondition onRollOver(int itemNumber) {
276         Program program = new Program();
277         program.setTarget("../item"+(itemNumber-1)); // tell the menu item just before this one to go to frame 1 menu closed
278
program.gotoFrame(0); //to go to frame 1 menu closed
279
program.setTarget("../item"+(itemNumber+1)); // tell the menu item just after this one
280
program.gotoFrame(0); // to go to frame 1 menu closed
281
program.setTarget(""); // go back to the timeline this button is on
282
program.gotoFrame(1); // go to frame 2 menu open
283
return(ActionCondition.onRollOver(program));
284     }
285     protected ActionCondition onRelease(String JavaDoc[][] data,int row) {
286         Program program = new Program();
287         if( urlColumn!=-1 )
288             program.getURL(data[row][urlColumn],data[row][windowColumn]);
289         else if( goToColumn!=-1 )
290             program.gotoFrame(Integer.parseInt(data[row][goToColumn]));
291         else if( goToLabelColumn!=-1 )
292             program.gotoLabel(data[row][goToLabelColumn]);
293         return(ActionCondition.onRelease(program));
294     }
295 }
296
Popular Tags