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:
042: package org.netbeans.modules.palette.ui;
043:
044: import java.awt.Component;
045: import java.awt.Insets;
046: import java.awt.Point;
047: import java.awt.dnd.Autoscroll;
048:
049: import javax.swing.JViewport;
050:
051: /** The support for autoscrolling in components contained in
052: * the viewport.
053: *
054: * @author Dafe Simonek
055: */
056: final class AutoscrollSupport extends Object implements Autoscroll {
057: // Attributes
058:
059: /** The component which we support with autoscrolling */
060: Component comp;
061:
062: /** The viewport containing asociated component */
063: JViewport viewport;
064:
065: /** The insets where autoscrolling is active */
066: Insets insets;
067:
068: /** Base sizes of scrolling during one autoscroll operation */
069: Insets scrollUnits;
070:
071: /** Insets to return from getAutoscrollInsets - insets
072: * where autoscroll could potencionally occur */
073: Insets autoscrollInsets;
074:
075: /** Creates a support for given component with given insets
076: * where autoscrolling is active */
077: AutoscrollSupport(Component comp) {
078: this .comp = comp;
079: this .insets = new Insets(20, 10, 20, 10);
080: this .scrollUnits = new Insets(20, 10, 20, 10);
081: }
082:
083: /** Performs autoscroll operation.
084: */
085: public void autoscroll(Point cursorLoc) {
086: JViewport viewport = getViewport();
087:
088: if (viewport == null) {
089: return;
090: }
091:
092: Point viewPos = viewport.getViewPosition();
093: int viewHeight = viewport.getExtentSize().height;
094: int viewWidth = viewport.getExtentSize().width;
095:
096: // perform scrolling
097: if ((cursorLoc.y - viewPos.y) < insets.top) {
098: // scroll up
099: viewport.setViewPosition(new Point(viewPos.x, Math.max(
100: viewPos.y - scrollUnits.top, 0)));
101: } else if (((viewPos.y + viewHeight) - cursorLoc.y) < insets.bottom) {
102: // scroll down
103: viewport.setViewPosition(new Point(viewPos.x, Math.min(
104: viewPos.y + scrollUnits.bottom, comp.getHeight()
105: - viewHeight)));
106: } else if ((cursorLoc.x - viewPos.x) < insets.left) {
107: // scroll left
108: viewport.setViewPosition(new Point(Math.max(viewPos.x
109: - scrollUnits.left, 0), viewPos.y));
110: } else if (((viewPos.x + viewWidth) - cursorLoc.x) < insets.right) {
111: // scroll right
112: viewport.setViewPosition(new Point(Math.min(viewPos.x
113: + scrollUnits.right, comp.getWidth() - viewWidth),
114: viewPos.y));
115: }
116: }
117:
118: public Insets getAutoscrollInsets() {
119: if (autoscrollInsets == null) {
120: int height = comp.getHeight();
121: int width = comp.getWidth();
122: autoscrollInsets = new Insets(height, width, height, width);
123: }
124:
125: return autoscrollInsets;
126: }
127:
128: /** @return insets where autoscroll is active
129: */
130: public Insets getInsets() {
131: return insets;
132: }
133:
134: /** Sets new active autoscroll insets
135: */
136: public void setInsets(Insets insets) {
137: this .insets = insets;
138: }
139:
140: /** @return Scroll units for one autoscroll operation.
141: */
142: public Insets getScrollUnits() {
143: return scrollUnits;
144: }
145:
146: /** Sets autoscroll scroll units.
147: * When autoscroll(..) method is called, it will scroll the
148: * component accordign to scroll unit in appropriate direction.
149: * So, scrollUnits.top says how much (in pixels) the component
150: * will autoscroll up etc...
151: */
152: public void setScrollUnits(Insets scrollUnits) {
153: this .scrollUnits = scrollUnits;
154: }
155:
156: /** Getter for viewport of asociated component.
157: * Can return null if component is not contained in any viewport.
158: */
159: JViewport getViewport() {
160: if (viewport == null) {
161: Component curComp = comp;
162:
163: while (!(curComp instanceof JViewport) && (curComp != null)) {
164: curComp = curComp.getParent();
165: }
166:
167: viewport = (JViewport) curComp;
168: }
169:
170: return viewport;
171: }
172: }
|