diff options
Diffstat (limited to 'libjava/javax/swing/AbstractButton.java')
-rw-r--r-- | libjava/javax/swing/AbstractButton.java | 216 |
1 files changed, 133 insertions, 83 deletions
diff --git a/libjava/javax/swing/AbstractButton.java b/libjava/javax/swing/AbstractButton.java index 745afc9..3c2a4cd 100644 --- a/libjava/javax/swing/AbstractButton.java +++ b/libjava/javax/swing/AbstractButton.java @@ -163,19 +163,19 @@ public abstract class AbstractButton extends JComponent Icon pressed_icon; /** The icon displayed when the button is disabled. */ - Icon disabled_icon; + Icon disabeldIcon; /** The icon displayed when the button is selected. */ - Icon selected_icon; + Icon selectedIcon; /** The icon displayed when the button is selected but disabled. */ - Icon disabled_selected_icon; + Icon disabledSelectedIcon; /** The icon displayed when the button is rolled over. */ - Icon rollover_icon; + Icon rolloverIcon; /** The icon displayed when the button is selected and rolled over. */ - Icon rollover_selected_icon; + Icon rolloverSelectedIcon; /** The icon currently displayed. */ Icon current_icon; @@ -183,26 +183,32 @@ public abstract class AbstractButton extends JComponent /** The text displayed in the button. */ String text; + /** The gap between icon and text, if both icon and text are non-<code>null</code>. */ + int iconTextGap; + /** The vertical alignment of the button's text and icon. */ - int vert_align; + int verticalAlignment; /** The horizontal alignment of the button's text and icon. */ - int hori_align; + int horizontalAlignment; /** The horizontal position of the button's text relative to its icon. */ - int hori_text_pos; + int horizontalTextPosition; /** The vertical position of the button's text relative to its icon. */ - int vert_text_pos; + int verticalTextPosition; /** Whether or not the button paints its border. */ - boolean paint_border; + boolean borderPainted; /** Whether or not the button paints its focus state. */ - boolean paint_focus; + boolean focusPainted; /** Whether or not the button fills its content area. */ - boolean content_area_filled; + boolean contentAreaFilled; + + /** Whether rollover is enabled. */ + boolean rollOverEnabled; /** The action taken when the button is clicked. */ Action action; @@ -230,6 +236,9 @@ public abstract class AbstractButton extends JComponent Action. */ PropertyChangeListener actionPropertyChangeListener; + /** ChangeEvent that is fired to button's ChangeEventListeners */ + private ChangeEvent changeEvent = new ChangeEvent(this); + /** Fired in a PropertyChangeEvent when the "borderPainted" property changes. */ public static final String BORDER_PAINTED_CHANGED_PROPERTY = "borderPainted"; @@ -533,17 +542,20 @@ public abstract class AbstractButton extends JComponent model.addChangeListener(changeListener); model.addItemListener(itemListener); - hori_align = CENTER; - hori_text_pos = TRAILING; - vert_align = CENTER; - vert_text_pos = CENTER; - paint_border = true; - content_area_filled = true; + horizontalAlignment = CENTER; + horizontalTextPosition = TRAILING; + verticalAlignment = CENTER; + verticalTextPosition = CENTER; + borderPainted = true; + contentAreaFilled = true; + + iconTextGap = 4; setAlignmentX(LEFT_ALIGNMENT); setAlignmentY(CENTER_ALIGNMENT); addFocusListener(new ButtonFocusListener()); + setDisplayedMnemonicIndex(-1); } /** @@ -642,6 +654,7 @@ public abstract class AbstractButton extends JComponent */ public void fireItemStateChanged(ItemEvent e) { + e.setSource(this); EventListener[] ll = listenerList.getListeners(ItemListener.class); for (int i = 0; i < ll.length; i++) ((ItemListener)ll[i]).itemStateChanged(e); @@ -655,6 +668,7 @@ public abstract class AbstractButton extends JComponent */ public void fireActionPerformed(ActionEvent e) { + e.setSource(this); EventListener[] ll = listenerList.getListeners(ActionListener.class); for (int i = 0; i < ll.length; i++) ((ActionListener)ll[i]).actionPerformed(e); @@ -671,7 +685,7 @@ public abstract class AbstractButton extends JComponent { EventListener[] ll = listenerList.getListeners(ChangeListener.class); for (int i = 0; i < ll.length; i++) - ((ChangeListener)ll[i]).stateChanged(e); + ((ChangeListener)ll[i]).stateChanged(changeEvent); } /** @@ -699,14 +713,7 @@ public abstract class AbstractButton extends JComponent */ public void setMnemonic(char mne) { - int old = getModel().getMnemonic(); - getModel().setMnemonic(mne); - if (old != getModel().getMnemonic()) - { - firePropertyChange(MNEMONIC_CHANGED_PROPERTY, old, (int) mne); - revalidate(); - repaint(); - } + setMnemonic((int) mne); } /** @@ -720,10 +727,23 @@ public abstract class AbstractButton extends JComponent */ public void setMnemonic(int mne) { - int old = mne; + int old = getModel().getMnemonic(); + + if (old != mne) + { getModel().setMnemonic(mne); - if (old != getModel().getMnemonic()) + + if (text != null && ! text.equals("")) { + // Since lower case char = upper case char for + // mnemonic, we will convert both text and mnemonic + // to upper case before checking if mnemonic character occurs + // in the menu item text. + int upperCaseMne = Character.toUpperCase((char) mne); + String upperCaseText = text.toUpperCase(); + setDisplayedMnemonicIndex(upperCaseText.indexOf(upperCaseMne)); + } + firePropertyChange(MNEMONIC_CHANGED_PROPERTY, old, mne); revalidate(); repaint(); @@ -748,7 +768,7 @@ public abstract class AbstractButton extends JComponent public void setDisplayedMnemonicIndex(int index) { - if (index < -1 || index >= text.length()) + if (index < -1 || (text != null && index >= text.length())) throw new IllegalArgumentException(); else mnemonicIndex = index; @@ -761,7 +781,7 @@ public abstract class AbstractButton extends JComponent * * @return An index into the button's "text" property */ - public int getDisplayedMnemonicIndex(int index) + public int getDisplayedMnemonicIndex() { return mnemonicIndex; } @@ -770,17 +790,16 @@ public abstract class AbstractButton extends JComponent /** * Set the "rolloverEnabled" property. When rollover is enabled, and the * look and feel supports it, the button will change its icon to - * rollover_icon, when the mouse passes over it. + * rolloverIcon, when the mouse passes over it. * * @param r Whether or not to enable rollover icon changes */ public void setRolloverEnabled(boolean r) { - boolean old = getModel().isRollover(); - getModel().setRollover(r); - if (old != getModel().isRollover()) + if (rollOverEnabled != r) { - firePropertyChange(ROLLOVER_ENABLED_CHANGED_PROPERTY, old, r); + rollOverEnabled = r; + firePropertyChange(ROLLOVER_ENABLED_CHANGED_PROPERTY, !r, r); revalidate(); repaint(); } @@ -794,7 +813,7 @@ public abstract class AbstractButton extends JComponent */ public boolean isRolloverEnabled() { - return getModel().isRollover(); + return rollOverEnabled; } /** @@ -842,7 +861,7 @@ public abstract class AbstractButton extends JComponent */ public int getHorizontalAlignment() { - return hori_align; + return horizontalAlignment; } /** @@ -858,8 +877,8 @@ public abstract class AbstractButton extends JComponent */ public void setHorizontalAlignment(int a) { - int old = hori_align; - hori_align = a; + int old = horizontalAlignment; + horizontalAlignment = a; if (old != a) { firePropertyChange(HORIZONTAL_ALIGNMENT_CHANGED_PROPERTY, old, a); @@ -879,7 +898,7 @@ public abstract class AbstractButton extends JComponent */ public int getHorizontalTextPosition() { - return hori_text_pos; + return horizontalTextPosition; } /** @@ -895,8 +914,8 @@ public abstract class AbstractButton extends JComponent */ public void setHorizontalTextPosition(int t) { - int old = hori_text_pos; - hori_text_pos = t; + int old = horizontalTextPosition; + horizontalTextPosition = t; if (old != t) { firePropertyChange(HORIZONTAL_TEXT_POSITION_CHANGED_PROPERTY, old, t); @@ -915,7 +934,7 @@ public abstract class AbstractButton extends JComponent */ public int getVerticalAlignment() { - return vert_align; + return verticalAlignment; } /** @@ -930,8 +949,8 @@ public abstract class AbstractButton extends JComponent */ public void setVerticalAlignment(int a) { - int old = vert_align; - vert_align = a; + int old = verticalAlignment; + verticalAlignment = a; if (old != a) { firePropertyChange(VERTICAL_ALIGNMENT_CHANGED_PROPERTY, old, a); @@ -951,7 +970,7 @@ public abstract class AbstractButton extends JComponent */ public int getVerticalTextPosition() { - return vert_text_pos; + return verticalTextPosition; } /** @@ -967,8 +986,8 @@ public abstract class AbstractButton extends JComponent */ public void setVerticalTextPosition(int t) { - int old = vert_text_pos; - vert_text_pos = t; + int old = verticalTextPosition; + verticalTextPosition = t; if (old != t) { firePropertyChange(VERTICAL_TEXT_POSITION_CHANGED_PROPERTY, old, t); @@ -986,7 +1005,7 @@ public abstract class AbstractButton extends JComponent */ public boolean isBorderPainted() { - return paint_border; + return borderPainted; } /** @@ -998,8 +1017,8 @@ public abstract class AbstractButton extends JComponent */ public void setBorderPainted(boolean b) { - boolean old = paint_border; - paint_border = b; + boolean old = borderPainted; + borderPainted = b; if (b != old) { firePropertyChange(BORDER_PAINTED_CHANGED_PROPERTY, old, b); @@ -1140,6 +1159,33 @@ public abstract class AbstractButton extends JComponent } /** + * Set the value of the {@link #iconTextGap} property. + * + * @param i The new value of the property + */ + public void setIconTextGap(int i) + { + int old = iconTextGap; + iconTextGap = i; + if (old != i) + { + fireStateChanged(new ChangeEvent(this)); + revalidate(); + repaint(); + } + } + + /** + * Get the value of the {@link #iconTextGap} property. + * + * @return The current value of the property + */ + public int getIconTextGap() + { + return iconTextGap; + } + + /** * Return the button's "margin" property, which is an {@link Insets} object * describing the distance between the button's border and its text and * icon. @@ -1214,11 +1260,11 @@ public abstract class AbstractButton extends JComponent */ public Icon getDisabledIcon() { - if (disabled_icon == null + if (disabeldIcon == null && default_icon instanceof ImageIcon) - disabled_icon = new ImageIcon(GrayFilter.createDisabledImage(((ImageIcon) default_icon).getImage())); + disabeldIcon = new ImageIcon(GrayFilter.createDisabledImage(((ImageIcon) default_icon).getImage())); - return disabled_icon; + return disabeldIcon; } /** @@ -1230,9 +1276,9 @@ public abstract class AbstractButton extends JComponent * * @param disabledIcon The new "disabledIcon" property */ - public void setDisabledIcon(Icon disabledIcon) + public void setDisabledIcon(Icon d) { - disabled_icon = disabledIcon; + disabeldIcon = d; revalidate(); repaint(); } @@ -1248,7 +1294,7 @@ public abstract class AbstractButton extends JComponent */ public boolean isFocusPainted() { - return paint_focus; + return focusPainted; } /** @@ -1260,14 +1306,14 @@ public abstract class AbstractButton extends JComponent * * @param b The new "paintFocus" property */ - public void setFocusPainted(boolean b) + public void setFocusPainted(boolean p) { - boolean old = paint_focus; - paint_focus = b; + boolean old = focusPainted; + focusPainted = p; - if (old != b) + if (old != focusPainted) { - firePropertyChange(FOCUS_PAINTED_CHANGED_PROPERTY, old, b); + firePropertyChange(FOCUS_PAINTED_CHANGED_PROPERTY, old, p); revalidate(); repaint(); } @@ -1391,7 +1437,13 @@ public abstract class AbstractButton extends JComponent setToolTipText((String)(a.getValue(Action.SHORT_DESCRIPTION))); if (a.getValue(Action.MNEMONIC_KEY) != null) setMnemonic(((Integer)(a.getValue(Action.MNEMONIC_KEY))).intValue()); + String actionCommand = (String)(a.getValue(Action.ACTION_COMMAND_KEY)); + + // Set actionCommand to button's text by default if it is not specified + if (actionCommand != null) setActionCommand((String)(a.getValue(Action.ACTION_COMMAND_KEY))); + else + setActionCommand(getText()); } } @@ -1416,7 +1468,6 @@ public abstract class AbstractButton extends JComponent { public void actionPerformed(ActionEvent e) { - e.setSource(AbstractButton.this); AbstractButton.this.fireActionPerformed(e); } }; @@ -1489,7 +1540,6 @@ public abstract class AbstractButton extends JComponent public void stateChanged(ChangeEvent e) { AbstractButton.this.fireStateChanged(e); - AbstractButton.this.revalidate(); AbstractButton.this.repaint(); } }; @@ -1569,7 +1619,7 @@ public abstract class AbstractButton extends JComponent */ public Icon getDisabledSelectedIcon() { - return disabled_selected_icon; + return disabledSelectedIcon; } /** @@ -1583,8 +1633,8 @@ public abstract class AbstractButton extends JComponent */ public void setDisabledSelectedIcon(Icon disabledSelectedIcon) { - Icon old = disabled_selected_icon; - disabled_selected_icon = disabledSelectedIcon; + Icon old = disabledSelectedIcon; + disabledSelectedIcon = disabledSelectedIcon; if (old != disabledSelectedIcon) { firePropertyChange(DISABLED_SELECTED_ICON_CHANGED_PROPERTY, old, @@ -1604,7 +1654,7 @@ public abstract class AbstractButton extends JComponent */ public Icon getRolloverIcon() { - return rollover_icon; + return rolloverIcon; } /** @@ -1614,10 +1664,10 @@ public abstract class AbstractButton extends JComponent * * @param rolloverIcon The new rollover icon */ - public void setRolloverIcon(Icon rolloverIcon) + public void setRolloverIcon(Icon r) { - Icon old = rollover_icon; - rollover_icon = rolloverIcon; + Icon old = rolloverIcon; + rolloverIcon = r; if (old != rolloverIcon) { firePropertyChange(ROLLOVER_ICON_CHANGED_PROPERTY, old, @@ -1637,7 +1687,7 @@ public abstract class AbstractButton extends JComponent */ public Icon getRolloverSelectedIcon() { - return rollover_selected_icon; + return rolloverSelectedIcon; } /** @@ -1648,10 +1698,10 @@ public abstract class AbstractButton extends JComponent * * @param rolloverSelectedIcon The new rollover selected icon */ - public void setRolloverSelectedIcon(Icon rolloverSelectedIcon) + public void setRolloverSelectedIcon(Icon r) { - Icon old = rollover_selected_icon; - rollover_selected_icon = rolloverSelectedIcon; + Icon old = rolloverSelectedIcon; + rolloverSelectedIcon = r; if (old != rolloverSelectedIcon) { firePropertyChange(ROLLOVER_SELECTED_ICON_CHANGED_PROPERTY, old, @@ -1673,7 +1723,7 @@ public abstract class AbstractButton extends JComponent */ public Icon getSelectedIcon() { - return selected_icon; + return selectedIcon; } /** @@ -1685,10 +1735,10 @@ public abstract class AbstractButton extends JComponent * * @param selectedIcon The new selected icon */ - public void setSelectedIcon(Icon selectedIcon) + public void setSelectedIcon(Icon s) { - Icon old = selected_icon; - selected_icon = selectedIcon; + Icon old = selectedIcon; + selectedIcon = s; if (old != selectedIcon) { firePropertyChange(SELECTED_ICON_CHANGED_PROPERTY, old, @@ -1750,7 +1800,7 @@ public abstract class AbstractButton extends JComponent */ public boolean isContentAreaFilled() { - return content_area_filled; + return contentAreaFilled; } /** @@ -1764,8 +1814,8 @@ public abstract class AbstractButton extends JComponent */ public void setContentAreaFilled(boolean b) { - boolean old = content_area_filled; - content_area_filled = b; + boolean old = contentAreaFilled; + contentAreaFilled = b; if (b != old) { firePropertyChange(CONTENT_AREA_FILLED_CHANGED_PROPERTY, old, b); |