KickJava   Java API By Example, From Geeks To Geeks.

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


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  *******************************************************************************/

12 package org.eclipse.jface.internal.databinding.internal.swt;
13
14 import org.eclipse.core.databinding.observable.Diffs;
15 import org.eclipse.jface.internal.databinding.provisional.swt.AbstractSWTObservableValue;
16 import org.eclipse.swt.widgets.Control;
17
18 /**
19  * @since 1.0
20  *
21  */

22 abstract public class SingleSelectionObservableValue extends
23         AbstractSWTObservableValue {
24
25     private boolean updating = false;
26
27     private int currentSelection;
28
29     /**
30      * @param control
31      * the control
32      */

33     public SingleSelectionObservableValue(Control control) {
34         super(control);
35
36         currentSelection = doGetSelectionIndex();
37         doAddSelectionListener(new Runnable JavaDoc(){
38             public void run() {
39                 if (!updating) {
40                     int newSelection = doGetSelectionIndex();
41                     fireValueChange(Diffs.createValueDiff(new Integer JavaDoc(
42                             currentSelection), new Integer JavaDoc(newSelection)));
43                     currentSelection = newSelection;
44                 }
45             }
46         });
47     }
48
49     /**
50      * @param runnable
51      */

52     protected abstract void doAddSelectionListener(Runnable JavaDoc runnable);
53
54     public void doSetValue(Object JavaDoc value) {
55         try {
56             updating = true;
57             int intValue = ((Integer JavaDoc) value).intValue();
58             doSetSelectionIndex(intValue);
59             currentSelection = intValue;
60         } finally {
61             updating = false;
62         }
63     }
64
65     /**
66      * @param intValue
67      * the selection index
68      */

69     protected abstract void doSetSelectionIndex(int intValue);
70
71     public Object JavaDoc doGetValue() {
72         return new Integer JavaDoc(doGetSelectionIndex());
73     }
74
75     /**
76      * @return the selection index
77      */

78     protected abstract int doGetSelectionIndex();
79
80     public Object JavaDoc getValueType() {
81         return Integer.TYPE;
82     }
83
84 }
85
Popular Tags