jdk/src/share/classes/javax/swing/plaf/synth/SynthArrowButton.java
author malenkov
Fri, 10 Sep 2010 20:48:17 +0400
changeset 6500 129126795742
parent 5506 202f599c92aa
child 23697 e556a715949f
permissions -rw-r--r--
6915566: Closed swing tests failing with assert errors when run with -ea -esa Reviewed-by: art, peterz
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2658
diff changeset
     2
 * Copyright (c) 2002, 2008, 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: 2658
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: 2658
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: 2658
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2658
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2658
diff changeset
    23
 * questions.
2
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
            }
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 715
diff changeset
   116
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 715
diff changeset
   117
            // handle scaling for sizeVarients for special case components. The
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 715
diff changeset
   118
            // key "JComponent.sizeVariant" scales for large/small/mini
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 715
diff changeset
   119
            // components are based on Apples LAF
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 715
diff changeset
   120
            Container parent = context.getComponent().getParent();
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 715
diff changeset
   121
            if (parent instanceof JComponent && !(parent instanceof JComboBox)) {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 715
diff changeset
   122
                Object scaleKey = ((JComponent)parent).
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 715
diff changeset
   123
                                    getClientProperty("JComponent.sizeVariant");
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 715
diff changeset
   124
                if (scaleKey != null){
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 715
diff changeset
   125
                    if ("large".equals(scaleKey)){
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 715
diff changeset
   126
                        dim = new Dimension(
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 715
diff changeset
   127
                                (int)(dim.width * 1.15),
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 715
diff changeset
   128
                                (int)(dim.height * 1.15));
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 715
diff changeset
   129
                    } else if ("small".equals(scaleKey)){
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 715
diff changeset
   130
                        dim = new Dimension(
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 715
diff changeset
   131
                                (int)(dim.width * 0.857),
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 715
diff changeset
   132
                                (int)(dim.height * 0.857));
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 715
diff changeset
   133
                    } else if ("mini".equals(scaleKey)){
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 715
diff changeset
   134
                        dim = new Dimension(
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 715
diff changeset
   135
                                (int)(dim.width * 0.714),
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 715
diff changeset
   136
                                (int)(dim.height * 0.714));
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 715
diff changeset
   137
                    }
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 715
diff changeset
   138
                }
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 715
diff changeset
   139
            }
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 715
diff changeset
   140
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            context.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            return dim;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
}