diff options
author | Roman Kennke <roman@kennke.org> | 2006-03-25 00:01:59 +0000 |
---|---|---|
committer | Roman Kennke <roman@kennke.org> | 2006-03-25 00:01:59 +0000 |
commit | 16ebbac53199e8abfbbf4b227f34b723de99dbcc (patch) | |
tree | 839fc73eeeb4ad4d444e3ebbf5023f39a6c36adf | |
parent | 63f5926a4f85a4c5f4c8349a7d5bc985badd503f (diff) | |
download | classpath-16ebbac53199e8abfbbf4b227f34b723de99dbcc.tar.gz |
2006-03-24 Roman Kennke <kennke@aicas.com>
* javax/swing/JButton.java
(def): Replaced field with defaultCapable field.
(is_def): Removed field.
(JButton): Initialize defaultCapable with true.
(isDefaultButton): Documented and implemented method by querying
the button's root pane if present.
(isDefaultCapable): Changed def field to defaultCapable.
Added documentation.
(paramString): Call isDefaultButton() instead of accessing field,
which got removed.
(setDefaultCapable): Changed def field to defaultCapable.
Added documentation.
* javax/swing/JRootPane.java
(setDefaultButton): Only change the default button if the
new button is defaultCapable.
* javax/swing/plaf/basic/BasicRootPaneUI.java
(DefaultPressAction): New class.
(DefaultReleaseAction): New class.
(installKeyboardActions): Implemented.
(uninstallKeyboardActions): Implemented.
(propertyChange): Implemented.
* javax/swing/plaf/metal/MetalBorders.java
(ButtonBorder.paintBorder): 'Outsourced' default theme
painting to paintDefaultButtonBorder().
(ButtonBorder.paintDefaultButtonBorder): New helper method
to paint the border in the default theme. This also fixes
painting of the border for default buttons.
(ButtonBorder.paintOceanButtonBorder): Added support for
default button painting. Fixed border for pressed/default state.
* javax/swing/plaf/metal/MetalButtonUI.java
(update): Only paint gradient when in OceanTheme and when the
button is not armed.
-rw-r--r-- | ChangeLog | 35 | ||||
-rw-r--r-- | javax/swing/JButton.java | 64 | ||||
-rw-r--r-- | javax/swing/JRootPane.java | 15 | ||||
-rw-r--r-- | javax/swing/plaf/basic/BasicRootPaneUI.java | 122 | ||||
-rw-r--r-- | javax/swing/plaf/metal/MetalBorders.java | 92 | ||||
-rw-r--r-- | javax/swing/plaf/metal/MetalButtonUI.java | 8 |
6 files changed, 288 insertions, 48 deletions
@@ -1,3 +1,38 @@ +2006-03-24 Roman Kennke <kennke@aicas.com> + + * javax/swing/JButton.java + (def): Replaced field with defaultCapable field. + (is_def): Removed field. + (JButton): Initialize defaultCapable with true. + (isDefaultButton): Documented and implemented method by querying + the button's root pane if present. + (isDefaultCapable): Changed def field to defaultCapable. + Added documentation. + (paramString): Call isDefaultButton() instead of accessing field, + which got removed. + (setDefaultCapable): Changed def field to defaultCapable. + Added documentation. + * javax/swing/JRootPane.java + (setDefaultButton): Only change the default button if the + new button is defaultCapable. + * javax/swing/plaf/basic/BasicRootPaneUI.java + (DefaultPressAction): New class. + (DefaultReleaseAction): New class. + (installKeyboardActions): Implemented. + (uninstallKeyboardActions): Implemented. + (propertyChange): Implemented. + * javax/swing/plaf/metal/MetalBorders.java + (ButtonBorder.paintBorder): 'Outsourced' default theme + painting to paintDefaultButtonBorder(). + (ButtonBorder.paintDefaultButtonBorder): New helper method + to paint the border in the default theme. This also fixes + painting of the border for default buttons. + (ButtonBorder.paintOceanButtonBorder): Added support for + default button painting. Fixed border for pressed/default state. + * javax/swing/plaf/metal/MetalButtonUI.java + (update): Only paint gradient when in OceanTheme and when the + button is not armed. + 2006-03-24 Audrius Meskauskas <AudriusA@Bioinformatics.org> * gnu/java/rmi/activation/ActivationSystemTransient.java: diff --git a/javax/swing/JButton.java b/javax/swing/JButton.java index b5745a51f..c1340bbc2 100644 --- a/javax/swing/JButton.java +++ b/javax/swing/JButton.java @@ -72,8 +72,11 @@ public class JButton extends AbstractButton } private static final long serialVersionUID = -1907255238954382202L; - boolean def; - boolean is_def; + + /** + * Indicates if this button is capable to become the default button. + */ + private boolean defaultCapable; public JButton() { @@ -101,6 +104,7 @@ public class JButton extends AbstractButton super(); init(text, icon); setModel(new DefaultButtonModel()); + defaultCapable = true; } protected void configurePropertiesFromAction(Action a) @@ -124,18 +128,48 @@ public class JButton extends AbstractButton return "ButtonUI"; } + /** + * Returns <code>true</code> if this button is the default button in + * its <code>JRootPane</code>. The default button gets automatically + * activated when the user presses <code>ENTER</code> (or whatever + * key this is bound to in the current Look and Feel). + * + * @return <code>true</code> if this button is the default button in + * its <code>JRootPane</code> + * + * @see #isDefaultCapable() + * @see #setDefaultCapable() + * @see JRootPane#getDefaultButton() + * @see JRootPane#setDefaultButton(JButton) + */ public boolean isDefaultButton() { - // Returns whether or not this button is the default button on the - // RootPane. - return is_def; + // The default button is managed by the JRootPane, so the safest way + // to determine this property is to ask the root pane of this button, + // if it exists. + JRootPane rp = SwingUtilities.getRootPane(this); + boolean isDefault = false; + if (rp != null) + isDefault = rp.getDefaultButton() == this; + return isDefault; } + /** + * Returns <code>true</code> if this button can act as the default button. + * This is <code>true</code> by default. + * + * @return <code>true</code> if this button can act as the default button + * + * @see #setDefaultCapable() + * @see #isDefaultButton() + * @see JRootPane#getDefaultButton() + * @see JRootPane#setDefaultButton(JButton) + */ public boolean isDefaultCapable() { // Returns whether or not this button is capable of being the default // button on the RootPane. - return def; + return defaultCapable; } protected String paramString() @@ -144,8 +178,8 @@ public class JButton extends AbstractButton // 41 is the maximum number of chars which may be needed. StringBuffer sb = new StringBuffer(41); - sb.append(",defaultButton=").append(is_def); - sb.append(",defaultCapable=").append(def); + sb.append(",defaultButton=").append(isDefaultButton()); + sb.append(",defaultCapable=").append(defaultCapable); return superParam + sb.toString(); } @@ -164,9 +198,21 @@ public class JButton extends AbstractButton super.removeNotify(); } + /** + * Sets the <code>defaultCapable</code> property which indicates if + * this button may become the default button in its <code>JRootPane</code>. + * + * @param defaultCapable <code>true</code> if this button can become the + * default button in its JRootPane, <code>false</code> otherwise + * + * @see #setDefaultCapable() + * @see #isDefaultButton() + * @see JRootPane#getDefaultButton() + * @see JRootPane#setDefaultButton(JButton) + */ public void setDefaultCapable(boolean defaultCapable) { - def = defaultCapable; + this.defaultCapable = defaultCapable; } public void updateUI() diff --git a/javax/swing/JRootPane.java b/javax/swing/JRootPane.java index dec43956c..b99384a32 100644 --- a/javax/swing/JRootPane.java +++ b/javax/swing/JRootPane.java @@ -624,12 +624,15 @@ public class JRootPane extends JComponent implements Accessible public void setDefaultButton(JButton newButton) { - if (defaultButton == newButton) - return; - - JButton oldButton = defaultButton; - defaultButton = newButton; - firePropertyChange("defaultButton", oldButton, newButton); + // We only change the default button if the new button is defaultCapable + // or null and the old and new button are different objects. + if (defaultButton != newButton + && (newButton == null || newButton.isDefaultCapable())) + { + JButton oldButton = defaultButton; + defaultButton = newButton; + firePropertyChange("defaultButton", oldButton, newButton); + } } /** diff --git a/javax/swing/plaf/basic/BasicRootPaneUI.java b/javax/swing/plaf/basic/BasicRootPaneUI.java index 28e3b67c1..933db4c6b 100644 --- a/javax/swing/plaf/basic/BasicRootPaneUI.java +++ b/javax/swing/plaf/basic/BasicRootPaneUI.java @@ -38,17 +38,98 @@ exception statement from your version. */ package javax.swing.plaf.basic; +import java.awt.event.ActionEvent; import java.beans.PropertyChangeEvent; import java.beans.PropertyChangeListener; +import javax.swing.AbstractAction; +import javax.swing.ButtonModel; +import javax.swing.InputMap; +import javax.swing.JButton; import javax.swing.JComponent; import javax.swing.JRootPane; +import javax.swing.LookAndFeel; +import javax.swing.SwingUtilities; +import javax.swing.UIManager; +import javax.swing.plaf.ActionMapUIResource; +import javax.swing.plaf.ComponentInputMapUIResource; import javax.swing.plaf.ComponentUI; import javax.swing.plaf.RootPaneUI; public class BasicRootPaneUI extends RootPaneUI implements PropertyChangeListener { + + /** + * Performed when the user activates the default button inside the JRootPane, + * usually by pressing 'ENTER'. + */ + private class DefaultPressAction + extends AbstractAction + { + /** + * The JRootPane for which this action should be installed. + */ + private JRootPane rootPane; + + /** + * Creates a new DefaultPressAction for the specified JRootPane. + */ + DefaultPressAction(JRootPane rp) + { + rootPane = rp; + } + + /** + * Performes the action. + */ + public void actionPerformed(ActionEvent ev) + { + JButton b = rootPane.getDefaultButton(); + if (b != null) + { + ButtonModel m = b.getModel(); + m.setArmed(true); + m.setPressed(true); + } + } + } + + /** + * Performed when the user activates the default button inside the JRootPane, + * usually by releasing 'ENTER'. + */ + private class DefaultReleaseAction + extends AbstractAction + { + /** + * The JRootPane for which this action should be installed. + */ + private JRootPane rootPane; + + /** + * Creates a new DefaultReleaseAction for the specified JRootPane. + */ + DefaultReleaseAction(JRootPane rp) + { + rootPane = rp; + } + + /** + * Performes the action. + */ + public void actionPerformed(ActionEvent ev) + { + JButton b = rootPane.getDefaultButton(); + if (b != null) + { + ButtonModel m = b.getModel(); + m.setPressed(false); + m.setArmed(false); + } + } + } + public static ComponentUI createUI(JComponent x) { return new BasicRootPaneUI(); @@ -107,14 +188,43 @@ public class BasicRootPaneUI extends RootPaneUI */ protected void installKeyboardActions(JRootPane rp) { - // We currently do not install any keyboard actions here. - // This method is here anyway for compatibility and to provide - // the necessary hooks to subclasses. + // Install the keyboard actions. + ActionMapUIResource am = new ActionMapUIResource(); + am.put("press", new DefaultPressAction(rp)); + am.put("release", new DefaultReleaseAction(rp)); + SwingUtilities.replaceUIActionMap(rp, am); + + // Install the input map from the UIManager. It seems like the actual + // bindings are installed in the JRootPane only when the defaultButton + // property receives a value. So we also only install an empty + // input map here, and fill it in propertyChange. + ComponentInputMapUIResource im = new ComponentInputMapUIResource(rp); + SwingUtilities.replaceUIInputMap(rp, JComponent.WHEN_IN_FOCUSED_WINDOW, + im); } public void propertyChange(PropertyChangeEvent event) { - // TODO: Implement this properly. + JRootPane source = (JRootPane) event.getSource(); + String propertyName = event.getPropertyName(); + if (propertyName.equals("defaultButton")) + { + Object newValue = event.getNewValue(); + InputMap im = + SwingUtilities.getUIInputMap(source, + JComponent.WHEN_IN_FOCUSED_WINDOW); + if (newValue != null) + { + Object[] keybindings = + (Object[]) UIManager.get + ("RootPane.defaultButtonWindowKeyBindings"); + LookAndFeel.loadKeyBindings(im, keybindings); + } + else + { + im.clear(); + } + } } /** @@ -176,6 +286,8 @@ public class BasicRootPaneUI extends RootPaneUI */ protected void uninstallKeyboardActions(JRootPane rp) { - // We do nothing here. + SwingUtilities.replaceUIActionMap(rp, null); + SwingUtilities.replaceUIInputMap(rp, JComponent.WHEN_IN_FOCUSED_WINDOW, + null); } } diff --git a/javax/swing/plaf/metal/MetalBorders.java b/javax/swing/plaf/metal/MetalBorders.java index 77e684127..98a00ee0a 100644 --- a/javax/swing/plaf/metal/MetalBorders.java +++ b/javax/swing/plaf/metal/MetalBorders.java @@ -139,24 +139,59 @@ public class MetalBorders if (MetalLookAndFeel.getCurrentTheme() instanceof OceanTheme) paintOceanButtonBorder(c, g, x, y, w, h); else - { - ButtonModel bmodel = null; - - if (c instanceof AbstractButton) - bmodel = ((AbstractButton) c).getModel(); + paintDefaultButtonBorder(c, g, x, y, w, h); + } - Color darkShadow = MetalLookAndFeel.getControlDarkShadow(); - Color shadow = MetalLookAndFeel.getControlShadow(); - Color light = MetalLookAndFeel.getControlHighlight(); - Color middle = MetalLookAndFeel.getControl(); + /** + * Paints the button border for the DefaultMetalTheme. + * + * @param c the component (button) + * @param g the graphics object to use + * @param x the upper left corner of the component, X coordinate + * @param y the upper left corner of the component, Y coordinate + * @param w the width of the component + * @param h the height of the component + */ + private void paintDefaultButtonBorder(Component c, Graphics g, int x, + int y, int w, int h) + { + ButtonModel bmodel = null; - if (c.isEnabled()) - { - // draw dark border - g.setColor(darkShadow); - g.drawRect(x, y, w - 2, h - 2); + if (c instanceof AbstractButton) + bmodel = ((AbstractButton) c).getModel(); + + Color darkShadow = MetalLookAndFeel.getControlDarkShadow(); + Color shadow = MetalLookAndFeel.getControlShadow(); + Color light = MetalLookAndFeel.getControlHighlight(); + Color middle = MetalLookAndFeel.getControl(); - if (!bmodel.isPressed()) + if (c.isEnabled()) + { + // draw dark border + g.setColor(darkShadow); + g.drawRect(x, y, w - 2, h - 2); + + // If the button is the default button, we paint a special border, + // regardless of the pressed state. + if (c instanceof JButton && ((JButton) c).isDefaultButton()) + { + g.drawRect(x + 1, y + 1, w - 4, h - 4); + // Draw white highlight. + g.setColor(light); + g.drawLine(x + 2, y + 2, x + w - 4, y + 2); + g.drawLine(x + 2, y + 2, x + 2, y + h - 4); + g.drawLine(x + 2, y + h - 1, x + w - 1, y + h - 1); + g.drawLine(x + w - 1, y + 2, x + w - 1, y + h - 1); + // Draw crossing pixels. + g.setColor(middle); + g.fillRect(x + w - 2, y + 2, 1, 1); + g.fillRect(x + 2, y + h - 2, 1, 1); + } + else + { + // The normal border. This is used when the button is not + // pressed or the button is not armed. + if (! (bmodel.isPressed() && bmodel.isArmed()) ) { // draw light border g.setColor(light); @@ -167,6 +202,8 @@ public class MetalBorders g.drawLine(x + 1, y + h - 2, x + 1, y + h - 2); g.drawLine(x + w - 2, y + 1, x + w - 2, y + 1); } + // The pressed border. This border is painted only when + // the button is both pressed and armed. else { // draw light border @@ -185,12 +222,12 @@ public class MetalBorders g.drawRect(x + w - 2, y + 1, 0, 0); } } - else - { - // draw disabled border - g.setColor(MetalLookAndFeel.getInactiveControlTextColor()); - g.drawRect(x, y, w - 2, h - 2); - } + } + else + { + // draw disabled border + g.setColor(MetalLookAndFeel.getInactiveControlTextColor()); + g.drawRect(x, y, w - 2, h - 2); } } @@ -219,11 +256,16 @@ public class MetalBorders if (c.isEnabled()) { - if (bmodel.isPressed()) + // Paint the pressed border if the button is pressed, or if + // the button is the default button. In the OceanTheme, the default + // button has the same border as a pressed button. + if (bmodel.isPressed() || ((c instanceof JButton) + && ((JButton) c).isDefaultButton())) { - // draw fat border - g.drawLine(x + 1, y + 1, x + w - 2, y + 1); - g.drawLine(x + 1, y + 1, x + 1, y + h - 2); + // Draw fat border. + g.setColor(darkShadow); + g.drawRect(x, y, w - 1, h - 1); + g.drawRect(x + 1, y + 1, w - 3, h - 3); } else if (bmodel.isRollover()) { diff --git a/javax/swing/plaf/metal/MetalButtonUI.java b/javax/swing/plaf/metal/MetalButtonUI.java index 485424cda..255669365 100644 --- a/javax/swing/plaf/metal/MetalButtonUI.java +++ b/javax/swing/plaf/metal/MetalButtonUI.java @@ -159,8 +159,8 @@ public class MetalButtonUI } /** - * Paints the background of the button to indicate that it is in the "pressed" - * state. + * Paints the background of the button to indicate that it is in the + * "pressed" state. * * @param g the graphics context. * @param b the button. @@ -234,8 +234,10 @@ public class MetalButtonUI { AbstractButton b = (AbstractButton) c; if (b.isContentAreaFilled() + && MetalLookAndFeel.getCurrentTheme() instanceof OceanTheme && UIManager.get(getPropertyPrefix() + "gradient") != null - && !b.getModel().isPressed() && b.isEnabled()) + && ! b.getModel().isPressed() && ! b.getModel().isArmed() + && b.isEnabled()) { MetalUtils.paintGradient(g, 0, 0, c.getWidth(), c.getHeight(), SwingConstants.VERTICAL, |