KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > palette > ui > AutoscrollSupport


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.palette.ui;
21
22 import java.awt.Component JavaDoc;
23 import java.awt.Insets JavaDoc;
24 import java.awt.Point JavaDoc;
25 import java.awt.dnd.Autoscroll JavaDoc;
26
27 import javax.swing.JViewport JavaDoc;
28
29
30 /** The support for autoscrolling in components contained in
31 * the viewport.
32 *
33 * @author Dafe Simonek
34 */

35 final class AutoscrollSupport extends Object JavaDoc implements Autoscroll JavaDoc {
36     // Attributes
37

38     /** The component which we support with autoscrolling */
39     Component JavaDoc comp;
40
41     /** The viewport containing asociated component */
42     JViewport JavaDoc viewport;
43
44     /** The insets where autoscrolling is active */
45     Insets JavaDoc insets;
46
47     /** Base sizes of scrolling during one autoscroll operation */
48     Insets JavaDoc scrollUnits;
49
50     /** Insets to return from getAutoscrollInsets - insets
51     * where autoscroll could potencionally occur */

52     Insets JavaDoc autoscrollInsets;
53
54     /** Creates a support for given component with given insets
55     * where autoscrolling is active */

56     AutoscrollSupport( Component JavaDoc comp ) {
57         this.comp = comp;
58         this.insets = new Insets JavaDoc(20, 10, 20, 10);
59         this.scrollUnits = new Insets JavaDoc(20, 10, 20, 10);
60     }
61
62     /** Performs autoscroll operation.
63     */

64     public void autoscroll(Point JavaDoc cursorLoc) {
65         JViewport JavaDoc viewport = getViewport();
66
67         if (viewport == null) {
68             return;
69         }
70
71         Point JavaDoc viewPos = viewport.getViewPosition();
72         int viewHeight = viewport.getExtentSize().height;
73         int viewWidth = viewport.getExtentSize().width;
74
75         // perform scrolling
76
if ((cursorLoc.y - viewPos.y) < insets.top) {
77             // scroll up
78
viewport.setViewPosition(new Point JavaDoc(viewPos.x, Math.max(viewPos.y - scrollUnits.top, 0)));
79         } else if (((viewPos.y + viewHeight) - cursorLoc.y) < insets.bottom) {
80             // scroll down
81
viewport.setViewPosition(
82                 new Point JavaDoc(viewPos.x, Math.min(viewPos.y + scrollUnits.bottom, comp.getHeight() - viewHeight))
83             );
84         } else if ((cursorLoc.x - viewPos.x) < insets.left) {
85             // scroll left
86
viewport.setViewPosition(new Point JavaDoc(Math.max(viewPos.x - scrollUnits.left, 0), viewPos.y));
87         } else if (((viewPos.x + viewWidth) - cursorLoc.x) < insets.right) {
88             // scroll right
89
viewport.setViewPosition(
90                 new Point JavaDoc(Math.min(viewPos.x + scrollUnits.right, comp.getWidth() - viewWidth), viewPos.y)
91             );
92         }
93     }
94
95     public Insets JavaDoc getAutoscrollInsets() {
96         if (autoscrollInsets == null) {
97             int height = comp.getHeight();
98             int width = comp.getWidth();
99             autoscrollInsets = new Insets JavaDoc(height, width, height, width);
100         }
101
102         return autoscrollInsets;
103     }
104
105     /** @return insets where autoscroll is active
106     */

107     public Insets JavaDoc getInsets() {
108         return insets;
109     }
110
111     /** Sets new active autoscroll insets
112     */

113     public void setInsets(Insets JavaDoc insets) {
114         this.insets = insets;
115     }
116
117     /** @return Scroll units for one autoscroll operation.
118     */

119     public Insets JavaDoc getScrollUnits() {
120         return scrollUnits;
121     }
122
123     /** Sets autoscroll scroll units.
124     * When autoscroll(..) method is called, it will scroll the
125     * component accordign to scroll unit in appropriate direction.
126     * So, scrollUnits.top says how much (in pixels) the component
127     * will autoscroll up etc...
128     */

129     public void setScrollUnits(Insets JavaDoc scrollUnits) {
130         this.scrollUnits = scrollUnits;
131     }
132
133     /** Getter for viewport of asociated component.
134     * Can return null if component is not contained in any viewport.
135     */

136     JViewport JavaDoc getViewport() {
137         if (viewport == null) {
138             Component JavaDoc curComp = comp;
139
140             while (!(curComp instanceof JViewport JavaDoc) && (curComp != null)) {
141                 curComp = curComp.getParent();
142             }
143
144             viewport = (JViewport JavaDoc) curComp;
145         }
146
147         return viewport;
148     }
149 }
150
Popular Tags