KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > swingwtx > swing > JLabel


1 /*
2    SwingWT
3    Copyright(c)2003-2004, R. Rawson-Tetley
4
5    For more information on distributing and using this program, please
6    see the accompanying "COPYING" file.
7
8    Contact me by electronic mail: bobintetley@users.sourceforge.net
9
10    $Log: JLabel.java,v $
11    Revision 1.28 2004/05/06 12:35:22 bobintetley
12    Parity with Swing constants for Binary Compatibility + fixes to JDesktopPane
13
14    Revision 1.27 2004/05/05 12:43:21 bobintetley
15    Patches/new files from Laurent Martell
16
17    Revision 1.26 2004/04/30 23:18:26 dannaab
18    List selection support, misc bug fixes
19
20    Revision 1.25 2004/04/30 13:20:43 bobintetley
21    Fix to unrealised peer preferred sizes, forwarding window events to
22    content panes and fix for mouse drag events.
23
24    Revision 1.24 2004/04/28 08:38:11 bobintetley
25    Hierarchy fixes, code cleanup for base classes, additional javadocs and use of flag to identify JComponent descendants with peers
26
27    Revision 1.23 2004/04/16 10:19:06 dannaab
28    Misc bug fixes, InputMap implementation, preliminary undo support
29
30    Revision 1.22 2004/03/30 10:42:46 bobintetley
31    Many minor bug fixes, event improvements by Dan Naab. Full swing.Icon support
32
33    Revision 1.21 2004/03/01 15:58:47 bobintetley
34    Various little bug fixes
35
36    Revision 1.20 2004/02/19 09:58:44 bobintetley
37    Various small bug fixes and JTextArea should be much faster/lighter
38
39    Revision 1.19 2004/01/26 10:57:45 bobintetley
40    HTML handling (throws it away - SWT can't do anything with it)
41
42    Revision 1.18 2004/01/26 08:11:00 bobintetley
43    Many bugfixes and addition of SwingSet
44
45    Revision 1.17 2003/12/22 14:03:22 bobintetley
46    Layout updates after loading new images into labels
47
48    Revision 1.16 2003/12/16 17:46:17 bobintetley
49    Additional thread safety methods
50
51    Revision 1.15 2003/12/16 15:47:45 bobintetley
52    Thread safety added to common methods
53
54    Revision 1.14 2003/12/16 13:14:33 bobintetley
55    Use of SwingWTUtils.isSWTControlAvailable instead of null test
56
57    Revision 1.13 2003/12/16 09:25:42 bobintetley
58    Fixed label images
59
60    Revision 1.12 2003/12/15 18:29:57 bobintetley
61    Changed setParent() method to setSwingWTParent() to avoid conflicts with applications
62
63    Revision 1.11 2003/12/15 15:52:48 bobintetley
64    Alignment methods
65
66    Revision 1.10 2003/12/14 09:13:38 bobintetley
67    Added CVS log to source headers
68
69 */

70
71
72 package swingwtx.swing;
73
74 import org.eclipse.swt.SWT;
75
76 import swingwt.awt.Component;
77
78 public class JLabel extends swingwtx.swing.JComponent implements SwingConstants {
79
80     /** The label's peer */
81     protected org.eclipse.swt.widgets.Label ppeer = null;
82     /** A cache of the label's text */
83     protected String JavaDoc pText = "";
84     /** A cache of the label's icon */
85     protected Icon pImage = null;
86     /** The label's horizontal text position */
87     protected int pHTextPosition = LEFT;
88     /** The label's vertical text position */
89     protected int pVTextPosition = TOP;
90     /** The label's horizontal alignment */
91     protected int pHAlign = LEFT;
92     /** The label's vertical alignment */
93     protected int pVAlign = TOP;
94     /** If this is the label for a particular component, then show the one it's tied to */
95     protected Component labelFor = null;
96     
97     /** Creates a new JLabel with empty text and no icon */
98     public JLabel() { }
99     /** Creates a new JLabel with the specified text
100      * @param text The text of the label
101      */

102     public JLabel(String JavaDoc text) { pText = text; }
103     /** Creates a new JLabel with the specified text and alignment
104      * @param text The text of the label
105      * @param align The label's horizontal alignment
106      */

107     public JLabel(String JavaDoc text, int align) { pText = text; setHorizontalAlignment(align); }
108     /** Creates a new JLabel with the specified icon
109      * @param icon The label's icon
110      */

111     public JLabel(Icon icon) { pImage = icon; }
112
113     public JLabel(String JavaDoc text, Icon icon, int align) { pText = text; pImage = icon; setHorizontalAlignment(align); }
114
115     /**
116      * @return The text on the label
117      */

118     public String JavaDoc getText()
119     {
120         return pText;
121     }
122
123     /**
124      * Sets the label's text
125      * @param text The new text
126      */

127     public void setText(String JavaDoc text) {
128         pText = text;
129         if (pText == null) pText = "";
130         SwingUtilities.invokeSync(new Runnable JavaDoc() {
131             public void run() {
132                 if (SwingWTUtils.isSWTControlAvailable(ppeer))
133                     ppeer.setText(SwingWTUtils.removeHTML(pText));
134                 if (parent != null) {
135                     // Invalidate parent in case our size has changed.
136
parent.invalidate();
137                     repaint();
138                 }
139             }
140         });
141     }
142     
143     /**
144      * If this label is accompanying a component, the component
145      * @param c The component this label is accompanying
146      */

147     public void setLabelFor(Component c) { labelFor = c; }
148     /**
149      * If this label is accompanying a component, the component
150      * @return The component
151      */

152     public Component getLabelFor() { return labelFor; }
153     
154     /** NOT IMPLEMENTED */
155     public void setDisplayedMnemonic(char c) {}
156     /** NOT IMPLEMENTED */
157     public void setDisplayedMnemonic(int c) {}
158     
159     /**
160      * Sets the icon for this label
161      * @param icon The icon to display in the label
162      */

163     public void setIcon(Icon icon) {
164         pImage = icon;
165         final JLabel pthis = this;
166         SwingUtilities.invokeSync(new Runnable JavaDoc() {
167             public void run() {
168                 if (SwingWTUtils.isSWTControlAvailable(ppeer)) {
169                     if (pImage == null)
170                         ppeer.setImage(null);
171                     else
172                         ppeer.setImage(SwingWTUtils.getSWTImageFromSwingIcon(pthis, pImage));
173                     // Tell the container to layout again in case the image is large
174
if (parent != null) {
175                         parent.invalidate();
176                         repaint();
177                     }
178                 }
179             }
180         });
181     }
182     
183     /** Gets the icon for this label
184      * @return The icon
185      */

186     public Icon getIcon() { return pImage; }
187     
188     /** NOT IMPLEMENTED */
189     public Icon getDisabledIcon() { return null; }
190     /** NOT IMPLEMENTED */
191     public void setDisabledIcon(Icon icon) { }
192     
193     /**
194      * Sets the horizontal alignment for this label. Use one of
195      * LEADING, LEFT, CENTER, RIGHT, TRAILING from SwingConstants
196      * @param align The alignment
197      */

198     public void setHorizontalAlignment(final int align) { pHAlign = align; SwingUtilities.invokeAsync(new Runnable JavaDoc() { public void run() { if (SwingWTUtils.isSWTControlAvailable(ppeer)) ppeer.setAlignment(SwingWTUtils.translateSwingAlignmentConstant(align) | SwingWTUtils.translateSwingAlignmentConstant(pVAlign));}}); }
199     /**
200      * Sets the vertical alignment for this label. Use one of
201      * LEADING, LEFT, CENTER, RIGHT, TRAILING from SwingConstants
202      * @param align The alignment
203      */

204     public void setVerticalAlignment(final int align) { pVAlign = align; SwingUtilities.invokeAsync(new Runnable JavaDoc() { public void run() {if (SwingWTUtils.isSWTControlAvailable(ppeer))ppeer.setAlignment(SwingWTUtils.translateSwingAlignmentConstant(align) | SwingWTUtils.translateSwingAlignmentConstant(pHAlign));}});}
205     /**
206      * Gets the horizontal alignment for the label
207      * @return The horizontal alignment
208      */

209     public int getHorizontalAlignment() { return pHAlign; }
210     /**
211      * Gets the vertical alignment for the label
212      * @return The vertical alignment
213      */

214     public int getVerticalAlignment() { return pVAlign; }
215     /** No meaning in native, calls across to setHorizontalAlignment() */
216     public void setHorizontalTextPosition(int textpos) { setHorizontalAlignment(textpos); }
217     /** No meaning in native, calls across to setVerticalAlignment() */
218     public void setVerticalTextPosition(int textpos) { setVerticalAlignment(textpos); }
219     /** No meaning in native, calls across to getHorizontalAlignment() */
220     public int getHorizontalTextPosition() { return getHorizontalAlignment(); }
221     /** No meaning in native, calls across to getVerticalAlignment() */
222     public int getVerticalTextPosition() { return getVerticalAlignment(); }
223     
224     /** Overriden to calculate non-realised
225      * preferred size.
226      */

227     protected swingwt.awt.Dimension calculatePreferredSize() {
228         swingwt.awt.Dimension size = new swingwt.awt.Dimension(
229             SwingWTUtils.getRenderStringWidth(pText),
230             SwingWTUtils.getRenderStringHeight(pText));
231         setSize(size);
232         return size;
233     }
234     
235     /**
236      * Once a parent component receives an "add" call for a child, this being
237      * the child, this should be called to tell us to instantiate the peer
238      * and load in any cached properties.
239      */

240     public void setSwingWTParent(swingwt.awt.Container parent) throws Exception JavaDoc {
241         descendantHasPeer = true;
242         ppeer = new org.eclipse.swt.widgets.Label(parent.getComposite(), SWT.NONE);
243         if (pText != null) ppeer.setText(SwingWTUtils.removeHTML(pText));
244         ppeer.setAlignment(SwingWTUtils.translateSwingAlignmentConstant(pHAlign) |
245             SwingWTUtils.translateSwingAlignmentConstant(pVAlign));
246         if (pImage != null) setIcon(pImage);
247         peer = ppeer;
248         this.parent = parent;
249     }
250 }
251
Popular Tags