001: /*
002: * Copyright 1995-2002 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: package sun.awt.motif;
026:
027: import java.awt.*;
028: import java.awt.peer.*;
029: import java.awt.event.AdjustmentEvent;
030:
031: class MScrollbarPeer extends MComponentPeer implements ScrollbarPeer {
032: static {
033: initIDs();
034: }
035:
036: private boolean inUpCall = false;
037:
038: native void create(MComponentPeer parent);
039:
040: MScrollbarPeer(Scrollbar target) {
041: super (target);
042: }
043:
044: // Initialize JNI field and method IDs
045: private static native void initIDs();
046:
047: public native void pSetValues(int value, int visible, int minimum,
048: int maximum);
049:
050: public native void setLineIncrement(int l);
051:
052: public native void setPageIncrement(int l);
053:
054: /**
055: * Returns default size of Motif scrollbar on the platform
056: * Currently uses hardcoded values
057: */
058: int getDefaultDimension() {
059: if (System.getProperty("os.name").equals("Linux")) {
060: return 15;
061: } else {
062: return 19;
063: }
064: }
065:
066: public Dimension getMinimumSize() {
067: if (((Scrollbar) target).getOrientation() == Scrollbar.VERTICAL) {
068: return new Dimension(getDefaultDimension(), 50);
069: } else {
070: return new Dimension(50, getDefaultDimension());
071: }
072: }
073:
074: // NOTE: Callback methods are called by privileged threads.
075: // DO NOT INVOKE CLIENT CODE ON THIS THREAD!
076:
077: private void postAdjustmentEvent(final int type, final int value,
078: final boolean isAdjusting) {
079: final Scrollbar sb = (Scrollbar) target;
080: MToolkit.executeOnEventHandlerThread(sb, new Runnable() {
081: public void run() {
082: inUpCall = true;
083: sb.setValueIsAdjusting(isAdjusting);
084: sb.setValue(value);
085: postEvent(new AdjustmentEvent(sb,
086: AdjustmentEvent.ADJUSTMENT_VALUE_CHANGED, type,
087: value, isAdjusting));
088: inUpCall = false;
089: }
090: });
091: }
092:
093: void lineUp(int value) {
094: postAdjustmentEvent(AdjustmentEvent.UNIT_DECREMENT, value,
095: false);
096: }
097:
098: void lineDown(int value) {
099: postAdjustmentEvent(AdjustmentEvent.UNIT_INCREMENT, value,
100: false);
101: }
102:
103: void pageUp(int value) {
104: postAdjustmentEvent(AdjustmentEvent.BLOCK_DECREMENT, value,
105: false);
106: }
107:
108: void pageDown(int value) {
109: postAdjustmentEvent(AdjustmentEvent.BLOCK_INCREMENT, value,
110: false);
111: }
112:
113: // SB_TOP/BOTTOM are mapped to tracking
114: void warp(int value) {
115: postAdjustmentEvent(AdjustmentEvent.TRACK, value, false);
116: }
117:
118: private boolean dragInProgress = false;
119:
120: void drag(final int value) {
121: if (!dragInProgress) {
122: dragInProgress = true;
123: }
124: postAdjustmentEvent(AdjustmentEvent.TRACK, value, true);
125: }
126:
127: void dragEnd(final int value) {
128: final Scrollbar sb = (Scrollbar) target;
129:
130: if (!dragInProgress) {
131: return;
132: }
133:
134: dragInProgress = false;
135: MToolkit.executeOnEventHandlerThread(sb, new Runnable() {
136: public void run() {
137: // NB: notification only, no sb.setValue()
138: // last TRACK event will have done it already
139: inUpCall = true;
140: sb.setValueIsAdjusting(false);
141: postEvent(new AdjustmentEvent(sb,
142: AdjustmentEvent.ADJUSTMENT_VALUE_CHANGED,
143: AdjustmentEvent.TRACK, value, false));
144: inUpCall = false;
145: }
146: });
147: }
148:
149: /**
150: * Set the value of the slider in the ScrollBar.
151: */
152: public void setValues(int value, int visible, int minimum,
153: int maximum) {
154: // Fix for BugTraq ID 4048060. Prevent unnecessary redrawing
155: // of the slider, when the slider is already in the correct
156: // position. Since the ScrollBar widget now receives the
157: // ButtonRelease X event before the Java Adjustor event is
158: // handled, the slider is already in the correct position and
159: // does not need to be set again and redrawn, when processing
160: // the Adjustor event.
161: if (!inUpCall) {
162: pSetValues(value, visible, minimum, maximum);
163: }
164: }
165:
166: public void print(Graphics g) {
167: Scrollbar sb = (Scrollbar) target;
168: Dimension d = sb.size();
169: Color bg = sb.getBackground();
170:
171: boolean horiz = (sb.getOrientation() == Scrollbar.HORIZONTAL);
172:
173: drawScrollbar(g, bg, horiz ? d.height : d.width,
174: horiz ? d.width : d.height, sb.getMinimum(), sb
175: .getMaximum(), sb.getValue(), sb.getVisible(),
176: horiz);
177:
178: target.print(g);
179: }
180:
181: /**
182: * DEPRECATED
183: */
184: public Dimension minimumSize() {
185: return getMinimumSize();
186: }
187:
188: protected boolean shouldFocusOnClick() {
189: // Changed in 1.4 - scroll bars are made focusable by mouse clicks.
190: return true;
191: }
192: }
|