Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: d73930c0730465ea69e18e2017d704000673c80d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*****************************************************************************
 * Copyright (c) 2011 CEA LIST.
 *
 * 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:
 *  Camille Letavernier (CEA LIST) camille.letavernier@cea.fr - Initial API and implementation
 *****************************************************************************/
package org.eclipse.papyrus.infra.widgets.databinding;

import org.eclipse.core.databinding.observable.value.AbstractObservableValue;
import org.eclipse.core.databinding.observable.value.ValueDiff;
import org.eclipse.papyrus.infra.tools.databinding.AggregatedObservable;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.widgets.Button;

/**
 * An ObservableValue for {@link Button}s with the {@link SWT.CHECK} style
 * Allows the checkbox to take four states ; one for each possible combination
 * of {@link Button#getSelection()} and {@link Button#getGrayed()}
 *
 * @author Camille Letavernier
 */
public class GrayedCheckboxObservableValue extends AbstractObservableValue implements SelectionListener {

	private Button checkbox;

	private Boolean currentValue;

	private AggregatedObservable aggregated;

	/**
	 * Constructor
	 *
	 * @param checkbox
	 *            The observed checkbox
	 * @param aggregated
	 *            The Observable aggregating the various observable booleans
	 */
	public GrayedCheckboxObservableValue(Button checkbox, AggregatedObservable aggregated) {
		this.checkbox = checkbox;
		this.checkbox.addSelectionListener(this);
		this.aggregated = aggregated;
	}

	@Override
	public Object getValueType() {
		return Object.class; // Can be either Boolean or BooleanWithDefaultState
	}

	@Override
	protected Boolean doGetValue() {
		return checkbox.getSelection();
	}

	@Override
	protected void doSetValue(Object value) {
		if (aggregated.hasDifferentValues()) {
			checkbox.setSelection(true);
			checkbox.setGrayed(true);
			return;
		} else {
			checkbox.setGrayed(false);
		}

		if (value instanceof Boolean) {
			Boolean booleanValue = (Boolean) value;
			checkbox.setSelection(booleanValue);

			this.currentValue = booleanValue;
		} else if (value == null) {
			checkbox.setSelection(false);
		}
	}

	@Override
	public synchronized void dispose() {
		checkbox.removeSelectionListener(this);
		super.dispose();
	}

	@Override
	public void widgetSelected(SelectionEvent e) {
		final Boolean oldValue = currentValue;
		final Boolean newValue = checkbox.getSelection();

		currentValue = newValue;
		checkbox.setGrayed(false);

		fireValueChange(new ValueDiff() {

			@Override
			public Object getOldValue() {
				return oldValue;
			}

			@Override
			public Object getNewValue() {
				return newValue;
			}
		});
	}

	@Override
	public void widgetDefaultSelected(SelectionEvent e) {
		// Nothing
	}

}

Back to the top