diff 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
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/org.eclipse.jface.databinding/src/org/eclipse/jface/internal/databinding/swt/ListObservableValue.d	Tue Apr 14 11:35:29 2009 +0200
@@ -0,0 +1,111 @@
+/*******************************************************************************
+ * Copyright (c) 2005, 2008 IBM Corporation and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *     IBM Corporation - initial API and implementation
+ *     Brad Reynolds - bug 164653
+ *     Ashley Cambrell - bug 198904
+ *******************************************************************************/
+module org.eclipse.jface.internal.databinding.swt.ListObservableValue;
+
+import java.lang.all;
+
+import org.eclipse.core.databinding.observable.Diffs;
+import org.eclipse.jface.internal.databinding.provisional.swt.AbstractSWTObservableValue;
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.widgets.Event;
+import org.eclipse.swt.widgets.List;
+import org.eclipse.swt.widgets.Listener;
+
+/**
+ * @since 3.2
+ * 
+ */
+public class ListObservableValue : AbstractSWTObservableValue {
+
+    private final List list;
+
+    private bool updating = false;
+
+    private String currentValue;
+
+    private Listener listener;
+
+    /**
+     * @param list
+     */
+    public this(List list) {
+        super(list);
+        this.list = list;
+        this.currentValue = cast(String) doGetValue();
+
+        if ((list.getStyle() & SWT.MULTI) > 0)
+            throw new IllegalArgumentException(
+                    "SWT.SINGLE support only for a List selection"); //$NON-NLS-1$
+
+        listener = new class() Listener {
+
+            public void handleEvent(Event event) {
+                if (!updating) {
+                    Object oldValue = currentValue;
+                    currentValue = cast(String) doGetValue();
+                    fireValueChange(Diffs.createValueDiff(oldValue,
+                            currentValue));
+                }
+            }
+
+        };
+        list.addListener(SWT.Selection, listener);
+    }
+
+    public void doSetValue(Object value) {
+        String oldValue = null;
+        if (list.getSelection() !is null && list.getSelection().length > 0)
+            oldValue = list.getSelection()[0];
+        try {
+            updating = true;
+            String items[] = list.getItems();
+            int index = -1;
+            if (items !is null && value !is null) {
+                for (int i = 0; i < items.length; i++) {
+                    if (value.equals(items[i])) {
+                        index = i;
+                        break;
+                    }
+                }
+                list.select(index); // -1 will not "unselect"
+            }
+            currentValue = cast(String) value;
+        } finally {
+            updating = false;
+        }
+        fireValueChange(Diffs.createValueDiff(oldValue, value));
+    }
+
+    public Object doGetValue() {
+        int index = list.getSelectionIndex();
+        if (index >= 0)
+            return list.getItem(index);
+        return null;
+    }
+
+    public Object getValueType() {
+        return String.classinfo;
+    }
+
+    /*
+     * (non-Javadoc)
+     *
+     * @see org.eclipse.core.databinding.observable.value.AbstractObservableValue#dispose()
+     */
+    public synchronized void dispose() {
+        super.dispose();
+        if (listener !is null && !list.isDisposed()) {
+            list.removeListener(SWT.Selection, listener);
+        }
+    }
+}