1 7 package javax.swing.text.rtf; 8 9 import javax.swing.text.StyleConstants ; 10 import javax.swing.text.AttributeSet ; 11 import javax.swing.text.MutableAttributeSet ; 12 import javax.swing.text.TabStop ; 13 import java.util.*; 14 import java.io.IOException ; 15 16 class RTFAttributes 17 { 18 static RTFAttribute attributes[]; 19 20 static { 21 Vector a = new Vector(); 22 int CHR = RTFAttribute.D_CHARACTER; 23 int PGF = RTFAttribute.D_PARAGRAPH; 24 int SEC = RTFAttribute.D_SECTION; 25 int DOC = RTFAttribute.D_DOCUMENT; 26 int PST = RTFAttribute.D_META; 27 Boolean True = Boolean.valueOf(true); 28 Boolean False = Boolean.valueOf(false); 29 30 a.addElement(new BooleanAttribute(CHR, StyleConstants.Italic, "i")); 31 a.addElement(new BooleanAttribute(CHR, StyleConstants.Bold, "b")); 32 a.addElement(new BooleanAttribute(CHR, StyleConstants.Underline, "ul")); 33 a.addElement(NumericAttribute.NewTwips(PGF, StyleConstants.LeftIndent, "li", 34 0f, 0)); 35 a.addElement(NumericAttribute.NewTwips(PGF, StyleConstants.RightIndent, "ri", 36 0f, 0)); 37 a.addElement(NumericAttribute.NewTwips(PGF, StyleConstants.FirstLineIndent, "fi", 38 0f, 0)); 39 40 a.addElement(new AssertiveAttribute(PGF, StyleConstants.Alignment, 41 "ql", StyleConstants.ALIGN_LEFT)); 42 a.addElement(new AssertiveAttribute(PGF, StyleConstants.Alignment, 43 "qr", StyleConstants.ALIGN_RIGHT)); 44 a.addElement(new AssertiveAttribute(PGF, StyleConstants.Alignment, 45 "qc", StyleConstants.ALIGN_CENTER)); 46 a.addElement(new AssertiveAttribute(PGF, StyleConstants.Alignment, 47 "qj", StyleConstants.ALIGN_JUSTIFIED)); 48 a.addElement(NumericAttribute.NewTwips(PGF, StyleConstants.SpaceAbove, 49 "sa", 0)); 50 a.addElement(NumericAttribute.NewTwips(PGF, StyleConstants.SpaceBelow, 51 "sb", 0)); 52 53 a.addElement(new AssertiveAttribute(PST, RTFReader.TabAlignmentKey, 54 "tqr", TabStop.ALIGN_RIGHT)); 55 a.addElement(new AssertiveAttribute(PST, RTFReader.TabAlignmentKey, 56 "tqc", TabStop.ALIGN_CENTER)); 57 a.addElement(new AssertiveAttribute(PST, RTFReader.TabAlignmentKey, 58 "tqdec", TabStop.ALIGN_DECIMAL)); 59 60 61 a.addElement(new AssertiveAttribute(PST, RTFReader.TabLeaderKey, 62 "tldot", TabStop.LEAD_DOTS)); 63 a.addElement(new AssertiveAttribute(PST, RTFReader.TabLeaderKey, 64 "tlhyph", TabStop.LEAD_HYPHENS)); 65 a.addElement(new AssertiveAttribute(PST, RTFReader.TabLeaderKey, 66 "tlul", TabStop.LEAD_UNDERLINE)); 67 a.addElement(new AssertiveAttribute(PST, RTFReader.TabLeaderKey, 68 "tlth", TabStop.LEAD_THICKLINE)); 69 a.addElement(new AssertiveAttribute(PST, RTFReader.TabLeaderKey, 70 "tleq", TabStop.LEAD_EQUALS)); 71 72 73 a.addElement(new BooleanAttribute(CHR, Constants.Caps, "caps")); 74 a.addElement(new BooleanAttribute(CHR, Constants.Outline, "outl")); 75 a.addElement(new BooleanAttribute(CHR, Constants.SmallCaps, "scaps")); 76 a.addElement(new BooleanAttribute(CHR, Constants.Shadow, "shad")); 77 a.addElement(new BooleanAttribute(CHR, Constants.Hidden, "v")); 78 a.addElement(new BooleanAttribute(CHR, Constants.Strikethrough, 79 "strike")); 80 a.addElement(new BooleanAttribute(CHR, Constants.Deleted, 81 "deleted")); 82 83 84 85 a.addElement(new AssertiveAttribute(DOC, "saveformat", "defformat", "RTF")); 86 a.addElement(new AssertiveAttribute(DOC, "landscape", "landscape")); 87 88 a.addElement(NumericAttribute.NewTwips(DOC, Constants.PaperWidth, 89 "paperw", 12240)); 90 a.addElement(NumericAttribute.NewTwips(DOC, Constants.PaperHeight, 91 "paperh", 15840)); 92 a.addElement(NumericAttribute.NewTwips(DOC, Constants.MarginLeft, 93 "margl", 1800)); 94 a.addElement(NumericAttribute.NewTwips(DOC, Constants.MarginRight, 95 "margr", 1800)); 96 a.addElement(NumericAttribute.NewTwips(DOC, Constants.MarginTop, 97 "margt", 1440)); 98 a.addElement(NumericAttribute.NewTwips(DOC, Constants.MarginBottom, 99 "margb", 1440)); 100 a.addElement(NumericAttribute.NewTwips(DOC, Constants.GutterWidth, 101 "gutter", 0)); 102 103 a.addElement(new AssertiveAttribute(PGF, Constants.WidowControl, 104 "nowidctlpar", False)); 105 a.addElement(new AssertiveAttribute(PGF, Constants.WidowControl, 106 "widctlpar", True)); 107 a.addElement(new AssertiveAttribute(DOC, Constants.WidowControl, 108 "widowctrl", True)); 109 110 111 RTFAttribute [] attrs = new RTFAttribute [a.size()]; 112 a.copyInto(attrs); 113 attributes = attrs; 114 } 115 116 static Dictionary attributesByKeyword() 117 { 118 Dictionary d = new Hashtable(attributes.length); 119 int i, m; 120 121 m = attributes.length; 122 for(i = 0; i < m; i++) 123 d.put(attributes[i].rtfName(), attributes[i]); 124 125 return d; 126 } 127 128 129 130 131 static abstract class GenericAttribute 132 { 133 int domain; 134 Object swingName; 135 String rtfName; 136 137 protected GenericAttribute(int d,Object s, String r) 138 { 139 domain = d; 140 swingName = s; 141 rtfName = r; 142 } 143 144 public int domain() { return domain; } 145 public Object swingName() { return swingName; } 146 public String rtfName() { return rtfName; } 147 148 abstract boolean set(MutableAttributeSet target); 149 abstract boolean set(MutableAttributeSet target, int parameter); 150 abstract boolean setDefault(MutableAttributeSet target); 151 152 public boolean write(AttributeSet source, 153 RTFGenerator target, 154 boolean force) 155 throws IOException  156 { 157 return writeValue(source.getAttribute(swingName), target, force); 158 } 159 160 public boolean writeValue(Object value, RTFGenerator target, 161 boolean force) 162 throws IOException 163 { 164 return false; 165 } 166 } 167 168 static class BooleanAttribute 169 extends GenericAttribute 170 implements RTFAttribute  171 { 172 boolean rtfDefault; 173 boolean swingDefault; 174 175 protected static final Boolean True = Boolean.valueOf(true); 176 protected static final Boolean False = Boolean.valueOf(false); 177 178 public BooleanAttribute(int d, Object s, 179 String r, boolean ds, boolean dr) 180 { 181 super(d, s, r); 182 swingDefault = ds; 183 rtfDefault = dr; 184 } 185 186 public BooleanAttribute(int d, Object s, String r) 187 { 188 super(d, s, r); 189 190 swingDefault = false; 191 rtfDefault = false; 192 } 193 194 public boolean set(MutableAttributeSet target) 195 { 196 198 target.addAttribute(swingName, True); 199 200 return true; 201 } 202 203 public boolean set(MutableAttributeSet target, int parameter) 204 { 205 206 Boolean value = ( parameter != 0 ? True : False ); 207 208 target.addAttribute(swingName, value); 209 210 return true; 211 } 212 213 public boolean setDefault(MutableAttributeSet target) 214 { 215 if (swingDefault != rtfDefault || 216 ( target.getAttribute(swingName) != null ) ) 217 target.addAttribute(swingName, Boolean.valueOf(rtfDefault)); 218 return true; 219 } 220 221 public boolean writeValue(Object o_value, 222 RTFGenerator target, 223 boolean force) 224 throws IOException  225 { 226 Boolean val; 227 228 if (o_value == null) 229 val = Boolean.valueOf(swingDefault); 230 else 231 val = (Boolean )o_value; 232 233 if (force || (val.booleanValue() != rtfDefault)) { 234 if (val.booleanValue()) { 235 target.writeControlWord(rtfName); 236 } else { 237 target.writeControlWord(rtfName, 0); 238 } 239 } 240 return true; 241 } 242 } 243 244 245 static class AssertiveAttribute 246 extends GenericAttribute 247 implements RTFAttribute  248 { 249 Object swingValue; 250 251 public AssertiveAttribute(int d, Object s, String r) 252 { 253 super(d, s, r); 254 swingValue = Boolean.valueOf(true); 255 } 256 257 public AssertiveAttribute(int d, Object s, String r, Object v) 258 { 259 super(d, s, r); 260 swingValue = v; 261 } 262 263 public AssertiveAttribute(int d, Object s, String r, int v) 264 { 265 super(d, s, r); 266 swingValue = new Integer (v); 267 } 268 269 public boolean set(MutableAttributeSet target) 270 { 271 if (swingValue == null) 272 target.removeAttribute(swingName); 273 else 274 target.addAttribute(swingName, swingValue); 275 276 return true; 277 } 278 279 public boolean set(MutableAttributeSet target, int parameter) 280 { 281 return false; 282 } 283 284 public boolean setDefault(MutableAttributeSet target) 285 { 286 target.removeAttribute(swingName); 287 return true; 288 } 289 290 public boolean writeValue(Object value, 291 RTFGenerator target, 292 boolean force) 293 throws IOException  294 { 295 if (value == null) { 296 return ! force; 297 } 298 299 if (value.equals(swingValue)) { 300 target.writeControlWord(rtfName); 301 return true; 302 } 303 304 return ! force; 305 } 306 } 307 308 309 static class NumericAttribute 310 extends GenericAttribute 311 implements RTFAttribute  312 { 313 int rtfDefault; 314 Number swingDefault; 315 float scale; 316 317 protected NumericAttribute(int d, Object s, String r) 318 { 319 super(d, s, r); 320 rtfDefault = 0; 321 swingDefault = null; 322 scale = 1f; 323 } 324 325 public NumericAttribute(int d, Object s, 326 String r, int ds, int dr) 327 { 328 this(d, s, r, new Integer (ds), dr, 1f); 329 } 330 331 public NumericAttribute(int d, Object s, 332 String r, Number ds, int dr, float sc) 333 { 334 super(d, s, r); 335 swingDefault = ds; 336 rtfDefault = dr; 337 scale = sc; 338 } 339 340 public static NumericAttribute NewTwips(int d, Object s, String r, 341 float ds, int dr) 342 { 343 return new NumericAttribute(d, s, r, new Float (ds), dr, 20f); 344 } 345 346 public static NumericAttribute NewTwips(int d, Object s, String r, 347 int dr) 348 { 349 return new NumericAttribute(d, s, r, null, dr, 20f); 350 } 351 352 public boolean set(MutableAttributeSet target) 353 { 354 return false; 355 } 356 357 public boolean set(MutableAttributeSet target, int parameter) 358 { 359 Number swingValue; 360 361 if (scale == 1f) 362 swingValue = new Integer (parameter); 363 else 364 swingValue = new Float (parameter / scale); 365 target.addAttribute(swingName, swingValue); 366 return true; 367 } 368 369 public boolean setDefault(MutableAttributeSet target) 370 { 371 Number old = (Number )target.getAttribute(swingName); 372 if (old == null) 373 old = swingDefault; 374 if (old != null && ( 375 (scale == 1f && old.intValue() == rtfDefault) || 376 (Math.round(old.floatValue() * scale) == rtfDefault) 377 )) 378 return true; 379 set(target, rtfDefault); 380 return true; 381 } 382 383 public boolean writeValue(Object o_value, 384 RTFGenerator target, 385 boolean force) 386 throws IOException  387 { 388 Number value = (Number )o_value; 389 if (value == null) 390 value = swingDefault; 391 if (value == null) { 392 396 return true; 397 } 398 int int_value = Math.round(value.floatValue() * scale); 399 if (force || (int_value != rtfDefault)) 400 target.writeControlWord(rtfName, int_value); 401 return true; 402 } 403 } 404 } 405 | Popular Tags |