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