1 14 package org.wings; 15 16 import javax.swing.event.ChangeEvent ; 17 import javax.swing.event.ChangeListener ; 18 import javax.swing.event.EventListenerList ; 19 import java.util.EventListener ; 20 21 22 31 public class SPagingBoundedRangeModel implements SBoundedRangeModel { 32 33 38 protected transient ChangeEvent changeEvent = null; 39 40 43 protected EventListenerList listenerList = new EventListenerList (); 44 45 protected int value = 0; 46 protected int extent = 0; 47 protected int min = 0; 48 protected int max = 100; 49 protected boolean isAdjusting = false; 50 51 55 private boolean delayEvents = false; 56 57 60 protected boolean gotDelayedEvent = false; 61 62 63 public SPagingBoundedRangeModel() { 64 super(); 65 } 66 67 public SPagingBoundedRangeModel(int value, int extent, int min, int max) { 68 setRangeProperties(value, extent, min, max, false); 69 } 70 71 78 public int getValue() { 79 return value; 80 } 81 82 83 90 public int getExtent() { 91 return extent; 92 } 93 94 95 102 public int getMinimum() { 103 return min; 104 } 105 106 107 114 public int getMaximum() { 115 return max; 116 } 117 118 128 public void setValue(int n) { 129 setRangeProperties(n, extent, min, max, isAdjusting); 130 } 131 132 133 143 public void setExtent(int n) { 144 setRangeProperties(value, n, min, max, isAdjusting); 145 } 146 147 148 158 public void setMinimum(int n) { 159 setRangeProperties(Math.max(value, n), extent, n, max, isAdjusting); 160 } 161 162 163 172 public void setMaximum(int n) { 173 setRangeProperties(Math.min(value, n), extent, min, n, isAdjusting); 174 } 175 176 177 184 public void setValueIsAdjusting(boolean b) { 185 setRangeProperties(value, extent, min, max, b); 186 } 187 188 196 public boolean getValueIsAdjusting() { 197 return isAdjusting; 198 } 199 200 216 public void setRangeProperties(int newValue, 217 int newExtent, 218 int newMin, 219 int newMax, 220 boolean adjusting) { 221 if (newMin > newMax) { 222 newMin = newMax; 223 } 224 if (newValue > newMax) { 225 newMax = newValue; 226 } 227 if (newValue < newMin) { 228 newMin = newValue; 229 } 230 if (newExtent < 0) { 231 newExtent = 0; 232 } 233 234 boolean isChange = 235 (newValue != value) || 236 (newExtent != extent) || 237 (newMin != min) || 238 (newMax != max) || 239 (adjusting != isAdjusting); 240 241 if (isChange) { 242 value = newValue; 243 extent = newExtent; 244 min = newMin; 245 max = newMax; 246 isAdjusting = adjusting; 247 248 fireStateChanged(); 249 } 250 } 251 252 public boolean getDelayEvents() { 253 return delayEvents; 254 } 255 256 public void setDelayEvents(boolean b) { 257 delayEvents = b; 258 } 259 260 261 269 public void addChangeListener(ChangeListener l) { 270 listenerList.add(ChangeListener .class, l); 271 } 272 273 274 281 public void removeChangeListener(ChangeListener l) { 282 listenerList.remove(ChangeListener .class, l); 283 } 284 285 286 297 public ChangeListener [] getChangeListeners() { 298 return (ChangeListener []) listenerList.getListeners(ChangeListener .class); 299 } 300 301 302 308 protected void fireStateChanged() { 309 if (delayEvents) { 310 gotDelayedEvent = true; 311 } else { 312 Object [] listeners = listenerList.getListenerList(); 313 for (int i = listeners.length - 2; i >= 0; i -= 2) { 314 if (listeners[i] == ChangeListener .class) { 315 if (changeEvent == null) { 316 changeEvent = new ChangeEvent (this); 317 } 318 ((ChangeListener ) listeners[i + 1]).stateChanged(changeEvent); 319 } 320 } 321 } 322 } 323 324 325 329 public String toString() { 330 String modelString = 331 "value=" + getValue() + ", " + 332 "extent=" + getExtent() + ", " + 333 "min=" + getMinimum() + ", " + 334 "max=" + getMaximum() + ", " + 335 "adj=" + getValueIsAdjusting(); 336 337 return getClass().getName() + "[" + modelString + "]"; 338 } 339 340 373 public EventListener [] getListeners(Class listenerType) { 374 return listenerList.getListeners(listenerType); 375 } 376 377 378 381 public void fireDelayedIntermediateEvents() { 382 } 383 384 public void fireDelayedFinalEvents() { 385 if (!delayEvents && gotDelayedEvent) { 386 fireStateChanged(); 387 gotDelayedEvent = false; 388 } 389 } 390 391 } | Popular Tags |