diff org.eclipse.jface.databinding/src/org/eclipse/jface/internal/databinding/swt/ButtonObservableValue.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/ButtonObservableValue.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.ButtonObservableValue;
+
+import java.lang.all;
+
+import org.eclipse.core.databinding.observable.Diffs;
+import org.eclipse.core.databinding.observable.Realm;
+import org.eclipse.jface.internal.databinding.provisional.swt.AbstractSWTObservableValue;
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.widgets.Button;
+import org.eclipse.swt.widgets.Event;
+import org.eclipse.swt.widgets.Listener;
+
+/**
+ * @since 1.0
+ * 
+ */
+public class ButtonObservableValue : AbstractSWTObservableValue {
+
+    private final Button button;
+
+    private bool selectionValue;
+
+    private Listener updateListener = new class() Listener {
+        public void handleEvent(Event event) {
+            bool oldSelectionValue = selectionValue;
+            selectionValue = button.getSelection();
+                        
+            notifyIfChanged(oldSelectionValue, selectionValue);
+        }
+    };
+
+    /**
+     * @param button
+     */
+    public this(Button button) {
+        super(button);
+        this.button = button;
+        init();
+    }
+    
+    /**
+     * @param realm
+     * @param button
+     */
+    public this(Realm realm, Button button) {
+        super(realm, button);
+        this.button = button;
+        init();
+    }
+    
+    private void init() {
+        button.addListener(SWT.Selection, updateListener);
+        button.addListener(SWT.DefaultSelection, updateListener);       
+    }
+
+    public void doSetValue(Object value) {
+        bool oldSelectionValue = selectionValue;
+        selectionValue = value is null ? false : (cast(Boolean) value)
+                .booleanValue();
+        
+        button.setSelection(selectionValue);
+        notifyIfChanged(oldSelectionValue, selectionValue);
+    }
+
+    public Object doGetValue() {
+        return button.getSelection() ? Boolean.TRUE : Boolean.FALSE;
+    }
+
+    public Object getValueType() {
+        return Boolean.TYPE;
+    }
+    
+    /*
+     * (non-Javadoc)
+     *
+     * @see org.eclipse.core.databinding.observable.value.AbstractObservableValue#dispose()
+     */
+    public synchronized void dispose() {
+        super.dispose();
+
+        if (!button.isDisposed()) {
+            button.removeListener(SWT.Selection, updateListener);
+            button.removeListener(SWT.DefaultSelection, updateListener);
+        }
+    }
+
+    /**
+     * Notifies consumers with a value change event only if a change occurred.
+     * 
+     * @param oldValue
+     * @param newValue
+     */
+    private void notifyIfChanged(bool oldValue, bool newValue) {
+        if (oldValue !is newValue) {
+            fireValueChange(Diffs.createValueDiff(oldValue ? Boolean.TRUE : Boolean.FALSE,
+                    newValue ? Boolean.TRUE : Boolean.FALSE));
+        }       
+    }
+}