1 12 package org.eclipse.jface.internal.databinding.internal.swt; 13 14 import org.eclipse.core.databinding.observable.Diffs; 15 import org.eclipse.core.runtime.Assert; 16 import org.eclipse.jface.internal.databinding.provisional.swt.AbstractSWTObservableValue; 17 import org.eclipse.swt.events.SelectionAdapter; 18 import org.eclipse.swt.events.SelectionEvent; 19 import org.eclipse.swt.events.SelectionListener; 20 import org.eclipse.swt.widgets.Scale; 21 22 26 public class ScaleObservableValue extends AbstractSWTObservableValue { 27 28 private final Scale scale; 29 30 private final String attribute; 31 32 private boolean updating = false; 33 34 private int currentSelection; 35 36 private SelectionListener listener; 37 38 42 public ScaleObservableValue(Scale scale, String attribute) { 43 super(scale); 44 this.scale = scale; 45 this.attribute = attribute; 46 if (attribute.equals(SWTProperties.SELECTION)) { 47 currentSelection = scale.getSelection(); 48 scale.addSelectionListener(listener = new SelectionAdapter() { 49 public void widgetSelected(SelectionEvent e) { 50 if (!updating) { 51 int newSelection = ScaleObservableValue.this.scale 52 .getSelection(); 53 fireValueChange(Diffs.createValueDiff(new Integer ( 54 currentSelection), new Integer (newSelection))); 55 currentSelection = newSelection; 56 } 57 } 58 }); 59 } else if (!attribute.equals(SWTProperties.MIN) 60 && !attribute.equals(SWTProperties.MAX)) { 61 throw new IllegalArgumentException ( 62 "Attribute name not valid: " + attribute); } 64 } 65 66 public void doSetValue(final Object value) { 67 int oldValue; 68 int newValue; 69 try { 70 updating = true; 71 newValue = ((Integer ) value).intValue(); 72 if (attribute.equals(SWTProperties.SELECTION)) { 73 oldValue = scale.getSelection(); 74 scale.setSelection(newValue); 75 currentSelection = newValue; 76 } else if (attribute.equals(SWTProperties.MIN)) { 77 oldValue = scale.getMinimum(); 78 scale.setMinimum(newValue); 79 } else if (attribute.equals(SWTProperties.MAX)) { 80 oldValue = scale.getMaximum(); 81 scale.setMaximum(newValue); 82 } else { 83 Assert.isTrue(false, "invalid attribute name:" + attribute); return; 85 } 86 fireValueChange(Diffs.createValueDiff(new Integer (oldValue), 87 new Integer (newValue))); 88 } finally { 89 updating = false; 90 } 91 } 92 93 public Object doGetValue() { 94 int value = 0; 95 if (attribute.equals(SWTProperties.SELECTION)) { 96 value = scale.getSelection(); 97 } else if (attribute.equals(SWTProperties.MIN)) { 98 value = scale.getMinimum(); 99 } else if (attribute.equals(SWTProperties.MAX)) { 100 value = scale.getMaximum(); 101 } 102 return new Integer (value); 103 } 104 105 public Object getValueType() { 106 return Integer.TYPE; 107 } 108 109 112 public String getAttribute() { 113 return attribute; 114 } 115 116 119 public synchronized void dispose() { 120 super.dispose(); 121 122 if (listener != null && !scale.isDisposed()) { 123 scale.removeSelectionListener(listener); 124 } 125 listener = null; 126 } 127 } 128 | Popular Tags |