1 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 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 dataSource,headerSymbolName,footerSymbolName,instanceName,closeLevel; 78 79 protected void initColumnIndexes(String [][] 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 [][] 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 [][] data) { 136 Instance instance = getInstance(); instance.name = instanceName; Script script = instance.copyScript(); Frame frame = null; 140 if( closeLevel.equalsIgnoreCase("All") ) { script.getFrameAt(0).addStopAction(); script.newFrame(); frame = script.getFrameAt(1); } else 145 frame = script.getFrameAt(0); frame.addStopAction(); String direction = "up"; double buttonWidth = 0; 149 double buttonHeight = 0; 150 double y = 0; double x = 0; int row = 0; int menuItem = 0; 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") ) { Script buttonScript = createButton(flashFile,data,row,menuItem); 158 buttonWidth = buttonScript.getBounds().getWidth(); buttonHeight = buttonScript.getBounds().getHeight(); 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); 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); addFooter(frame,row,x,buttonHeight-itemPad); 180 addHeader(frame,row,x,y+buttonHeight-itemPad); 181 } 182 } 183 protected void makeSubMenu(FlashFile flashFile,String [][] data,int row,Script buttonScript,double buttonWidth,double buttonHeight) { 184 String 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(); subButtonHeight = subButtonScript.getBounds().getHeight(); 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 [][] data,int row,int itemNumber) { 231 String 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); } 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(); Script overClipScript = flashFile.getScript(data[row][overClipColumn]).copyScript(); 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)); program.gotoFrame(0); program.setTarget("../item"+(itemNumber+1)); program.gotoFrame(0); program.setTarget(""); program.gotoFrame(1); return(ActionCondition.onRollOver(program)); 284 } 285 protected ActionCondition onRelease(String [][] 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 |