KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Peter Centgraf - bug 175763
11  *******************************************************************************/

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 /**
23  * @since 1.0
24  *
25  */

26 public class ScaleObservableValue extends AbstractSWTObservableValue {
27
28     private final Scale scale;
29
30     private final String JavaDoc attribute;
31
32     private boolean updating = false;
33
34     private int currentSelection;
35     
36     private SelectionListener listener;
37
38     /**
39      * @param scale
40      * @param attribute
41      */

42     public ScaleObservableValue(Scale scale, String JavaDoc 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 JavaDoc(
54                                 currentSelection), new Integer JavaDoc(newSelection)));
55                         currentSelection = newSelection;
56                     }
57                 }
58             });
59         } else if (!attribute.equals(SWTProperties.MIN)
60                 && !attribute.equals(SWTProperties.MAX)) {
61             throw new IllegalArgumentException JavaDoc(
62                     "Attribute name not valid: " + attribute); //$NON-NLS-1$
63
}
64     }
65
66     public void doSetValue(final Object JavaDoc value) {
67         int oldValue;
68         int newValue;
69         try {
70             updating = true;
71             newValue = ((Integer JavaDoc) 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); //$NON-NLS-1$
84
return;
85             }
86             fireValueChange(Diffs.createValueDiff(new Integer JavaDoc(oldValue),
87                     new Integer JavaDoc(newValue)));
88         } finally {
89             updating = false;
90         }
91     }
92
93     public Object JavaDoc 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 JavaDoc(value);
103     }
104
105     public Object JavaDoc getValueType() {
106         return Integer.TYPE;
107     }
108
109     /**
110      * @return attribute being observed
111      */

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

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