Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: aa7e8228f4005dcb3e411e8b1060de5ca43c84b0 (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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*****************************************************************************
 * Copyright (c) 2010, 2016 CEA LIST, Esterel Technologies SAS and others.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *  Camille Letavernier (CEA LIST) camille.letavernier@cea.fr - Initial API and implementation
 *  Thibault Le Ouay t.leouay@sherpa-eng.com - Add binding implementation
 *  Sebastien Gabel (Esterel Technologies SAS) - Bug 497361
 *
 *****************************************************************************/
package org.eclipse.papyrus.infra.widgets.editors;

import org.eclipse.core.databinding.observable.value.IObservableValue;
import org.eclipse.core.databinding.observable.value.SelectObservableValue;
import org.eclipse.jface.databinding.swt.typed.WidgetProperties;
import org.eclipse.jface.fieldassist.ControlDecoration;
import org.eclipse.jface.fieldassist.FieldDecorationRegistry;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;

/**
 * A Property Editor representing a Boolean value
 * as a Radio, with two options (true/false).
 *
 * @author Camille Letavernier
 *
 */
public class BooleanRadio extends AbstractValueEditor {

	/**
	 * The "true" radio button
	 */
	protected Button trueRadio;

	/**
	 * The "false" radio button
	 */
	protected Button falseRadio;

	private ControlDecoration controlDecoration;


	/**
	 *
	 * Constructor. Creates a new Property Editor for a Boolean
	 * value, represented by two radio buttons.
	 *
	 * @param parent
	 *            This editor's parent composite
	 * @param style
	 *            The style applied to this editor's radio buttons
	 */
	public BooleanRadio(Composite parent, int style) {
		this(parent, style, null);
	}

	/**
	 *
	 * Constructor. Creates a new Property Editor for a Boolean
	 * value, represented by two radio buttons.
	 *
	 * @param parent
	 *            This editor's parent composite
	 * @param style
	 *            The style applied to this editor's radio buttons
	 * @param label
	 *            The label for this editor
	 */
	public BooleanRadio(Composite parent, int style, String label) {
		super(parent, label);

		((GridLayout) getLayout()).numColumns = 3;

		trueRadio = factory.createButton(this, "true", style | SWT.RADIO); //$NON-NLS-1$
		trueRadio.setBackground(this.getBackground()); // For Radio buttons, we need to force the color

		falseRadio = factory.createButton(this, "false", style | SWT.RADIO); //$NON-NLS-1$
		falseRadio.setBackground(this.getBackground()); // For Radio buttons, we need to force the color

		setWidgetObservable(getObservable(), true);
		controlDecoration = new ControlDecoration(trueRadio, SWT.TOP | SWT.LEFT);

		// Use a dedicated grid data for each radio, otherwise one of the two labels may be truncated.
		GridData trueGd = new GridData();
		trueGd.horizontalIndent = FieldDecorationRegistry.getDefault().getMaximumDecorationWidth();
		trueRadio.setLayoutData(trueGd);

		GridData falseGd = new GridData();
		falseGd.horizontalIndent = FieldDecorationRegistry.getDefault().getMaximumDecorationWidth();
		falseRadio.setLayoutData(falseGd);
	}

	/**
	 * Defines a single observable value, encapsulating the ones
	 * from each radio button (true / false)
	 *
	 * @return The encapsulating observable value
	 */
	private IObservableValue getObservable() {
		IObservableValue trueObservable = WidgetProperties.widgetSelection().observe(trueRadio);
		IObservableValue falseObservable = WidgetProperties.widgetSelection().observe(falseRadio);

		SelectObservableValue observable = new SelectObservableValue();
		observable.addOption(true, trueObservable);
		observable.addOption(false, falseObservable);

		return observable;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public Object getEditableType() {
		return Boolean.class;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public Boolean getValue() {
		return trueRadio.getSelection();
	}

	@Override
	public void setReadOnly(boolean readOnly) {
		trueRadio.setEnabled(!readOnly);
		falseRadio.setEnabled(!readOnly);
	}

	@Override
	public boolean isReadOnly() {
		return !trueRadio.isEnabled() || !falseRadio.isEnabled();
	}

	@Override
	public void setToolTipText(String text) {
		trueRadio.setToolTipText(text);
		falseRadio.setToolTipText(text);
		super.setLabelToolTipText(text);
	}

	public void setTrueLabel(String label) {
		trueRadio.setText(label);
	}

	public String getTrueLabel() {
		return trueRadio.getText();
	}

	public void setFalseLabel(String label) {
		falseRadio.setText(label);
	}

	public String getFalseLabel() {
		return falseRadio.getText();
	}
}

Back to the top