001: /*
002: * @(#)PPCScrollPanePeer.java 1.10 06/10/10
003: *
004: * Copyright 1990-2006 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: */
026:
027: package sun.awt.pocketpc;
028:
029: import java.awt.*;
030: import java.awt.event.AdjustmentEvent;
031: import sun.awt.peer.*;
032:
033: /**
034: *
035: *
036: * @author Nicholas Allen
037: */
038:
039: class PPCScrollPanePeer extends PPCPanelPeer implements ScrollPanePeer {
040: private static native void initIDs();
041:
042: static {
043: initIDs();
044: }
045:
046: int scrollbarWidth;
047: int scrollbarHeight;
048: boolean ignore;
049:
050: native void create(PPCComponentPeer parent);
051:
052: native int getOffset(int orient);
053:
054: PPCScrollPanePeer(Component target) {
055: super (target);
056: scrollbarWidth = getVScrollbarWidthNative();
057: scrollbarHeight = getHScrollbarHeightNative();
058: ignore = false;
059: }
060:
061: void initialize() {
062: super .initialize();
063: setInsets();
064: }
065:
066: public void setUnitIncrement(Adjustable adj, int p) {
067: // The unitIncrement is grabbed from the target as needed.
068: }
069:
070: public Insets insets() {
071: return getInsets();
072: }
073:
074: private native void setInsets();
075:
076: public native synchronized void setScrollPosition(int x, int y);
077:
078: public int getHScrollbarHeight() {
079: return scrollbarHeight;
080: }
081:
082: private native int getHScrollbarHeightNative();
083:
084: public int getVScrollbarWidth() {
085: return scrollbarWidth;
086: }
087:
088: private native int getVScrollbarWidthNative();
089:
090: public Point getScrollOffset() {
091: int x, y;
092: x = getOffset(Scrollbar.HORIZONTAL);
093: y = getOffset(Scrollbar.VERTICAL);
094: return new Point(x, y);
095: }
096:
097: /**
098: * The child component has been resized. The scrollbars must be
099: * updated with the new sizes. At the native level the sizes of
100: * the actual windows may not have changed yet, so the size
101: * information from the java-level is passed down and used.
102: */
103: public void childResized(int width, int height) {
104: ScrollPane sp = (ScrollPane) target;
105: Dimension vs = sp.getSize();
106: setSpans(vs.width, vs.height, width, height);
107: setInsets();
108: }
109:
110: native synchronized void setSpans(int viewWidth, int viewHeight,
111: int childWidth, int childHeight);
112:
113: /**
114: * Called by ScrollPane's internal observer of the scrollpane's adjustables.
115: * This is called whenever a scroll position is changed in one
116: * of adjustables, whether it was modified externally or from the
117: * native scrollbars themselves. If the change was sourced by the
118: * native scrollbars then ignore will be set to true.
119: */
120: public void setValue(Adjustable adj, int v) {
121: if (!ignore) {
122: Component c = getScrollChild();
123: Point p = c.getLocation();
124: switch (adj.getOrientation()) {
125: case Adjustable.VERTICAL:
126: setScrollPosition(-(p.x), v);
127: break;
128: case Adjustable.HORIZONTAL:
129: setScrollPosition(v, -(p.y));
130: break;
131: }
132: }
133: }
134:
135: private native Component getScrollChild();
136:
137: /**
138: * Callback from windows to indicate that the vertical scrollbar was
139: * adjusted and a new value is desired. If this is a valid
140: * value the adjustable will change and setValue will be called.
141: */
142: void scrolledVertical(int value) {
143: ScrollPane sp = (ScrollPane) target;
144: Adjustable adj = sp.getVAdjustable();
145: if (adj != null) {
146: Adjustor e = new Adjustor(adj, value, this );
147: PPCToolkit.postEvent(e);
148: }
149: }
150:
151: /**
152: * Callback from windows to indicate that the horizontal scrollbar was
153: * adjusted and a new value is desired. If this is a valid
154: * value the adjustable will change and setValue will be called.
155: */
156: void scrolledHorizontal(int value) {
157: ScrollPane sp = (ScrollPane) target;
158: Adjustable adj = sp.getHAdjustable();
159: if (adj != null) {
160: Adjustor e = new Adjustor(adj, value, this );
161: PPCToolkit.postEvent(e);
162: }
163: }
164:
165: /**
166: * This is used to change the adjustable on another thread
167: * to represent a change made in the native scrollbar. Since
168: * the change was reflected immediately at the native level,
169: * notification from the adjustable is temporarily ignored.
170: */
171: class Adjustor extends AWTEvent implements java.awt.ActiveEvent {
172:
173: Adjustor(Adjustable adj, int value, PPCScrollPanePeer peer) {
174: super (adj, 0);
175: this .adj = adj;
176: this .value = value;
177: this .peer = peer;
178: }
179:
180: public void dispatch() {
181: peer.ignore = true;
182: adj.setValue(value);
183: peer.ignore = false;
184: }
185:
186: int value;
187: Adjustable adj;
188: PPCScrollPanePeer peer;
189: }
190: }
|