1 /*
   2  * Copyright (c) 1997, 1998, 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 
  26 package com.sun.java.swing.plaf.motif;
  27 
  28 import javax.swing.*;
  29 import javax.swing.event.*;
  30 import javax.swing.plaf.*;
  31 import javax.swing.plaf.basic.BasicArrowButton;
  32 
  33 import java.awt.*;
  34 import java.awt.event.*;
  35 
  36 /**
  37  * Motif scroll bar button.
  38  * <p>
  39  * <strong>Warning:</strong>
  40  * Serialized objects of this class will not be compatible with
  41  * future Swing releases.  The current serialization support is appropriate
  42  * for short term storage or RMI between applications running the same
  43  * version of Swing.  A future release of Swing will provide support for
  44  * long term persistence.
  45  */
  46 public class MotifScrollBarButton extends BasicArrowButton
  47 {
  48     private Color darkShadow = UIManager.getColor("controlShadow");
  49     private Color lightShadow = UIManager.getColor("controlLtHighlight");
  50 
  51 
  52     public MotifScrollBarButton(int direction)
  53     {
  54         super(direction);
  55 
  56         switch (direction) {
  57         case NORTH:
  58         case SOUTH:
  59         case EAST:
  60         case WEST:
  61             this.direction = direction;
  62             break;
  63         default:
  64             throw new IllegalArgumentException("invalid direction");
  65         }
  66 
  67         setRequestFocusEnabled(false);
  68         setOpaque(true);
  69         setBackground(UIManager.getColor("ScrollBar.background"));
  70         setForeground(UIManager.getColor("ScrollBar.foreground"));
  71     }
  72 
  73 
  74     public Dimension getPreferredSize() {
  75         switch (direction) {
  76         case NORTH:
  77         case SOUTH:
  78             return new Dimension(11, 12);
  79         case EAST:
  80         case WEST:
  81         default:
  82             return new Dimension(12, 11);
  83         }
  84     }
  85 
  86     public Dimension getMinimumSize() {
  87         return getPreferredSize();
  88     }
  89 
  90     public Dimension getMaximumSize() {
  91         return getPreferredSize();
  92     }
  93 
  94     public boolean isFocusTraversable() {
  95         return false;
  96     }
  97 
  98     public void paint(Graphics g)
  99     {
 100         int w = getWidth();
 101         int h = getHeight();
 102 
 103         if (isOpaque()) {
 104             g.setColor(getBackground());
 105             g.fillRect(0, 0, w, h);
 106         }
 107 
 108         boolean isPressed = getModel().isPressed();
 109         Color lead = (isPressed) ? darkShadow : lightShadow;
 110         Color trail = (isPressed) ? lightShadow : darkShadow;
 111         Color fill = getBackground();
 112 
 113         int cx = w / 2;
 114         int cy = h / 2;
 115         int s = Math.min(w, h);
 116 
 117         switch (direction) {
 118         case NORTH:
 119             g.setColor(lead);
 120             g.drawLine(cx, 0, cx, 0);
 121             for (int x = cx - 1, y = 1, dx = 1; y <= s - 2; y += 2) {
 122                 g.setColor(lead);
 123                 g.drawLine(x, y, x, y);
 124                 if (y >= (s - 2)) {
 125                     g.drawLine(x, y + 1, x, y + 1);
 126                 }
 127                 g.setColor(fill);
 128                 g.drawLine(x + 1, y, x + dx, y);
 129                 if (y < (s - 2)) {
 130                     g.drawLine(x, y + 1, x + dx + 1, y + 1);
 131                 }
 132                 g.setColor(trail);
 133                 g.drawLine(x + dx + 1, y, x + dx + 1, y);
 134                 if (y >= (s - 2)) {
 135                     g.drawLine(x + 1, y + 1, x + dx + 1, y + 1);
 136                 }
 137                 dx += 2;
 138                 x -= 1;
 139             }
 140             break;
 141 
 142         case SOUTH:
 143             g.setColor(trail);
 144             g.drawLine(cx, s, cx, s);
 145             for (int x = cx - 1, y = s - 1, dx = 1; y >= 1; y -= 2) {
 146                 g.setColor(lead);
 147                 g.drawLine(x, y, x, y);
 148                 if (y <= 2) {
 149                     g.drawLine(x, y - 1, x + dx + 1, y - 1);
 150                 }
 151                 g.setColor(fill);
 152                 g.drawLine(x + 1, y, x + dx, y);
 153                 if (y > 2) {
 154                     g.drawLine(x, y - 1, x + dx + 1, y - 1);
 155                 }
 156                 g.setColor(trail);
 157                 g.drawLine(x + dx + 1, y, x + dx + 1, y);
 158 
 159                 dx += 2;
 160                 x -= 1;
 161             }
 162             break;
 163 
 164         case EAST:
 165             g.setColor(lead);
 166             g.drawLine(s, cy, s, cy);
 167             for (int y = cy - 1, x = s - 1, dy = 1; x >= 1; x -= 2) {
 168                 g.setColor(lead);
 169                 g.drawLine(x, y, x, y);
 170                 if (x <= 2) {
 171                     g.drawLine(x - 1, y, x - 1, y + dy + 1);
 172                 }
 173                 g.setColor(fill);
 174                 g.drawLine(x, y + 1, x, y + dy);
 175                 if (x > 2) {
 176                     g.drawLine(x - 1, y, x - 1, y + dy + 1);
 177                 }
 178                 g.setColor(trail);
 179                 g.drawLine(x, y + dy + 1, x, y + dy + 1);
 180 
 181                 dy += 2;
 182                 y -= 1;
 183             }
 184             break;
 185 
 186         case WEST:
 187             g.setColor(trail);
 188             g.drawLine(0, cy, 0, cy);
 189             for (int y = cy - 1, x = 1, dy = 1; x <= s - 2; x += 2) {
 190                 g.setColor(lead);
 191                 g.drawLine(x, y, x, y);
 192                 if (x >= (s - 2)) {
 193                     g.drawLine(x + 1, y, x + 1, y);
 194                 }
 195                 g.setColor(fill);
 196                 g.drawLine(x, y + 1, x, y + dy);
 197                 if (x < (s - 2)) {
 198                     g.drawLine(x + 1, y, x + 1, y + dy + 1);
 199                 }
 200                 g.setColor(trail);
 201                 g.drawLine(x, y + dy + 1, x, y + dy + 1);
 202                 if (x >= (s - 2)) {
 203                     g.drawLine(x + 1, y + 1, x + 1, y + dy + 1);
 204                 }
 205                 dy += 2;
 206                 y -= 1;
 207             }
 208             break;
 209         }
 210     }
 211 }