001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.openide.explorer.view;
042:
043: import java.awt.Component;
044: import java.awt.Insets;
045: import java.awt.Point;
046: import java.awt.dnd.Autoscroll;
047:
048: import javax.swing.JViewport;
049:
050: /** The support for autoscrolling in components contained in
051: * the viewport.
052: *
053: * @author Dafe Simonek
054: */
055: final class AutoscrollSupport extends Object implements Autoscroll {
056: // Attributes
057:
058: /** The component which we support with autoscrolling */
059: Component comp;
060:
061: /** The viewport containing asociated component */
062: JViewport viewport;
063:
064: /** The insets where autoscrolling is active */
065: Insets insets;
066:
067: /** Base sizes of scrolling during one autoscroll operation */
068: Insets scrollUnits;
069:
070: /** Insets to return from getAutoscrollInsets - insets
071: * where autoscroll could potencionally occur */
072: Insets autoscrollInsets;
073:
074: /** Creates a support for given component with given insets
075: * where autoscrolling is active */
076: AutoscrollSupport(Component comp, Insets insets) {
077: this .comp = comp;
078: this .insets = insets;
079: this .scrollUnits = insets;
080: }
081:
082: /** Performs autoscroll operation.
083: */
084: public void autoscroll(Point cursorLoc) {
085: JViewport viewport = getViewport();
086:
087: if (viewport == null) {
088: return;
089: }
090:
091: Point viewPos = viewport.getViewPosition();
092: int viewHeight = viewport.getExtentSize().height;
093: int viewWidth = viewport.getExtentSize().width;
094:
095: // perform scrolling
096: if ((cursorLoc.y - viewPos.y) < insets.top) {
097: // scroll up
098: viewport.setViewPosition(new Point(viewPos.x, Math.max(
099: viewPos.y - scrollUnits.top, 0)));
100: } else if (((viewPos.y + viewHeight) - cursorLoc.y) < insets.bottom) {
101: // scroll down
102: viewport.setViewPosition(new Point(viewPos.x, Math.min(
103: viewPos.y + scrollUnits.bottom, comp.getHeight()
104: - viewHeight)));
105: } else if ((cursorLoc.x - viewPos.x) < insets.left) {
106: // scroll left
107: viewport.setViewPosition(new Point(Math.max(viewPos.x
108: - scrollUnits.left, 0), viewPos.y));
109: } else if (((viewPos.x + viewWidth) - cursorLoc.x) < insets.right) {
110: // scroll right
111: viewport.setViewPosition(new Point(Math.min(viewPos.x
112: + scrollUnits.right, comp.getWidth() - viewWidth),
113: viewPos.y));
114: }
115: }
116:
117: public Insets getAutoscrollInsets() {
118: if (autoscrollInsets == null) {
119: int height = comp.getHeight();
120: int width = comp.getWidth();
121: autoscrollInsets = new Insets(height, width, height, width);
122: }
123:
124: return autoscrollInsets;
125: }
126:
127: /** @return insets where autoscroll is active
128: */
129: public Insets getInsets() {
130: return insets;
131: }
132:
133: /** Sets new active autoscroll insets
134: */
135: public void setInsets(Insets insets) {
136: this .insets = insets;
137: }
138:
139: /** @return Scroll units for one autoscroll operation.
140: */
141: public Insets getScrollUnits() {
142: return scrollUnits;
143: }
144:
145: /** Sets autoscroll scroll units.
146: * When autoscroll(..) method is called, it will scroll the
147: * component accordign to scroll unit in appropriate direction.
148: * So, scrollUnits.top says how much (in pixels) the component
149: * will autoscroll up etc...
150: */
151: public void setScrollUnits(Insets scrollUnits) {
152: this .scrollUnits = scrollUnits;
153: }
154:
155: /** Getter for viewport of asociated component.
156: * Can return null if component is not contained in any viewport.
157: */
158: JViewport getViewport() {
159: if (viewport == null) {
160: Component curComp = comp;
161:
162: while (!(curComp instanceof JViewport) && (curComp != null)) {
163: curComp = comp.getParent();
164: }
165:
166: viewport = (JViewport) curComp;
167: }
168:
169: return viewport;
170: }
171: }
|