1 19 package org.openide.explorer.view; 20 21 import java.awt.Component ; 22 import java.awt.Insets ; 23 import java.awt.Point ; 24 import java.awt.dnd.Autoscroll ; 25 26 import javax.swing.JViewport ; 27 28 29 34 final class AutoscrollSupport extends Object implements Autoscroll { 35 37 38 Component comp; 39 40 41 JViewport viewport; 42 43 44 Insets insets; 45 46 47 Insets scrollUnits; 48 49 51 Insets autoscrollInsets; 52 53 55 AutoscrollSupport(Component comp, Insets insets) { 56 this.comp = comp; 57 this.insets = insets; 58 this.scrollUnits = insets; 59 } 60 61 63 public void autoscroll(Point cursorLoc) { 64 JViewport viewport = getViewport(); 65 66 if (viewport == null) { 67 return; 68 } 69 70 Point viewPos = viewport.getViewPosition(); 71 int viewHeight = viewport.getExtentSize().height; 72 int viewWidth = viewport.getExtentSize().width; 73 74 if ((cursorLoc.y - viewPos.y) < insets.top) { 76 viewport.setViewPosition(new Point (viewPos.x, Math.max(viewPos.y - scrollUnits.top, 0))); 78 } else if (((viewPos.y + viewHeight) - cursorLoc.y) < insets.bottom) { 79 viewport.setViewPosition( 81 new Point (viewPos.x, Math.min(viewPos.y + scrollUnits.bottom, comp.getHeight() - viewHeight)) 82 ); 83 } else if ((cursorLoc.x - viewPos.x) < insets.left) { 84 viewport.setViewPosition(new Point (Math.max(viewPos.x - scrollUnits.left, 0), viewPos.y)); 86 } else if (((viewPos.x + viewWidth) - cursorLoc.x) < insets.right) { 87 viewport.setViewPosition( 89 new Point (Math.min(viewPos.x + scrollUnits.right, comp.getWidth() - viewWidth), viewPos.y) 90 ); 91 } 92 } 93 94 public Insets getAutoscrollInsets() { 95 if (autoscrollInsets == null) { 96 int height = comp.getHeight(); 97 int width = comp.getWidth(); 98 autoscrollInsets = new Insets (height, width, height, width); 99 } 100 101 return autoscrollInsets; 102 } 103 104 106 public Insets getInsets() { 107 return insets; 108 } 109 110 112 public void setInsets(Insets insets) { 113 this.insets = insets; 114 } 115 116 118 public Insets getScrollUnits() { 119 return scrollUnits; 120 } 121 122 128 public void setScrollUnits(Insets scrollUnits) { 129 this.scrollUnits = scrollUnits; 130 } 131 132 135 JViewport getViewport() { 136 if (viewport == null) { 137 Component curComp = comp; 138 139 while (!(curComp instanceof JViewport ) && (curComp != null)) { 140 curComp = comp.getParent(); 141 } 142 143 viewport = (JViewport ) curComp; 144 } 145 146 return viewport; 147 } 148 } 149 | Popular Tags |