1 14 package org.wings; 15 16 import javax.swing.*; 17 import javax.swing.event.ChangeEvent ; 18 import javax.swing.event.ChangeListener ; 19 import java.awt.*; 20 import java.awt.event.AdjustmentEvent ; 21 import java.awt.event.AdjustmentListener ; 22 import java.io.Serializable ; 23 24 28 public abstract class SAbstractAdjustable 29 extends SComponent 30 implements Adjustable, LowLevelEventListener { 31 public static final int UNIT = 0; 32 33 public static final int BLOCK = 1; 34 35 public static final int MARGIN = 2; 36 37 41 private final ChangeListener fwdAdjustmentEvents = new ModelListener(); 42 43 49 protected SBoundedRangeModel model; 50 51 54 protected int unitIncrement; 55 56 59 protected int blockIncrement; 60 61 64 protected int orientation; 65 66 84 public SAbstractAdjustable(int value, int extent, int min, int max) { 85 this(new SDefaultBoundedRangeModel(value, extent, min, max)); 86 } 87 88 89 public SAbstractAdjustable(SBoundedRangeModel model) { 90 this.model = model; 91 this.model.addChangeListener(fwdAdjustmentEvents); 92 this.unitIncrement = 1; 93 this.blockIncrement = (model.getExtent() == 0) ? 1 : model.getExtent(); 94 } 95 96 106 public SAbstractAdjustable() { 107 this(0, 10, 0, 100); 108 } 109 110 116 public final SBoundedRangeModel getModel() { 117 return model; 118 } 119 120 121 130 public void setModel(SBoundedRangeModel newModel) { 131 reloadIfChange(this.model, newModel); 132 if (model != null) { 133 model.removeChangeListener(fwdAdjustmentEvents); 134 } 135 model = newModel; 136 if (model != null) { 137 model.addChangeListener(fwdAdjustmentEvents); 138 } 139 } 140 141 142 161 public int getUnitIncrement(int direction) { 162 return unitIncrement; 163 } 164 165 166 174 public void setUnitIncrement(int unitIncrement) { 175 reloadIfChange(this.unitIncrement, unitIncrement); 176 this.unitIncrement = unitIncrement; 177 } 178 179 198 public int getBlockIncrement(int direction) { 199 return blockIncrement; 200 } 201 202 203 211 public void setBlockIncrement(int blockIncrement) { 212 reloadIfChange(this.blockIncrement, blockIncrement); 213 this.blockIncrement = blockIncrement; 214 } 215 216 217 223 public final int getUnitIncrement() { 224 return unitIncrement; 225 } 226 227 228 234 public final int getBlockIncrement() { 235 return blockIncrement; 236 } 237 238 239 245 public final int getValue() { 246 return getModel().getValue(); 247 } 248 249 250 260 public void setValue(int value) { 261 getModel().setValue(value); 262 } 263 264 public final int getExtent() { 265 return getModel().getExtent(); 266 } 267 268 269 public void setExtent(int value) { 270 getModel().setExtent(value); 271 } 272 273 274 282 public final int getVisibleAmount() { 283 return getModel().getExtent(); 284 } 285 286 287 295 public void setVisibleAmount(int extent) { 296 getModel().setExtent(extent); 297 } 298 299 300 307 public final int getMinimum() { 308 return getModel().getMinimum(); 309 } 310 311 312 320 public void setMinimum(int minimum) { 321 getModel().setMinimum(minimum); 322 } 323 324 325 331 public final int getMaximum() { 332 return getModel().getMaximum(); 333 } 334 335 336 345 public void setMaximum(int maximum) { 346 getModel().setMaximum(maximum); 347 } 348 349 356 public final int getOrientation() { 357 return orientation; 358 } 359 360 367 public void setOrientation(int orientation) { 368 switch (orientation) { 369 case SConstants.VERTICAL: 370 case SConstants.HORIZONTAL: 371 this.orientation = orientation; 372 break; 373 default: 374 throw new IllegalArgumentException ("orientation must be one of: VERTICAL, HORIZONTAL"); 375 } 376 } 377 378 379 385 public final boolean getValueIsAdjusting() { 386 return getModel().getValueIsAdjusting(); 387 } 388 389 390 403 public void setValueIsAdjusting(boolean b) { 404 getModel().setValueIsAdjusting(b); 405 } 406 407 408 422 public void setValues(int newValue, int newExtent, int newMin, int newMax) { 423 BoundedRangeModel m = getModel(); 424 m.setRangeProperties(newValue, newExtent, newMin, newMax, m.getValueIsAdjusting()); 425 } 426 427 public void processLowLevelEvent(String action, String [] values) { 428 processKeyEvents(values); 429 430 getModel().setDelayEvents(true); 431 for (int i = 0; i < values.length; i++) { 432 try { 433 setValue(Integer.parseInt(values[i])); 434 } catch (NumberFormatException ex) { 435 } 437 } 438 439 SForm.addArmedComponent(this); 440 getModel().setDelayEvents(false); 441 } 442 443 public void fireIntermediateEvents() { 444 getModel().fireDelayedIntermediateEvents(); 445 } 446 447 public void fireFinalEvents() { 448 super.fireFinalEvents(); 449 getModel().fireDelayedFinalEvents(); 450 } 451 452 469 public void addAdjustmentListener(AdjustmentListener l) { 470 addEventListener(AdjustmentListener .class, l); 471 } 472 473 474 480 public void removeAdjustmentListener(AdjustmentListener l) { 481 removeEventListener(AdjustmentListener .class, l); 482 } 483 484 485 491 protected void fireAdjustmentValueChanged(int id, int type, int value) { 492 AdjustmentEvent e = null; 493 494 Object [] listeners = getListenerList(); 495 for (int i = listeners.length - 2; i >= 0; i -= 2) { 496 if (listeners[i] == AdjustmentListener .class) { 497 if (e == null) { 498 e = new AdjustmentEvent (this, id, type, value); 499 } 500 ((AdjustmentListener ) listeners[i + 1]).adjustmentValueChanged(e); 501 } 502 } 503 } 504 505 512 private class ModelListener implements ChangeListener , Serializable { 513 public void stateChanged(ChangeEvent e) { 514 int id = AdjustmentEvent.ADJUSTMENT_VALUE_CHANGED; 515 int type = AdjustmentEvent.TRACK; 516 fireAdjustmentValueChanged(id, type, getValue()); 517 reload(); 518 } 519 } 520 521 522 private boolean epochCheckEnabled = true; 523 524 525 public boolean isEpochCheckEnabled() { 526 return epochCheckEnabled; 527 } 528 529 530 public void setEpochCheckEnabled(boolean epochCheckEnabled) { 531 this.epochCheckEnabled = epochCheckEnabled; 532 } 533 534 } 535 | Popular Tags |