Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: fae53605829df7221d85267370bf8bcf378f4ba7 (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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/**
 * 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.papyrus.emf.facet.util.ui.utils;

import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyListener;
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.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Spinner;
import org.eclipse.swt.widgets.Text;

/**
 * This util class provides methods for the creation of basics ui components
 * like buttons or text field.
 *
 * @since 0.3
 */
public final class UIUtils {

	private static final int WIDTH_HINT = 110;

	private UIUtils() {
		// Private constructor.
	}

	/**
	 * Create the label (with the given labelName) in the given composite. The
	 * layoutData of the label is {@link #getLabelGridData()}.
	 *
	 * @param parent
	 *            the composite parent.
	 * @param labelName
	 *            the label name.
	 */
	public static Label createLabel(final Composite parent,
			final String labelName) {
		final Label label = new Label(parent, SWT.NONE);
		label.setText(labelName);
		label.setLayoutData(getLabelGridData());
		return label;
	}

	/**
	 * Create and return a new button.
	 *
	 * @param parent
	 *            the button parent.
	 * @param text
	 *            the text to display on the button.
	 * @param enabled
	 *            set if the button can be pressed or not.
	 * @param selectionListener
	 *            the listener when the button is pressed (can be null).
	 * @return the new button.
	 */
	public static Button createButton(final Composite parent,
			final String text, final boolean enabled,
			final SelectionListener selectionListener) {
		final Button button = new Button(parent, SWT.NONE);
		button.setText(text);
		button.setEnabled(enabled);
		if (selectionListener != null) {
			button.addSelectionListener(selectionListener);
		}
		return button;
	}

	/**
	 * Create and return a new text field.
	 *
	 * @param parent
	 *            the text field parent.
	 * @param text
	 *            the text to display on the text field.
	 * @param enabled
	 *            set if the text field can be edit or not.
	 * @param selectListener
	 *            the listener when the text field is edited (can be null).
	 * @return the new text field.
	 */
	public static Text createTextField(final Composite parent,
			final String text, final boolean enabled,
			final ModifyListener modifyListener) {
		final Text textField = new Text(parent, SWT.BORDER);
		textField.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		textField.setEnabled(enabled);
		if (text != null) {
			textField.setText(text);
		}
		if (modifyListener != null) {
			textField.addModifyListener(modifyListener);
		}
		return textField;
	}

	/**
	 * Create a combobox with a label.
	 *
	 * @param parent
	 *            the parent of the combobox.
	 * @param enabled
	 *            if the combobox is enabled.
	 * @param elements
	 *            the elements of the combobox
	 * @param listener
	 *            the listener of the combobox (can be null)
	 * @return the combobox created.
	 */
	public static Combo createCombobox(final Composite parent,
			final boolean enabled, final List<String> elements,
			final String label, final SelectionListener listener) {
		final Composite composite = new Composite(parent, SWT.NONE);
		final GridLayout gridLayout = new GridLayout(2, false);
		gridLayout.marginWidth = 0;
		composite.setLayout(gridLayout);
		final Label comboboxLabel = new Label(composite, SWT.NONE);
		comboboxLabel.setText(label);
		final GridData gridData = new GridData(SWT.FILL);
		comboboxLabel.setLayoutData(gridData);
		final Combo combobox = new Combo(composite, SWT.DROP_DOWN
				| SWT.READ_ONLY);
		combobox.setLayoutData(new GridData(SWT.FILL));
		combobox.setEnabled(enabled);
		for (final String element : elements) {
			combobox.add(element);
		}
		combobox.select(0);
		if (listener != null) {
			combobox.addSelectionListener(listener);
		}
		return combobox;
	}

	/**
	 * Create and return a new text field.
	 *
	 * @param parent
	 *            the text field parent.
	 * @param text
	 *            the text to display on the text field.
	 * @param enabled
	 *            set if the text field can be edit or not.
	 * @param selectListener
	 *            the listener when the text field is edited (can be null).
	 * @return the new text field.
	 */
	public static Text createTextArea(final Composite parent,
			final String text, final boolean enabled,
			final ModifyListener modifyListener) {
		final Text textArea = new Text(parent, SWT.MULTI | SWT.BORDER
				| SWT.WRAP | SWT.V_SCROLL);
		textArea.setLayoutData(new GridData(GridData.FILL_BOTH));
		textArea.setEnabled(enabled);
		if (text != null) {
			textArea.setText(text);
		}
		if (modifyListener != null) {
			textArea.addModifyListener(modifyListener);
		}
		return textArea;
	}

	/**
	 * @return the labelGridData
	 */
	public static GridData getLabelGridData() {
		final GridData labelGridData = new GridData(SWT.FILL);
		labelGridData.widthHint = WIDTH_HINT;
		return labelGridData;
	}

	/**
	 * Create and return a new checkbox.
	 *
	 * @param parent
	 *            the checkbox parent.
	 * @param enabled
	 *            set if the checkbox can be checked or not.
	 * @param selected
	 *            set if the checkbox is initially selected or not.
	 * @param selectionListener
	 *            the listener when the checkbox is selected (can be null).
	 * @return the new checkbox.
	 */
	public static Button createCheckbox(final Composite composite,
			final boolean enabled, final boolean selected,
			final SelectionListener selectionListener) {
		final Button checkbox = new Button(composite, SWT.CHECK);
		checkbox.setEnabled(enabled);
		checkbox.setSelection(selected);
		checkbox.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		if (selectionListener != null) {
			checkbox.addSelectionListener(selectionListener);
		}
		return checkbox;
	}

	/**
	 * Create and return a new spinner.
	 *
	 * @param parent
	 *            the parent of the spinner.
	 * @param maxValue
	 *            the max value the spinner can reach.
	 * @param minValue
	 *            the min value the spinner can reach.
	 * @param increment
	 *            the increment of the spinner.
	 * @param changeable
	 *            if the spinner is changeable or not.
	 * @param modifyListener
	 *            the listener when the spinner is changed (can be null).
	 * @return the new spinner.
	 *
	 * @see Spinner
	 */
	public static Spinner createSpinner(final Composite parent,
			final int maxValue, final int minValue, final int increment,
			final boolean changeable, final ModifyListener modifyListener) {
		final Spinner spinner = new Spinner(parent, SWT.BORDER);
		spinner.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		spinner.setMaximum(maxValue);
		spinner.setMinimum(minValue);
		spinner.setIncrement(increment);
		spinner.setEnabled(changeable);
		if (modifyListener != null) {
			spinner.addModifyListener(modifyListener);
		}
		return spinner;
	}

}

Back to the top