1 /*
   2  * Copyright (c) 2002, 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package javax.swing.plaf.synth;
  26 
  27 import java.awt.*;
  28 import javax.swing.*;
  29 import javax.swing.plaf.UIResource;
  30 
  31 /**
  32  * JButton object that draws a scaled Arrow in one of the cardinal directions.
  33  *
  34  * @author Scott Violet
  35  */
  36 @SuppressWarnings("serial") // Superclass is not serializable across versions
  37 class SynthArrowButton extends JButton implements SwingConstants, UIResource {
  38     private int direction;
  39 
  40     public SynthArrowButton(int direction) {
  41         super();
  42         super.setFocusable(false);
  43         setDirection(direction);
  44         setDefaultCapable(false);
  45     }
  46 
  47     public String getUIClassID() {
  48         return "ArrowButtonUI";
  49     }
  50 
  51     public void updateUI() {
  52         setUI(new SynthArrowButtonUI());
  53     }
  54 
  55     public void setDirection(int dir) {
  56         direction = dir;
  57         putClientProperty("__arrow_direction__", Integer.valueOf(dir));
  58         repaint();
  59     }
  60 
  61     public int getDirection() {
  62         return direction;
  63     }
  64 
  65     public void setFocusable(boolean focusable) {}
  66 
  67     private static class SynthArrowButtonUI extends SynthButtonUI {
  68         protected void installDefaults(AbstractButton b) {
  69             super.installDefaults(b);
  70             updateStyle(b);
  71         }
  72 
  73         protected void paint(SynthContext context, Graphics g) {
  74             SynthArrowButton button = (SynthArrowButton)context.
  75                                       getComponent();
  76             context.getPainter().paintArrowButtonForeground(
  77                 context, g, 0, 0, button.getWidth(), button.getHeight(),
  78                 button.getDirection());
  79         }
  80 
  81         void paintBackground(SynthContext context, Graphics g, JComponent c) {
  82             context.getPainter().paintArrowButtonBackground(context, g, 0, 0,
  83                                                 c.getWidth(), c.getHeight());
  84         }
  85 
  86         public void paintBorder(SynthContext context, Graphics g, int x,
  87                                 int y, int w, int h) {
  88             context.getPainter().paintArrowButtonBorder(context, g, x, y, w,h);
  89         }
  90 
  91         public Dimension getMinimumSize() {
  92             return new Dimension(5, 5);
  93         }
  94 
  95         public Dimension getMaximumSize() {
  96             return new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE);
  97         }
  98 
  99         public Dimension getPreferredSize(JComponent c) {
 100             SynthContext context = getContext(c);
 101             Dimension dim = null;
 102             if (context.getComponent().getName() == "ScrollBar.button") {
 103                 // ScrollBar arrow buttons can be non-square when
 104                 // the ScrollBar.squareButtons property is set to FALSE
 105                 // and the ScrollBar.buttonSize property is non-null
 106                 dim = (Dimension)
 107                     context.getStyle().get(context, "ScrollBar.buttonSize");
 108             }
 109             if (dim == null) {
 110                 // For all other cases (including Spinner, ComboBox), we will
 111                 // fall back on the single ArrowButton.size value to create
 112                 // a square return value
 113                 int size =
 114                     context.getStyle().getInt(context, "ArrowButton.size", 16);
 115                 dim = new Dimension(size, size);
 116             }
 117 
 118             // handle scaling for sizeVarients for special case components. The
 119             // key "JComponent.sizeVariant" scales for large/small/mini
 120             // components are based on Apples LAF
 121             Container parent = context.getComponent().getParent();
 122             if (parent instanceof JComponent && !(parent instanceof JComboBox)) {
 123                 Object scaleKey = ((JComponent)parent).
 124                                     getClientProperty("JComponent.sizeVariant");
 125                 if (scaleKey != null){
 126                     if ("large".equals(scaleKey)){
 127                         dim = new Dimension(
 128                                 (int)(dim.width * 1.15),
 129                                 (int)(dim.height * 1.15));
 130                     } else if ("small".equals(scaleKey)){
 131                         dim = new Dimension(
 132                                 (int)(dim.width * 0.857),
 133                                 (int)(dim.height * 0.857));
 134                     } else if ("mini".equals(scaleKey)){
 135                         dim = new Dimension(
 136                                 (int)(dim.width * 0.714),
 137                                 (int)(dim.height * 0.714));
 138                     }
 139                 }
 140             }
 141 
 142             context.dispose();
 143             return dim;
 144         }
 145     }
 146 }