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