1 package com.lowagie.text.rtf.graphic; 2 3 import java.awt.Color ; 4 import java.awt.Point ; 5 import java.io.ByteArrayOutputStream ; 6 import java.io.IOException ; 7 import java.io.OutputStream ; 8 9 import com.lowagie.text.rtf.RtfAddableElement; 10 11 34 public class RtfShapeProperty extends RtfAddableElement { 35 39 public static final String PROPERTY_VERTICIES = "pVerticies"; 40 44 public static final String PROPERTY_GEO_TOP = "geoTop"; 45 49 public static final String PROPERTY_GEO_LEFT = "geoLeft"; 50 54 public static final String PROPERTY_GEO_RIGHT = "geoRight"; 55 59 public static final String PROPERTY_GEO_BOTTOM = "geoBottom"; 60 64 public static final String PROPERTY_LAYOUT_IN_CELL = "fLayoutInCell"; 65 69 public static final String PROPERTY_FLIP_V = "fFlipV"; 70 74 public static final String PROPERTY_FLIP_H = "fFlipH"; 75 79 public static final String PROPERTY_FILL_COLOR = "fillColor"; 80 84 public static final String PROPERTY_LINE_COLOR = "lineColor"; 85 89 public static final String PROPERTY_ADJUST_VALUE = "adjustValue"; 90 91 94 private static final int PROPERTY_TYPE_LONG = 1; 95 98 private static final int PROPERTY_TYPE_BOOLEAN = 2; 99 102 private static final int PROPERTY_TYPE_DOUBLE = 3; 103 106 private static final int PROPERTY_TYPE_COLOR = 4; 107 110 private static final int PROPERTY_TYPE_ARRAY = 5; 111 112 115 private int type = 0; 116 119 private String name = ""; 120 123 private Object value = null; 124 125 131 private RtfShapeProperty(String name, Object value) { 132 this.name = name; 133 this.value = value; 134 } 135 136 142 public RtfShapeProperty(String name, long value) { 143 this(name, new Long (value)); 144 this.type = PROPERTY_TYPE_LONG; 145 } 146 147 153 public RtfShapeProperty(String name, double value) { 154 this(name, new Double (value)); 155 this.type = PROPERTY_TYPE_DOUBLE; 156 } 157 158 164 public RtfShapeProperty(String name, boolean value) { 165 this(name, new Boolean (value)); 166 this.type = PROPERTY_TYPE_BOOLEAN; 167 } 168 169 175 public RtfShapeProperty(String name, Color value) { 176 this(name, (Object ) value); 177 this.type = PROPERTY_TYPE_COLOR; 178 } 179 180 186 public RtfShapeProperty(String name, int[] value) { 187 this(name, (Object ) value); 188 this.type = PROPERTY_TYPE_ARRAY; 189 } 190 191 197 public RtfShapeProperty(String name, Point [] value) { 198 this(name, (Object ) value); 199 this.type = PROPERTY_TYPE_ARRAY; 200 } 201 202 207 public String getName() { 208 return this.name; 209 } 210 211 216 public byte[] write() 217 { 218 ByteArrayOutputStream result = new ByteArrayOutputStream (); 219 try { 220 writeContent(result); 221 } catch(IOException ioe) { 222 ioe.printStackTrace(); 223 } 224 return result.toByteArray(); 225 } 226 227 231 public void writeContent(final OutputStream result) throws IOException 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 ) 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 color = (Color ) 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 []) { 272 Point [] values = (Point []) 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 |