comparison 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
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.ButtonObservableValue;
14
15 import java.lang.all;
16
17 import org.eclipse.core.databinding.observable.Diffs;
18 import org.eclipse.core.databinding.observable.Realm;
19 import org.eclipse.jface.internal.databinding.provisional.swt.AbstractSWTObservableValue;
20 import org.eclipse.swt.SWT;
21 import org.eclipse.swt.widgets.Button;
22 import org.eclipse.swt.widgets.Event;
23 import org.eclipse.swt.widgets.Listener;
24
25 /**
26 * @since 1.0
27 *
28 */
29 public class ButtonObservableValue : AbstractSWTObservableValue {
30
31 private final Button button;
32
33 private bool selectionValue;
34
35 private Listener updateListener = new class() Listener {
36 public void handleEvent(Event event) {
37 bool oldSelectionValue = selectionValue;
38 selectionValue = button.getSelection();
39
40 notifyIfChanged(oldSelectionValue, selectionValue);
41 }
42 };
43
44 /**
45 * @param button
46 */
47 public this(Button button) {
48 super(button);
49 this.button = button;
50 init();
51 }
52
53 /**
54 * @param realm
55 * @param button
56 */
57 public this(Realm realm, Button button) {
58 super(realm, button);
59 this.button = button;
60 init();
61 }
62
63 private void init() {
64 button.addListener(SWT.Selection, updateListener);
65 button.addListener(SWT.DefaultSelection, updateListener);
66 }
67
68 public void doSetValue(Object value) {
69 bool oldSelectionValue = selectionValue;
70 selectionValue = value is null ? false : (cast(Boolean) value)
71 .booleanValue();
72
73 button.setSelection(selectionValue);
74 notifyIfChanged(oldSelectionValue, selectionValue);
75 }
76
77 public Object doGetValue() {
78 return button.getSelection() ? Boolean.TRUE : Boolean.FALSE;
79 }
80
81 public Object getValueType() {
82 return Boolean.TYPE;
83 }
84
85 /*
86 * (non-Javadoc)
87 *
88 * @see org.eclipse.core.databinding.observable.value.AbstractObservableValue#dispose()
89 */
90 public synchronized void dispose() {
91 super.dispose();
92
93 if (!button.isDisposed()) {
94 button.removeListener(SWT.Selection, updateListener);
95 button.removeListener(SWT.DefaultSelection, updateListener);
96 }
97 }
98
99 /**
100 * Notifies consumers with a value change event only if a change occurred.
101 *
102 * @param oldValue
103 * @param newValue
104 */
105 private void notifyIfChanged(bool oldValue, bool newValue) {
106 if (oldValue !is newValue) {
107 fireValueChange(Diffs.createValueDiff(oldValue ? Boolean.TRUE : Boolean.FALSE,
108 newValue ? Boolean.TRUE : Boolean.FALSE));
109 }
110 }
111 }