001: /*
002: * Copyright 2002-2006 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package sun.awt.X11;
027:
028: import java.awt.*;
029: import java.awt.event.*;
030: import java.awt.peer.*;
031: import java.util.logging.*;
032:
033: class XScrollbarPeer extends XComponentPeer implements ScrollbarPeer,
034: XScrollbarClient {
035: private final static Logger log = Logger
036: .getLogger("sun.awt.X11.XScrollbarPeer");
037:
038: private static final int DEFAULT_LENGTH = 50;
039: private static final int DEFAULT_WIDTH_SOLARIS = 19;
040: private static final int DEFAULT_WIDTH_LINUX;
041:
042: XScrollbar tsb;
043:
044: static {
045: DEFAULT_WIDTH_LINUX = XToolkit.getUIDefaults().getInt(
046: "ScrollBar.defaultWidth");
047: }
048:
049: public void preInit(XCreateWindowParams params) {
050: super .preInit(params);
051: Scrollbar target = (Scrollbar) this .target;
052: if (target.getOrientation() == Scrollbar.VERTICAL) {
053: tsb = new XVerticalScrollbar(this );
054: } else {
055: tsb = new XHorizontalScrollbar(this );
056: }
057: int min = target.getMinimum();
058: int max = target.getMaximum();
059: int vis = target.getVisibleAmount();
060: int val = target.getValue();
061: int line = target.getLineIncrement();
062: int page = target.getPageIncrement();
063: tsb.setValues(val, vis, min, max, line, page);
064: }
065:
066: /**
067: * Create a scrollbar.
068: */
069: XScrollbarPeer(Scrollbar target) {
070: super (target);
071: this .target = target;
072: xSetVisible(true);
073: }
074:
075: /**
076: * Returns default size of scrollbar on the platform
077: * Currently uses hardcoded values
078: */
079: private int getDefaultDimension() {
080: if (System.getProperty("os.name").equals("Linux")) {
081: return DEFAULT_WIDTH_LINUX;
082: } else {
083: return DEFAULT_WIDTH_SOLARIS;
084: }
085: }
086:
087: /**
088: * Compute the minimum size for the scrollbar.
089: */
090: public Dimension getMinimumSize() {
091: Scrollbar sb = (Scrollbar) target;
092: return (sb.getOrientation() == Scrollbar.VERTICAL) ? new Dimension(
093: getDefaultDimension(), DEFAULT_LENGTH)
094: : new Dimension(DEFAULT_LENGTH, getDefaultDimension());
095: }
096:
097: public void repaint() {
098: Graphics g = getGraphics();
099: if (g != null)
100: paint(g);
101: }
102:
103: /**
104: * Paint the scrollbar.
105: */
106: public void paint(Graphics g) {
107: Scrollbar sb = (Scrollbar) target;
108: Color colors[] = getGUIcolors();
109: g.setColor(colors[BACKGROUND_COLOR]);
110: tsb.paint(g, colors, true);
111: // paint the whole scrollbar
112: }
113:
114: public void repaintScrollbarRequest(XScrollbar sb) {
115: repaint();
116: }
117:
118: /**
119: * The value has changed.
120: */
121: public void notifyValue(XScrollbar obj, int type, int value,
122: boolean isAdjusting) {
123: Scrollbar sb = (Scrollbar) target;
124: sb.setValue(value);
125: postEvent(new AdjustmentEvent(sb,
126: AdjustmentEvent.ADJUSTMENT_VALUE_CHANGED, type, value,
127: isAdjusting));
128: }
129:
130: /**
131: *
132: * @see java.awt.event.MouseEvent
133: * MouseEvent.MOUSE_CLICKED
134: * MouseEvent.MOUSE_PRESSED
135: * MouseEvent.MOUSE_RELEASED
136: * MouseEvent.MOUSE_MOVED
137: * MouseEvent.MOUSE_ENTERED
138: * MouseEvent.MOUSE_EXITED
139: * MouseEvent.MOUSE_DRAGGED
140: */
141: public void handleJavaMouseEvent(MouseEvent mouseEvent) {
142: super .handleJavaMouseEvent(mouseEvent);
143:
144: int x = mouseEvent.getX();
145: int y = mouseEvent.getY();
146: int modifiers = mouseEvent.getModifiers();
147: int id = mouseEvent.getID();
148:
149: if ((mouseEvent.getModifiers() & InputEvent.BUTTON1_MASK) == 0) {
150: return;
151: }
152:
153: switch (mouseEvent.getID()) {
154: case MouseEvent.MOUSE_PRESSED:
155: target.requestFocus();
156: tsb.handleMouseEvent(id, modifiers, x, y);
157: break;
158:
159: case MouseEvent.MOUSE_RELEASED:
160: tsb.handleMouseEvent(id, modifiers, x, y);
161: break;
162:
163: case MouseEvent.MOUSE_DRAGGED:
164: tsb.handleMouseEvent(id, modifiers, x, y);
165: break;
166: }
167: }
168:
169: public void handleJavaKeyEvent(KeyEvent event) {
170: super .handleJavaKeyEvent(event);
171: if (log.isLoggable(Level.FINEST))
172: log.finer("KeyEvent on scrollbar: " + event);
173: if (!(event.isConsumed())
174: && event.getID() == KeyEvent.KEY_RELEASED) {
175: switch (event.getKeyCode()) {
176: case KeyEvent.VK_UP:
177: log.finer("Scrolling up");
178: tsb
179: .notifyValue(tsb.getValue()
180: - tsb.getUnitIncrement());
181: break;
182: case KeyEvent.VK_DOWN:
183: log.finer("Scrolling down");
184: tsb
185: .notifyValue(tsb.getValue()
186: + tsb.getUnitIncrement());
187: break;
188: case KeyEvent.VK_LEFT:
189: log.finer("Scrolling up");
190: tsb
191: .notifyValue(tsb.getValue()
192: - tsb.getUnitIncrement());
193: break;
194: case KeyEvent.VK_RIGHT:
195: log.finer("Scrolling down");
196: tsb
197: .notifyValue(tsb.getValue()
198: + tsb.getUnitIncrement());
199: break;
200: case KeyEvent.VK_PAGE_UP:
201: log.finer("Scrolling page up");
202: tsb.notifyValue(tsb.getValue()
203: - tsb.getBlockIncrement());
204: break;
205: case KeyEvent.VK_PAGE_DOWN:
206: log.finer("Scrolling page down");
207: tsb.notifyValue(tsb.getValue()
208: + tsb.getBlockIncrement());
209: break;
210: case KeyEvent.VK_HOME:
211: log.finer("Scrolling to home");
212: tsb.notifyValue(0);
213: break;
214: case KeyEvent.VK_END:
215: log.finer("Scrolling to end");
216: tsb.notifyValue(tsb.getMaximum());
217: break;
218: }
219: }
220: }
221:
222: public void setValue(int value) {
223: tsb.setValue(value);
224: repaint();
225: }
226:
227: public void setValues(int value, int visible, int minimum,
228: int maximum) {
229:
230: tsb.setValues(value, visible, minimum, maximum);
231: repaint();
232: }
233:
234: public void setLineIncrement(int l) {
235: tsb.setUnitIncrement(l);
236: }
237:
238: public void setPageIncrement(int p) {
239: tsb.setBlockIncrement(p);
240: }
241:
242: public void layout() {
243: super.layout();
244: tsb.setSize(width, height);
245: }
246: }
|