001: /*******************************************************************************
002: * Copyright (c) 2005, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: * Brad Reynolds - bug 164653
011: * Ashley Cambrell - bug 198904
012: *******************************************************************************/package org.eclipse.jface.internal.databinding.internal.swt;
013:
014: import org.eclipse.core.databinding.observable.Diffs;
015: import org.eclipse.core.databinding.observable.Realm;
016: import org.eclipse.jface.internal.databinding.provisional.swt.AbstractSWTObservableValue;
017: import org.eclipse.jface.util.Assert;
018: import org.eclipse.swt.events.ModifyEvent;
019: import org.eclipse.swt.events.ModifyListener;
020: import org.eclipse.swt.widgets.Spinner;
021:
022: /**
023: * @since 1.0
024: *
025: */
026: public class SpinnerObservableValue extends AbstractSWTObservableValue {
027:
028: private final Spinner spinner;
029:
030: private final String attribute;
031:
032: private boolean updating = false;
033:
034: private int currentSelection;
035:
036: private ModifyListener modifyListener;
037:
038: /**
039: * @param spinner
040: * @param attribute
041: */
042: public SpinnerObservableValue(Spinner spinner, String attribute) {
043: super (spinner);
044: this .spinner = spinner;
045: this .attribute = attribute;
046: init();
047: }
048:
049: /**
050: * @param realm
051: * @param spinner
052: * @param attribute
053: */
054: public SpinnerObservableValue(Realm realm, Spinner spinner,
055: String attribute) {
056: super (realm, spinner);
057: this .spinner = spinner;
058: this .attribute = attribute;
059: init();
060: }
061:
062: private void init() {
063: if (attribute.equals(SWTProperties.SELECTION)) {
064: currentSelection = spinner.getSelection();
065: modifyListener = new ModifyListener() {
066: public void modifyText(ModifyEvent e) {
067: if (!updating) {
068: int newSelection = SpinnerObservableValue.this .spinner
069: .getSelection();
070: notifyIfChanged(currentSelection, newSelection);
071: currentSelection = newSelection;
072: }
073: }
074: };
075: spinner.addModifyListener(modifyListener);
076: } else if (!attribute.equals(SWTProperties.MIN)
077: && !attribute.equals(SWTProperties.MAX)) {
078: throw new IllegalArgumentException(
079: "Attribute name not valid: " + attribute); //$NON-NLS-1$
080: }
081: }
082:
083: public void doSetValue(final Object value) {
084: int oldValue;
085: int newValue;
086: try {
087: updating = true;
088: newValue = ((Integer) value).intValue();
089: if (attribute.equals(SWTProperties.SELECTION)) {
090: oldValue = spinner.getSelection();
091: spinner.setSelection(newValue);
092: currentSelection = newValue;
093: } else if (attribute.equals(SWTProperties.MIN)) {
094: oldValue = spinner.getMinimum();
095: spinner.setMinimum(newValue);
096: } else if (attribute.equals(SWTProperties.MAX)) {
097: oldValue = spinner.getMaximum();
098: spinner.setMaximum(newValue);
099: } else {
100: Assert.isTrue(false,
101: "invalid attribute name:" + attribute); //$NON-NLS-1$
102: return;
103: }
104: notifyIfChanged(oldValue, newValue);
105: } finally {
106: updating = false;
107: }
108: }
109:
110: public Object doGetValue() {
111: int value = 0;
112: if (attribute.equals(SWTProperties.SELECTION)) {
113: value = spinner.getSelection();
114: } else if (attribute.equals(SWTProperties.MIN)) {
115: value = spinner.getMinimum();
116: } else if (attribute.equals(SWTProperties.MAX)) {
117: value = spinner.getMaximum();
118: }
119: return new Integer(value);
120: }
121:
122: public Object getValueType() {
123: return Integer.TYPE;
124: }
125:
126: /**
127: * @return attribute being observed
128: */
129: public String getAttribute() {
130: return attribute;
131: }
132:
133: /*
134: * (non-Javadoc)
135: *
136: * @see org.eclipse.core.databinding.observable.value.AbstractObservableValue#dispose()
137: */
138: public synchronized void dispose() {
139: super .dispose();
140: if (modifyListener != null && !spinner.isDisposed()) {
141: spinner.removeModifyListener(modifyListener);
142: }
143: }
144:
145: private void notifyIfChanged(int oldValue, int newValue) {
146: if (oldValue != newValue) {
147: fireValueChange(Diffs.createValueDiff(
148: new Integer(oldValue), new Integer(newValue)));
149: }
150: }
151: }
|