KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openlaszlo > iv > flash > api > shape > ShapeStyles


1 /*
2  * $Id: ShapeStyles.java,v 1.2 2002/02/15 23:44:28 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.api.shape;
52
53 import org.openlaszlo.iv.flash.util.*;
54 import org.openlaszlo.iv.flash.api.*;
55 import org.openlaszlo.iv.flash.parser.*;
56 import java.io.PrintStream JavaDoc;
57
58 /**
59  * This object represents collection of shape styles.
60  *
61  * @author Dmitry Skavish
62  */

63 public final class ShapeStyles extends FlashItem {
64
65     /**
66      * Vector of {@link FillStyle}s records
67      */

68     public IVVector fillStyles;
69
70     /**
71      * Vector of {@link LineStyle}s records.
72      */

73     public IVVector lineStyles;
74
75     /**
76      * Creates empty shapestyle
77      */

78     public ShapeStyles() {
79         this(new IVVector(), new IVVector());
80     }
81
82     public ShapeStyles( IVVector fillStyles, IVVector lineStyles ) {
83         this.fillStyles = fillStyles;
84         this.lineStyles = lineStyles;
85     }
86
87     /**
88      * Adds new fillstyle to this array.
89      *
90      * @param fs new fillstyle to be added
91      * @return index of added fillstyle
92      */

93     public int addFillStyle( FillStyle fs ) {
94         fillStyles.addElement(fs);
95         return fillStyles.size();
96     }
97
98     /**
99      * Adds new linestyle to this array.
100      *
101      * @param ls new linestyle to be added
102      * @return index of added linestyle
103      */

104     public int addLineStyle( LineStyle ls ) {
105         lineStyles.addElement(ls);
106         return lineStyles.size();
107     }
108
109     /**
110      * Returns index of specified fillstyle
111      *
112      * @param fs fillstyle which index is to be returned
113      * @return index of specified fillstyle or -1
114      */

115     public int getFillStyleIndex( FillStyle fs ) {
116         for( int i=0; i<fillStyles.size(); i++ ) {
117             FillStyle ffs = (FillStyle) fillStyles.elementAt(i);
118             if( ffs == fs ) return i+1;
119         }
120         return -1;
121     }
122
123     /**
124      * Returns index of specified linestyle
125      *
126      * @param ls linestyle which index is to be returned
127      * @return index of specified linestyle or -1
128      */

129     public int getLineStyleIndex( LineStyle ls ) {
130         for( int i=0; i<lineStyles.size(); i++ ) {
131             LineStyle lls = (LineStyle) lineStyles.elementAt(i);
132             if( lls == ls ) return i+1;
133         }
134         return -1;
135     }
136
137     /**
138      * Returns line style by its index
139      *
140      * @param index index of line style to be returned
141      * @return line style at specified index
142      */

143     public LineStyle getLineStyle( int index ) {
144         return (LineStyle) lineStyles.elementAt(index);
145     }
146
147     /**
148      * Returns fill style by its index
149      *
150      * @param index index of fill style to be returned
151      * @return fill style at specified index
152      */

153     public FillStyle getFillStyle( int index ) {
154         return (FillStyle) fillStyles.elementAt(index);
155     }
156
157     /**
158      * Returns maximum number of bits required to hold fillstyles' indexes
159      *
160      * @return maximum number of bits required to hold fillstyles' indexes
161      */

162     public int calcNFillBits() {
163         return Util.getMinBitsU( fillStyles.size() );
164     }
165
166     /**
167      * Returns maximum number of bits required to hold linestyles' indexes
168      *
169      * @return maximum number of bits required to hold linestyles' indexes
170      */

171     public int calcNLineBits() {
172         return Util.getMinBitsU( lineStyles.size() );
173     }
174
175     public void collectDeps( DepsCollector dc ) {
176         int fillStyleSize = fillStyles.size();
177         for( int i=0; i<fillStyleSize; i++ ) {
178             FillStyle fillStyle = (FillStyle) fillStyles.elementAt(i);
179             if( fillStyle.getBitmap() != null ) dc.addDep(fillStyle.getBitmap());
180         }
181     }
182
183     public static ShapeStyles parse( Parser p, boolean withAlpha ) {
184         // Get the number of fills.
185
int nFills = p.getUByte();
186         if( nFills == 255 ) {
187             nFills = p.getUWord();
188         }
189         IVVector fillStyles = new IVVector(nFills);
190
191         // Get each of the fill style.
192
for( int i=0; i<nFills; i++ ) {
193             fillStyles.addElement( FillStyle.parse(p, withAlpha) );
194         }
195
196         int nLines = p.getUByte();
197         if( nLines == 255 ) {
198             nLines = p.getUWord();
199         }
200         IVVector lineStyles = new IVVector(nLines);
201
202         // Get each of the line styles.
203
for( int i=0; i<nLines; i++ ) {
204             lineStyles.addElement( LineStyle.parse(p, withAlpha) );
205         }
206
207         return new ShapeStyles(fillStyles, lineStyles);
208     }
209
210     public void write( FlashOutput fob ) {
211         int fillStyleSize = fillStyles.size();
212         if( fillStyleSize > 254 ) {
213             fob.writeByte(255);
214             fob.writeWord(fillStyleSize);
215         } else {
216             fob.writeByte(fillStyleSize);
217         }
218         fillStyles.write(fob);
219         int lineStyleSize = lineStyles.size();
220         if( lineStyleSize > 254 ) {
221             fob.writeByte(255);
222             fob.writeWord(lineStyleSize);
223         } else {
224             fob.writeByte(lineStyleSize);
225         }
226         lineStyles.write(fob);
227     }
228
229     public void printContent( PrintStream JavaDoc out, String JavaDoc indent ) {
230         out.println( indent+"ShapeStyles:" );
231         fillStyles.printContent(out, indent+" ");
232         lineStyles.printContent(out, indent+" ");
233     }
234
235     protected FlashItem copyInto( FlashItem item, ScriptCopier copier ) {
236         ((ShapeStyles)item).fillStyles = fillStyles.getCopy(copier);
237         ((ShapeStyles)item).lineStyles = lineStyles.getCopy(copier);
238         return item;
239     }
240
241     public FlashItem getCopy( ScriptCopier copier ) {
242         return copyInto( new ShapeStyles(null, null), copier );
243     }
244 }
245
Popular Tags