KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > internal > databinding > internal > swt > SpinnerObservableValue


1 /*******************************************************************************
2  * Copyright (c) 2005, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  * Brad Reynolds - bug 164653
11  * Ashley Cambrell - bug 198904
12  *******************************************************************************/

13 package org.eclipse.jface.internal.databinding.internal.swt;
14
15 import org.eclipse.core.databinding.observable.Diffs;
16 import org.eclipse.jface.internal.databinding.provisional.swt.AbstractSWTObservableValue;
17 import org.eclipse.jface.util.Assert;
18 import org.eclipse.swt.events.ModifyEvent;
19 import org.eclipse.swt.events.ModifyListener;
20 import org.eclipse.swt.widgets.Spinner;
21
22 /**
23  * @since 1.0
24  *
25  */

26 public class SpinnerObservableValue extends AbstractSWTObservableValue {
27
28     private final Spinner spinner;
29
30     private final String JavaDoc attribute;
31
32     private boolean updating = false;
33
34     private int currentSelection;
35
36     private ModifyListener modifyListener;
37
38     /**
39      * @param spinner
40      * @param attribute
41      */

42     public SpinnerObservableValue(Spinner spinner, String JavaDoc attribute) {
43         super(spinner);
44         this.spinner = spinner;
45         this.attribute = attribute;
46         if (attribute.equals(SWTProperties.SELECTION)) {
47             currentSelection = spinner.getSelection();
48             modifyListener = new ModifyListener() {
49                 public void modifyText(ModifyEvent e) {
50                     if (!updating) {
51                         int newSelection = SpinnerObservableValue.this.spinner
52                                 .getSelection();
53                         fireValueChange(Diffs.createValueDiff(new Integer JavaDoc(
54                                 currentSelection), new Integer JavaDoc(newSelection)));
55                         currentSelection = newSelection;
56                     }
57                 }
58             };
59             spinner.addModifyListener(modifyListener);
60         } else if (!attribute.equals(SWTProperties.MIN)
61                 && !attribute.equals(SWTProperties.MAX)) {
62             throw new IllegalArgumentException JavaDoc(
63                     "Attribute name not valid: " + attribute); //$NON-NLS-1$
64
}
65     }
66
67     public void doSetValue(final Object JavaDoc value) {
68         int oldValue;
69         int newValue;
70         try {
71             updating = true;
72             newValue = ((Integer JavaDoc) value).intValue();
73             if (attribute.equals(SWTProperties.SELECTION)) {
74                 oldValue = spinner.getSelection();
75                 spinner.setSelection(newValue);
76                 currentSelection = newValue;
77             } else if (attribute.equals(SWTProperties.MIN)) {
78                 oldValue = spinner.getMinimum();
79                 spinner.setMinimum(newValue);
80             } else if (attribute.equals(SWTProperties.MAX)) {
81                 oldValue = spinner.getMaximum();
82                 spinner.setMaximum(newValue);
83             } else {
84                 Assert.isTrue(false, "invalid attribute name:" + attribute); //$NON-NLS-1$
85
return;
86             }
87             fireValueChange(Diffs.createValueDiff(new Integer JavaDoc(oldValue),
88                     new Integer JavaDoc(newValue)));
89         } finally {
90             updating = false;
91         }
92     }
93
94     public Object JavaDoc doGetValue() {
95         int value = 0;
96         if (attribute.equals(SWTProperties.SELECTION)) {
97             value = spinner.getSelection();
98         } else if (attribute.equals(SWTProperties.MIN)) {
99             value = spinner.getMinimum();
100         } else if (attribute.equals(SWTProperties.MAX)) {
101             value = spinner.getMaximum();
102         }
103         return new Integer JavaDoc(value);
104     }
105
106     public Object JavaDoc getValueType() {
107         return Integer.TYPE;
108     }
109
110     /**
111      * @return attribute being observed
112      */

113     public String JavaDoc getAttribute() {
114         return attribute;
115     }
116
117     /*
118      * (non-Javadoc)
119      *
120      * @see org.eclipse.core.databinding.observable.value.AbstractObservableValue#dispose()
121      */

122     public synchronized void dispose() {
123         super.dispose();
124         if (modifyListener != null && !spinner.isDisposed()) {
125             spinner.removeModifyListener(modifyListener);
126         }
127     }
128 }
129
Popular Tags