1 /*
   2  * Copyright (c) 2002, 2008, 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 class SynthArrowButton extends JButton implements SwingConstants, UIResource {
  37     private int direction;
  38 
  39     public SynthArrowButton(int direction) {
  40         super();
  41         super.setFocusable(false);
  42         setDirection(direction);
  43         setDefaultCapable(false);
  44     }
  45 
  46     public String getUIClassID() {
  47         return "ArrowButtonUI";
  48     }
  49 
  50     public void updateUI() {
  51         setUI(new SynthArrowButtonUI());
  52     }
  53 
  54     public void setDirection(int dir) {
  55         direction = dir;
  56         putClientProperty("__arrow_direction__", Integer.valueOf(dir));
  57         repaint();
  58     }
  59 
  60     public int getDirection() {
  61         return direction;
  62     }
  63 
  64     public void setFocusable(boolean focusable) {}
  65 
  66     private static class SynthArrowButtonUI extends SynthButtonUI {
  67         protected void installDefaults(AbstractButton b) {
  68             super.installDefaults(b);
  69             updateStyle(b);
  70         }
  71 
  72         protected void paint(SynthContext context, Graphics g) {
  73             SynthArrowButton button = (SynthArrowButton)context.
  74                                       getComponent();
  75             context.getPainter().paintArrowButtonForeground(
  76                 context, g, 0, 0, button.getWidth(), button.getHeight(),
  77                 button.getDirection());
  78         }
  79 
  80         void paintBackground(SynthContext context, Graphics g, JComponent c) {
  81             context.getPainter().paintArrowButtonBackground(context, g, 0, 0,
  82                                                 c.getWidth(), c.getHeight());
  83         }
  84 
  85         public void paintBorder(SynthContext context, Graphics g, int x,
  86                                 int y, int w, int h) {
  87             context.getPainter().paintArrowButtonBorder(context, g, x, y, w,h);
  88         }
  89 
  90         public Dimension getMinimumSize() {
  91             return new Dimension(5, 5);
  92         }
  93 
  94         public Dimension getMaximumSize() {
  95             return new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE);
  96         }
  97 
  98         public Dimension getPreferredSize(JComponent c) {
  99             SynthContext context = getContext(c);
 100             Dimension dim = null;
 101             if (context.getComponent().getName() == "ScrollBar.button") {
 102                 // ScrollBar arrow buttons can be non-square when
 103                 // the ScrollBar.squareButtons property is set to FALSE
 104                 // and the ScrollBar.buttonSize property is non-null
 105                 dim = (Dimension)
 106                     context.getStyle().get(context, "ScrollBar.buttonSize");
 107             }
 108             if (dim == null) {
 109                 // For all other cases (including Spinner, ComboBox), we will
 110                 // fall back on the single ArrowButton.size value to create
 111                 // a square return value
 112                 int size =
 113                     context.getStyle().getInt(context, "ArrowButton.size", 16);
 114                 dim = new Dimension(size, size);
 115             }
 116 
 117             // handle scaling for sizeVarients for special case components. The
 118             // key "JComponent.sizeVariant" scales for large/small/mini
 119             // components are based on Apples LAF
 120             Container parent = context.getComponent().getParent();
 121             if (parent instanceof JComponent && !(parent instanceof JComboBox)) {
 122                 Object scaleKey = ((JComponent)parent).
 123                                     getClientProperty("JComponent.sizeVariant");
 124                 if (scaleKey != null){
 125                     if ("large".equals(scaleKey)){
 126                         dim = new Dimension(
 127                                 (int)(dim.width * 1.15),
 128                                 (int)(dim.height * 1.15));
 129                     } else if ("small".equals(scaleKey)){
 130                         dim = new Dimension(
 131                                 (int)(dim.width * 0.857),
 132                                 (int)(dim.height * 0.857));
 133                     } else if ("mini".equals(scaleKey)){
 134                         dim = new Dimension(
 135                                 (int)(dim.width * 0.714),
 136                                 (int)(dim.height * 0.714));
 137                     }
 138                 }
 139             }
 140 
 141             context.dispose();
 142             return dim;
 143         }
 144     }
 145 }