KickJava   Java API By Example, From Geeks To Geeks.

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


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.swt.SWT;
18 import org.eclipse.swt.widgets.Event;
19 import org.eclipse.swt.widgets.List;
20 import org.eclipse.swt.widgets.Listener;
21
22 /**
23  * @since 3.2
24  *
25  */

26 public class ListObservableValue extends AbstractSWTObservableValue {
27
28     private final List list;
29
30     private boolean updating = false;
31
32     private String JavaDoc currentValue;
33
34     private Listener listener;
35
36     /**
37      * @param list
38      */

39     public ListObservableValue(List list) {
40         super(list);
41         this.list = list;
42         this.currentValue = (String JavaDoc) doGetValue();
43
44         if ((list.getStyle() & SWT.MULTI) > 0)
45             throw new IllegalArgumentException JavaDoc(
46                     "SWT.SINGLE support only for a List selection"); //$NON-NLS-1$
47

48         listener = new Listener() {
49
50             public void handleEvent(Event event) {
51                 if (!updating) {
52                     Object JavaDoc oldValue = currentValue;
53                     currentValue = (String JavaDoc) doGetValue();
54                     fireValueChange(Diffs.createValueDiff(oldValue,
55                             currentValue));
56                 }
57             }
58
59         };
60         list.addListener(SWT.Selection, listener);
61     }
62
63     public void doSetValue(Object JavaDoc value) {
64         String JavaDoc oldValue = null;
65         if (list.getSelection() != null && list.getSelection().length > 0)
66             oldValue = list.getSelection()[0];
67         try {
68             updating = true;
69             String JavaDoc items[] = list.getItems();
70             int index = -1;
71             if (items != null && value != null) {
72                 for (int i = 0; i < items.length; i++) {
73                     if (value.equals(items[i])) {
74                         index = i;
75                         break;
76                     }
77                 }
78                 list.select(index); // -1 will not "unselect"
79
}
80             currentValue = (String JavaDoc) value;
81         } finally {
82             updating = false;
83         }
84         fireValueChange(Diffs.createValueDiff(oldValue, value));
85     }
86
87     public Object JavaDoc doGetValue() {
88         int index = list.getSelectionIndex();
89         if (index >= 0)
90             return list.getItem(index);
91         return null;
92     }
93
94     public Object JavaDoc getValueType() {
95         return String JavaDoc.class;
96     }
97
98     /*
99      * (non-Javadoc)
100      *
101      * @see org.eclipse.core.databinding.observable.value.AbstractObservableValue#dispose()
102      */

103     public synchronized void dispose() {
104         super.dispose();
105         if (listener != null && !list.isDisposed()) {
106             list.removeListener(SWT.Selection, listener);
107         }
108     }
109 }
110
Popular Tags