KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jimm > datavision > layout > swing > SwingTextField


1 package jimm.datavision.layout.swing;
2 import jimm.datavision.field.Field;
3 import jimm.datavision.field.Format;
4 import java.awt.Color JavaDoc;
5 import javax.swing.JTextPane JavaDoc;
6 import javax.swing.text.Style JavaDoc;
7 import javax.swing.text.StyleContext JavaDoc;
8 import javax.swing.text.StyleConstants JavaDoc;
9
10 /**
11  * A Swing field is the visual representation of a report field.
12  *
13  * @author Jim Menard, <a HREF="mailto:jimm@io.com">jimm@io.com</a>
14  * @see SwingLE
15  */

16 public class SwingTextField extends AbstractSwingField {
17
18 public static final Color JavaDoc HIDDEN_FG_COLOR = Color.gray;
19
20 /**
21  * Constructor.
22  *
23  * @param f report field
24  */

25 public SwingTextField(Field f) {
26     this(f, f.toString());
27 }
28
29 /**
30  * Constructor.
31  *
32  * @param f report field
33  * @param str label text
34  */

35 public SwingTextField(Field f, String JavaDoc str) {
36     super(f, new JTextPane JavaDoc());
37     JTextPane JavaDoc textPane = (JTextPane JavaDoc)getComponent();
38     textPane.setText(str);
39     textPane.setEditable(false);
40     format();
41 }
42
43 /**
44  * Formats the label according to the field's formatting specifications.
45  */

46 public void format() {
47     JTextPane JavaDoc textPane = (JTextPane JavaDoc)getComponent();
48
49     Format format = field.getFormat();
50     Style JavaDoc style = StyleContext.getDefaultStyleContext()
51     .getStyle(StyleContext.DEFAULT_STYLE);
52
53     // Save selection and select all text
54
int selStart = textPane.getSelectionStart();
55     int selEnd = textPane.getSelectionEnd();
56     textPane.selectAll();
57
58     StyleConstants.setBold(style, format.isBold());
59     StyleConstants.setItalic(style, format.isItalic());
60     StyleConstants.setUnderline(style, format.isUnderline());
61     StyleConstants.setFontSize(style, (int)format.getSize());
62     StyleConstants.setFontFamily(style, format.getFontFamilyName());
63
64     // Color is based on visibility flag of field
65
StyleConstants.setForeground(style, getColor(format));
66
67     // Align
68
switch (format.getAlign()) {
69     case Format.ALIGN_CENTER:
70     StyleConstants.setAlignment(style, StyleConstants.ALIGN_CENTER);
71     break;
72     case Format.ALIGN_RIGHT:
73     StyleConstants.setAlignment(style, StyleConstants.ALIGN_RIGHT);
74     break;
75     default:
76     StyleConstants.setAlignment(style, StyleConstants.ALIGN_LEFT);
77     break;
78     }
79     textPane.setParagraphAttributes(style, true);
80
81     // Restore selection
82
textPane.setCaretPosition(selStart);
83     textPane.moveCaretPosition(selEnd);
84
85     makeBorders();
86 }
87
88 /**
89  * Returns field color based on visibility.
90  */

91 public Color JavaDoc getColor() {
92     return getColor(field.getFormat());
93 }
94
95 /**
96  * Returns field color based on visibility.
97  */

98 public Color JavaDoc getColor(Format format) {
99     Color JavaDoc color = format.getColor();
100     if (!field.isVisible()) { // If hidden, lighten color
101
if (color.equals(Color.black))
102         color = HIDDEN_FG_COLOR;
103     else {
104         float[] hsb = new float[3];
105
106         Color.RGBtoHSB(color.getRed(), color.getGreen(), color.getBlue(),
107                hsb);
108         hsb[1] = (float)0.5; // Saturation
109
hsb[2] = (float)0.9; // Brightness
110
color = Color.getHSBColor(hsb[0], hsb[1], hsb[2]);
111     }
112     }
113     return color;
114 }
115
116
117 }
118
Popular Tags