Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 50ac60924ab34808e07d169e7ad101ea11b2dfa9 (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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/**
 * 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
 *  	Grégoire Dupé (Mia-Software) - Bug 387470 - [EFacet][Custom] Editors
 */
package org.eclipse.papyrus.emf.facet.efacet.sdk.ui.internal.query;

import java.util.LinkedList;
import java.util.List;

import org.eclipse.papyrus.emf.facet.efacet.metamodel.v0_2_0.efacet.extensible.Query;
import org.eclipse.papyrus.emf.facet.efacet.sdk.ui.internal.Messages;
import org.eclipse.papyrus.emf.facet.efacet.sdk.ui.internal.exported.IQueryContext;
import org.eclipse.papyrus.emf.facet.efacet.sdk.ui.internal.exported.IQueryDialogFactoryStrategy;
import org.eclipse.papyrus.emf.facet.efacet.sdk.ui.internal.exported.widget.component.query.ICreateQueryWidget;
import org.eclipse.papyrus.emf.facet.util.ui.internal.exported.dialog.IDialog;
import org.eclipse.papyrus.emf.facet.util.ui.internal.exported.dialog.IWithResultDialogCallback;
import org.eclipse.papyrus.emf.facet.util.ui.internal.exported.util.dialog.SynchronizedAbstractDialog;
import org.eclipse.papyrus.emf.facet.util.ui.internal.exported.util.widget.AbstractWidget;
import org.eclipse.papyrus.emf.facet.util.ui.utils.PropertyElement;
import org.eclipse.papyrus.emf.facet.util.ui.utils.UIUtils;
import org.eclipse.papyrus.emf.facet.util.ui.utils.WidgetProperties;
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.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Text;

/**
 * Provide the composite for the query type selection.</p>
 * 
 * Label : [ combobox ] [ TextField ]
 * 
 * When the user select a {@link AbstractQueryDialogFactory}, the associated
 * dialog {@link GetQueryDialog} is created (with the method
 * {@link #createQueryWidgetDialog(AbstractQueryDialogFactory)} using the
 * attributes of the query type) and displayed.
 * 
 * @see Query
 * @see AbstractQueryDialogFactory
 * @see GetQueryDialog
 */
public class SelectQueryTypeWidget extends AbstractWidget {

	private static final int COLUMN_NUMBER = 2;
	private Combo combobox;
	private final PropertyElement queryProp;
	private Text textField;
	private final GridLayout gridLayout;
	private String alertMessage;
	private final boolean comboboxEnabled;
	private final IQueryContext queryContext;

	/**
	 * Constructor.
	 * 
	 * @param parent
	 *            the parent of the composite.
	 * @param properties
	 *            the {@link WidgetProperties} of the parent.
	 * @param queryProp
	 *            the property element that this composite will edit.
	 */
	public SelectQueryTypeWidget(final Composite parent,
			final PropertyElement propertyElement,
			final IQueryContext queryContext) {
		this(parent, propertyElement, false, queryContext);
	}

	/**
	 * Constructor.
	 * 
	 * @param parent
	 *            the parent of the composite.
	 * @param properties
	 *            the {@link WidgetProperties} of the parent.
	 * @param queryProp
	 *            the property element that this composite will edit.
	 */
	public SelectQueryTypeWidget(final Composite parent,
			final PropertyElement queryProperty, final boolean comboboxEnabled,
			final IQueryContext queryContext) {
		super(parent);
		this.queryProp = queryProperty;
		this.comboboxEnabled = comboboxEnabled;
		this.gridLayout = new GridLayout(COLUMN_NUMBER, false);
		this.gridLayout.marginWidth = 0;
		this.queryContext = queryContext;
	}

	@Override
	protected void addSubWidgets() {
		final Composite composite = new Composite(this, SWT.NONE);
		composite.setLayout(this.gridLayout);
		composite.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		boolean enabled = this.comboboxEnabled;
		if (!this.comboboxEnabled) {
			enabled = this.queryProp.getValue() != null;
		}

		final List<String> elements = new LinkedList<String>();
		elements.add(Messages.Select_query_type);
		elements.addAll(IQueryWidgetCompositeFactoryFactory.INSTANCE
				.getRegisteredQueryWidgetsComposite());
		final SelectionListener listener = new SelectionListener() {

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

			public void widgetDefaultSelected(final SelectionEvent event) {
				// NOTHING
			}
		};
		this.combobox = UIUtils.createCombobox(composite, enabled, elements,
				Messages.SelectQueryTypeWizardPage_Query_Type, listener);
		this.textField = UIUtils.createTextField(composite, "", false, null); //$NON-NLS-1$
		refresh();
	}

	/**
	 * Select the item in the combobox with the name in parameter.
	 * 
	 * @param selection
	 *            the name of the selection.
	 */
	public IDialog<ICreateQueryWidget> selectQueryType(final String selection) {
		IDialog<ICreateQueryWidget> result = null;
		int index = 0;
		for (final String item : this.combobox.getItems()) {
			if (item.equals(selection)) {
				this.combobox.select(index);
				result = onSelection();
				break;
			}
			++index;
		}
		return result;
	}

	@Override
	public String getError() {
		String result = null;

		if (this.queryProp.getValue() == null) {
			result = Messages.Facet_mustSelectQuery;
		}

		return result;
	}

	/**
	 * This method check if the comboBox is enabled and set the default value if
	 * not (combobox selection and textField alert message).
	 */
	private void refresh() {
		this.combobox.select(0);
		if (this.combobox.isEnabled()) {
			if (this.queryProp.getValue() != null) {
				final Query query = (Query) this.queryProp.getValue();
				this.textField.setText(query.toString());
			}
		} else {
			this.queryProp.setValue(null);
			// The alert message has the default color because it's not possible
			// to change the text color if the text field is disabled.
			if (this.alertMessage != null) {
				this.textField.setText(this.alertMessage);
			}
		}
	}

	/**
	 * @param enabled
	 *            set if the selection combobox is enabled or not.
	 */
	public void setSelectionEnabled(final boolean enabled) {
		this.combobox.setEnabled(enabled);
		refresh();
	}

	/**
	 * @return if the combobox is enabled or not.
	 */
	public boolean isSelectionEnabled() {
		return this.combobox.isEnabled();
	}

	/**
	 * Set the alert message that will be displayed if the checkbox is not
	 * checked.
	 * 
	 * @param alertMessage
	 *            the alertMessage to set
	 */
	public void setAlertMessage(final String alertMessage) {
		this.alertMessage = alertMessage;
	}

	/**
	 * The action when an element of the combobox is selected.</p>
	 * 
	 * If the default element is selected, the property value is set to null.
	 */
	protected IDialog<ICreateQueryWidget> onSelection() {
		IDialog<ICreateQueryWidget> result = null;
		final int selectionIndex = this.combobox.getSelectionIndex();
		if (selectionIndex == 0) {
			this.queryProp.setValue(null);
			this.textField.setText(this.alertMessage);
		} else {
			final String selectionName = this.combobox.getItem(selectionIndex);
			final IQueryDialogFactoryStrategy strategy = IQueryWidgetCompositeFactoryFactory.INSTANCE
					.getQueryDialogFactoryStrategy(selectionName);
			final IWithResultDialogCallback<IQueryCreationResult> callback = new IWithResultDialogCallback<IQueryCreationResult>() {
				public void commited(final IQueryCreationResult qcResult) {
					onDialogCommitted(qcResult);
				}

				public void canceled(final IQueryCreationResult qcResult) {
					checkQuery(null);
				}
			};
			final IDialog<ICreateQueryWidget> dialog = new GetQueryDialog(
					callback,
					strategy, this.queryContext);
			Display.getDefault().asyncExec(new Runnable() {
				public void run() {
					dialog.open();
				}
			});
			result = new SynchronizedAbstractDialog<ICreateQueryWidget>(dialog,
					Display.getDefault());
		}
		return result;
	}

	@Override
	public void notifyChanged() {
		// No action has to be done if a change appends.
	}

	/**
	 * Action to do when the dialog opened with the button "..." is closed with
	 * the "OK" button.
	 */
	protected void onDialogCommitted(final IQueryCreationResult qcResult) {
		if (checkQuery(qcResult.getQuery()) && (qcResult.getLabel() != null)) {
			this.textField.setText(qcResult.getLabel());
		}
	}

	/**
	 * Check if the query is the query exists.
	 * 
	 * @param query
	 * 
	 * @return true if the query exists.
	 */
	protected boolean checkQuery(final Query query) {
		if (query == null) {
			this.combobox.select(0);
			if (this.alertMessage != null) {
				this.textField.setText(this.alertMessage);
			}
		}
		if (query != null) {
			this.queryProp.setValue(query);
		}
		return query != null;
	}

	/**
	 * @return the text of the text associated with the query.
	 */
	public String getText() {
		return this.textField.getText();
	}
}

Back to the top