KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lowagie > text > rtf > graphic > RtfShapeProperty


1 package com.lowagie.text.rtf.graphic;
2
3 import java.awt.Color JavaDoc;
4 import java.awt.Point JavaDoc;
5 import java.io.ByteArrayOutputStream JavaDoc;
6 import java.io.IOException JavaDoc;
7 import java.io.OutputStream JavaDoc;
8
9 import com.lowagie.text.rtf.RtfAddableElement;
10
11 /**
12  * The RtfShapeProperty stores all shape properties that are
13  * not handled by the RtfShape and RtfShapePosition.<br /><br />
14  *
15  * There is a huge selection of properties that can be set. For
16  * the most important properites there are constants for the
17  * property name, for all others you must find the correct
18  * property name in the RTF specification (version 1.6).<br /><br />
19  *
20  * The following types of property values are supported:
21  * <ul>
22  * <li>long</li>
23  * <li>double</li>
24  * <li>boolean</li>
25  * <li>Color</li>
26  * <li>int[]</li>
27  * <li>Point[]</li>
28  * </ul>
29  *
30  * @version $Id: RtfShapeProperty.java 2776 2007-05-23 20:01:40Z hallm $
31  * @author Mark Hall (mhall@edu.uni-klu.ac.at)
32  * @author Thomas Bickel (tmb99@inode.at)
33  */

34 public class RtfShapeProperty extends RtfAddableElement {
35     /**
36      * Property for defining vertices in freeform shapes. Requires a
37      * Point array as the value.
38      */

39     public static final String JavaDoc PROPERTY_VERTICIES = "pVerticies";
40     /**
41      * Property for defining the minimum vertical coordinate that is
42      * visible. Requires a long value.
43      */

44     public static final String JavaDoc PROPERTY_GEO_TOP = "geoTop";
45     /**
46      * Property for defining the minimum horizontal coordinate that is
47      * visible. Requires a long value.
48      */

49     public static final String JavaDoc PROPERTY_GEO_LEFT = "geoLeft";
50     /**
51      * Property for defining the maximum horizontal coordinate that is
52      * visible. Requires a long value.
53      */

54     public static final String JavaDoc PROPERTY_GEO_RIGHT = "geoRight";
55     /**
56      * Property for defining the maximum vertical coordinate that is
57      * visible. Requires a long value.
58      */

59     public static final String JavaDoc PROPERTY_GEO_BOTTOM = "geoBottom";
60     /**
61      * Property for defining that the shape is in a table cell. Requires
62      * a boolean value.
63      */

64     public static final String JavaDoc PROPERTY_LAYOUT_IN_CELL = "fLayoutInCell";
65     /**
66      * Property for signalling a vertical flip of the shape. Requires a
67      * boolean value.
68      */

69     public static final String JavaDoc PROPERTY_FLIP_V = "fFlipV";
70     /**
71      * Property for signalling a horizontal flip of the shape. Requires a
72      * boolean value.
73      */

74     public static final String JavaDoc PROPERTY_FLIP_H = "fFlipH";
75     /**
76      * Property for defining the fill color of the shape. Requires a
77      * Color value.
78      */

79     public static final String JavaDoc PROPERTY_FILL_COLOR = "fillColor";
80     /**
81      * Property for defining the line color of the shape. Requires a
82      * Color value.
83      */

84     public static final String JavaDoc PROPERTY_LINE_COLOR = "lineColor";
85     /**
86      * Property for defining the first adjust handle for shapes. Used
87      * with the rounded rectangle. Requires a long value.
88      */

89     public static final String JavaDoc PROPERTY_ADJUST_VALUE = "adjustValue";
90
91     /**
92      * The stored value is a long.
93      */

94     private static final int PROPERTY_TYPE_LONG = 1;
95     /**
96      * The stored value is boolean.
97      */

98     private static final int PROPERTY_TYPE_BOOLEAN = 2;
99     /**
100      * The stored value is a double.
101      */

102     private static final int PROPERTY_TYPE_DOUBLE = 3;
103     /**
104      * The stored value is a Color.
105      */

106     private static final int PROPERTY_TYPE_COLOR = 4;
107     /**
108      * The stored value is either an int or a Point array.
109      */

110     private static final int PROPERTY_TYPE_ARRAY = 5;
111     
112     /**
113      * The value type.
114      */

115     private int type = 0;
116     /**
117      * The RtfShapeProperty name.
118      */

119     private String JavaDoc name = "";
120     /**
121      * The RtfShapeProperty value.
122      */

123     private Object JavaDoc value = null;
124     
125     /**
126      * Internaly used to create the RtfShape.
127      *
128      * @param name The property name to use.
129      * @param value The property value to use.
130      */

131     private RtfShapeProperty(String JavaDoc name, Object JavaDoc value) {
132         this.name = name;
133         this.value = value;
134     }
135     
136     /**
137      * Constructs a RtfShapeProperty with a long value.
138      *
139      * @param name The property name to use.
140      * @param value The long value to use.
141      */

142     public RtfShapeProperty(String JavaDoc name, long value) {
143         this(name, new Long JavaDoc(value));
144         this.type = PROPERTY_TYPE_LONG;
145     }
146     
147     /**
148      * Constructs a RtfShapeProperty with a double value.
149      *
150      * @param name The property name to use.
151      * @param value The double value to use.
152      */

153     public RtfShapeProperty(String JavaDoc name, double value) {
154         this(name, new Double JavaDoc(value));
155         this.type = PROPERTY_TYPE_DOUBLE;
156     }
157     
158     /**
159      * Constructs a RtfShapeProperty with a boolean value.
160      *
161      * @param name The property name to use.
162      * @param value The boolean value to use.
163      */

164     public RtfShapeProperty(String JavaDoc name, boolean value) {
165         this(name, new Boolean JavaDoc(value));
166         this.type = PROPERTY_TYPE_BOOLEAN;
167     }
168     
169     /**
170      * Constructs a RtfShapeProperty with a Color value.
171      *
172      * @param name The property name to use.
173      * @param value The Color value to use.
174      */

175     public RtfShapeProperty(String JavaDoc name, Color JavaDoc value) {
176         this(name, (Object JavaDoc) value);
177         this.type = PROPERTY_TYPE_COLOR;
178     }
179     
180     /**
181      * Constructs a RtfShapeProperty with an int array value.
182      *
183      * @param name The property name to use.
184      * @param value The int array to use.
185      */

186     public RtfShapeProperty(String JavaDoc name, int[] value) {
187         this(name, (Object JavaDoc) value);
188         this.type = PROPERTY_TYPE_ARRAY;
189     }
190     
191     /**
192      * Constructs a RtfShapeProperty with a Point array value.
193      *
194      * @param name The property name to use.
195      * @param value The Point array to use.
196      */

197     public RtfShapeProperty(String JavaDoc name, Point JavaDoc[] value) {
198         this(name, (Object JavaDoc) value);
199         this.type = PROPERTY_TYPE_ARRAY;
200     }
201     
202     /**
203      * Gets the name of this RtfShapeProperty.
204      *
205      * @return The name of this RtfShapeProperty.
206      */

207     public String JavaDoc getName() {
208         return this.name;
209     }
210     
211     /**
212      * Writes the property definition. How the property
213      * is written depends on the property type.
214      * @deprecated replaced by {@link #writeContent(OutputStream)}
215      */

216     public byte[] write()
217     {
218         ByteArrayOutputStream JavaDoc result = new ByteArrayOutputStream JavaDoc();
219         try {
220             writeContent(result);
221         } catch(IOException JavaDoc ioe) {
222             ioe.printStackTrace();
223         }
224         return result.toByteArray();
225     }
226     
227     /**
228      * Writes the property definition. How the property
229      * is written depends on the property type.
230      */

231     public void writeContent(final OutputStream JavaDoc result) throws IOException JavaDoc
232     {
233         result.write(OPEN_GROUP);
234         result.write("\\sp".getBytes());
235         result.write(OPEN_GROUP);
236         result.write("\\sn".getBytes());
237         result.write(DELIMITER);
238         result.write(this.name.getBytes());
239         result.write(CLOSE_GROUP);
240         result.write(OPEN_GROUP);
241         result.write("\\sv".getBytes());
242         result.write(DELIMITER);
243         switch(this.type) {
244         case PROPERTY_TYPE_LONG:
245         case PROPERTY_TYPE_DOUBLE:
246             result.write(this.value.toString().getBytes());
247             break;
248         case PROPERTY_TYPE_BOOLEAN:
249             if(((Boolean JavaDoc) this.value).booleanValue()) {
250                 result.write("1".getBytes());
251             } else {
252                 result.write("0".getBytes());
253             }
254             break;
255         case PROPERTY_TYPE_COLOR:
256             Color JavaDoc color = (Color JavaDoc) this.value;
257             result.write(intToByteArray(color.getRed() | (color.getGreen() << 8) | (color.getBlue() << 16)));
258             break;
259         case PROPERTY_TYPE_ARRAY:
260             if(this.value instanceof int[]) {
261                 int[] values = (int[]) this.value;
262                 result.write("4;".getBytes());
263                 result.write(intToByteArray(values.length));
264                 result.write(COMMA_DELIMITER);
265                 for(int i = 0; i < values.length; i++) {
266                     result.write(intToByteArray(values[i]));
267                     if(i < values.length - 1) {
268                         result.write(COMMA_DELIMITER);
269                     }
270                 }
271             } else if(this.value instanceof Point JavaDoc[]) {
272                 Point JavaDoc[] values = (Point JavaDoc[]) this.value;
273                 result.write("8;".getBytes());
274                 result.write(intToByteArray(values.length));
275                 result.write(COMMA_DELIMITER);
276                 for(int i = 0; i < values.length; i++) {
277                     result.write("(".getBytes());
278                     result.write(intToByteArray(values[i].x));
279                     result.write(",".getBytes());
280                     result.write(intToByteArray(values[i].y));
281                     result.write(")".getBytes());
282                     if(i < values.length - 1) {
283                         result.write(COMMA_DELIMITER);
284                     }
285                 }
286             }
287             break;
288         }
289         result.write(CLOSE_GROUP);
290         result.write(CLOSE_GROUP);
291     }
292     
293 }
294
Popular Tags