KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > swing > text > rtf > RTFAttributes


1 /*
2  * @(#)RTFAttributes.java 1.13 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7 package javax.swing.text.rtf;
8
9 import javax.swing.text.StyleConstants JavaDoc;
10 import javax.swing.text.AttributeSet JavaDoc;
11 import javax.swing.text.MutableAttributeSet JavaDoc;
12 import javax.swing.text.TabStop JavaDoc;
13 import java.util.*;
14 import java.io.IOException JavaDoc;
15
16 class RTFAttributes
17 {
18     static RTFAttribute JavaDoc 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 JavaDoc True = Boolean.valueOf(true);
28     Boolean JavaDoc 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     /* The following aren't actually recognized by Swing */
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 JavaDoc[] attrs = new RTFAttribute JavaDoc[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 JavaDoc swingName;
135     String JavaDoc rtfName;
136     
137     protected GenericAttribute(int d,Object JavaDoc s, String JavaDoc r)
138     {
139         domain = d;
140         swingName = s;
141         rtfName = r;
142     }
143
144     public int domain() { return domain; }
145     public Object JavaDoc swingName() { return swingName; }
146     public String JavaDoc rtfName() { return rtfName; }
147     
148     abstract boolean set(MutableAttributeSet JavaDoc target);
149     abstract boolean set(MutableAttributeSet JavaDoc target, int parameter);
150     abstract boolean setDefault(MutableAttributeSet JavaDoc target);
151     
152     public boolean write(AttributeSet JavaDoc source,
153                  RTFGenerator JavaDoc target,
154                  boolean force)
155         throws IOException JavaDoc
156     {
157         return writeValue(source.getAttribute(swingName), target, force);
158     }
159
160     public boolean writeValue(Object JavaDoc value, RTFGenerator JavaDoc target,
161                   boolean force)
162         throws IOException JavaDoc
163     {
164         return false;
165     }
166     }
167
168     static class BooleanAttribute
169         extends GenericAttribute
170     implements RTFAttribute JavaDoc
171     {
172         boolean rtfDefault;
173     boolean swingDefault;
174
175     protected static final Boolean JavaDoc True = Boolean.valueOf(true);
176     protected static final Boolean JavaDoc False = Boolean.valueOf(false);
177     
178     public BooleanAttribute(int d, Object JavaDoc s,
179                 String JavaDoc 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 JavaDoc s, String JavaDoc r)
187     {
188         super(d, s, r);
189         
190         swingDefault = false;
191         rtfDefault = false;
192     }
193
194     public boolean set(MutableAttributeSet JavaDoc target)
195     {
196         /* TODO: There's some ambiguity about whether this should
197            *set* or *toggle* the attribute. */

198         target.addAttribute(swingName, True);
199
200         return true; /* true indicates we were successful */
201     }
202
203     public boolean set(MutableAttributeSet JavaDoc target, int parameter)
204     {
205         /* See above note in the case that parameter==1 */
206         Boolean JavaDoc value = ( parameter != 0 ? True : False );
207         
208         target.addAttribute(swingName, value);
209         
210         return true; /* true indicates we were successful */
211     }
212     
213     public boolean setDefault(MutableAttributeSet JavaDoc 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 JavaDoc o_value,
222                   RTFGenerator JavaDoc target,
223                   boolean force)
224         throws IOException JavaDoc
225     {
226         Boolean JavaDoc val;
227
228         if (o_value == null)
229           val = Boolean.valueOf(swingDefault);
230         else
231           val = (Boolean JavaDoc)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 JavaDoc
248     {
249     Object JavaDoc swingValue;
250
251     public AssertiveAttribute(int d, Object JavaDoc s, String JavaDoc r)
252     {
253         super(d, s, r);
254         swingValue = Boolean.valueOf(true);
255     }
256
257     public AssertiveAttribute(int d, Object JavaDoc s, String JavaDoc r, Object JavaDoc v)
258     {
259         super(d, s, r);
260         swingValue = v;
261     }
262
263     public AssertiveAttribute(int d, Object JavaDoc s, String JavaDoc r, int v)
264     {
265         super(d, s, r);
266         swingValue = new Integer JavaDoc(v);
267     }
268     
269     public boolean set(MutableAttributeSet JavaDoc 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 JavaDoc target, int parameter)
280     {
281         return false;
282     }
283     
284     public boolean setDefault(MutableAttributeSet JavaDoc target)
285     {
286         target.removeAttribute(swingName);
287         return true;
288     }
289
290     public boolean writeValue(Object JavaDoc value,
291                   RTFGenerator JavaDoc target,
292                   boolean force)
293         throws IOException JavaDoc
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 JavaDoc
312     {
313     int rtfDefault;
314     Number JavaDoc swingDefault;
315     float scale;
316     
317     protected NumericAttribute(int d, Object JavaDoc s, String JavaDoc r)
318     {
319         super(d, s, r);
320         rtfDefault = 0;
321         swingDefault = null;
322         scale = 1f;
323     }
324
325     public NumericAttribute(int d, Object JavaDoc s,
326                 String JavaDoc r, int ds, int dr)
327     {
328         this(d, s, r, new Integer JavaDoc(ds), dr, 1f);
329     }
330
331     public NumericAttribute(int d, Object JavaDoc s,
332                 String JavaDoc r, Number JavaDoc 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 JavaDoc s, String JavaDoc r,
341                         float ds, int dr)
342     {
343         return new NumericAttribute(d, s, r, new Float JavaDoc(ds), dr, 20f);
344     }
345
346     public static NumericAttribute NewTwips(int d, Object JavaDoc s, String JavaDoc r,
347                         int dr)
348     {
349         return new NumericAttribute(d, s, r, null, dr, 20f);
350     }
351
352     public boolean set(MutableAttributeSet JavaDoc target)
353     {
354         return false;
355     }
356
357     public boolean set(MutableAttributeSet JavaDoc target, int parameter)
358     {
359         Number JavaDoc swingValue;
360
361         if (scale == 1f)
362             swingValue = new Integer JavaDoc(parameter);
363         else
364             swingValue = new Float JavaDoc(parameter / scale);
365         target.addAttribute(swingName, swingValue);
366         return true;
367     }
368
369     public boolean setDefault(MutableAttributeSet JavaDoc target)
370     {
371         Number JavaDoc old = (Number JavaDoc)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 JavaDoc o_value,
384                   RTFGenerator JavaDoc target,
385                   boolean force)
386         throws IOException JavaDoc
387     {
388         Number JavaDoc value = (Number JavaDoc)o_value;
389         if (value == null)
390             value = swingDefault;
391         if (value == null) {
392         /* TODO: What is the proper behavior if the Swing object does
393            not specify a value, and we don't know its default value?
394            Currently we pretend that the RTF default value is
395            equivalent (probably a workable assumption) */

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