jdk/src/share/classes/javax/swing/plaf/synth/SynthArrowButton.java
author dav
Mon, 07 Apr 2008 14:53:51 +0400
changeset 438 2ae294e4518c
parent 2 90ce3da70b43
child 715 f16baef3a20e
permissions -rw-r--r--
6613529: Avoid duplicate object creation within JDK packages Summary: avoid using constructors when unique values are not necessary Reviewed-by: volk, igor, peterz
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2002-2006 Sun Microsystems, Inc.  All Rights Reserved.
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
package javax.swing.plaf.synth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.awt.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import javax.swing.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import javax.swing.plaf.UIResource;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * JButton object that draws a scaled Arrow in one of the cardinal directions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * @author Scott Violet
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
class SynthArrowButton extends JButton implements SwingConstants, UIResource {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
    private int direction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    public SynthArrowButton(int direction) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
        super();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
        super.setFocusable(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
        setDirection(direction);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
        setDefaultCapable(false);
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 String getUIClassID() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        return "ArrowButtonUI";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    public void updateUI() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        setUI(new SynthArrowButtonUI());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    public void setDirection(int dir) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        direction = dir;
438
2ae294e4518c 6613529: Avoid duplicate object creation within JDK packages
dav
parents: 2
diff changeset
    56
        putClientProperty("__arrow_direction__", Integer.valueOf(dir));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    public int getDirection() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        return direction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    public void setFocusable(boolean focusable) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    private static class SynthArrowButtonUI extends SynthButtonUI {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        protected void installDefaults(AbstractButton b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            super.installDefaults(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            updateStyle(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        protected void paint(SynthContext context, Graphics g) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            SynthArrowButton button = (SynthArrowButton)context.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                                      getComponent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            context.getPainter().paintArrowButtonForeground(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                context, g, 0, 0, button.getWidth(), button.getHeight(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                button.getDirection());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        void paintBackground(SynthContext context, Graphics g, JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            context.getPainter().paintArrowButtonBackground(context, g, 0, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                                                c.getWidth(), c.getHeight());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        public void paintBorder(SynthContext context, Graphics g, int x,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                                int y, int w, int h) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            context.getPainter().paintArrowButtonBorder(context, g, x, y, w,h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        public Dimension getMinimumSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            return new Dimension(5, 5);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        public Dimension getMaximumSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            return new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        public Dimension getPreferredSize(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            SynthContext context = getContext(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            Dimension dim = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            if (context.getComponent().getName() == "ScrollBar.button") {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                // ScrollBar arrow buttons can be non-square when
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                // the ScrollBar.squareButtons property is set to FALSE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                // and the ScrollBar.buttonSize property is non-null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                dim = (Dimension)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                    context.getStyle().get(context, "ScrollBar.buttonSize");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            if (dim == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                // For all other cases (including Spinner, ComboBox), we will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                // fall back on the single ArrowButton.size value to create
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                // a square return value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                int size =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                    context.getStyle().getInt(context, "ArrowButton.size", 16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                dim = new Dimension(size, size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            context.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            return dim;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
}