1 50 51 55 56 package org.openlaszlo.iv.flash.commands; 57 58 import org.openlaszlo.iv.flash.parser.*; 59 import org.openlaszlo.iv.flash.api.*; 60 import org.openlaszlo.iv.flash.api.shape.*; 61 import org.openlaszlo.iv.flash.api.text.*; 62 import org.openlaszlo.iv.flash.util.*; 63 64 import org.openlaszlo.iv.flash.context.Context; 65 import java.io.*; 66 import java.text.*; 67 import java.util.*; 68 import java.awt.geom.*; 69 70 74 public class TableCommand extends GenericCommand { 75 76 protected String datasource; protected String halign; protected String valign; protected int rows, cols; protected double contentscale; protected String defsym; protected String labelformat; protected double labelscale; protected int labelsize; protected boolean borders; protected AlphaColor bordercolor; protected int borderthickness; protected String mediafile; protected String instancename; protected String [] rlabels; protected String [] clabels; 93 protected int tableSize; protected int cellWidth, cellHeight; 95 protected int winWidth; 96 protected int winHeight; 97 98 public TableCommand() {} 99 100 protected String [] parseLabels( String s ) { 101 if( s == null || s.length() == 0 ) return null; 102 StringTokenizer st = new StringTokenizer(s, ",\t\n \r"); 103 IVVector v = new IVVector(); 104 while( st.hasMoreTokens() ) { 105 v.addElement( st.nextToken() ); 106 } 107 String [] rs = new String [v.size()]; 108 v.copyInto(rs); 109 return rs; 110 } 111 112 protected double parseLabelSizing( String s ) { 113 try { 114 labelsize = Integer.parseInt(s) * 20; 115 return 1.0; 116 } catch( NumberFormatException e ) { 117 labelsize = 18 * 20; 118 return parseSizing(s); 119 } 120 } 121 122 protected double parseSizing( String s ) { 123 if( s.equalsIgnoreCase("fixed") ) return 1.0; 124 if( s.equalsIgnoreCase("auto") ) return -1.0; 125 if( s.equalsIgnoreCase("half") ) return 0.5; 126 if( s.equalsIgnoreCase("double") ) return 2.0; 127 return Util.toDouble(s, -1.0); 128 } 129 130 protected void initParms( Context context ) throws IVException { 131 datasource = getParameter( context, "datasource", "" ); 132 halign = getParameter( context, "halign" ); 133 valign = getParameter( context, "valign" ); 134 rows = getIntParameter( context, "rows", 0 ); 135 cols = getIntParameter( context, "cols", 0 ); 136 contentscale = parseSizing( getParameter( context, "sizing" ) ); 137 defsym = getParameter( context, "defsym" ); 138 labelformat = getParameter( context, "labelformat" ); 139 labelscale = parseLabelSizing( getParameter( context, "labelsizing" ) ); 140 rlabels = parseLabels( getParameter( context, "rlabels" ) ); 141 clabels = parseLabels( getParameter( context, "clabels" ) ); 142 mediafile = getParameter( context, "mediafile" ); 143 borders = getBoolParameter( context, "borders", true ); 144 bordercolor = getColorParameter( context, "bordercolor", AlphaColor.black ); 145 borderthickness = borders? getIntParameter( context, "borderthickness", 20 ): 0; 146 instancename = getParameter( context, "instancename" ); 147 } 148 149 protected Instance putSymbolInCell( Frame frame, FlashDef sym, Rectangle2D rect, int layer, double scale, boolean prop ) { 150 double scaleX, scaleY, translateX = rect.getMinX(), translateY = rect.getMinY(); 151 152 Rectangle2D bounds = sym.getBounds(); 153 double x = bounds.getX(); 154 double y = bounds.getY(); 155 if( scale < 0.0 ) { 156 double xk = cellWidth/bounds.getWidth(); 157 double yk = cellHeight/bounds.getHeight(); 158 if( prop ) { 159 double k = Math.min(xk,yk); 160 scaleX = scaleY = k; 161 } else { 162 scaleX = xk; 163 scaleY = yk; 164 } 165 } else { 166 scaleX = scaleY = scale; 167 } 168 169 AffineTransform m = new AffineTransform(scaleX, 0, 0, scaleY, translateX-scaleX*x, translateY-scaleY*y); 173 174 return frame.addInstance(sym, layer, m, null); 175 } 176 177 protected Rectangle2D getCellRect( int col, int row ) { 178 int xmin = borderthickness+(cellWidth+borderthickness)*col; 179 int ymin = borderthickness+(cellHeight+borderthickness)*row; 180 181 return GeomHelper.newRectangle(xmin,ymin,cellWidth,cellHeight); 182 } 183 184 protected Script makeTable( FlashFile file, Context context, Script parent, int frameNum ) throws IVException { 185 186 String [][] data; 187 try { 188 UrlDataSource ds = new UrlDataSource(datasource,file); 189 data = ds.getData(); 190 } catch( IOException e ) { 191 throw new IVException(Resource.ERRDATAREAD, new Object [] {datasource, getCommandName()}, e); 192 } 193 194 if( data.length < 1 ) { 195 throw new IVException( Resource.INVALDATASOURCE, new Object [] {datasource, getCommandName()} ); 196 } 197 198 Instance mainInst = getInstance(); 199 Rectangle2D winBounds = GeomHelper.getTransformedSize( mainInst.matrix, 200 GeomHelper.newRectangle(-1024, -1024, 2048, 2048) ); winWidth = (int) winBounds.getWidth(); 202 winHeight = (int) winBounds.getHeight(); 203 204 int clipIdx = findColumn( "clip", data ); 205 206 if( rows <= 0 || cols <= 0 ) { 207 throw new IVException( Resource.ROWSORCOLS, new Object [] {getCommandName()} ); 208 } 209 210 boolean isLabels = rlabels != null || clabels != null; 211 if( defsym != null || isLabels ) { 212 if( mediafile != null ) { 214 try { 215 file.addExternalFile( mediafile, true ); 216 } catch( IVException e ) { 217 Log.log( e ); 218 } 219 } 220 file.getDefaultSymbolFile(); 222 } 223 224 225 Script tableScript = new Script(1); 226 Frame frame = tableScript.newFrame(); 227 228 int totalRows = rows; 229 int totalCols = cols; 230 if( rlabels != null ) totalCols++; 231 if( clabels != null ) totalRows++; 232 233 cellWidth = (winWidth-borderthickness*(totalCols+1)) / totalCols; 235 cellHeight = (winHeight-borderthickness*(totalRows+1)) / totalRows; 236 237 if( cellWidth <= 0 || cellHeight <= 0 ) { 238 Log.logRB( Resource.BORDERTOOTHICK ); 239 } 240 241 Rectangle2D[][] cellRects = new Rectangle2D[totalCols][totalRows]; 243 for( int c=0; c<totalCols; c++ ) { 244 for( int r=0; r<totalRows; r++ ) { 245 cellRects[c][r] = getCellRect( c, r ); 246 } 247 } 248 if( borders ) { 250 Shape shape = new Shape(); 251 int fullColor = shape.addLineStyle( new LineStyle(25, bordercolor) ); 252 int reducedColor = shape.addLineStyle( new LineStyle(25, bordercolor.getReducedColor()) ); 253 shape.setLineStyle(fullColor); 255 for( int c=0; c<totalCols; c++ ) { 256 for( int r=0; r<totalRows; r++ ) { 257 Rectangle2D rect = cellRects[c][r]; 258 shape.movePenTo((int)rect.getMinX(), (int)rect.getMaxY()); 259 shape.drawLineTo((int)rect.getMinX(), (int)rect.getMinY()); 260 shape.drawLineTo((int)rect.getMaxX(), (int)rect.getMinY()); 261 } 262 } 263 shape.movePenTo(winWidth, 0); 265 shape.drawLineTo(winWidth, winHeight); 266 shape.drawLineTo(0, winHeight); 267 268 shape.setLineStyle(reducedColor); 270 for( int c=0; c<totalCols; c++ ) { 271 for( int r=0; r<totalRows; r++ ) { 272 Rectangle2D rect = cellRects[c][r]; 273 shape.movePenTo((int)rect.getMaxX(), (int)rect.getMinY()); 274 shape.drawLineTo((int)rect.getMaxX(), (int)rect.getMaxY()); 275 shape.drawLineTo((int)rect.getMinX(), (int)rect.getMaxY()); 276 } 277 } 278 shape.movePenTo(0, winHeight); 280 shape.drawLineTo(0, 0); 281 shape.drawLineTo(winWidth, 0); 282 283 frame.addInstance(shape, 2, null, null); 285 shape.setBounds( 0, 0, winWidth, winHeight ); 286 } 287 288 Script defSymbol = null; 289 if( defsym != null ) { 290 defSymbol = file.getScript(defsym); 291 } 292 293 Font labelFont = null; 294 if( isLabels ) { 295 if( labelformat == null ) labelformat = "Arial"; 296 if( labelformat.equals( "Times" ) ) labelformat = "Times New Roman"; 298 else if( labelformat.equals( "Courier" ) ) labelformat = "Courier New"; 299 300 labelFont = getFont( file, labelformat ); 301 } 302 303 if( clipIdx == -1 && defSymbol == null ) { 304 throw new IVException( Resource.CLIPORDEFSYM ); 305 } 306 307 int col = 0, row = 0; 308 int layer = 3; 309 int rbase = clabels != null? 1: 0; 310 int cbase = rlabels != null? 1: 0; 311 312 if( labelFont != null ) { 314 if( rlabels != null ) { 315 int nLabels = Math.min(totalRows, rlabels.length); 316 for( int i=0; i<nLabels; i++ ) { 317 String label = rlabels[i]; 318 Rectangle2D r = cellRects[0][i+rbase]; 319 Text text = newText( file, label, labelFont, labelsize, AlphaColor.black, (int) r.getWidth(), (int) r.getHeight() ); 320 ((TextItem)text.getTextItems().elementAt(0)).align = 2; putSymbolInCell( frame, text, r, layer++, labelscale, true ); 322 } 323 col++; 324 } 325 if( clabels != null ) { 326 int nLabels = Math.min(totalCols, clabels.length); 327 for( int i=0; i<nLabels; i++ ) { 328 String label = clabels[i]; 329 Rectangle2D r = cellRects[i+cbase][0]; 330 Text text = newText( file, label, labelFont, labelsize, AlphaColor.black, (int) r.getWidth(), (int) r.getHeight() ); 331 ((TextItem)text.getTextItems().elementAt(0)).align = 2; putSymbolInCell( frame, text, r, layer++, labelscale, true ); 333 } 334 row++; 335 } 336 } 337 338 for( int r=1; r<data.length; r++ ) { 340 Context myContext = makeContext( context, data, r ); 341 Script template; 342 if( defSymbol != null ) { 343 template = defSymbol; 344 } else { 345 String clipName = data[r][clipIdx]; 346 template = file.getScript(clipName); 347 if( template == null ) { 348 throw new IVException( Resource.CMDSCRIPTNOTFOUND, new Object [] {clipName, getCommandName()} ); 349 } 350 } 351 Script myScript = template.copyScript(); 352 file.processScript( myScript, myContext ); 353 354 Instance myInst = putSymbolInCell(frame, myScript, cellRects[col][row], layer++, contentscale, true); 355 356 if( !halign.equalsIgnoreCase("left") && !valign.equalsIgnoreCase("top") ) { 357 double translateX = 0.0, translateY = 0.0; 358 Rectangle2D bounds = myInst.getBounds(); 359 double width = bounds.getWidth(); 360 double height = bounds.getHeight(); 361 362 if( halign.equalsIgnoreCase("right") ) { 363 translateX = cellWidth-width; 364 } else if( halign.equalsIgnoreCase("center") ) { 365 translateX = (cellWidth-width)/2; 366 } 367 368 if( valign.equalsIgnoreCase("bottom") ) { 369 translateY = cellHeight-height; 370 } else if( valign.equalsIgnoreCase("center") ) { 371 translateY = (cellHeight-height)/2; 372 } 373 374 myInst.matrix.preConcatenate(AffineTransform.getTranslateInstance(translateX,translateY)); 375 } 376 377 col++; 378 if( col >= totalCols ) { 379 row++; 380 col = cbase; 381 if( row >= totalRows ) break; 382 } 383 384 } 385 386 return tableScript; 387 } 388 389 public void doCommand( FlashFile file, Context context, Script parent, int frameNum ) throws IVException { 390 initParms( context ); 391 392 Script tableScript = makeTable( file, context, parent, frameNum ); 393 394 GeomHelper.deScaleMatrix( getInstance().matrix ); 395 getInstance().matrix.translate(-winWidth/2, -winHeight/2); 396 397 if( instancename != null ) { 398 getInstance().name = instancename; 399 } 400 401 getInstance().setScript( tableScript ); 402 addMask(parent, frameNum, getInstance(), winWidth, winHeight); 403 } 404 405 } 406 | Popular Tags |