KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jdesktop > swing > LabelProperties


1 /*
2  * $Id: LabelProperties.java,v 1.3 2004/09/02 00:50:37 aim Exp $
3  *
4  * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
5  * Santa Clara, California 95054, U.S.A. All rights reserved.
6  */

7
8 package org.jdesktop.swing;
9
10 import java.awt.Color JavaDoc;
11 import java.awt.Font JavaDoc;
12
13 import java.beans.PropertyChangeEvent JavaDoc;
14 import java.beans.PropertyChangeListener JavaDoc;
15
16 import javax.swing.AbstractButton JavaDoc;
17 import javax.swing.Icon JavaDoc;
18 import javax.swing.JLabel JavaDoc;
19 import javax.swing.SwingConstants JavaDoc;
20 import javax.swing.table.TableCellRenderer JavaDoc;
21
22 /**
23  * Class used to store label properties in a single object so that they
24  * may be applied as a set on renderers.
25  * @author Amy Fowler
26  * @version 1.0
27  */

28
29 public class LabelProperties extends JLabel JavaDoc {
30     private static final int BACKGROUND_SET = 1;
31     private static final int FOREGROUND_SET = 2;
32     private static final int FONT_SET = 4;
33     private static final int HORIZONTAL_ALIGNMENT_SET = 8;
34     private static final int HORIZONTAL_TEXT_POSITION_SET = 16;
35     private static final int ICON_SET = 32;
36     private static final int ICON_TEXT_GAP_SET = 64;
37     private static final int TEXT_SET = 128;
38     private static final int VERTICAL_ALIGNMENT_SET = 256;
39     private static final int VERTICAL_TEXT_POSITION_SET = 512;
40
41     private int setFlags = 0;
42
43     public LabelProperties() {
44         super();
45         addPropertyChangeListener(new PropertyChangeListener JavaDoc() {
46             public void propertyChange(PropertyChangeEvent JavaDoc e) {
47                 String JavaDoc propertyName = e.getPropertyName();
48                 Object JavaDoc value = e.getNewValue();
49                 if (propertyName.equals("background")) {
50                     if (value != null) {
51                         setFlags |= BACKGROUND_SET;
52                     } else {
53                         setFlags &= (~BACKGROUND_SET);
54                     }
55                 }
56                 else if (propertyName.equals("font")) {
57                     if (value != null) {
58                         setFlags |= FONT_SET;
59                     } else {
60                         setFlags &= (~FONT_SET);
61                     }
62                 }
63                 else if (propertyName.equals("foreground")) {
64                     if (value != null) {
65                         setFlags |= FOREGROUND_SET;
66                     } else {
67                         setFlags &= (~FOREGROUND_SET);
68                     }
69                 }
70                 else if (propertyName.equals("horizontalAlignment")) {
71                     if (value != null && ((Integer JavaDoc)value).intValue() != -1) {
72                         setFlags |= HORIZONTAL_ALIGNMENT_SET;
73                     } else {
74                         setFlags &= (~HORIZONTAL_ALIGNMENT_SET);
75                     }
76                 }
77                 else if (propertyName.equals("horizontalTextPosition")) {
78                     if (value != null && ((Integer JavaDoc)value).intValue() != -1) {
79                         setFlags |= HORIZONTAL_TEXT_POSITION_SET;
80                     } else {
81                         setFlags &= (~HORIZONTAL_TEXT_POSITION_SET);
82                     }
83                 }
84                 else if (propertyName.equals("icon")) {
85                     if (value != null) {
86                         setFlags |= ICON_SET;
87                     } else {
88                         setFlags &= (~ICON_SET);
89                     }
90                 }
91                 else if (propertyName.equals("iconTextGap")) {
92                     if (value != null && ((Integer JavaDoc)value).intValue() != -1) {
93                         setFlags |= ICON_TEXT_GAP_SET;
94                     } else {
95                         setFlags &= (~ICON_TEXT_GAP_SET);
96                     }
97                 }
98                 else if (propertyName.equals("text")) {
99                     if (value != null) {
100                         setFlags |= TEXT_SET;
101                     } else {
102                         setFlags &= (~TEXT_SET);
103                     }
104                 }
105                 else if (propertyName.equals("verticalAlignment")) {
106                     if (value != null && ((Integer JavaDoc)value).intValue() != -1) {
107                         setFlags |= VERTICAL_ALIGNMENT_SET;
108                     } else {
109                         setFlags &= (~VERTICAL_ALIGNMENT_SET);
110                     }
111                 }
112                 else if (propertyName.equals("verticalTextPosition")) {
113                     if (value != null && ((Integer JavaDoc)value).intValue() != -1) {
114                         setFlags |= VERTICAL_TEXT_POSITION_SET;
115                     } else {
116                         setFlags &= (~VERTICAL_TEXT_POSITION_SET);
117                     }
118                 }
119             }
120         });
121     }
122
123     public LabelProperties(Color JavaDoc background, Color JavaDoc foreground, Font JavaDoc font,
124                            int horizontalAlignment, int horizontalTextPosition,
125                            int verticalAlignment, int verticalTextPosition,
126                            Icon JavaDoc icon, int iconTextGap, String JavaDoc text) {
127         this();
128         setBackground(background);
129         setForeground(foreground);
130         setFont(font);
131         setHorizontalAlignment(horizontalAlignment);
132         setHorizontalTextPosition(horizontalTextPosition);
133         setVerticalAlignment(verticalAlignment);
134         setVerticalTextPosition(verticalTextPosition);
135         setIcon(icon);
136         setIconTextGap(iconTextGap);
137         setText(text);
138     }
139
140     public boolean isBackgroundSet() {
141         return (setFlags & BACKGROUND_SET) > 0;
142     }
143
144     public boolean isForegroundSet() {
145         return (setFlags & FOREGROUND_SET) > 0;
146     }
147
148     public boolean isFontSet() {
149         return (setFlags & FONT_SET) > 0;
150     }
151
152     public boolean isHorizontalAlignmentSet() {
153         return (setFlags & HORIZONTAL_ALIGNMENT_SET) > 0;
154     }
155
156     public boolean isHorizontalTextPositionSet() {
157         return (setFlags & HORIZONTAL_TEXT_POSITION_SET) > 0;
158     }
159
160     public boolean isIconSet() {
161         return (setFlags & ICON_SET) > 0;
162     }
163
164     public boolean isIconTextGapSet() {
165         return (setFlags & ICON_TEXT_GAP_SET) > 0;
166     }
167
168     public boolean isTextSet() {
169         return (setFlags & TEXT_SET) > 0;
170     }
171
172     public boolean isVerticalAlignmentSet() {
173         return (setFlags & VERTICAL_ALIGNMENT_SET) > 0;
174     }
175
176     public boolean isVerticalTextPositionSet() {
177         return (setFlags & VERTICAL_TEXT_POSITION_SET) > 0;
178     }
179
180     public boolean noPropertiesSet() {
181         return setFlags == 0;
182     }
183
184     public void applyPropertiesTo(JLabel JavaDoc label) {
185         if (noPropertiesSet()) {
186             return;
187         }
188         if (isBackgroundSet()) {
189             label.setBackground(getBackground());
190         }
191         if (isForegroundSet()) {
192             label.setForeground(getForeground());
193         }
194         if (isFontSet()) {
195             label.setFont(getFont());
196         }
197         if (isHorizontalAlignmentSet()) {
198             label.setHorizontalAlignment(getHorizontalAlignment());
199         }
200         if (isHorizontalTextPositionSet()) {
201             label.setHorizontalTextPosition(getHorizontalTextPosition());
202         }
203         if (isIconSet()) {
204             label.setIcon(getIcon());
205         }
206         if (isIconTextGapSet()) {
207             label.setIconTextGap(getIconTextGap());
208         }
209         if (isTextSet()) {
210             label.setText(getText());
211         }
212         if (isVerticalAlignmentSet()) {
213             label.setVerticalAlignment(getVerticalAlignment());
214         }
215         if (isVerticalTextPositionSet()) {
216             label.setVerticalTextPosition(getVerticalTextPosition());
217         }
218     }
219
220     public void applyPropertiesTo(AbstractButton JavaDoc button) {
221          if (noPropertiesSet()) {
222              return;
223          }
224          if (isBackgroundSet()) {
225              button.setBackground(getBackground());
226          }
227          if (isForegroundSet()) {
228              button.setForeground(getForeground());
229          }
230          if (isFontSet()) {
231              button.setFont(getFont());
232          }
233          if (isHorizontalAlignmentSet()) {
234              button.setHorizontalAlignment(getHorizontalAlignment());
235          }
236          if (isHorizontalTextPositionSet()) {
237              button.setHorizontalTextPosition(getHorizontalTextPosition());
238          }
239          if (isIconSet()) {
240              button.setIcon(getIcon());
241          }
242          if (isIconTextGapSet()) {
243              button.setIconTextGap(getIconTextGap());
244          }
245          if (isTextSet()) {
246              button.setText(getText());
247          }
248          if (isVerticalAlignmentSet()) {
249              button.setVerticalAlignment(getVerticalAlignment());
250          }
251          if (isVerticalTextPositionSet()) {
252              button.setVerticalTextPosition(getVerticalTextPosition());
253          }
254      }
255
256      public void applyPropertiesTo(LabelProperties props) {
257          if (noPropertiesSet()) {
258              return;
259          }
260          if (isBackgroundSet()) {
261              props.setBackground(getBackground());
262          }
263          if (isForegroundSet()) {
264              props.setForeground(getForeground());
265          }
266          if (isFontSet()) {
267              props.setFont(getFont());
268          }
269          if (isHorizontalAlignmentSet()) {
270              props.setHorizontalAlignment(getHorizontalAlignment());
271          }
272          if (isHorizontalTextPositionSet()) {
273              props.setHorizontalTextPosition(getHorizontalTextPosition());
274          }
275          if (isIconSet()) {
276              props.setIcon(getIcon());
277          }
278          if (isIconTextGapSet()) {
279              props.setIconTextGap(getIconTextGap());
280          }
281          if (isTextSet()) {
282              props.setText(getText());
283          }
284          if (isVerticalAlignmentSet()) {
285              props.setVerticalAlignment(getVerticalAlignment());
286          }
287          if (isVerticalTextPositionSet()) {
288              props.setVerticalTextPosition(getVerticalTextPosition());
289          }
290      }
291
292      public void applyPropertiesTo(TableCellRenderer JavaDoc renderer) {
293          if (renderer instanceof JLabel JavaDoc) {
294              applyPropertiesTo( (JLabel JavaDoc) renderer);
295          }
296          else if (renderer instanceof AbstractButton JavaDoc) {
297              applyPropertiesTo( (AbstractButton JavaDoc) renderer);
298          }
299      }
300 }
301
Popular Tags