KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > explorer > view > 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 package org.openide.explorer.view;
20
21 import java.awt.Component JavaDoc;
22 import java.awt.Insets JavaDoc;
23 import java.awt.Point JavaDoc;
24 import java.awt.dnd.Autoscroll JavaDoc;
25
26 import javax.swing.JViewport JavaDoc;
27
28
29 /** The support for autoscrolling in components contained in
30 * the viewport.
31 *
32 * @author Dafe Simonek
33 */

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

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

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

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

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

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

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

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

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

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