KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jimm > datavision > field > Format


1 package jimm.datavision.field;
2 import jimm.datavision.Writeable;
3 import jimm.util.XMLWriter;
4 import java.awt.Color JavaDoc;
5 import java.awt.Font JavaDoc;
6 import java.util.Observable JavaDoc;
7
8 /**
9  * A format describes how to display a field. It specifies font family name,
10  * size, attributes (bold, italic, underline, wrap), alignment, and print
11  * format.
12  * <p>
13  * If a field's value is <code>null</code>, then the getter returns the value
14  * of the report's default field's format (which will never be
15  * <code>null</code>.
16  *
17  * @author Jim Menard, <a HREF="mailto:jimm@io.com">jimm@io.com</a>
18  */

19 public class Format extends Observable JavaDoc implements Writeable, Cloneable JavaDoc {
20
21 protected static final String JavaDoc DEFAULT_FONT_FAMILY_NAME = "Times New Roman";
22
23 protected static final int DEFAULT_SIZE = 11;
24 protected static final Color JavaDoc DEFAULT_COLOR = Color.black;
25
26 /** Used to specify left alignment. */
27 public static final int ALIGN_LEFT = 0;
28 /** Used to specify center alignment. */
29 public static final int ALIGN_CENTER = 1;
30 /** Used to specify right alignment. */
31 public static final int ALIGN_RIGHT = 2;
32
33 protected Field field;
34 protected String JavaDoc fontFamilyName;
35 protected Double JavaDoc size; // In points
36
protected Boolean JavaDoc bold;
37 protected Boolean JavaDoc italic;
38 protected Boolean JavaDoc underline;
39 protected Boolean JavaDoc wrap;
40 protected Integer JavaDoc align; // ALIGN_{LEFT,CENTER,RIGHT}
41
protected String JavaDoc format; // Output formatting string
42
protected Color JavaDoc color;
43 protected Font JavaDoc font; // Lazily instantiated
44

45 /**
46  * Returns an <code>ALIGN_*</code> constant, given one of "left",
47  * "center", or "right". If the specified string is null or is not one
48  * of these values, <code>ALIGN_LEFT</code> is returned.
49  *
50  * @param s the string "left", "center", or "right" (case is not
51  * significant)
52  * @return one of <code>ALIGN_LEFT</code>, <code>ALIGN_CENTER</code>,
53  * or <code>ALIGN_RIGHT</code>
54  */

55 public static int alignFromString(String JavaDoc s) {
56     if (s == null) return ALIGN_LEFT;
57     String JavaDoc copy = s.toLowerCase();
58     if ("center".equals(copy)) return ALIGN_CENTER;
59     if ("right".equals(copy)) return ALIGN_RIGHT;
60     return ALIGN_LEFT;
61 }
62
63 /**
64  * Given an <code>ALIGN_*</code> constant, return the string used
65  * to represent that value in a report XML file. If <i>align</i> is not
66  * one of those values, returns "left".
67  *
68  * @param align one of "left", "center", or "right"
69  */

70 public static String JavaDoc alignToString(int align) {
71     switch (align) {
72     case ALIGN_RIGHT: return "right";
73     case ALIGN_CENTER: return "center";
74     default: return "left";
75     }
76 }
77
78 public static Format createEmptyFormat() {
79     return new Format();
80 }
81
82 public static Format createDefaultFormat() {
83     return new DefaultFormat();
84 }
85
86 /**
87  * Constructor. Creates an empty format.
88  */

89 Format() {}
90
91 /**
92  * Normally you don't need to call this, because {@link Field#setFormat}
93  * calls this.
94  *
95  * @param f a field
96  */

97 void setField(Field f) { field = f; }
98
99 /**
100  * Returns a clone of this format.
101  */

102 public Object JavaDoc clone() {
103     Format f = new Format();
104     fillClonedField(f);
105     return f;
106 }
107
108 protected void fillClonedField(Format f) {
109     f.field = field;
110     f.setFontFamilyName(getFontFamilyName());
111     f.setSize(getSize());
112     f.setBold(isBold());
113     f.setItalic(isItalic());
114     f.setUnderline(isUnderline());
115     f.setWrap(isWrap());
116     f.setAlign(getAlign());
117     f.setFormat(getFormat());
118     f.setColor(getColor());
119 }
120
121 public boolean equals(Object JavaDoc obj) {
122     if (obj == null || !(obj instanceof Format))
123     return false;
124     if (obj == this)
125     return true;
126
127     Format f = (Format)obj;
128     // These getters never return null, because if our ivars are null we look
129
// up to the default field's format for its value.
130
return getSize() == f.getSize()
131     && isBold() == f.isBold()
132     && isItalic() == f.isItalic()
133     && isUnderline() == f.isUnderline()
134     && isWrap() == f.isWrap()
135     && getAlign() == f.getAlign()
136     && (format == f.getFormat()
137         || (format != null && format.equals(f.getFormat())))
138     && getColor().equals(f.getColor());
139 }
140
141 public int hashCode() {
142     return (color == null ? 0 : color.hashCode())
143     + getFormat().hashCode()
144     + (int)getSize()
145     + (isBold() ? 101 : 100)
146     + (isItalic() ? 501 : 500)
147     + (isUnderline() ? 1001 : 1000)
148     + (isWrap() ? 5001 : 5000)
149     + (getAlign() + 10000);
150 }
151
152 /**
153  * Returns this field's report's default field's format (*whew*).
154  */

155 public Format getDefaultFormat() {
156     return field.getReport().getDefaultField().getFormat();
157 }
158
159 /**
160  * Returns the font family name for this format.
161  *
162  * @return the font family name
163  */

164 public String JavaDoc getFontFamilyName() {
165     return fontFamilyName == null ? getDefaultFormat().getFontFamilyName()
166     : fontFamilyName;
167 }
168
169 /**
170  * Sets the font family name
171  *
172  * @param newFontFamilyName the new font family name
173  */

174 public void setFontFamilyName(String JavaDoc newFontFamilyName) {
175     if (newFontFamilyName != null) {
176     newFontFamilyName = newFontFamilyName.trim();
177     if (newFontFamilyName.length() == 0)
178         newFontFamilyName = null;
179     }
180
181     if (fontFamilyName != newFontFamilyName
182     && (fontFamilyName == null
183         || !fontFamilyName.equals(newFontFamilyName)))
184     {
185     fontFamilyName = newFontFamilyName;
186     font = null;
187     setChanged();
188     notifyObservers();
189     }
190 }
191
192 /**
193  * Based on our font family name, alignment flags, and size, return a
194  * font. Never returns <code>null</code>.
195  *
196  * @return a font; never <code>null</code>
197  */

198 public Font JavaDoc getFont() {
199     if (font == null) {
200     String JavaDoc name = getFontFamilyName();
201
202     int style = 0;
203     if (isBold())
204         style = Font.BOLD;
205     if (isItalic())
206         style |= Font.ITALIC;
207     if (style == 0)
208         style = Font.PLAIN;
209
210     font = new Font JavaDoc(name, style, (int)getSize());
211     }
212     return font;
213 }
214
215 /** Clears the font we may be holding on to. */
216 public void clearFontCache() { font = null; }
217
218 /**
219  * Returns the size for this format.
220  *
221  * @return the size
222  */

223 public double getSize() {
224     return size == null ? getDefaultFormat().getSize() : size.doubleValue();
225 }
226
227 /**
228  * Sets the size
229  *
230  * @param newSize the new size
231  */

232 public void setSize(double newSize) {
233     Double JavaDoc newSizeObj = new Double JavaDoc(newSize);
234     if (size != newSizeObj && (size == null || !size.equals(newSizeObj))) {
235     size = newSizeObj;
236     setChanged();
237     notifyObservers();
238     }
239 }
240
241 /**
242  * Returns the bold state.
243  *
244  * @return the bold state
245  */

246 public boolean isBold() {
247     return bold == null ? getDefaultFormat().isBold() : bold.booleanValue();
248 }
249
250 /**
251  * Sets the bold state.
252  *
253  * @param newBold the new value
254  */

255 public void setBold(boolean newBold) {
256     Boolean JavaDoc newBoldObj = newBold ? Boolean.TRUE : Boolean.FALSE;
257     if (bold != newBoldObj && (bold == null || !bold.equals(newBoldObj))) {
258     bold = newBoldObj;
259     setChanged();
260     notifyObservers();
261     }
262 }
263
264 /**
265  * Returns the italic state.
266  *
267  * @return the italic state
268  */

269 public boolean isItalic() {
270     return italic == null ? getDefaultFormat().isItalic()
271     : italic.booleanValue();
272 }
273
274 /**
275  * Sets the italic state.
276  *
277  * @param newItalic the new value
278  */

279 public void setItalic(boolean newItalic) {
280     Boolean JavaDoc newItalicObj = newItalic ? Boolean.TRUE : Boolean.FALSE;
281     if (italic != newItalicObj
282     && (italic == null || !italic.equals(newItalicObj)))
283     {
284     italic = newItalicObj;
285     setChanged();
286     notifyObservers();
287     }
288 }
289
290 /**
291  * Returns the underline state.
292  *
293  * @return the underline state
294  */

295 public boolean isUnderline() {
296     return underline == null ? getDefaultFormat().isUnderline()
297     : underline.booleanValue();
298 }
299
300 /**
301  * Sets the underline state.
302  *
303  * @param newUnderline the new underline state
304  */

305 public void setUnderline(boolean newUnderline) {
306     Boolean JavaDoc newUnderlineObj = newUnderline ? Boolean.TRUE : Boolean.FALSE;
307     if (underline != newUnderlineObj
308     && (underline == null || !underline.equals(newUnderlineObj)))
309     {
310     underline = newUnderlineObj;
311     setChanged();
312     notifyObservers();
313     }
314 }
315
316 /**
317  * Returns the wrap state.
318  *
319  * @return the wrap state
320  */

321 public boolean isWrap() {
322     return wrap == null ? getDefaultFormat().isWrap() : wrap.booleanValue();
323 }
324
325 /**
326  * Sets the wrap state.
327  *
328  * @param newWrap the new wrap state
329  */

330 public void setWrap(boolean newWrap) {
331     Boolean JavaDoc newWrapObj = newWrap ? Boolean.TRUE : Boolean.FALSE;
332     if (wrap != newWrapObj && (wrap == null || !wrap.equals(newWrapObj))) {
333     wrap = newWrapObj;
334     setChanged();
335     notifyObservers();
336     }
337 }
338
339 /**
340  * Returns the alignment.
341  *
342  * @return one of the <code>ALIGN_*</code> values
343  */

344 public int getAlign() {
345     return align == null ? getDefaultFormat().getAlign() : align.intValue();
346 }
347
348 /**
349  * Sets the alignment.
350  *
351  * @param newAlign one of the <code>ALIGN_*</code> values
352  */

353 public void setAlign(int newAlign) {
354     Integer JavaDoc newAlignObj = new Integer JavaDoc(newAlign);
355     if (align != newAlignObj && (align == null || !align.equals(newAlignObj))) {
356     align = newAlignObj;
357     setChanged();
358     notifyObservers();
359     }
360 }
361
362 /**
363  * Returns the format string for this field. May return <code>null</code>.
364  *
365  * @return the format string, possibly <code>null</code>
366  */

367 public String JavaDoc getFormat() {
368     return format == null ? getDefaultFormat().getFormat() : format;
369 }
370
371 /**
372  * Sets the format string.
373  *
374  * @param newFormat the new format string
375  */

376 public void setFormat(String JavaDoc newFormat) {
377     if (format != newFormat && (format == null || !format.equals(newFormat))) {
378     format = newFormat;
379     setChanged();
380     notifyObservers();
381     }
382 }
383
384 /**
385  * Returns the color for this format.
386  *
387  * @return the color
388  */

389 public Color JavaDoc getColor() {
390     return color == null ? getDefaultFormat().getColor() : color;
391 }
392
393 /**
394  * Sets the color
395  *
396  * @param newColor the new color
397  */

398 public void setColor(Color JavaDoc newColor) {
399     if (color != newColor) {
400     color = newColor;
401     setChanged();
402     notifyObservers();
403     }
404 }
405
406 /**
407  * Returns a string representation of this format. Mainly used for
408  * debugging.
409  *
410  * @return pretty string, inn't it?
411  */

412 public String JavaDoc toString() {
413     return "Format[size=" + size + ", bold=" + bold + ", italic=" + italic
414     + ", underline=" + underline + ", wrap=" + wrap + ", align="
415     + (align == null ? "null" : alignToString(align.intValue()))
416     + ", format=" + format + ", color=" + color + ", font="
417     + fontFamilyName + "]";
418 }
419
420 /**
421  * Writes this format as an XML tag. Only writes the differences between this
422  * format and the default one.
423  *
424  * @param out a writer that knows how to write XML
425  */

426 public void writeXML(XMLWriter out) {
427     Format def = getDefaultFormat();
428
429     if (this.equals(def)) // Don't write anything if there are no diffs
430
return;
431
432     out.startElement("format");
433     if (fontFamilyName != null
434     && !def.getFontFamilyName().equals(fontFamilyName))
435     out.attr("font", fontFamilyName);
436     if (size != null && size.doubleValue() != def.getSize())
437     out.attr("size", size);
438     if (bold != null && bold.booleanValue() != def.isBold())
439     out.attr("bold", bold);
440     if (italic != null && italic.booleanValue() != def.isItalic())
441     out.attr("italic", italic);
442     if (underline != null && underline.booleanValue() != def.isUnderline())
443     out.attr("underline", underline);
444     if (wrap != null && wrap.booleanValue() != def.isWrap())
445     out.attr("wrap", wrap);
446     if (align != null && align.intValue() != def.getAlign())
447     out.attr("align", alignToString(align.intValue()));
448     if (color != null && !color.equals(def.getColor()))
449     out.attr("color", color);
450     if (format != null && !format.equals(def.getFormat())
451     && format.length() > 0)
452     out.attr("format", format);
453     out.endElement();
454 }
455
456 }
457
458 /** Only used by the report. */
459 class DefaultFormat extends Format {
460
461 /**
462  * Creates a format with all default values filled in.
463  */

464 DefaultFormat() {
465     fontFamilyName = DEFAULT_FONT_FAMILY_NAME;
466     size = new Double JavaDoc(DEFAULT_SIZE);
467     bold = italic = underline = Boolean.FALSE;
468     wrap = Boolean.TRUE;
469     align = new Integer JavaDoc(ALIGN_LEFT);
470     color = DEFAULT_COLOR;
471 }
472
473 public String JavaDoc getFormat() { return format; }
474
475 /**
476  * Returns a clone of this format.
477  */

478 public Object JavaDoc clone() {
479     DefaultFormat f = new DefaultFormat();
480     fillClonedField(f);
481     return f;
482 }
483
484 /**
485  * Writes this format as an XML tag.
486  *
487  * @param out a writer that knows how to write XML
488  */

489 public void writeXML(XMLWriter out) {
490     out.startElement("format");
491     out.attr("font", fontFamilyName);
492     out.attr("size", size);
493     out.attr("bold", bold);
494     out.attr("italic", italic);
495     out.attr("underline", underline);
496     out.attr("wrap", wrap);
497     out.attr("align", alignToString(align.intValue()));
498     out.attr("color", color);
499     if (format != null && format.length() > 0)
500     out.attr("format", format);
501     out.endElement();
502 }
503
504 }
505
Popular Tags