1 26 27 package org.nightlabs.editor2d.viewer; 28 29 import java.awt.Rectangle ; 30 import java.awt.event.ActionEvent ; 31 import java.awt.event.ActionListener ; 32 33 import javax.swing.Timer ; 34 35 import org.nightlabs.editor2d.viewer.preferences.Preferences; 36 37 43 public abstract class AbstractAutoScrollSupport 44 implements IAutoScrollSupport 45 { 46 50 public static final int DEFAULT_SCROLL_STEP = 51 Preferences.getPreferenceStore().getDefaultInt(Preferences.PREFERENCE_SCROLL_STEP); 52 public static final int DEFAULT_SCROLL_TOLERANCE = 53 Preferences.getPreferenceStore().getDefaultInt(Preferences.PREFERENCE_SCROLL_TOLERANCE); 54 public static final int DEFAULT_TIMER_DELAY = 55 Preferences.getPreferenceStore().getDefaultInt(Preferences.PREFERENCE_TIMER_DELAY); 56 57 protected int scrollStep = DEFAULT_SCROLL_STEP; 59 60 64 public void setScrollStep(int step) { 65 scrollStep = step; 66 } 67 68 72 public int getScrollStep() { 73 return scrollStep; 74 } 75 76 protected int scrollTolerance = DEFAULT_SCROLL_TOLERANCE; 78 79 84 public void setScrollTolerance(int scrollTolerance) { 85 this.scrollTolerance = scrollTolerance; 86 } 87 88 93 public int getScrollTolerance() { 94 return scrollTolerance; 95 } 96 97 protected int timerDelay = DEFAULT_TIMER_DELAY; 99 100 105 public void setTimerDelay(int timerDelay) { 106 this.timerDelay = timerDelay; 107 timerDown.setDelay(timerDelay); 108 timerUp.setDelay(timerDelay); 109 timerLeft.setDelay(timerDelay); 110 timerRight.setDelay(timerDelay); 111 } 112 113 118 public int getTimerDelay() { 119 return timerDelay; 120 } 121 122 protected ActionListener scrollRight = new ActionListener () 123 { 124 public void actionPerformed(ActionEvent e) { 125 doScrollRight(scrollStep); 126 } 127 }; 128 129 protected ActionListener scrollLeft = new ActionListener () 130 { 131 public void actionPerformed(ActionEvent e) { 132 doScrollLeft(scrollStep); 133 } 134 }; 135 136 protected ActionListener scrollUp = new ActionListener () 137 { 138 public void actionPerformed(ActionEvent e) { 139 doScrollUp(scrollStep); 140 } 141 }; 142 143 protected ActionListener scrollDown = new ActionListener () 144 { 145 public void actionPerformed(ActionEvent e) { 146 doScrollDown(scrollStep); 147 } 148 }; 149 150 private Timer timerLeft = new Timer (timerDelay, scrollLeft); 151 private Timer timerRight = new Timer (timerDelay, scrollRight); 152 private Timer timerUp = new Timer (timerDelay, scrollUp); 153 private Timer timerDown = new Timer (timerDelay, scrollDown); 154 155 private Rectangle upperScrollArea = null; 156 private Rectangle bottomScrollArea = null; 157 private Rectangle leftScrollArea = null; 158 private Rectangle rightScrollArea = null; 159 160 private boolean isInTop = false; 161 private boolean isInBottom = false; 162 private boolean isInLeft = false; 163 private boolean isInRight = false; 164 165 protected void initAutoScroll(Rectangle bounds) 166 { 167 int width = bounds.width; 168 int height = bounds.height; 169 upperScrollArea = new Rectangle (0, 0, width, scrollTolerance); 170 bottomScrollArea = new Rectangle (0, height-scrollTolerance, width, scrollTolerance); 171 leftScrollArea = new Rectangle (0, 0, scrollTolerance, height); 172 rightScrollArea = new Rectangle (width - scrollTolerance, 0, scrollTolerance, height); 173 } 174 175 protected abstract void scrollDown(int scrollStep); 176 protected abstract void scrollUp(int scrollStep); 177 protected abstract void scrollLeft(int scrollStep); 178 protected abstract void scrollRight(int scrollStep); 179 180 protected void mouseMoved(int x, int y) 181 { 182 if (!upperScrollArea.contains(x,y)) { 183 if (isInTop == true) { 184 isInTop = false; 185 timerUp.stop(); 186 } 187 } 188 else { 189 if (isInTop == false) { 190 isInTop = true; 191 timerUp.restart(); 192 } 193 } 194 195 if (!bottomScrollArea.contains(x, y)) { 196 if (isInBottom == true) { 197 isInBottom = false; 198 timerDown.stop(); 199 } 200 } 201 else { 202 if (isInBottom == false) { 203 isInBottom = true; 204 timerDown.restart(); 205 } 206 } 207 208 if (!leftScrollArea.contains(x, y)) { 209 if (isInLeft == true) { 210 isInLeft = false; 211 timerLeft.stop(); 212 } 213 } 214 else { 215 if (isInLeft == false) { 216 isInLeft = true; 217 timerLeft.restart(); 218 } 219 } 220 221 if (!rightScrollArea.contains(x, y)) { 222 if (isInRight == true) { 223 isInRight = false; 224 timerRight.stop(); 225 } 226 } 227 else { 228 if (isInRight == false) { 229 isInRight = true; 230 timerRight.restart(); 231 } 232 } 233 } 234 235 protected void mouseExited() 236 { 237 if (isInTop) { 238 isInTop = false; 239 timerUp.stop(); 240 } 241 if (isInBottom) { 242 isInBottom = false; 243 timerDown.stop(); 244 } 245 if (isInRight) { 246 isInRight = false; 247 timerRight.stop(); 248 } 249 if (isInLeft) { 250 isInLeft = false; 251 timerLeft.stop(); 252 } 253 } 254 255 protected void stopTimers() 256 { 257 timerLeft.stop(); 258 timerRight.stop(); 259 timerUp.stop(); 260 timerDown.stop(); 261 } 262 263 protected void doScrollDown(final int scrollStep) { 264 scrollDown(scrollStep); 265 } 266 267 protected void doScrollUp(final int scrollStep) { 268 scrollUp(scrollStep); 269 } 270 271 protected void doScrollLeft(final int scrollStep) { 272 scrollLeft(scrollStep); 273 } 274 275 protected void doScrollRight(final int scrollStep){ 276 scrollRight(scrollStep); 277 } 278 279 } 280 | Popular Tags |