001: package com.lowagie.text.rtf.graphic;
002:
003: import com.lowagie.text.DocumentException;
004: import com.lowagie.text.ExceptionConverter;
005: import com.lowagie.text.Image;
006: import java.awt.Color;
007: import java.awt.Point;
008: import java.io.ByteArrayOutputStream;
009: import java.io.IOException;
010: import java.io.OutputStream;
011:
012: import com.lowagie.text.rtf.RtfAddableElement;
013:
014: /**
015: * The RtfShapeProperty stores all shape properties that are
016: * not handled by the RtfShape and RtfShapePosition.<br /><br />
017: *
018: * There is a huge selection of properties that can be set. For
019: * the most important properites there are constants for the
020: * property name, for all others you must find the correct
021: * property name in the RTF specification (version 1.6).<br /><br />
022: *
023: * The following types of property values are supported:
024: * <ul>
025: * <li>long</li>
026: * <li>double</li>
027: * <li>boolean</li>
028: * <li>Color</li>
029: * <li>int[]</li>
030: * <li>Point[]</li>
031: * </ul>
032: *
033: * @version $Id: RtfShapeProperty.java 2844 2007-06-19 11:41:07Z psoares33 $
034: * @author Mark Hall (mhall@edu.uni-klu.ac.at)
035: * @author Thomas Bickel (tmb99@inode.at)
036: */
037: public class RtfShapeProperty extends RtfAddableElement {
038: /**
039: * Property for defining an image.
040: */
041: public static final String PROPERTY_IMAGE = "pib";
042: /**
043: * Property for defining vertices in freeform shapes. Requires a
044: * Point array as the value.
045: */
046: public static final String PROPERTY_VERTICIES = "pVerticies";
047: /**
048: * Property for defining the minimum vertical coordinate that is
049: * visible. Requires a long value.
050: */
051: public static final String PROPERTY_GEO_TOP = "geoTop";
052: /**
053: * Property for defining the minimum horizontal coordinate that is
054: * visible. Requires a long value.
055: */
056: public static final String PROPERTY_GEO_LEFT = "geoLeft";
057: /**
058: * Property for defining the maximum horizontal coordinate that is
059: * visible. Requires a long value.
060: */
061: public static final String PROPERTY_GEO_RIGHT = "geoRight";
062: /**
063: * Property for defining the maximum vertical coordinate that is
064: * visible. Requires a long value.
065: */
066: public static final String PROPERTY_GEO_BOTTOM = "geoBottom";
067: /**
068: * Property for defining that the shape is in a table cell. Requires
069: * a boolean value.
070: */
071: public static final String PROPERTY_LAYOUT_IN_CELL = "fLayoutInCell";
072: /**
073: * Property for signalling a vertical flip of the shape. Requires a
074: * boolean value.
075: */
076: public static final String PROPERTY_FLIP_V = "fFlipV";
077: /**
078: * Property for signalling a horizontal flip of the shape. Requires a
079: * boolean value.
080: */
081: public static final String PROPERTY_FLIP_H = "fFlipH";
082: /**
083: * Property for defining the fill color of the shape. Requires a
084: * Color value.
085: */
086: public static final String PROPERTY_FILL_COLOR = "fillColor";
087: /**
088: * Property for defining the line color of the shape. Requires a
089: * Color value.
090: */
091: public static final String PROPERTY_LINE_COLOR = "lineColor";
092: /**
093: * Property for defining the first adjust handle for shapes. Used
094: * with the rounded rectangle. Requires a long value.
095: */
096: public static final String PROPERTY_ADJUST_VALUE = "adjustValue";
097:
098: /**
099: * The stored value is a long.
100: */
101: private static final int PROPERTY_TYPE_LONG = 1;
102: /**
103: * The stored value is boolean.
104: */
105: private static final int PROPERTY_TYPE_BOOLEAN = 2;
106: /**
107: * The stored value is a double.
108: */
109: private static final int PROPERTY_TYPE_DOUBLE = 3;
110: /**
111: * The stored value is a Color.
112: */
113: private static final int PROPERTY_TYPE_COLOR = 4;
114: /**
115: * The stored value is either an int or a Point array.
116: */
117: private static final int PROPERTY_TYPE_ARRAY = 5;
118: /**
119: * The stored value is an Image.
120: */
121: private static final int PROPERTY_TYPE_IMAGE = 6;
122:
123: /**
124: * The value type.
125: */
126: private int type = 0;
127: /**
128: * The RtfShapeProperty name.
129: */
130: private String name = "";
131: /**
132: * The RtfShapeProperty value.
133: */
134: private Object value = null;
135:
136: /**
137: * Internaly used to create the RtfShape.
138: *
139: * @param name The property name to use.
140: * @param value The property value to use.
141: */
142: private RtfShapeProperty(String name, Object value) {
143: this .name = name;
144: this .value = value;
145: }
146:
147: /**
148: * Constructs a RtfShapeProperty with a long value.
149: *
150: * @param name The property name to use.
151: * @param value The long value to use.
152: */
153: public RtfShapeProperty(String name, long value) {
154: this (name, new Long(value));
155: this .type = PROPERTY_TYPE_LONG;
156: }
157:
158: /**
159: * Constructs a RtfShapeProperty with a double value.
160: *
161: * @param name The property name to use.
162: * @param value The double value to use.
163: */
164: public RtfShapeProperty(String name, double value) {
165: this (name, new Double(value));
166: this .type = PROPERTY_TYPE_DOUBLE;
167: }
168:
169: /**
170: * Constructs a RtfShapeProperty with a boolean value.
171: *
172: * @param name The property name to use.
173: * @param value The boolean value to use.
174: */
175: public RtfShapeProperty(String name, boolean value) {
176: this (name, new Boolean(value));
177: this .type = PROPERTY_TYPE_BOOLEAN;
178: }
179:
180: /**
181: * Constructs a RtfShapeProperty with a Color value.
182: *
183: * @param name The property name to use.
184: * @param value The Color value to use.
185: */
186: public RtfShapeProperty(String name, Color value) {
187: this (name, (Object) value);
188: this .type = PROPERTY_TYPE_COLOR;
189: }
190:
191: /**
192: * Constructs a RtfShapeProperty with an int array value.
193: *
194: * @param name The property name to use.
195: * @param value The int array to use.
196: */
197: public RtfShapeProperty(String name, int[] value) {
198: this (name, (Object) value);
199: this .type = PROPERTY_TYPE_ARRAY;
200: }
201:
202: /**
203: * Constructs a RtfShapeProperty with a Point array value.
204: *
205: * @param name The property name to use.
206: * @param value The Point array to use.
207: */
208: public RtfShapeProperty(String name, Point[] value) {
209: this (name, (Object) value);
210: this .type = PROPERTY_TYPE_ARRAY;
211: }
212:
213: /**
214: * Constructs a RtfShapeProperty with an Image value.
215: *
216: * @param name The property name to use.
217: * @param value The Image to use.
218: */
219: public RtfShapeProperty(String name, Image value) {
220: this .name = name;
221: this .value = value;
222: this .type = PROPERTY_TYPE_IMAGE;
223: }
224:
225: /**
226: * Gets the name of this RtfShapeProperty.
227: *
228: * @return The name of this RtfShapeProperty.
229: */
230: public String getName() {
231: return this .name;
232: }
233:
234: /**
235: * Writes the property definition. How the property
236: * is written depends on the property type.
237: * @deprecated replaced by {@link #writeContent(OutputStream)}
238: */
239: public byte[] write() {
240: ByteArrayOutputStream result = new ByteArrayOutputStream();
241: try {
242: writeContent(result);
243: } catch (IOException ioe) {
244: ioe.printStackTrace();
245: }
246: return result.toByteArray();
247: }
248:
249: /**
250: * Writes the property definition. How the property
251: * is written depends on the property type.
252: */
253: public void writeContent(final OutputStream result)
254: throws IOException {
255: result.write(OPEN_GROUP);
256: result.write("\\sp".getBytes());
257: result.write(OPEN_GROUP);
258: result.write("\\sn".getBytes());
259: result.write(DELIMITER);
260: result.write(this .name.getBytes());
261: result.write(CLOSE_GROUP);
262: result.write(OPEN_GROUP);
263: result.write("\\sv".getBytes());
264: result.write(DELIMITER);
265: switch (this .type) {
266: case PROPERTY_TYPE_LONG:
267: case PROPERTY_TYPE_DOUBLE:
268: result.write(this .value.toString().getBytes());
269: break;
270: case PROPERTY_TYPE_BOOLEAN:
271: if (((Boolean) this .value).booleanValue()) {
272: result.write("1".getBytes());
273: } else {
274: result.write("0".getBytes());
275: }
276: break;
277: case PROPERTY_TYPE_COLOR:
278: Color color = (Color) this .value;
279: result
280: .write(intToByteArray(color.getRed()
281: | (color.getGreen() << 8)
282: | (color.getBlue() << 16)));
283: break;
284: case PROPERTY_TYPE_ARRAY:
285: if (this .value instanceof int[]) {
286: int[] values = (int[]) this .value;
287: result.write("4;".getBytes());
288: result.write(intToByteArray(values.length));
289: result.write(COMMA_DELIMITER);
290: for (int i = 0; i < values.length; i++) {
291: result.write(intToByteArray(values[i]));
292: if (i < values.length - 1) {
293: result.write(COMMA_DELIMITER);
294: }
295: }
296: } else if (this .value instanceof Point[]) {
297: Point[] values = (Point[]) this .value;
298: result.write("8;".getBytes());
299: result.write(intToByteArray(values.length));
300: result.write(COMMA_DELIMITER);
301: for (int i = 0; i < values.length; i++) {
302: result.write("(".getBytes());
303: result.write(intToByteArray(values[i].x));
304: result.write(",".getBytes());
305: result.write(intToByteArray(values[i].y));
306: result.write(")".getBytes());
307: if (i < values.length - 1) {
308: result.write(COMMA_DELIMITER);
309: }
310: }
311: }
312: break;
313: case PROPERTY_TYPE_IMAGE:
314: Image image = (Image) this .value;
315: RtfImage img = null;
316: try {
317: img = new RtfImage(this .doc, image);
318: } catch (DocumentException de) {
319: throw new ExceptionConverter(de);
320: }
321: img.setTopLevelElement(true);
322: result.write(OPEN_GROUP);
323: img.writeContent(result);
324: result.write(CLOSE_GROUP);
325: break;
326: }
327: result.write(CLOSE_GROUP);
328: result.write(CLOSE_GROUP);
329: }
330:
331: }
|