KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: TableCommand.java,v 1.3 2002/02/24 02:10:19 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 /*
52  * 03/23/2001 fixed some problems with help from Patrick Talbot
53  * 12/11/2001 fixed alignment of symbols in table cells (now bounds are taken into account)
54  */

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 /**
71  * Insert Table command
72  *
73  */

74 public class TableCommand extends GenericCommand {
75
76     protected String JavaDoc datasource; // datasource
77
protected String JavaDoc halign; // left, center, right
78
protected String JavaDoc valign; // top, center, bottom
79
protected int rows, cols; // number of rows and columns
80
protected double contentscale; // auto, fixed, half, double
81
protected String JavaDoc defsym; // default symbol name
82
protected String JavaDoc labelformat; // Arial, Times, Courier, name of the font
83
protected double labelscale; // auto, fixed, half, double
84
protected int labelsize; // size of label text
85
protected boolean borders; // true, false
86
protected AlphaColor bordercolor; // border color
87
protected int borderthickness; // thickness of border in twips
88
protected String JavaDoc mediafile; // external symbol file
89
protected String JavaDoc instancename; // instancename
90
protected String JavaDoc[] rlabels; // labels for rows
91
protected String JavaDoc[] clabels; // labels for columns
92

93     protected int tableSize; // in twips
94
protected int cellWidth, cellHeight;
95     protected int winWidth;
96     protected int winHeight;
97
98     public TableCommand() {}
99
100     protected String JavaDoc[] parseLabels( String JavaDoc 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 JavaDoc[] rs = new String JavaDoc[v.size()];
108         v.copyInto(rs);
109         return rs;
110     }
111
112     protected double parseLabelSizing( String JavaDoc s ) {
113         try {
114             labelsize = Integer.parseInt(s) * 20;
115             return 1.0;
116         } catch( NumberFormatException JavaDoc e ) {
117             labelsize = 18 * 20;
118             return parseSizing(s);
119         }
120     }
121
122     protected double parseSizing( String JavaDoc 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 = AffineTransform.getTranslateInstance(translateX,translateY);
170
//m.scale(scaleX, scaleY);
171
//m.translate(-x,-y);
172
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 JavaDoc[][] 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 JavaDoc[] {datasource, getCommandName()}, e);
192         }
193
194         if( data.length < 1 ) {
195             throw new IVException( Resource.INVALDATASOURCE, new Object JavaDoc[] {datasource, getCommandName()} );
196         }
197
198         Instance mainInst = getInstance();
199         Rectangle2D winBounds = GeomHelper.getTransformedSize( mainInst.matrix,
200             GeomHelper.newRectangle(-1024, -1024, 2048, 2048) ); // mask of the list
201
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 JavaDoc[] {getCommandName()} );
208         }
209
210         boolean isLabels = rlabels != null || clabels != null;
211         if( defsym != null || isLabels ) {
212             // loading symbol file
213
if( mediafile != null ) {
214                 try {
215                     file.addExternalFile( mediafile, true );
216                 } catch( IVException e ) {
217                     Log.log( e );
218                 }
219             }
220             // load default symbol file
221
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         // calculate cell width and height
234
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         // buidl grid cells
242
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         // draw grid (borders)
249
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             // draw cells with full color
254
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             // draw main table with full color
264
shape.movePenTo(winWidth, 0);
265             shape.drawLineTo(winWidth, winHeight);
266             shape.drawLineTo(0, winHeight);
267
268             // draw cells with reduced color
269
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             // draw main table with reduced color
279
shape.movePenTo(0, winHeight);
280             shape.drawLineTo(0, 0);
281             shape.drawLineTo(winWidth, 0);
282
283             // add shape to frame
284
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             // map generator font names to font names from DefaultSymbolFile
297
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         // draw labels
313
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 JavaDoc 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; // center
321
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 JavaDoc 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; // center
332
putSymbolInCell( frame, text, r, layer++, labelscale, true );
333                 }
334                 row++;
335             }
336         }
337
338         // process datasource
339
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 JavaDoc clipName = data[r][clipIdx];
346                 template = file.getScript(clipName);
347                 if( template == null ) {
348                     throw new IVException( Resource.CMDSCRIPTNOTFOUND, new Object JavaDoc[] {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