1 7 8 package javax.swing; 9 10 import javax.swing.event.*; 11 import java.io.Serializable ; 12 import java.util.EventListener ; 13 14 31 public class DefaultBoundedRangeModel implements BoundedRangeModel , Serializable 32 { 33 38 protected transient ChangeEvent changeEvent = null; 39 40 41 protected EventListenerList listenerList = new EventListenerList(); 42 43 private int value = 0; 44 private int extent = 0; 45 private int min = 0; 46 private int max = 100; 47 private boolean isAdjusting = false; 48 49 50 61 public DefaultBoundedRangeModel() { 62 } 63 64 65 73 public DefaultBoundedRangeModel(int value, int extent, int min, int max) 74 { 75 if ((max >= min) && 76 (value >= min) && 77 ((value + extent) >= value) && 78 ((value + extent) <= max)) { 79 this.value = value; 80 this.extent = extent; 81 this.min = min; 82 this.max = max; 83 } 84 else { 85 throw new IllegalArgumentException ("invalid range properties"); 86 } 87 } 88 89 90 96 public int getValue() { 97 return value; 98 } 99 100 101 107 public int getExtent() { 108 return extent; 109 } 110 111 112 118 public int getMinimum() { 119 return min; 120 } 121 122 123 129 public int getMaximum() { 130 return max; 131 } 132 133 134 144 public void setValue(int n) { 145 n = Math.min(n, Integer.MAX_VALUE - extent); 146 147 int newValue = Math.max(n, min); 148 if (newValue + extent > max) { 149 newValue = max - extent; 150 } 151 setRangeProperties(newValue, extent, min, max, isAdjusting); 152 } 153 154 155 164 public void setExtent(int n) { 165 int newExtent = Math.max(0, n); 166 if(value + newExtent > max) { 167 newExtent = max - value; 168 } 169 setRangeProperties(value, newExtent, min, max, isAdjusting); 170 } 171 172 173 182 public void setMinimum(int n) { 183 int newMax = Math.max(n, max); 184 int newValue = Math.max(n, value); 185 int newExtent = Math.min(newMax - newValue, extent); 186 setRangeProperties(newValue, newExtent, n, newMax, isAdjusting); 187 } 188 189 190 198 public void setMaximum(int n) { 199 int newMin = Math.min(n, min); 200 int newExtent = Math.min(n - newMin, extent); 201 int newValue = Math.min(n - newExtent, value); 202 setRangeProperties(newValue, newExtent, newMin, n, isAdjusting); 203 } 204 205 206 213 public void setValueIsAdjusting(boolean b) { 214 setRangeProperties(value, extent, min, max, b); 215 } 216 217 218 226 public boolean getValueIsAdjusting() { 227 return isAdjusting; 228 } 229 230 231 247 public void setRangeProperties(int newValue, int newExtent, int newMin, int newMax, boolean adjusting) 248 { 249 if (newMin > newMax) { 250 newMin = newMax; 251 } 252 if (newValue > newMax) { 253 newMax = newValue; 254 } 255 if (newValue < newMin) { 256 newMin = newValue; 257 } 258 259 263 if (((long)newExtent + (long)newValue) > newMax) { 264 newExtent = newMax - newValue; 265 } 266 267 if (newExtent < 0) { 268 newExtent = 0; 269 } 270 271 boolean isChange = 272 (newValue != value) || 273 (newExtent != extent) || 274 (newMin != min) || 275 (newMax != max) || 276 (adjusting != isAdjusting); 277 278 if (isChange) { 279 value = newValue; 280 extent = newExtent; 281 min = newMin; 282 max = newMax; 283 isAdjusting = adjusting; 284 285 fireStateChanged(); 286 } 287 } 288 289 290 298 public void addChangeListener(ChangeListener l) { 299 listenerList.add(ChangeListener.class, l); 300 } 301 302 303 310 public void removeChangeListener(ChangeListener l) { 311 listenerList.remove(ChangeListener.class, l); 312 } 313 314 315 328 public ChangeListener[] getChangeListeners() { 329 return (ChangeListener[])listenerList.getListeners( 330 ChangeListener.class); 331 } 332 333 334 340 protected void fireStateChanged() 341 { 342 Object [] listeners = listenerList.getListenerList(); 343 for (int i = listeners.length - 2; i >= 0; i -=2 ) { 344 if (listeners[i] == ChangeListener.class) { 345 if (changeEvent == null) { 346 changeEvent = new ChangeEvent(this); 347 } 348 ((ChangeListener)listeners[i+1]).stateChanged(changeEvent); 349 } 350 } 351 } 352 353 354 358 public String toString() { 359 String modelString = 360 "value=" + getValue() + ", " + 361 "extent=" + getExtent() + ", " + 362 "min=" + getMinimum() + ", " + 363 "max=" + getMaximum() + ", " + 364 "adj=" + getValueIsAdjusting(); 365 366 return getClass().getName() + "[" + modelString + "]"; 367 } 368 369 404 public <T extends EventListener > T[] getListeners(Class <T> listenerType) { 405 return listenerList.getListeners(listenerType); 406 } 407 } 408 409 | Popular Tags |