40 |
import java.awt.Component; |
import java.awt.Component; |
41 |
import java.awt.Image; |
import java.awt.Image; |
42 |
import java.awt.Font; |
import java.awt.Font; |
43 |
|
import java.awt.event.KeyEvent; |
44 |
import javax.accessibility.Accessible; |
import javax.accessibility.Accessible; |
45 |
import javax.accessibility.AccessibleContext; |
import javax.accessibility.AccessibleContext; |
46 |
import javax.swing.Icon; |
import javax.swing.Icon; |
64 |
protected Component labelFor; |
protected Component labelFor; |
65 |
|
|
66 |
/** The label's text. */ |
/** The label's text. */ |
67 |
private transient String labelText; |
private transient String text; |
68 |
|
|
69 |
/** Where the label will be positioned horizontally. */ |
/** Where the label will be positioned horizontally. */ |
70 |
private transient int horizontalAlignment = CENTER; |
private transient int horizontalAlignment = LEADING; |
71 |
|
|
72 |
/** Where the label text will be placed horizontally relative to the icon. */ |
/** Where the label text will be placed horizontally relative to the icon. */ |
73 |
private transient int horizontalTextPosition = TRAILING; |
private transient int horizontalTextPosition = TRAILING; |
79 |
private transient int verticalTextPosition = CENTER; |
private transient int verticalTextPosition = CENTER; |
80 |
|
|
81 |
/** The icon painted when the label is enabled. */ |
/** The icon painted when the label is enabled. */ |
82 |
private transient Icon activeIcon; |
private transient Icon icon; |
83 |
|
|
84 |
/** The icon painted when the label is disabled. */ |
/** The icon painted when the label is disabled. */ |
85 |
private transient Icon disabledIcon; |
private transient Icon disabledIcon; |
86 |
|
|
87 |
/** The label's mnemnonic key. */ |
/** The label's mnemnonic key. */ |
88 |
private transient char mnemonicKey; |
private transient int displayedMnemonic = KeyEvent.VK_UNDEFINED; |
89 |
|
|
90 |
/** The index of the menemonic character in the text. */ |
/** The index of the menemonic character in the text. */ |
91 |
private transient int underlinedChar = -1; |
private transient int displayedMnemonicIndex = -1; |
92 |
|
|
93 |
/** The gap between the icon and the text. */ |
/** The gap between the icon and the text. */ |
94 |
private transient int iconTextGap = 4; |
private transient int iconTextGap = 4; |
210 |
*/ |
*/ |
211 |
public JLabel(String text, Icon icon, int horizontalAlignment) |
public JLabel(String text, Icon icon, int horizontalAlignment) |
212 |
{ |
{ |
213 |
labelText = text; |
this.text = text; |
214 |
activeIcon = icon; |
this.icon = icon; |
215 |
this.horizontalAlignment = horizontalAlignment; |
this.horizontalAlignment = horizontalAlignment; |
216 |
updateUI(); |
updateUI(); |
217 |
} |
} |
274 |
*/ |
*/ |
275 |
public String getText() |
public String getText() |
276 |
{ |
{ |
277 |
return labelText; |
return text; |
278 |
} |
} |
279 |
|
|
280 |
/** |
/** |
281 |
* This method changes the "text" property. The given text will be painted |
* This method changes the "text" property. The given text will be painted |
282 |
* in the label. |
* in the label. |
283 |
* |
* |
284 |
* @param text The label's text. |
* @param newText The label's text. |
285 |
*/ |
*/ |
286 |
public void setText(String text) |
public void setText(String newText) |
287 |
{ |
{ |
288 |
if (text != labelText) |
if (text != newText) |
289 |
{ |
{ |
290 |
String oldText = labelText; |
String oldText = text; |
291 |
labelText = text; |
text = newText; |
292 |
firePropertyChange(TEXT_CHANGED_PROPERTY, oldText, labelText); |
firePropertyChange(TEXT_CHANGED_PROPERTY, oldText, newText); |
293 |
if (labelText != null && labelText.length() <= underlinedChar) |
|
294 |
setDisplayedMnemonicIndex(labelText.length() - 1); |
if (text != null |
295 |
|
&& text.length() <= displayedMnemonicIndex) |
296 |
|
setDisplayedMnemonicIndex(text.length() - 1); |
297 |
} |
} |
298 |
} |
} |
299 |
|
|
305 |
*/ |
*/ |
306 |
public Icon getIcon() |
public Icon getIcon() |
307 |
{ |
{ |
308 |
return activeIcon; |
return icon; |
309 |
} |
} |
310 |
|
|
311 |
/** |
/** |
312 |
* This method changes the "icon" property. This icon (the active icon) will |
* This method changes the "icon" property. This icon (the active icon) will |
313 |
* be the one displayed when the label is enabled. |
* be the one displayed when the label is enabled. |
314 |
* |
* |
315 |
* @param icon The active icon. |
* @param newIcon The active icon. |
316 |
*/ |
*/ |
317 |
public void setIcon(Icon icon) |
public void setIcon(Icon newIcon) |
318 |
{ |
{ |
319 |
if (icon != activeIcon) |
if (icon != newIcon) |
320 |
{ |
{ |
321 |
Icon oldIcon = activeIcon; |
Icon oldIcon = icon; |
322 |
activeIcon = icon; |
icon = newIcon; |
323 |
firePropertyChange(ICON_CHANGED_PROPERTY, oldIcon, activeIcon); |
firePropertyChange(ICON_CHANGED_PROPERTY, oldIcon, newIcon); |
324 |
} |
} |
325 |
} |
} |
326 |
|
|
334 |
*/ |
*/ |
335 |
public Icon getDisabledIcon() |
public Icon getDisabledIcon() |
336 |
{ |
{ |
337 |
if (disabledIcon == null && activeIcon instanceof ImageIcon) |
if (disabledIcon == null && icon instanceof ImageIcon) |
338 |
disabledIcon = new ImageIcon(GrayFilter.createDisabledImage(((ImageIcon) activeIcon).getImage())); |
disabledIcon = new ImageIcon(GrayFilter.createDisabledImage(((ImageIcon) icon).getImage())); |
339 |
|
|
340 |
return disabledIcon; |
return disabledIcon; |
341 |
} |
} |
342 |
|
|
344 |
* This method changes the "disabledIcon" property. This icon (the disabled |
* This method changes the "disabledIcon" property. This icon (the disabled |
345 |
* icon) will be the one displayed when the label is disabled. |
* icon) will be the one displayed when the label is disabled. |
346 |
* |
* |
347 |
* @param disabledIcon The disabled icon. |
* @param newIcon The disabled icon. |
348 |
*/ |
*/ |
349 |
public void setDisabledIcon(Icon disabledIcon) |
public void setDisabledIcon(Icon newIcon) |
350 |
{ |
{ |
351 |
if (disabledIcon != this.disabledIcon) |
if (disabledIcon != newIcon) |
352 |
{ |
{ |
353 |
Icon oldDisabledIcon = this.disabledIcon; |
Icon oldIcon = disabledIcon; |
354 |
this.disabledIcon = disabledIcon; |
disabledIcon = newIcon; |
355 |
firePropertyChange(DISABLED_ICON_CHANGED_PROPERTY, oldDisabledIcon, |
firePropertyChange(DISABLED_ICON_CHANGED_PROPERTY, oldIcon, newIcon); |
|
this.disabledIcon); |
|
356 |
} |
} |
357 |
} |
} |
358 |
|
|
361 |
* label is used as a label for another component, the label will give |
* label is used as a label for another component, the label will give |
362 |
* focus to that component when the mnemonic is activated. |
* focus to that component when the mnemonic is activated. |
363 |
* |
* |
364 |
* @param key The keycode to use for the mnemonic. |
* @param mnemonic The keycode to use for the mnemonic. |
365 |
*/ |
*/ |
366 |
public void setDisplayedMnemonic(int key) |
public void setDisplayedMnemonic(int mnemonic) |
367 |
{ |
{ |
368 |
setDisplayedMnemonic((char) key); |
if (displayedMnemonic != mnemonic) |
369 |
|
{ |
370 |
|
firePropertyChange(DISPLAYED_MNEMONIC_CHANGED_PROPERTY, |
371 |
|
displayedMnemonic, mnemonic); |
372 |
|
displayedMnemonic = mnemonic; |
373 |
|
|
374 |
|
if (text != null) |
375 |
|
setDisplayedMnemonicIndex(text.indexOf(mnemonic)); |
376 |
|
} |
377 |
} |
} |
378 |
|
|
379 |
|
|
380 |
/** |
/** |
381 |
* This method sets the character that will be the mnemonic used. If the |
* This method sets the character that will be the mnemonic used. If the |
382 |
* label is used as a label for another component, the label will give |
* label is used as a label for another component, the label will give |
383 |
* focus to that component when the mnemonic is activated. |
* focus to that component when the mnemonic is activated. |
384 |
* |
* |
385 |
* @param aChar The character to use for the mnemonic. |
* @param menmonic The character to use for the mnemonic. |
386 |
*/ |
*/ |
387 |
public void setDisplayedMnemonic(char aChar) |
public void setDisplayedMnemonic(char mnemonic) |
|
{ |
|
|
if (aChar != mnemonicKey) |
|
388 |
{ |
{ |
389 |
char oldKey = mnemonicKey; |
setDisplayedMnemonic((int) mnemonic); |
|
mnemonicKey = aChar; |
|
|
firePropertyChange(DISPLAYED_MNEMONIC_CHANGED_PROPERTY, oldKey, |
|
|
mnemonicKey); |
|
|
if (labelText != null) |
|
|
setDisplayedMnemonicIndex(labelText.indexOf(mnemonicKey)); |
|
|
} |
|
390 |
} |
} |
391 |
|
|
392 |
/** |
/** |
396 |
*/ |
*/ |
397 |
public int getDisplayedMnemonic() |
public int getDisplayedMnemonic() |
398 |
{ |
{ |
399 |
return (int) mnemonicKey; |
return (int) displayedMnemonic; |
400 |
} |
} |
401 |
|
|
402 |
/** |
/** |
405 |
* no mnemonic. If the index is less than -1 or if the index is equal to |
* no mnemonic. If the index is less than -1 or if the index is equal to |
406 |
* the length, this method will throw an IllegalArgumentException. |
* the length, this method will throw an IllegalArgumentException. |
407 |
* |
* |
408 |
* @param index The index of the character to underline. |
* @param newIndex The index of the character to underline. |
409 |
* |
* |
410 |
* @throws IllegalArgumentException If index less than -1 or index equals |
* @throws IllegalArgumentException If index less than -1 or index equals |
411 |
* length. |
* length. |
412 |
*/ |
*/ |
413 |
public void setDisplayedMnemonicIndex(int index) |
public void setDisplayedMnemonicIndex(int newIndex) |
414 |
throws IllegalArgumentException |
throws IllegalArgumentException |
415 |
{ |
{ |
416 |
if (index < -1 || labelText != null && index >= labelText.length()) |
if (newIndex < -1 || (text != null && newIndex >= text.length())) |
417 |
throw new IllegalArgumentException(); |
throw new IllegalArgumentException(); |
418 |
|
|
419 |
if (labelText == null || labelText.charAt(index) != mnemonicKey) |
if (text == null || text.charAt(newIndex) != displayedMnemonic) |
420 |
index = -1; |
newIndex = -1; |
421 |
|
|
422 |
if (index != underlinedChar) |
if (newIndex != displayedMnemonicIndex) |
423 |
{ |
{ |
|
int oldIndex = underlinedChar; |
|
|
underlinedChar = index; |
|
424 |
firePropertyChange(DISPLAYED_MNEMONIC_INDEX_CHANGED_PROPERTY, |
firePropertyChange(DISPLAYED_MNEMONIC_INDEX_CHANGED_PROPERTY, |
425 |
oldIndex, underlinedChar); |
displayedMnemonicIndex, newIndex); |
426 |
|
displayedMnemonicIndex = newIndex; |
427 |
} |
} |
428 |
} |
} |
429 |
|
|
435 |
*/ |
*/ |
436 |
public int getDisplayedMnemonicIndex() |
public int getDisplayedMnemonicIndex() |
437 |
{ |
{ |
438 |
return underlinedChar; |
return displayedMnemonicIndex; |
439 |
} |
} |
440 |
|
|
441 |
/** |
/** |
493 |
* This method changes the "iconTextGap" property. The iconTextGap |
* This method changes the "iconTextGap" property. The iconTextGap |
494 |
* determines how much space there is between the icon and the text. |
* determines how much space there is between the icon and the text. |
495 |
* |
* |
496 |
* @param iconTextGap The gap between the icon and the text. |
* @param newGap The gap between the icon and the text. |
497 |
*/ |
*/ |
498 |
public void setIconTextGap(int iconTextGap) |
public void setIconTextGap(int newGap) |
499 |
{ |
{ |
500 |
if (iconTextGap != this.iconTextGap) |
if (iconTextGap != newGap) |
501 |
{ |
{ |
502 |
int oldIconTextGap = this.iconTextGap; |
firePropertyChange(ICON_TEXT_GAP_CHANGED_PROPERTY, iconTextGap, |
503 |
this.iconTextGap = iconTextGap; |
newGap); |
504 |
firePropertyChange(ICON_TEXT_GAP_CHANGED_PROPERTY, oldIconTextGap, |
iconTextGap = newGap; |
|
iconTextGap); |
|
505 |
} |
} |
506 |
} |
} |
507 |
|
|
634 |
public boolean imageUpdate(Image img, int infoflags, int x, int y, int w, |
public boolean imageUpdate(Image img, int infoflags, int x, int y, int w, |
635 |
int h) |
int h) |
636 |
{ |
{ |
637 |
Icon currIcon = (isEnabled()) ? activeIcon : disabledIcon; |
Icon currIcon = isEnabled() ? icon : disabledIcon; |
638 |
|
|
639 |
//Is this the correct way to check for image equality? |
// XXX: Is this the correct way to check for image equality? |
640 |
if (currIcon != null && currIcon instanceof ImageIcon) |
if (currIcon != null && currIcon instanceof ImageIcon) |
641 |
return (((ImageIcon) currIcon).getImage() == img); |
return (((ImageIcon) currIcon).getImage() == img); |
642 |
|
|
643 |
return false; |
return false; |
644 |
} |
} |
645 |
|
|
667 |
{ |
{ |
668 |
if (c != labelFor) |
if (c != labelFor) |
669 |
{ |
{ |
670 |
Component oldLabelFor = labelFor; |
firePropertyChange(LABEL_FOR_CHANGED_PROPERTY, labelFor, c); |
671 |
labelFor = c; |
labelFor = c; |
|
firePropertyChange(LABEL_FOR_CHANGED_PROPERTY, oldLabelFor, labelFor); |
|
672 |
} |
} |
673 |
} |
} |
674 |
|
|