1 |
14 |
jlechner |
/* JLabel.java --
|
2 |
|
|
Copyright (C) 2002, 2004, 2005 Free Software Foundation, Inc.
|
3 |
|
|
|
4 |
|
|
This file is part of GNU Classpath.
|
5 |
|
|
|
6 |
|
|
GNU Classpath is free software; you can redistribute it and/or modify
|
7 |
|
|
it under the terms of the GNU General Public License as published by
|
8 |
|
|
the Free Software Foundation; either version 2, or (at your option)
|
9 |
|
|
any later version.
|
10 |
|
|
|
11 |
|
|
GNU Classpath is distributed in the hope that it will be useful, but
|
12 |
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
14 |
|
|
General Public License for more details.
|
15 |
|
|
|
16 |
|
|
You should have received a copy of the GNU General Public License
|
17 |
|
|
along with GNU Classpath; see the file COPYING. If not, write to the
|
18 |
|
|
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
19 |
|
|
02110-1301 USA.
|
20 |
|
|
|
21 |
|
|
Linking this library statically or dynamically with other modules is
|
22 |
|
|
making a combined work based on this library. Thus, the terms and
|
23 |
|
|
conditions of the GNU General Public License cover the whole
|
24 |
|
|
combination.
|
25 |
|
|
|
26 |
|
|
As a special exception, the copyright holders of this library give you
|
27 |
|
|
permission to link this library with independent modules to produce an
|
28 |
|
|
executable, regardless of the license terms of these independent
|
29 |
|
|
modules, and to copy and distribute the resulting executable under
|
30 |
|
|
terms of your choice, provided that you also meet, for each linked
|
31 |
|
|
independent module, the terms and conditions of the license of that
|
32 |
|
|
module. An independent module is a module which is not derived from
|
33 |
|
|
or based on this library. If you modify this library, you may extend
|
34 |
|
|
this exception to your version of the library, but you are not
|
35 |
|
|
obligated to do so. If you do not wish to do so, delete this
|
36 |
|
|
exception statement from your version. */
|
37 |
|
|
|
38 |
|
|
|
39 |
|
|
package javax.swing;
|
40 |
|
|
|
41 |
|
|
import java.awt.Component;
|
42 |
|
|
import java.awt.Font;
|
43 |
|
|
import java.awt.Image;
|
44 |
|
|
import java.awt.Point;
|
45 |
|
|
import java.awt.Rectangle;
|
46 |
|
|
import java.awt.event.KeyEvent;
|
47 |
|
|
|
48 |
|
|
import javax.accessibility.Accessible;
|
49 |
|
|
import javax.accessibility.AccessibleContext;
|
50 |
|
|
import javax.accessibility.AccessibleExtendedComponent;
|
51 |
|
|
import javax.accessibility.AccessibleText;
|
52 |
|
|
import javax.swing.plaf.LabelUI;
|
53 |
|
|
import javax.swing.text.AttributeSet;
|
54 |
|
|
import javax.swing.text.SimpleAttributeSet;
|
55 |
|
|
|
56 |
|
|
/**
|
57 |
|
|
* A swing widget that displays a text message and/or an icon.
|
58 |
|
|
*/
|
59 |
|
|
public class JLabel extends JComponent implements Accessible, SwingConstants
|
60 |
|
|
{
|
61 |
|
|
|
62 |
|
|
/**
|
63 |
|
|
* Accessibility support for JLabel.
|
64 |
|
|
*/
|
65 |
|
|
protected class AccessibleJLabel
|
66 |
|
|
extends JComponent.AccessibleJComponent
|
67 |
|
|
implements AccessibleText, AccessibleExtendedComponent
|
68 |
|
|
{
|
69 |
|
|
/**
|
70 |
|
|
* Returns the selected text. This is an empty string since JLabels
|
71 |
|
|
* are not selectable.
|
72 |
|
|
*
|
73 |
|
|
* @return the selected text
|
74 |
|
|
*/
|
75 |
|
|
public String getSelectedText()
|
76 |
|
|
{
|
77 |
|
|
// We return "" here since JLabel's text is not selectable.
|
78 |
|
|
return "";
|
79 |
|
|
}
|
80 |
|
|
|
81 |
|
|
/**
|
82 |
|
|
* Returns the start index of the selected text.
|
83 |
|
|
*
|
84 |
|
|
* @return the start index of the selected text
|
85 |
|
|
*/
|
86 |
|
|
public int getSelectionStart()
|
87 |
|
|
{
|
88 |
|
|
// TODO: Figure out what should be returned here, because JLabels don't
|
89 |
|
|
// allow selection. I guess -1 for now.
|
90 |
|
|
return -1;
|
91 |
|
|
}
|
92 |
|
|
|
93 |
|
|
/**
|
94 |
|
|
* Returns the end index of the selected text.
|
95 |
|
|
*
|
96 |
|
|
* @return the end index of the selected text
|
97 |
|
|
*/
|
98 |
|
|
public int getSelectionEnd()
|
99 |
|
|
{
|
100 |
|
|
// TODO: Figure out what should be returned here, because JLabels don't
|
101 |
|
|
// allow selection. I guess -1 for now.
|
102 |
|
|
return -1;
|
103 |
|
|
}
|
104 |
|
|
|
105 |
|
|
/**
|
106 |
|
|
* Returns an {@link AttributeSet} that reflects the text attributes of
|
107 |
|
|
* the specified character. We return an empty
|
108 |
|
|
* <code>AttributeSet</code> here, because JLabels don't support text
|
109 |
|
|
* attributes (at least not yet).
|
110 |
|
|
*
|
111 |
|
|
* @param index the index of the character
|
112 |
|
|
*
|
113 |
|
|
* @return an {@link AttributeSet} that reflects the text attributes of
|
114 |
|
|
* the specified character
|
115 |
|
|
*/
|
116 |
|
|
public AttributeSet getCharacterAttribute(int index)
|
117 |
|
|
{
|
118 |
|
|
return new SimpleAttributeSet();
|
119 |
|
|
}
|
120 |
|
|
|
121 |
|
|
/**
|
122 |
|
|
* Returns the character, word or sentence at the specified index. The
|
123 |
|
|
* <code>part</code> parameter determines what is returned, the character,
|
124 |
|
|
* word or sentence after the index.
|
125 |
|
|
*
|
126 |
|
|
* @param part one of {@link AccessibleText#CHARACTER},
|
127 |
|
|
* {@link AccessibleText#WORD} or
|
128 |
|
|
* {@link AccessibleText#SENTENCE}, specifying what is returned
|
129 |
|
|
* @param index the index
|
130 |
|
|
*
|
131 |
|
|
* @return the character, word or sentence after <code>index</code>
|
132 |
|
|
*/
|
133 |
|
|
public String getAtIndex(int part, int index)
|
134 |
|
|
{
|
135 |
|
|
String result = "";
|
136 |
|
|
int startIndex = -1;
|
137 |
|
|
int endIndex = -1;
|
138 |
|
|
switch(part)
|
139 |
|
|
{
|
140 |
|
|
case AccessibleText.CHARACTER:
|
141 |
|
|
result = String.valueOf(text.charAt(index));
|
142 |
|
|
break;
|
143 |
|
|
case AccessibleText.WORD:
|
144 |
|
|
startIndex = text.lastIndexOf(' ', index);
|
145 |
|
|
endIndex = text.indexOf(' ', startIndex + 1);
|
146 |
|
|
if (endIndex == -1)
|
147 |
|
|
endIndex = startIndex + 1;
|
148 |
|
|
result = text.substring(startIndex + 1, endIndex);
|
149 |
|
|
break;
|
150 |
|
|
case AccessibleText.SENTENCE:
|
151 |
|
|
default:
|
152 |
|
|
startIndex = text.lastIndexOf('.', index);
|
153 |
|
|
endIndex = text.indexOf('.', startIndex + 1);
|
154 |
|
|
if (endIndex == -1)
|
155 |
|
|
endIndex = startIndex + 1;
|
156 |
|
|
result = text.substring(startIndex + 1, endIndex);
|
157 |
|
|
break;
|
158 |
|
|
}
|
159 |
|
|
return result;
|
160 |
|
|
}
|
161 |
|
|
|
162 |
|
|
/**
|
163 |
|
|
* Returns the character, word or sentence after the specified index. The
|
164 |
|
|
* <code>part</code> parameter determines what is returned, the character,
|
165 |
|
|
* word or sentence after the index.
|
166 |
|
|
*
|
167 |
|
|
* @param part one of {@link AccessibleText#CHARACTER},
|
168 |
|
|
* {@link AccessibleText#WORD} or
|
169 |
|
|
* {@link AccessibleText#SENTENCE}, specifying what is returned
|
170 |
|
|
* @param index the index
|
171 |
|
|
*
|
172 |
|
|
* @return the character, word or sentence after <code>index</code>
|
173 |
|
|
*/
|
174 |
|
|
public String getAfterIndex(int part, int index)
|
175 |
|
|
{
|
176 |
|
|
String result = "";
|
177 |
|
|
int startIndex = -1;
|
178 |
|
|
int endIndex = -1;
|
179 |
|
|
switch(part)
|
180 |
|
|
{
|
181 |
|
|
case AccessibleText.CHARACTER:
|
182 |
|
|
result = String.valueOf(text.charAt(index + 1));
|
183 |
|
|
break;
|
184 |
|
|
case AccessibleText.WORD:
|
185 |
|
|
startIndex = text.indexOf(' ', index);
|
186 |
|
|
endIndex = text.indexOf(' ', startIndex + 1);
|
187 |
|
|
if (endIndex == -1)
|
188 |
|
|
endIndex = startIndex + 1;
|
189 |
|
|
result = text.substring(startIndex + 1, endIndex);
|
190 |
|
|
break;
|
191 |
|
|
case AccessibleText.SENTENCE:
|
192 |
|
|
default:
|
193 |
|
|
startIndex = text.indexOf('.', index);
|
194 |
|
|
endIndex = text.indexOf('.', startIndex + 1);
|
195 |
|
|
if (endIndex == -1)
|
196 |
|
|
endIndex = startIndex + 1;
|
197 |
|
|
result = text.substring(startIndex + 1, endIndex);
|
198 |
|
|
break;
|
199 |
|
|
}
|
200 |
|
|
return result;
|
201 |
|
|
}
|
202 |
|
|
|
203 |
|
|
/**
|
204 |
|
|
* Returns the character, word or sentence before the specified index. The
|
205 |
|
|
* <code>part</code> parameter determines what is returned, the character,
|
206 |
|
|
* word or sentence before the index.
|
207 |
|
|
*
|
208 |
|
|
* @param part one of {@link AccessibleText#CHARACTER},
|
209 |
|
|
* {@link AccessibleText#WORD} or
|
210 |
|
|
* {@link AccessibleText#SENTENCE}, specifying what is returned
|
211 |
|
|
* @param index the index
|
212 |
|
|
*
|
213 |
|
|
* @return the character, word or sentence before <code>index</code>
|
214 |
|
|
*/
|
215 |
|
|
public String getBeforeIndex(int part, int index)
|
216 |
|
|
{
|
217 |
|
|
String result = "";
|
218 |
|
|
int startIndex = -1;
|
219 |
|
|
int endIndex = -1;
|
220 |
|
|
switch(part)
|
221 |
|
|
{
|
222 |
|
|
case AccessibleText.CHARACTER:
|
223 |
|
|
result = String.valueOf(text.charAt(index - 1));
|
224 |
|
|
break;
|
225 |
|
|
case AccessibleText.WORD:
|
226 |
|
|
endIndex = text.lastIndexOf(' ', index);
|
227 |
|
|
if (endIndex == -1)
|
228 |
|
|
endIndex = 0;
|
229 |
|
|
startIndex = text.lastIndexOf(' ', endIndex - 1);
|
230 |
|
|
result = text.substring(startIndex + 1, endIndex);
|
231 |
|
|
break;
|
232 |
|
|
case AccessibleText.SENTENCE:
|
233 |
|
|
default:
|
234 |
|
|
endIndex = text.lastIndexOf('.', index);
|
235 |
|
|
if (endIndex == -1)
|
236 |
|
|
endIndex = 0;
|
237 |
|
|
startIndex = text.lastIndexOf('.', endIndex - 1);
|
238 |
|
|
result = text.substring(startIndex + 1, endIndex);
|
239 |
|
|
break;
|
240 |
|
|
}
|
241 |
|
|
return result;
|
242 |
|
|
}
|
243 |
|
|
|
244 |
|
|
/**
|
245 |
|
|
* Returns the caret position. This method returns -1 because JLabel don't
|
246 |
|
|
* have a caret.
|
247 |
|
|
*
|
248 |
|
|
* @return the caret position
|
249 |
|
|
*/
|
250 |
|
|
public int getCaretPosition()
|
251 |
|
|
{
|
252 |
|
|
return -1;
|
253 |
|
|
}
|
254 |
|
|
|
255 |
|
|
/**
|
256 |
|
|
* Returns the number of characters that are displayed by the JLabel.
|
257 |
|
|
*
|
258 |
|
|
* @return the number of characters that are displayed by the JLabel
|
259 |
|
|
*/
|
260 |
|
|
public int getCharCount()
|
261 |
|
|
{
|
262 |
|
|
return text.length();
|
263 |
|
|
}
|
264 |
|
|
|
265 |
|
|
/**
|
266 |
|
|
* Returns the bounding box of the character at the specified index.
|
267 |
|
|
*
|
268 |
|
|
* @param index the index of the character that we return the
|
269 |
|
|
* bounds for
|
270 |
|
|
*
|
271 |
|
|
* @return the bounding box of the character at the specified index
|
272 |
|
|
*/
|
273 |
|
|
public Rectangle getCharacterBounds(int index)
|
274 |
|
|
{
|
275 |
|
|
// FIXME: Implement this correctly.
|
276 |
|
|
return new Rectangle();
|
277 |
|
|
}
|
278 |
|
|
|
279 |
|
|
/**
|
280 |
|
|
* Returns the index of the character that is located at the specified
|
281 |
|
|
* point.
|
282 |
|
|
*
|
283 |
|
|
* @param point the location that we lookup the character for
|
284 |
|
|
*
|
285 |
|
|
* @return the index of the character that is located at the specified
|
286 |
|
|
* point
|
287 |
|
|
*/
|
288 |
|
|
public int getIndexAtPoint(Point point)
|
289 |
|
|
{
|
290 |
|
|
// FIXME: Implement this correctly.
|
291 |
|
|
return 0;
|
292 |
|
|
}
|
293 |
|
|
}
|
294 |
|
|
|
295 |
|
|
/** DOCUMENT ME! */
|
296 |
|
|
private static final long serialVersionUID = 5496508283662221534L;
|
297 |
|
|
|
298 |
|
|
/**
|
299 |
|
|
* The Component the label will give focus to when its mnemonic is
|
300 |
|
|
* activated.
|
301 |
|
|
*/
|
302 |
|
|
protected Component labelFor;
|
303 |
|
|
|
304 |
|
|
/** The label's text. */
|
305 |
|
|
transient String text;
|
306 |
|
|
|
307 |
|
|
/** Where the label will be positioned horizontally. */
|
308 |
|
|
private transient int horizontalAlignment = LEADING;
|
309 |
|
|
|
310 |
|
|
/** Where the label text will be placed horizontally relative to the icon. */
|
311 |
|
|
private transient int horizontalTextPosition = TRAILING;
|
312 |
|
|
|
313 |
|
|
/** Where the label will be positioned vertically. */
|
314 |
|
|
private transient int verticalAlignment = CENTER;
|
315 |
|
|
|
316 |
|
|
/** Where the label text will be place vertically relative to the icon. */
|
317 |
|
|
private transient int verticalTextPosition = CENTER;
|
318 |
|
|
|
319 |
|
|
/** The icon painted when the label is enabled. */
|
320 |
|
|
private transient Icon icon;
|
321 |
|
|
|
322 |
|
|
/** The icon painted when the label is disabled. */
|
323 |
|
|
private transient Icon disabledIcon;
|
324 |
|
|
|
325 |
|
|
/** The label's mnemnonic key. */
|
326 |
|
|
private transient int displayedMnemonic = KeyEvent.VK_UNDEFINED;
|
327 |
|
|
|
328 |
|
|
/** The index of the menemonic character in the text. */
|
329 |
|
|
private transient int displayedMnemonicIndex = -1;
|
330 |
|
|
|
331 |
|
|
/** The gap between the icon and the text. */
|
332 |
|
|
private transient int iconTextGap = 4;
|
333 |
|
|
|
334 |
|
|
/**
|
335 |
|
|
* Creates a new vertically centered, horizontally on the leading edge
|
336 |
|
|
* JLabel object with text and no icon.
|
337 |
|
|
*/
|
338 |
|
|
public JLabel()
|
339 |
|
|
{
|
340 |
|
|
this(null, null, LEADING);
|
341 |
|
|
}
|
342 |
|
|
|
343 |
|
|
/**
|
344 |
|
|
* Creates a new vertically and horizontally centered
|
345 |
|
|
* JLabel object with no text and the given icon.
|
346 |
|
|
*
|
347 |
|
|
* @param image The icon to use with the label.
|
348 |
|
|
*/
|
349 |
|
|
public JLabel(Icon image)
|
350 |
|
|
{
|
351 |
|
|
this(null, image, CENTER);
|
352 |
|
|
}
|
353 |
|
|
|
354 |
|
|
/**
|
355 |
|
|
* Creates a new vertically centered JLabel object with no text and the
|
356 |
|
|
* given icon and horizontal alignment. By default, the text is TRAILING
|
357 |
|
|
* the image.
|
358 |
|
|
*
|
359 |
|
|
* @param image The icon to use with the label.
|
360 |
|
|
* @param horizontalAlignment The horizontal alignment of the label.
|
361 |
|
|
*/
|
362 |
|
|
public JLabel(Icon image, int horizontalAlignment)
|
363 |
|
|
{
|
364 |
|
|
this(null, image, horizontalAlignment);
|
365 |
|
|
}
|
366 |
|
|
|
367 |
|
|
/**
|
368 |
|
|
* Creates a new horizontally leading and vertically centered JLabel
|
369 |
|
|
* object with no icon and the given text.
|
370 |
|
|
*
|
371 |
|
|
* @param text The text to use with the label.
|
372 |
|
|
*/
|
373 |
|
|
public JLabel(String text)
|
374 |
|
|
{
|
375 |
|
|
this(text, null, LEADING);
|
376 |
|
|
}
|
377 |
|
|
|
378 |
|
|
/**
|
379 |
|
|
* Creates a new vertically centered JLabel object with no icon and the
|
380 |
|
|
* given text and horizontal alignment.
|
381 |
|
|
*
|
382 |
|
|
* @param text The text to use with the label.
|
383 |
|
|
* @param horizontalAlignment The horizontal alignment of the label.
|
384 |
|
|
*/
|
385 |
|
|
public JLabel(String text, int horizontalAlignment)
|
386 |
|
|
{
|
387 |
|
|
this(text, null, horizontalAlignment);
|
388 |
|
|
}
|
389 |
|
|
|
390 |
|
|
/**
|
391 |
|
|
* Creates a new vertically centered JLabel object with the given text,
|
392 |
|
|
* icon, and horizontal alignment.
|
393 |
|
|
*
|
394 |
|
|
* @param text The text to use with the label.
|
395 |
|
|
* @param icon The icon to use with the label.
|
396 |
|
|
* @param horizontalAlignment The horizontal alignment of the label.
|
397 |
|
|
*/
|
398 |
|
|
public JLabel(String text, Icon icon, int horizontalAlignment)
|
399 |
|
|
{
|
400 |
|
|
this.text = text;
|
401 |
|
|
this.icon = icon;
|
402 |
|
|
this.horizontalAlignment = horizontalAlignment;
|
403 |
|
|
setAlignmentX(0.0F);
|
404 |
|
|
updateUI();
|
405 |
|
|
}
|
406 |
|
|
|
407 |
|
|
/**
|
408 |
|
|
* This method returns the label's UI delegate.
|
409 |
|
|
*
|
410 |
|
|
* @return The label's UI delegate.
|
411 |
|
|
*/
|
412 |
|
|
public LabelUI getUI()
|
413 |
|
|
{
|
414 |
|
|
return (LabelUI) ui;
|
415 |
|
|
}
|
416 |
|
|
|
417 |
|
|
/**
|
418 |
|
|
* This method sets the label's UI delegate.
|
419 |
|
|
*
|
420 |
|
|
* @param ui The label's UI delegate.
|
421 |
|
|
*/
|
422 |
|
|
public void setUI(LabelUI ui)
|
423 |
|
|
{
|
424 |
|
|
super.setUI(ui);
|
425 |
|
|
}
|
426 |
|
|
|
427 |
|
|
/**
|
428 |
|
|
* This method resets the label's UI delegate to the default UI for the
|
429 |
|
|
* current look and feel.
|
430 |
|
|
*/
|
431 |
|
|
public void updateUI()
|
432 |
|
|
{
|
433 |
|
|
setUI((LabelUI) UIManager.getUI(this));
|
434 |
|
|
}
|
435 |
|
|
|
436 |
|
|
/**
|
437 |
|
|
* This method returns a name to identify which look and feel class will be
|
438 |
|
|
* the UI delegate for this label.
|
439 |
|
|
*
|
440 |
|
|
* @return The UIClass identifier. "LabelUI"
|
441 |
|
|
*/
|
442 |
|
|
public String getUIClassID()
|
443 |
|
|
{
|
444 |
|
|
return "LabelUI";
|
445 |
|
|
}
|
446 |
|
|
|
447 |
|
|
/**
|
448 |
|
|
* This method is used primarily for debugging purposes and returns a string
|
449 |
|
|
* that can be used to represent this label.
|
450 |
|
|
*
|
451 |
|
|
* @return A string to represent this label.
|
452 |
|
|
*/
|
453 |
|
|
protected String paramString()
|
454 |
|
|
{
|
455 |
|
|
return "JLabel";
|
456 |
|
|
}
|
457 |
|
|
|
458 |
|
|
/**
|
459 |
|
|
* This method returns the label text.
|
460 |
|
|
*
|
461 |
|
|
* @return The label text.
|
462 |
|
|
*/
|
463 |
|
|
public String getText()
|
464 |
|
|
{
|
465 |
|
|
return text;
|
466 |
|
|
}
|
467 |
|
|
|
468 |
|
|
/**
|
469 |
|
|
* This method changes the "text" property. The given text will be painted
|
470 |
|
|
* in the label.
|
471 |
|
|
*
|
472 |
|
|
* @param newText The label's text.
|
473 |
|
|
*/
|
474 |
|
|
public void setText(String newText)
|
475 |
|
|
{
|
476 |
|
|
if (text != newText)
|
477 |
|
|
{
|
478 |
|
|
String oldText = text;
|
479 |
|
|
text = newText;
|
480 |
|
|
firePropertyChange("text", oldText, newText);
|
481 |
|
|
|
482 |
|
|
if (text != null && text.length() <= displayedMnemonicIndex)
|
483 |
|
|
setDisplayedMnemonicIndex(text.length() - 1);
|
484 |
|
|
revalidate();
|
485 |
|
|
repaint();
|
486 |
|
|
}
|
487 |
|
|
}
|
488 |
|
|
|
489 |
|
|
/**
|
490 |
|
|
* This method returns the active icon. The active icon is painted when the
|
491 |
|
|
* label is enabled.
|
492 |
|
|
*
|
493 |
|
|
* @return The active icon.
|
494 |
|
|
*/
|
495 |
|
|
public Icon getIcon()
|
496 |
|
|
{
|
497 |
|
|
return icon;
|
498 |
|
|
}
|
499 |
|
|
|
500 |
|
|
/**
|
501 |
|
|
* This method changes the "icon" property. This icon (the active icon) will
|
502 |
|
|
* be the one displayed when the label is enabled.
|
503 |
|
|
*
|
504 |
|
|
* @param newIcon The active icon.
|
505 |
|
|
*/
|
506 |
|
|
public void setIcon(Icon newIcon)
|
507 |
|
|
{
|
508 |
|
|
if (icon != newIcon)
|
509 |
|
|
{
|
510 |
|
|
Icon oldIcon = icon;
|
511 |
|
|
icon = newIcon;
|
512 |
|
|
firePropertyChange("icon", oldIcon, newIcon);
|
513 |
|
|
}
|
514 |
|
|
}
|
515 |
|
|
|
516 |
|
|
/**
|
517 |
|
|
* This method returns the disabled icon. The disabled icon is painted when
|
518 |
|
|
* the label is disabled. If the disabled icon is null and the active icon
|
519 |
|
|
* is an ImageIcon, this method returns a grayed version of the icon. The
|
520 |
|
|
* grayed version of the icon becomes the disabledIcon.
|
521 |
|
|
*
|
522 |
|
|
* @return The disabled icon.
|
523 |
|
|
*/
|
524 |
|
|
public Icon getDisabledIcon()
|
525 |
|
|
{
|
526 |
|
|
if (disabledIcon == null && icon instanceof ImageIcon)
|
527 |
|
|
disabledIcon = new ImageIcon(GrayFilter.createDisabledImage(((ImageIcon) icon)
|
528 |
|
|
.getImage()));
|
529 |
|
|
|
530 |
|
|
return disabledIcon;
|
531 |
|
|
}
|
532 |
|
|
|
533 |
|
|
/**
|
534 |
|
|
* This method changes the "disabledIcon" property. This icon (the disabled
|
535 |
|
|
* icon) will be the one displayed when the label is disabled.
|
536 |
|
|
*
|
537 |
|
|
* @param newIcon The disabled icon.
|
538 |
|
|
*/
|
539 |
|
|
public void setDisabledIcon(Icon newIcon)
|
540 |
|
|
{
|
541 |
|
|
if (disabledIcon != newIcon)
|
542 |
|
|
{
|
543 |
|
|
Icon oldIcon = disabledIcon;
|
544 |
|
|
disabledIcon = newIcon;
|
545 |
|
|
firePropertyChange("disabledIcon", oldIcon, newIcon);
|
546 |
|
|
}
|
547 |
|
|
}
|
548 |
|
|
|
549 |
|
|
/**
|
550 |
|
|
* This method sets the keycode that will be the label's mnemonic. If the
|
551 |
|
|
* label is used as a label for another component, the label will give
|
552 |
|
|
* focus to that component when the mnemonic is activated.
|
553 |
|
|
*
|
554 |
|
|
* @param mnemonic The keycode to use for the mnemonic.
|
555 |
|
|
*/
|
556 |
|
|
public void setDisplayedMnemonic(int mnemonic)
|
557 |
|
|
{
|
558 |
|
|
if (displayedMnemonic != mnemonic)
|
559 |
|
|
{
|
560 |
|
|
firePropertyChange("displayedMnemonic",
|
561 |
|
|
displayedMnemonic, mnemonic);
|
562 |
|
|
displayedMnemonic = mnemonic;
|
563 |
|
|
|
564 |
|
|
if (text != null)
|
565 |
|
|
setDisplayedMnemonicIndex(text.toUpperCase().indexOf(mnemonic));
|
566 |
|
|
}
|
567 |
|
|
}
|
568 |
|
|
|
569 |
|
|
/**
|
570 |
|
|
* This method sets the character that will be the mnemonic used. If the
|
571 |
|
|
* label is used as a label for another component, the label will give
|
572 |
|
|
* focus to that component when the mnemonic is activated.
|
573 |
|
|
*
|
574 |
|
|
* @param mnemonic The character to use for the mnemonic.
|
575 |
|
|
*/
|
576 |
|
|
public void setDisplayedMnemonic(char mnemonic)
|
577 |
|
|
{
|
578 |
|
|
setDisplayedMnemonic((int) Character.toUpperCase(mnemonic));
|
579 |
|
|
}
|
580 |
|
|
|
581 |
|
|
/**
|
582 |
|
|
* This method returns the keycode that is used for the label's mnemonic.
|
583 |
|
|
*
|
584 |
|
|
* @return The keycode that is used for the label's mnemonic.
|
585 |
|
|
*/
|
586 |
|
|
public int getDisplayedMnemonic()
|
587 |
|
|
{
|
588 |
|
|
return (int) displayedMnemonic;
|
589 |
|
|
}
|
590 |
|
|
|
591 |
|
|
/**
|
592 |
|
|
* This method sets which character in the text will be the underlined
|
593 |
|
|
* character. If the given index is -1, then this indicates that there is
|
594 |
|
|
* no mnemonic. If the index is less than -1 or if the index is equal to
|
595 |
|
|
* the length, this method will throw an IllegalArgumentException.
|
596 |
|
|
*
|
597 |
|
|
* @param newIndex The index of the character to underline.
|
598 |
|
|
*
|
599 |
|
|
* @throws IllegalArgumentException If index less than -1 or index equals
|
600 |
|
|
* length.
|
601 |
|
|
*/
|
602 |
|
|
public void setDisplayedMnemonicIndex(int newIndex)
|
603 |
|
|
throws IllegalArgumentException
|
604 |
|
|
{
|
605 |
|
|
if (newIndex < -1 || (text != null && newIndex >= text.length()))
|
606 |
|
|
throw new IllegalArgumentException();
|
607 |
|
|
|
608 |
|
|
if (newIndex == -1
|
609 |
|
|
|| text == null
|
610 |
|
|
|| text.charAt(newIndex) != displayedMnemonic)
|
611 |
|
|
newIndex = -1;
|
612 |
|
|
|
613 |
|
|
if (newIndex != displayedMnemonicIndex)
|
614 |
|
|
{
|
615 |
|
|
int oldIndex = displayedMnemonicIndex;
|
616 |
|
|
displayedMnemonicIndex = newIndex;
|
617 |
|
|
firePropertyChange("displayedMnemonicIndex",
|
618 |
|
|
oldIndex, newIndex);
|
619 |
|
|
}
|
620 |
|
|
}
|
621 |
|
|
|
622 |
|
|
/**
|
623 |
|
|
* This method returns which character in the text will be the underlined
|
624 |
|
|
* character.
|
625 |
|
|
*
|
626 |
|
|
* @return The index of the character that will be underlined.
|
627 |
|
|
*/
|
628 |
|
|
public int getDisplayedMnemonicIndex()
|
629 |
|
|
{
|
630 |
|
|
return displayedMnemonicIndex;
|
631 |
|
|
}
|
632 |
|
|
|
633 |
|
|
/**
|
634 |
|
|
* This method ensures that the key is valid as a horizontal alignment.
|
635 |
|
|
* Valid keys are: LEFT, CENTER, RIGHT, LEADING, TRAILING
|
636 |
|
|
*
|
637 |
|
|
* @param key The key to check.
|
638 |
|
|
* @param message The message of the exception to be thrown if the key is
|
639 |
|
|
* invalid.
|
640 |
|
|
*
|
641 |
|
|
* @return The key if it's valid.
|
642 |
|
|
*
|
643 |
|
|
* @throws IllegalArgumentException If the key is invalid.
|
644 |
|
|
*/
|
645 |
|
|
protected int checkHorizontalKey(int key, String message)
|
646 |
|
|
{
|
647 |
|
|
if (key != LEFT && key != CENTER && key != RIGHT && key != LEADING
|
648 |
|
|
&& key != TRAILING)
|
649 |
|
|
throw new IllegalArgumentException(message);
|
650 |
|
|
else
|
651 |
|
|
return key;
|
652 |
|
|
}
|
653 |
|
|
|
654 |
|
|
/**
|
655 |
|
|
* This method ensures that the key is valid as a vertical alignment. Valid
|
656 |
|
|
* keys are: TOP, CENTER, and BOTTOM.
|
657 |
|
|
*
|
658 |
|
|
* @param key The key to check.
|
659 |
|
|
* @param message The message of the exception to be thrown if the key is
|
660 |
|
|
* invalid.
|
661 |
|
|
*
|
662 |
|
|
* @return The key if it's valid.
|
663 |
|
|
*
|
664 |
|
|
* @throws IllegalArgumentException If the key is invalid.
|
665 |
|
|
*/
|
666 |
|
|
protected int checkVerticalKey(int key, String message)
|
667 |
|
|
{
|
668 |
|
|
if (key != TOP && key != BOTTOM && key != CENTER)
|
669 |
|
|
throw new IllegalArgumentException(message);
|
670 |
|
|
else
|
671 |
|
|
return key;
|
672 |
|
|
}
|
673 |
|
|
|
674 |
|
|
/**
|
675 |
|
|
* This method returns the gap between the icon and the text.
|
676 |
|
|
*
|
677 |
|
|
* @return The gap between the icon and the text.
|
678 |
|
|
*/
|
679 |
|
|
public int getIconTextGap()
|
680 |
|
|
{
|
681 |
|
|
return iconTextGap;
|
682 |
|
|
}
|
683 |
|
|
|
684 |
|
|
/**
|
685 |
|
|
* This method changes the "iconTextGap" property. The iconTextGap
|
686 |
|
|
* determines how much space there is between the icon and the text.
|
687 |
|
|
*
|
688 |
|
|
* @param newGap The gap between the icon and the text.
|
689 |
|
|
*/
|
690 |
|
|
public void setIconTextGap(int newGap)
|
691 |
|
|
{
|
692 |
|
|
if (iconTextGap != newGap)
|
693 |
|
|
{
|
694 |
|
|
firePropertyChange("iconTextGap", iconTextGap, newGap);
|
695 |
|
|
iconTextGap = newGap;
|
696 |
|
|
}
|
697 |
|
|
}
|
698 |
|
|
|
699 |
|
|
/**
|
700 |
|
|
* This method returns the vertical alignment of the label.
|
701 |
|
|
*
|
702 |
|
|
* @return The vertical alignment of the label.
|
703 |
|
|
*/
|
704 |
|
|
public int getVerticalAlignment()
|
705 |
|
|
{
|
706 |
|
|
return verticalAlignment;
|
707 |
|
|
}
|
708 |
|
|
|
709 |
|
|
/**
|
710 |
|
|
* This method changes the "verticalAlignment" property of the label. The
|
711 |
|
|
* vertical alignment determines how where the label will be placed
|
712 |
|
|
* vertically. If the alignment is not valid, it will default to the
|
713 |
|
|
* center.
|
714 |
|
|
*
|
715 |
|
|
* @param alignment The vertical alignment of the label.
|
716 |
|
|
*/
|
717 |
|
|
public void setVerticalAlignment(int alignment)
|
718 |
|
|
{
|
719 |
|
|
if (alignment == verticalAlignment)
|
720 |
|
|
return;
|
721 |
|
|
|
722 |
|
|
int oldAlignment = verticalAlignment;
|
723 |
|
|
verticalAlignment = checkVerticalKey(alignment, "verticalAlignment");
|
724 |
|
|
firePropertyChange("verticalAlignment", oldAlignment, verticalAlignment);
|
725 |
|
|
}
|
726 |
|
|
|
727 |
|
|
/**
|
728 |
|
|
* This method returns the horziontal alignment of the label.
|
729 |
|
|
*
|
730 |
|
|
* @return The horizontal alignment of the label.
|
731 |
|
|
*/
|
732 |
|
|
public int getHorizontalAlignment()
|
733 |
|
|
{
|
734 |
|
|
return horizontalAlignment;
|
735 |
|
|
}
|
736 |
|
|
|
737 |
|
|
/**
|
738 |
|
|
* This method changes the "horizontalAlignment" property. The horizontal
|
739 |
|
|
* alignment determines where the label will be placed horizontally.
|
740 |
|
|
*
|
741 |
|
|
* @param alignment The horizontal alignment of the label.
|
742 |
|
|
*/
|
743 |
|
|
public void setHorizontalAlignment(int alignment)
|
744 |
|
|
{
|
745 |
|
|
if (horizontalAlignment == alignment)
|
746 |
|
|
return;
|
747 |
|
|
|
748 |
|
|
int oldAlignment = horizontalAlignment;
|
749 |
|
|
horizontalAlignment = checkHorizontalKey(alignment, "horizontalAlignment");
|
750 |
|
|
firePropertyChange("horizontalAlignment", oldAlignment,
|
751 |
|
|
horizontalAlignment);
|
752 |
|
|
}
|
753 |
|
|
|
754 |
|
|
/**
|
755 |
|
|
* This method returns the vertical text position of the label.
|
756 |
|
|
*
|
757 |
|
|
* @return The vertical text position of the label.
|
758 |
|
|
*/
|
759 |
|
|
public int getVerticalTextPosition()
|
760 |
|
|
{
|
761 |
|
|
return verticalTextPosition;
|
762 |
|
|
}
|
763 |
|
|
|
764 |
|
|
/**
|
765 |
|
|
* This method changes the "verticalTextPosition" property of the label. The
|
766 |
|
|
* vertical text position determines where the text will be placed
|
767 |
|
|
* vertically relative to the icon.
|
768 |
|
|
*
|
769 |
|
|
* @param textPosition The vertical text position.
|
770 |
|
|
*/
|
771 |
|
|
public void setVerticalTextPosition(int textPosition)
|
772 |
|
|
{
|
773 |
|
|
if (textPosition != verticalTextPosition)
|
774 |
|
|
{
|
775 |
|
|
int oldPos = verticalTextPosition;
|
776 |
|
|
verticalTextPosition = checkVerticalKey(textPosition,
|
777 |
|
|
"verticalTextPosition");
|
778 |
|
|
firePropertyChange("verticalTextPosition", oldPos,
|
779 |
|
|
verticalTextPosition);
|
780 |
|
|
}
|
781 |
|
|
}
|
782 |
|
|
|
783 |
|
|
/**
|
784 |
|
|
* This method returns the horizontal text position of the label.
|
785 |
|
|
*
|
786 |
|
|
* @return The horizontal text position.
|
787 |
|
|
*/
|
788 |
|
|
public int getHorizontalTextPosition()
|
789 |
|
|
{
|
790 |
|
|
return horizontalTextPosition;
|
791 |
|
|
}
|
792 |
|
|
|
793 |
|
|
/**
|
794 |
|
|
* This method changes the "horizontalTextPosition" property of the label.
|
795 |
|
|
* The horizontal text position determines where the text will be placed
|
796 |
|
|
* horizontally relative to the icon.
|
797 |
|
|
*
|
798 |
|
|
* @param textPosition The horizontal text position.
|
799 |
|
|
*/
|
800 |
|
|
public void setHorizontalTextPosition(int textPosition)
|
801 |
|
|
{
|
802 |
|
|
if (textPosition != horizontalTextPosition)
|
803 |
|
|
{
|
804 |
|
|
int oldPos = horizontalTextPosition;
|
805 |
|
|
horizontalTextPosition = checkHorizontalKey(textPosition,
|
806 |
|
|
"horizontalTextPosition");
|
807 |
|
|
firePropertyChange("horizontalTextPosition", oldPos,
|
808 |
|
|
horizontalTextPosition);
|
809 |
|
|
}
|
810 |
|
|
}
|
811 |
|
|
|
812 |
|
|
/**
|
813 |
|
|
* This method simply returns false if the current icon image (current icon
|
814 |
|
|
* will depend on whether the label is enabled) is not equal to the passed
|
815 |
|
|
* in image.
|
816 |
|
|
*
|
817 |
|
|
* @param img The image to check.
|
818 |
|
|
* @param infoflags The bitwise inclusive OR of ABORT, ALLBITS, ERROR,
|
819 |
|
|
* FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, and WIDTH
|
820 |
|
|
* @param x The x position
|
821 |
|
|
* @param y The y position
|
822 |
|
|
* @param w The width
|
823 |
|
|
* @param h The height
|
824 |
|
|
*
|
825 |
|
|
* @return Whether the current icon image is equal to the image given.
|
826 |
|
|
*/
|
827 |
|
|
public boolean imageUpdate(Image img, int infoflags, int x, int y, int w,
|
828 |
|
|
int h)
|
829 |
|
|
{
|
830 |
|
|
Icon currIcon = isEnabled() ? icon : disabledIcon;
|
831 |
|
|
|
832 |
|
|
// XXX: Is this the correct way to check for image equality?
|
833 |
|
|
if (currIcon != null && currIcon instanceof ImageIcon)
|
834 |
|
|
return (((ImageIcon) currIcon).getImage() == img);
|
835 |
|
|
|
836 |
|
|
return false;
|
837 |
|
|
}
|
838 |
|
|
|
839 |
|
|
/**
|
840 |
|
|
* This method returns the component that the label gives focus to when the
|
841 |
|
|
* mnemonic is activated.
|
842 |
|
|
*
|
843 |
|
|
* @return The component that gets focus when the label's mnemonic is
|
844 |
|
|
* activated.
|
845 |
|
|
*/
|
846 |
|
|
public Component getLabelFor()
|
847 |
|
|
{
|
848 |
|
|
return labelFor;
|
849 |
|
|
}
|
850 |
|
|
|
851 |
|
|
/**
|
852 |
|
|
* This method changes the "labelFor" property. The component that the label
|
853 |
|
|
* is acting as a label for will request focus when the label's mnemonic
|
854 |
|
|
* is activated.
|
855 |
|
|
*
|
856 |
|
|
* @param c The component that gets focus when the label's mnemonic is
|
857 |
|
|
* activated.
|
858 |
|
|
*/
|
859 |
|
|
public void setLabelFor(Component c)
|
860 |
|
|
{
|
861 |
|
|
if (c != labelFor)
|
862 |
|
|
{
|
863 |
|
|
Component oldLabelFor = labelFor;
|
864 |
|
|
labelFor = c;
|
865 |
|
|
firePropertyChange("labelFor", oldLabelFor, labelFor);
|
866 |
|
|
}
|
867 |
|
|
}
|
868 |
|
|
|
869 |
|
|
/**
|
870 |
|
|
* This method overrides setFont so that we can call for a repaint after the
|
871 |
|
|
* font is changed.
|
872 |
|
|
*
|
873 |
|
|
* @param f The font for this label.
|
874 |
|
|
*/
|
875 |
|
|
public void setFont(Font f)
|
876 |
|
|
{
|
877 |
|
|
super.setFont(f);
|
878 |
|
|
repaint();
|
879 |
|
|
}
|
880 |
|
|
|
881 |
|
|
/**
|
882 |
|
|
* DOCUMENT ME!
|
883 |
|
|
*
|
884 |
|
|
* @return The accessible context.
|
885 |
|
|
*/
|
886 |
|
|
public AccessibleContext getAccessibleContext()
|
887 |
|
|
{
|
888 |
|
|
if (accessibleContext == null)
|
889 |
|
|
accessibleContext = new AccessibleJLabel();
|
890 |
|
|
return accessibleContext;
|
891 |
|
|
}
|
892 |
|
|
}
|