src/java.desktop/share/classes/javax/swing/plaf/basic/BasicButtonListener.java
author erikj
Tue, 12 Sep 2017 19:03:39 +0200
changeset 47216 71c04702a3d5
parent 40133 jdk/src/java.desktop/share/classes/javax/swing/plaf/basic/BasicButtonListener.java@ed7ac669ab43
child 47960 ea50ef230511
permissions -rw-r--r--
8187443: Forest Consolidation: Move files to unified layout Reviewed-by: darcy, ihse
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
40133
ed7ac669ab43 7096375: Swing ignores first click after decreasing system's time
aghaisas
parents: 35650
diff changeset
     2
 * Copyright (c) 1997, 2016, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package javax.swing.plaf.basic;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import sun.swing.DefaultLookup;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import sun.swing.UIAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.awt.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.awt.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.beans.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import javax.swing.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import javax.swing.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import javax.swing.plaf.ActionMapUIResource;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import javax.swing.plaf.ButtonUI;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import javax.swing.plaf.ComponentInputMapUIResource;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * Button Listener
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * @author Jeff Dinkins
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * @author Arnaud Weber (keyboard UI support)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
public class BasicButtonListener implements MouseListener, MouseMotionListener,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
                                   FocusListener, ChangeListener, PropertyChangeListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    private long lastPressedTimestamp = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    private boolean shouldDiscardRelease = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     * Populates Buttons actions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    static void loadActionMap(LazyActionMap map) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        map.put(new Actions(Actions.PRESS));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        map.put(new Actions(Actions.RELEASE));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
25761
c408b10ef757 8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents: 23010
diff changeset
    61
    /**
c408b10ef757 8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents: 23010
diff changeset
    62
     * Constructs a new instance of {@code BasicButtonListener}.
c408b10ef757 8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents: 23010
diff changeset
    63
     *
c408b10ef757 8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents: 23010
diff changeset
    64
     * @param b an abstract button
c408b10ef757 8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents: 23010
diff changeset
    65
     */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    public BasicButtonListener(AbstractButton b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    public void propertyChange(PropertyChangeEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        String prop = e.getPropertyName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        if(prop == AbstractButton.MNEMONIC_CHANGED_PROPERTY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            updateMnemonicBinding((AbstractButton)e.getSource());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        else if(prop == AbstractButton.CONTENT_AREA_FILLED_CHANGED_PROPERTY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            checkOpacity((AbstractButton) e.getSource() );
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        else if(prop == AbstractButton.TEXT_CHANGED_PROPERTY ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                "font" == prop || "foreground" == prop) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            AbstractButton b = (AbstractButton) e.getSource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            BasicHTML.updateRenderer(b, b.getText());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
25761
c408b10ef757 8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents: 23010
diff changeset
    84
    /**
c408b10ef757 8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents: 23010
diff changeset
    85
     * Checks the opacity of the {@code AbstractButton}.
c408b10ef757 8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents: 23010
diff changeset
    86
     *
c408b10ef757 8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents: 23010
diff changeset
    87
     * @param b an abstract button
c408b10ef757 8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents: 23010
diff changeset
    88
     */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    protected void checkOpacity(AbstractButton b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        b.setOpaque( b.isContentAreaFilled() );
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * Register default key actions: pressing space to "click" a
25761
c408b10ef757 8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents: 23010
diff changeset
    95
     * button and registering the keyboard mnemonic (if any).
c408b10ef757 8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents: 23010
diff changeset
    96
     *
c408b10ef757 8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents: 23010
diff changeset
    97
     * @param c a component
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    public void installKeyboardActions(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        AbstractButton b = (AbstractButton)c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        // Update the mnemonic binding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        updateMnemonicBinding(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        LazyActionMap.installLazyActionMap(c, BasicButtonListener.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                                           "Button.actionMap");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        InputMap km = getInputMap(JComponent.WHEN_FOCUSED, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        SwingUtilities.replaceUIInputMap(c, JComponent.WHEN_FOCUSED, km);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    /**
25761
c408b10ef757 8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents: 23010
diff changeset
   113
     * Unregister default key actions.
c408b10ef757 8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents: 23010
diff changeset
   114
     *
c408b10ef757 8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents: 23010
diff changeset
   115
     * @param c a component
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    public void uninstallKeyboardActions(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        SwingUtilities.replaceUIInputMap(c, JComponent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                                         WHEN_IN_FOCUSED_WINDOW, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        SwingUtilities.replaceUIInputMap(c, JComponent.WHEN_FOCUSED, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        SwingUtilities.replaceUIActionMap(c, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * Returns the InputMap for condition <code>condition</code>. Called as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * part of <code>installKeyboardActions</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    InputMap getInputMap(int condition, JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        if (condition == JComponent.WHEN_FOCUSED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            BasicButtonUI ui = (BasicButtonUI)BasicLookAndFeel.getUIOfType(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                         ((AbstractButton)c).getUI(), BasicButtonUI.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            if (ui != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                return (InputMap)DefaultLookup.get(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                             c, ui, ui.getPropertyPrefix() + "focusInputMap");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * Resets the binding for the mnemonic in the WHEN_IN_FOCUSED_WINDOW
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * UI InputMap.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    void updateMnemonicBinding(AbstractButton b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        int m = b.getMnemonic();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        if(m != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            InputMap map = SwingUtilities.getUIInputMap(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                                b, JComponent.WHEN_IN_FOCUSED_WINDOW);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            if (map == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                map = new ComponentInputMapUIResource(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                SwingUtilities.replaceUIInputMap(b,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                               JComponent.WHEN_IN_FOCUSED_WINDOW, map);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            map.clear();
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 7668
diff changeset
   156
            map.put(KeyStroke.getKeyStroke(m, BasicLookAndFeel.getFocusAcceleratorKeyMask(), false),
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                    "pressed");
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 7668
diff changeset
   158
            map.put(KeyStroke.getKeyStroke(m, BasicLookAndFeel.getFocusAcceleratorKeyMask(), true),
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                    "released");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            map.put(KeyStroke.getKeyStroke(m, 0, true), "released");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            InputMap map = SwingUtilities.getUIInputMap(b, JComponent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                                             WHEN_IN_FOCUSED_WINDOW);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            if (map != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                map.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    public void stateChanged(ChangeEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        AbstractButton b = (AbstractButton) e.getSource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        b.repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    public void focusGained(FocusEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        AbstractButton b = (AbstractButton) e.getSource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        if (b instanceof JButton && ((JButton)b).isDefaultCapable()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            JRootPane root = b.getRootPane();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            if (root != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
               BasicButtonUI ui = (BasicButtonUI)BasicLookAndFeel.getUIOfType(
1290
da8902cd496c 6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents: 2
diff changeset
   182
                         b.getUI(), BasicButtonUI.class);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
               if (ui != null && DefaultLookup.getBoolean(b, ui,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                                   ui.getPropertyPrefix() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                                   "defaultButtonFollowsFocus", true)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                   root.putClientProperty("temporaryDefaultButton", b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                   root.setDefaultButton((JButton)b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                   root.putClientProperty("temporaryDefaultButton", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
               }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        b.repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    public void focusLost(FocusEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        AbstractButton b = (AbstractButton) e.getSource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        JRootPane root = b.getRootPane();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        if (root != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
           JButton initialDefault = (JButton)root.getClientProperty("initialDefaultButton");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
           if (b != initialDefault) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
               BasicButtonUI ui = (BasicButtonUI)BasicLookAndFeel.getUIOfType(
1290
da8902cd496c 6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents: 2
diff changeset
   202
                         b.getUI(), BasicButtonUI.class);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
               if (ui != null && DefaultLookup.getBoolean(b, ui,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                                   ui.getPropertyPrefix() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                                   "defaultButtonFollowsFocus", true)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                   root.setDefaultButton(initialDefault);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
               }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
           }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        ButtonModel model = b.getModel();
5955
94a5ce4a297b 6711682: JCheckBox in JTable: checkbox doesn't alaways respond to the first mouse click
alexp
parents: 5506
diff changeset
   212
        model.setPressed(false);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        model.setArmed(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        b.repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    public void mouseMoved(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    public void mouseDragged(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    public void mouseClicked(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    public void mousePressed(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
       if (SwingUtilities.isLeftMouseButton(e) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
          AbstractButton b = (AbstractButton) e.getSource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
          if(b.contains(e.getX(), e.getY())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
              long lastTime = lastPressedTimestamp;
40133
ed7ac669ab43 7096375: Swing ignores first click after decreasing system's time
aghaisas
parents: 35650
diff changeset
   233
              lastPressedTimestamp = e.getWhen();
ed7ac669ab43 7096375: Swing ignores first click after decreasing system's time
aghaisas
parents: 35650
diff changeset
   234
              long timeSinceLastClick = lastPressedTimestamp - lastTime;
ed7ac669ab43 7096375: Swing ignores first click after decreasing system's time
aghaisas
parents: 35650
diff changeset
   235
              if (lastTime != -1 &&
ed7ac669ab43 7096375: Swing ignores first click after decreasing system's time
aghaisas
parents: 35650
diff changeset
   236
                  timeSinceLastClick > 0 &&
ed7ac669ab43 7096375: Swing ignores first click after decreasing system's time
aghaisas
parents: 35650
diff changeset
   237
                  timeSinceLastClick < b.getMultiClickThreshhold()) {
ed7ac669ab43 7096375: Swing ignores first click after decreasing system's time
aghaisas
parents: 35650
diff changeset
   238
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                  shouldDiscardRelease = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                  return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
              }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
             ButtonModel model = b.getModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
             if (!model.isEnabled()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                // Disabled buttons ignore all input...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
             }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
             if (!model.isArmed()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                // button not armed, should be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                model.setArmed(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
             }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
             model.setPressed(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
             if(!b.hasFocus() && b.isRequestFocusEnabled()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                b.requestFocus();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
             }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
          }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
       }
1290
da8902cd496c 6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents: 2
diff changeset
   258
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    public void mouseReleased(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        if (SwingUtilities.isLeftMouseButton(e)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            // Support for multiClickThreshhold
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            if (shouldDiscardRelease) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                shouldDiscardRelease = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            AbstractButton b = (AbstractButton) e.getSource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            ButtonModel model = b.getModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            model.setPressed(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            model.setArmed(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        }
1290
da8902cd496c 6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents: 2
diff changeset
   272
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    public void mouseEntered(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        AbstractButton b = (AbstractButton) e.getSource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        ButtonModel model = b.getModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        if (b.isRolloverEnabled() && !SwingUtilities.isLeftMouseButton(e)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            model.setRollover(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        if (model.isPressed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                model.setArmed(true);
1290
da8902cd496c 6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents: 2
diff changeset
   282
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    public void mouseExited(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        AbstractButton b = (AbstractButton) e.getSource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        ButtonModel model = b.getModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        if(b.isRolloverEnabled()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            model.setRollover(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        model.setArmed(false);
1290
da8902cd496c 6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents: 2
diff changeset
   291
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * Actions for Buttons. Two types of action are supported:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * pressed: Moves the button to a pressed state
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * released: Disarms the button.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    private static class Actions extends UIAction {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        private static final String PRESS = "pressed";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        private static final String RELEASE = "released";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        Actions(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            super(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        public void actionPerformed(ActionEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            AbstractButton b = (AbstractButton)e.getSource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            String key = getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            if (key == PRESS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                ButtonModel model = b.getModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                model.setArmed(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                model.setPressed(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                if(!b.hasFocus()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                    b.requestFocus();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            else if (key == RELEASE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                ButtonModel model = b.getModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                model.setPressed(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                model.setArmed(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
35650
15f1f0ff7790 8133039: Provide public API to sun.swing.UIAction#isEnabled(Object)
alexsch
parents: 25859
diff changeset
   325
        @Override
15f1f0ff7790 8133039: Provide public API to sun.swing.UIAction#isEnabled(Object)
alexsch
parents: 25859
diff changeset
   326
        public boolean accept(Object sender) {
15f1f0ff7790 8133039: Provide public API to sun.swing.UIAction#isEnabled(Object)
alexsch
parents: 25859
diff changeset
   327
            return !((sender instanceof AbstractButton) &&
15f1f0ff7790 8133039: Provide public API to sun.swing.UIAction#isEnabled(Object)
alexsch
parents: 25859
diff changeset
   328
                    !((AbstractButton)sender).getModel().isEnabled());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    }
40133
ed7ac669ab43 7096375: Swing ignores first click after decreasing system's time
aghaisas
parents: 35650
diff changeset
   331
}