comparison org.eclipse.jface.databinding/src/org/eclipse/jface/internal/databinding/swt/ListObservableValue.d @ 78:0a55d2d5a946

Added file for databinding
author Frank Benoit <benoit@tionex.de>
date Tue, 14 Apr 2009 11:35:29 +0200
parents
children 6be48cf9f95c
comparison
equal deleted inserted replaced
76:f05e6e8b2f2d 78:0a55d2d5a946
1 /*******************************************************************************
2 * Copyright (c) 2005, 2008 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 module org.eclipse.jface.internal.databinding.swt.ListObservableValue;
14
15 import java.lang.all;
16
17 import org.eclipse.core.databinding.observable.Diffs;
18 import org.eclipse.jface.internal.databinding.provisional.swt.AbstractSWTObservableValue;
19 import org.eclipse.swt.SWT;
20 import org.eclipse.swt.widgets.Event;
21 import org.eclipse.swt.widgets.List;
22 import org.eclipse.swt.widgets.Listener;
23
24 /**
25 * @since 3.2
26 *
27 */
28 public class ListObservableValue : AbstractSWTObservableValue {
29
30 private final List list;
31
32 private bool updating = false;
33
34 private String currentValue;
35
36 private Listener listener;
37
38 /**
39 * @param list
40 */
41 public this(List list) {
42 super(list);
43 this.list = list;
44 this.currentValue = cast(String) doGetValue();
45
46 if ((list.getStyle() & SWT.MULTI) > 0)
47 throw new IllegalArgumentException(
48 "SWT.SINGLE support only for a List selection"); //$NON-NLS-1$
49
50 listener = new class() Listener {
51
52 public void handleEvent(Event event) {
53 if (!updating) {
54 Object oldValue = currentValue;
55 currentValue = cast(String) doGetValue();
56 fireValueChange(Diffs.createValueDiff(oldValue,
57 currentValue));
58 }
59 }
60
61 };
62 list.addListener(SWT.Selection, listener);
63 }
64
65 public void doSetValue(Object value) {
66 String oldValue = null;
67 if (list.getSelection() !is null && list.getSelection().length > 0)
68 oldValue = list.getSelection()[0];
69 try {
70 updating = true;
71 String items[] = list.getItems();
72 int index = -1;
73 if (items !is null && value !is null) {
74 for (int i = 0; i < items.length; i++) {
75 if (value.equals(items[i])) {
76 index = i;
77 break;
78 }
79 }
80 list.select(index); // -1 will not "unselect"
81 }
82 currentValue = cast(String) value;
83 } finally {
84 updating = false;
85 }
86 fireValueChange(Diffs.createValueDiff(oldValue, value));
87 }
88
89 public Object doGetValue() {
90 int index = list.getSelectionIndex();
91 if (index >= 0)
92 return list.getItem(index);
93 return null;
94 }
95
96 public Object getValueType() {
97 return String.classinfo;
98 }
99
100 /*
101 * (non-Javadoc)
102 *
103 * @see org.eclipse.core.databinding.observable.value.AbstractObservableValue#dispose()
104 */
105 public synchronized void dispose() {
106 super.dispose();
107 if (listener !is null && !list.isDisposed()) {
108 list.removeListener(SWT.Selection, listener);
109 }
110 }
111 }