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 
26 package sun.awt.X11;
27 
28 import java.awt.*;
29 import java.awt.event.*;
30 import java.awt.peer.*;
31 import sun.util.logging.PlatformLogger;
32 
33 class XScrollbarPeer extends XComponentPeer implements ScrollbarPeer, XScrollbarClient {
34     private static final PlatformLogger log = PlatformLogger.getLogger("sun.awt.X11.XScrollbarPeer");
35 
36     private static final int DEFAULT_LENGTH = 50;
37     private static final int DEFAULT_WIDTH_SOLARIS = 19;
38     private static final int DEFAULT_WIDTH_LINUX;
39 
40     XScrollbar tsb;
41 
42     static {
43         DEFAULT_WIDTH_LINUX = XToolkit.getUIDefaults().getInt("ScrollBar.defaultWidth");
44     }
45 
46     @SuppressWarnings("deprecation")
preInit(XCreateWindowParams params)47     public void preInit(XCreateWindowParams params) {
48         super.preInit(params);
49         Scrollbar target = (Scrollbar) this.target;
50         if (target.getOrientation() == Scrollbar.VERTICAL) {
51             tsb = new XVerticalScrollbar(this);
52         } else {
53             tsb = new XHorizontalScrollbar(this);
54         }
55         int min = target.getMinimum();
56         int max = target.getMaximum();
57         int vis = target.getVisibleAmount();
58         int val = target.getValue();
59         int line = target.getLineIncrement();
60         int page = target.getPageIncrement();
61         tsb.setValues(val, vis, min, max, line, page);
62     }
63 
64     /**
65      * Create a scrollbar.
66      */
XScrollbarPeer(Scrollbar target)67     XScrollbarPeer(Scrollbar target) {
68         super(target);
69         this.target = target;
70         xSetVisible(true);
71     }
72 
73     /**
74      * Returns default size of scrollbar on the platform
75      * Currently uses hardcoded values
76      */
getDefaultDimension()77     private int getDefaultDimension() {
78         if (System.getProperty("os.name").equals("Linux")) {
79             return DEFAULT_WIDTH_LINUX;
80         } else {
81             return DEFAULT_WIDTH_SOLARIS;
82         }
83     }
84 
85     /**
86      * Compute the minimum size for the scrollbar.
87      */
getMinimumSize()88     public Dimension getMinimumSize() {
89         Scrollbar sb = (Scrollbar)target;
90         return (sb.getOrientation() == Scrollbar.VERTICAL)
91             ? new Dimension(getDefaultDimension(), DEFAULT_LENGTH)
92                 : new Dimension(DEFAULT_LENGTH, getDefaultDimension());
93     }
94     /**
95      * Paint the scrollbar.
96      */
97     @Override
paintPeer(final Graphics g)98     void paintPeer(final Graphics g) {
99         final Color[] colors = getGUIcolors();
100         g.setColor(colors[BACKGROUND_COLOR]);
101         tsb.paint(g, colors, true);
102         // paint the whole scrollbar
103     }
104 
repaintScrollbarRequest(XScrollbar sb)105     public void repaintScrollbarRequest(XScrollbar sb) {
106      repaint();
107     }
108 
109     /**
110      * The value has changed.
111      */
notifyValue(XScrollbar obj, int type, int value, boolean isAdjusting)112     public void notifyValue(XScrollbar obj, int type, int value, boolean isAdjusting) {
113         Scrollbar sb = (Scrollbar)target;
114         sb.setValue(value);
115         postEvent( new AdjustmentEvent(sb, AdjustmentEvent.ADJUSTMENT_VALUE_CHANGED, type, value, isAdjusting));
116     }
117 
118     /**
119      *
120      * @see java.awt.event.MouseEvent
121      * MouseEvent.MOUSE_CLICKED
122      * MouseEvent.MOUSE_PRESSED
123      * MouseEvent.MOUSE_RELEASED
124      * MouseEvent.MOUSE_MOVED
125      * MouseEvent.MOUSE_ENTERED
126      * MouseEvent.MOUSE_EXITED
127      * MouseEvent.MOUSE_DRAGGED
128      */
129     @SuppressWarnings("deprecation")
handleJavaMouseEvent( MouseEvent mouseEvent )130     public void handleJavaMouseEvent( MouseEvent mouseEvent ) {
131         super.handleJavaMouseEvent(mouseEvent);
132 
133         int x = mouseEvent.getX();
134         int y = mouseEvent.getY();
135         int modifiers = mouseEvent.getModifiers();
136         int id = mouseEvent.getID();
137 
138 
139         if ((mouseEvent.getModifiers() & InputEvent.BUTTON1_MASK) == 0) {
140             return;
141         }
142 
143         switch (mouseEvent.getID()) {
144           case MouseEvent.MOUSE_PRESSED:
145               target.requestFocus();
146               tsb.handleMouseEvent(id, modifiers,x,y);
147               break;
148 
149           case MouseEvent.MOUSE_RELEASED:
150               tsb.handleMouseEvent(id, modifiers,x,y);
151               break;
152 
153           case MouseEvent.MOUSE_DRAGGED:
154               tsb.handleMouseEvent(id, modifiers,x,y);
155               break;
156         }
157     }
158 
handleJavaKeyEvent(KeyEvent event)159     public void handleJavaKeyEvent(KeyEvent event) {
160         super.handleJavaKeyEvent(event);
161         if (log.isLoggable(PlatformLogger.Level.FINER)) {
162             log.finer("KeyEvent on scrollbar: " + event);
163         }
164         if (!(event.isConsumed()) && event.getID() == KeyEvent.KEY_RELEASED) {
165             switch(event.getKeyCode()) {
166             case KeyEvent.VK_UP:
167                 log.finer("Scrolling up");
168                 tsb.notifyValue(tsb.getValue() - tsb.getUnitIncrement());
169                 break;
170             case KeyEvent.VK_DOWN:
171                 log.finer("Scrolling down");
172                 tsb.notifyValue(tsb.getValue() + tsb.getUnitIncrement());
173                 break;
174             case KeyEvent.VK_LEFT:
175                 log.finer("Scrolling up");
176                 tsb.notifyValue(tsb.getValue() - tsb.getUnitIncrement());
177                 break;
178             case KeyEvent.VK_RIGHT:
179                 log.finer("Scrolling down");
180                 tsb.notifyValue(tsb.getValue() + tsb.getUnitIncrement());
181                 break;
182             case KeyEvent.VK_PAGE_UP:
183                 log.finer("Scrolling page up");
184                 tsb.notifyValue(tsb.getValue() - tsb.getBlockIncrement());
185                 break;
186             case KeyEvent.VK_PAGE_DOWN:
187                 log.finer("Scrolling page down");
188                 tsb.notifyValue(tsb.getValue() + tsb.getBlockIncrement());
189                 break;
190             case KeyEvent.VK_HOME:
191                 log.finer("Scrolling to home");
192                 tsb.notifyValue(0);
193                 break;
194             case KeyEvent.VK_END:
195                 log.finer("Scrolling to end");
196                 tsb.notifyValue(tsb.getMaximum());
197                 break;
198             }
199         }
200     }
201 
setValue(int value)202     public void setValue(int value) {
203         tsb.setValue(value);
204         repaint();
205     }
206 
setValues(int value, int visible, int minimum, int maximum)207     public void setValues(int value, int visible, int minimum, int maximum) {
208 
209         tsb.setValues(value, visible, minimum, maximum);
210         repaint();
211     }
212 
setLineIncrement(int l)213     public void setLineIncrement(int l) {
214         tsb.setUnitIncrement(l);
215     }
216 
setPageIncrement(int p)217     public void setPageIncrement(int p) {
218         tsb.setBlockIncrement(p);
219     }
220 
layout()221     public void layout() {
222         super.layout();
223         tsb.setSize(width, height);
224     }
225 }
226