jdk/src/java.desktop/share/classes/javax/swing/plaf/synth/SynthArrowButton.java
author alexsch
Fri, 15 Apr 2016 19:15:12 +0400
changeset 37698 4d798c873df0
parent 25859 3317bb8137f4
permissions -rw-r--r--
8132791: No access to SynthContext.getContext() Reviewed-by: serb, ssadetsky
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
23697
e556a715949f 8034169: Fix serial lint warnings in javax.swing
darcy
parents: 5506
diff changeset
     2
 * Copyright (c) 2002, 2014, 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
 */
23697
e556a715949f 8034169: Fix serial lint warnings in javax.swing
darcy
parents: 5506
diff changeset
    36
@SuppressWarnings("serial") // Superclass is not serializable across versions
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
class SynthArrowButton extends JButton implements SwingConstants, UIResource {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    private int direction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    public SynthArrowButton(int direction) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
        super();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
        super.setFocusable(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
        setDirection(direction);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
        setDefaultCapable(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    public String getUIClassID() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        return "ArrowButtonUI";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    public void updateUI() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        setUI(new SynthArrowButtonUI());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    public void setDirection(int dir) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        direction = dir;
438
2ae294e4518c 6613529: Avoid duplicate object creation within JDK packages
dav
parents: 2
diff changeset
    57
        putClientProperty("__arrow_direction__", Integer.valueOf(dir));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    public int getDirection() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        return direction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    public void setFocusable(boolean focusable) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    private static class SynthArrowButtonUI extends SynthButtonUI {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        protected void installDefaults(AbstractButton b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            super.installDefaults(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            updateStyle(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        protected void paint(SynthContext context, Graphics g) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            SynthArrowButton button = (SynthArrowButton)context.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                                      getComponent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            context.getPainter().paintArrowButtonForeground(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                context, g, 0, 0, button.getWidth(), button.getHeight(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                button.getDirection());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        void paintBackground(SynthContext context, Graphics g, JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            context.getPainter().paintArrowButtonBackground(context, g, 0, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                                                c.getWidth(), c.getHeight());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        public void paintBorder(SynthContext context, Graphics g, int x,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                                int y, int w, int h) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            context.getPainter().paintArrowButtonBorder(context, g, x, y, w,h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        public Dimension getMinimumSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            return new Dimension(5, 5);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        public Dimension getMaximumSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            return new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        public Dimension getPreferredSize(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            SynthContext context = getContext(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            Dimension dim = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            if (context.getComponent().getName() == "ScrollBar.button") {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                // ScrollBar arrow buttons can be non-square when
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                // the ScrollBar.squareButtons property is set to FALSE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                // and the ScrollBar.buttonSize property is non-null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                dim = (Dimension)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                    context.getStyle().get(context, "ScrollBar.buttonSize");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            if (dim == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                // For all other cases (including Spinner, ComboBox), we will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                // fall back on the single ArrowButton.size value to create
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                // a square return value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                int size =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                    context.getStyle().getInt(context, "ArrowButton.size", 16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                dim = new Dimension(size, size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            }
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 715
diff changeset
   117
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 715
diff changeset
   118
            // handle scaling for sizeVarients for special case components. The
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 715
diff changeset
   119
            // key "JComponent.sizeVariant" scales for large/small/mini
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 715
diff changeset
   120
            // components are based on Apples LAF
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 715
diff changeset
   121
            Container parent = context.getComponent().getParent();
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 715
diff changeset
   122
            if (parent instanceof JComponent && !(parent instanceof JComboBox)) {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 715
diff changeset
   123
                Object scaleKey = ((JComponent)parent).
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 715
diff changeset
   124
                                    getClientProperty("JComponent.sizeVariant");
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 715
diff changeset
   125
                if (scaleKey != null){
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 715
diff changeset
   126
                    if ("large".equals(scaleKey)){
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 715
diff changeset
   127
                        dim = new Dimension(
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 715
diff changeset
   128
                                (int)(dim.width * 1.15),
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 715
diff changeset
   129
                                (int)(dim.height * 1.15));
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 715
diff changeset
   130
                    } else if ("small".equals(scaleKey)){
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 715
diff changeset
   131
                        dim = new Dimension(
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 715
diff changeset
   132
                                (int)(dim.width * 0.857),
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 715
diff changeset
   133
                                (int)(dim.height * 0.857));
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 715
diff changeset
   134
                    } else if ("mini".equals(scaleKey)){
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 715
diff changeset
   135
                        dim = new Dimension(
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 715
diff changeset
   136
                                (int)(dim.width * 0.714),
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 715
diff changeset
   137
                                (int)(dim.height * 0.714));
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
            }
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 715
diff changeset
   141
2
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
}