Skip to main content
summaryrefslogtreecommitdiffstats
blob: e4aa8ba53b2961480c32fe1e2bb18fdb8227e178 (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
/**
 * Copyright (c) 2012 Mia-Software.
 *  
 * 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:
 *  	Alban Ménager (Soft-Maint) - Bug 387470 - [EFacet][Custom] Editors
 */
package org.eclipse.emf.facet.util.ui.internal.exported.util.widget.component.properties;

import org.eclipse.emf.facet.util.ui.internal.exported.util.widget.AbstractWidget;
import org.eclipse.emf.facet.util.ui.utils.PropertyElement;
import org.eclipse.emf.facet.util.ui.utils.UIUtils;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;

/**
 * This Abstract class provides all the elements to create:</p>
 * 
 * Label : [x]</p>
 * 
 * The label is set by {@link #getLabel()}. The checkbox is initialized with the
 * {@link PropertyElement} attributes
 * <nl>
 * <li>{@link PropertyElement#getValue()} (the value of the property must be a
 * boolean)</li>
 * <li>{@link PropertyElement#isChangeable()}</li>
 * </nl>
 * 
 * The {@link #getError()} method return 'null' because no error can be returned
 * (the value of the checkbox is 'true' or 'false').
 * 
 * @see GetChangeableWidget
 * @see GetOrderedWidget
 * @see GetDerivedWidget
 * @see GetTransientWidget
 * @see GetUniqueWidget
 * @see GetVolatileWidget
 * @since 0.3
 */
public abstract class AbstractGetPropertyWidget extends AbstractWidget {

	private static final int COLUMN_NUMBER = 2;
	private static final int WIDTH_HINT_DEF = 65;
	private int labelWidthHint;
	private final PropertyElement propertyElement;
	private Button propertyButton;

	/**
	 * Constructor. Initialize the composite with a layout of
	 * {@value #COLUMN_NUMBER} columns and the layoutData with the
	 * 'fillGridData' attribute of {@link AbstractWidget}.
	 * 
	 * @param parent
	 *            the parent containing this composite.
	 * @param style
	 *            the style of the composite.
	 * @param editingDomain
	 *            the current editing domain.
	 * @param propertyElement
	 *            the property element that this composite will edit.
	 * 
	 * @see AbstractWidget
	 */
	protected AbstractGetPropertyWidget(final Composite parent,
			final PropertyElement propertyElement) {
		super(parent);
		this.propertyElement = propertyElement;
		this.labelWidthHint = WIDTH_HINT_DEF;
		this.setLayout(new GridLayout(COLUMN_NUMBER, false));
		this.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
	}

	@Override
	protected void addSubWidgets() {
		if ((getLabel() != null) && !"".equals(getLabel())) { //$NON-NLS-1$
			final Label label = UIUtils.createLabel(this, getLabel());
			final GridData layoutData = new GridData(SWT.FILL);
			layoutData.widthHint = this.labelWidthHint;
			label.setLayoutData(layoutData);
			label.setEnabled(this.propertyElement.isChangeable());
		}
		createCheckbox();
	}

	public void setPropertyEnabled(final boolean enabled) {
		this.propertyButton.setSelection(enabled);
	}

	/**
	 * Create a checkbox initialized with the {@link PropertyElement} attributes
	 * <nl>
	 * <li>Default value : {@link PropertyElement#getValue()} (the value of the
	 * property must be a boolean)</li>
	 * <li>Enabled : {@link PropertyElement#isChangeable()}</li>
	 * </nl>
	 */
	private void createCheckbox() {
		if (this.propertyElement.getValue() == null) {
			this.propertyElement.setValue(Boolean.TRUE);
		}
		SelectionListener selectionListener = null;
		if (this.propertyElement.isChangeable()) {
			selectionListener = new SelectionListener() {

				public void widgetSelected(final SelectionEvent event) {
					onSelection((Button) event.getSource());
				}

				public void widgetDefaultSelected(final SelectionEvent event) {
					// Nothing.
				}
			};
		}
		this.propertyButton = UIUtils.createCheckbox(this,
				this.propertyElement.isChangeable(),
				((Boolean) this.propertyElement.getValue()).booleanValue(),
				selectionListener);
	}

	/**
	 * @param source
	 */
	protected void onSelection(final Button source) {
		this.propertyElement.setValue(Boolean.valueOf(source.getSelection()));
	}

	/**
	 * @return the label of the label field. If no label has to be displayed,
	 *         null or void must be returned.
	 */
	protected abstract String getLabel();

	/**
	 * Method to change the distance between the label and the checkbox.
	 * 
	 * @param lABEL_WIDTH_HINT
	 *            the lABEL_WIDTH_HINT to set
	 */
	public void setLabelWidthHint(final int labelWidthHint) {
		this.labelWidthHint = labelWidthHint;
	}

	/**
	 * @return the value of the property.
	 */
	public boolean isSelected() {
		return this.propertyButton.getSelection();
	}
}

Back to the top