-
Notifications
You must be signed in to change notification settings - Fork 3
/
SWTPromptServices.java
430 lines (371 loc) · 12.3 KB
/
SWTPromptServices.java
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
/*******************************************************************************
* Copyright (c) 2019, 2023 Obeo.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v20.html
*
* Contributors:
* Obeo - initial API and implementation
*
*******************************************************************************/
package org.eclipse.acceleo.query.ide.ui.services;
import java.lang.reflect.Method;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.eclipse.acceleo.annotations.api.documentation.Documentation;
import org.eclipse.acceleo.annotations.api.documentation.Example;
import org.eclipse.acceleo.annotations.api.documentation.Param;
import org.eclipse.acceleo.annotations.api.documentation.ServiceProvider;
import org.eclipse.acceleo.query.ast.Call;
import org.eclipse.acceleo.query.ide.ui.dialog.AbstractPromptDialog;
import org.eclipse.acceleo.query.ide.ui.dialog.EObjectSelectionDialog;
import org.eclipse.acceleo.query.runtime.IReadOnlyQueryEnvironment;
import org.eclipse.acceleo.query.runtime.IService;
import org.eclipse.acceleo.query.runtime.IValidationResult;
import org.eclipse.acceleo.query.runtime.impl.AbstractServiceProvider;
import org.eclipse.acceleo.query.runtime.impl.JavaMethodService;
import org.eclipse.acceleo.query.runtime.impl.ValidationServices;
import org.eclipse.acceleo.query.validation.type.IType;
import org.eclipse.emf.common.notify.AdapterFactory;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.edit.provider.ComposedAdapterFactory;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.widgets.Display;
/**
* Services prompting for value.
*
* @author <a href="mailto:yvan.lussaud@obeo.fr">Yvan Lussaud</a>
*/
// @formatter:off
@ServiceProvider(value = "Services available for prompting user for values using SWT for graphical user interface.")
// @formatter:on
@SuppressWarnings({"checkstyle:javadocmethod", "checkstyle:javadoctype", "restriction" })
public class SWTPromptServices extends AbstractServiceProvider {
private static class EOBjectPromptService extends JavaMethodService {
/**
* Constructor.
*
* @param publicMethod
* the {@link Method}
* @param serviceInstance
* the instance
* @param forWorkspace
* tells if the {@link IService} will be used in a workspace
*/
EOBjectPromptService(Method publicMethod, Object serviceInstance, boolean forWorkspace) {
super(publicMethod, serviceInstance, forWorkspace);
}
@Override
public Set<IType> getType(Call call, ValidationServices services, IValidationResult validationResult,
IReadOnlyQueryEnvironment environment, List<IType> argTypes) {
return new LinkedHashSet<>(argTypes);
}
@Override
public Set<IType> validateAllType(ValidationServices services,
IReadOnlyQueryEnvironment queryEnvironment, Map<List<IType>, Set<IType>> allTypes) {
final Set<IType> res = new LinkedHashSet<>();
for (Set<IType> types : allTypes.values()) {
res.addAll(types);
}
return res;
}
}
/**
* A {@link Runnable} with a result value.
*
* @author <a href="mailto:yvan.lussaud@obeo.fr">Yvan Lussaud</a>
* @param <T>
* the kind of result
*/
private abstract static class RunnableWithResult<T> implements Runnable {
/**
* The result.
*/
protected T result;
/**
* Gets the result.
*
* @return the result
*/
public T getResult() {
return result;
};
}
/**
* The cancel message.
*/
private static final String M2DOC_DIALOG_CANCELLED = "M2Doc dialog cancelled.";
/**
* The {@link ResourceSet} for model elements.
*/
private final ResourceSet resourceSetForModels;
/**
* The {@link IReadOnlyQueryEnvironment}.
*/
private final IReadOnlyQueryEnvironment queryEnvironment;
/**
* The {@link AdapterFactory}.
*/
private final ComposedAdapterFactory adapterFactory = new ComposedAdapterFactory(
ComposedAdapterFactory.Descriptor.Registry.INSTANCE);
/**
* Constructor.
*
* @param queryEnvironment
* the {@link IReadOnlyQueryEnvironment}
* @param resourceSetForModels
* the {@link ResourceSet} for model elements
*/
public SWTPromptServices(IReadOnlyQueryEnvironment queryEnvironment, ResourceSet resourceSetForModels) {
this.resourceSetForModels = resourceSetForModels;
this.queryEnvironment = queryEnvironment;
}
// @formatter:off
@Documentation(value = "Prompts for a String value with the given message.", params = {
@Param(name = "message", value = "The message displayed to the user"), }, result = "Prompts the user and return the input of the user as a String.", examples = {
@Example(expression = "'Enter your name: '.promptString()", result = "prompts the user"), })
// @formatter:on
public String promptString(final String message) {
final RunnableWithResult<String> runnable = new RunnableWithResult<String>() {
@Override
public void run() {
final AbstractPromptDialog dialog = new AbstractPromptDialog(Display.getDefault()
.getActiveShell(), message) {
@Override
public boolean validate(String input) {
return input != null;
}
};
if (dialog.open() == Window.OK) {
result = dialog.getValue();
} else {
result = null;
}
}
};
Display.getDefault().syncExec(runnable);
if (runnable.getResult() != null) {
return runnable.getResult();
} else {
throw new IllegalStateException(M2DOC_DIALOG_CANCELLED);
}
}
// @formatter:off
@Documentation(value = "Prompts for a Integer value with the given message.", params = {
@Param(name = "message", value = "The message displayed to the user"), }, result = "Prompts the user and return the input of the user as an Integer.", examples = {
@Example(expression = "'Enter your age: '.promptInteger()", result = "prompts the user"), })
// @formatter:on
public Integer promptInteger(final String message) {
final RunnableWithResult<Integer> runnable = new RunnableWithResult<Integer>() {
@Override
public void run() {
final AbstractPromptDialog dialog = new AbstractPromptDialog(Display.getDefault()
.getActiveShell(), message) {
@Override
public boolean validate(String input) {
return validateInteger(input);
}
};
if (dialog.open() == Window.OK) {
result = Integer.valueOf(dialog.getValue());
} else {
result = null;
}
}
};
Display.getDefault().syncExec(runnable);
if (runnable.getResult() != null) {
return runnable.getResult();
} else {
throw new IllegalStateException(M2DOC_DIALOG_CANCELLED);
}
}
private boolean validateInteger(String input) {
boolean res;
try {
Integer.valueOf(input);
res = true;
// CHECKSTYLE:OFF
} catch (Exception e) {
// CHECKSTYLE:ON
res = false;
}
return res;
}
// @formatter:off
@Documentation(value = "Prompts for a Long value with the given message.", params = {
@Param(name = "message", value = "The message displayed to the user"), }, result = "Prompts the user and return the input of the user as an Long.", examples = {
@Example(expression = "'Enter your age: '.promptLong()", result = "prompts the user"), })
// @formatter:on
public Long promptLong(final String message) {
final RunnableWithResult<Long> runnable = new RunnableWithResult<Long>() {
@Override
public void run() {
final AbstractPromptDialog dialog = new AbstractPromptDialog(Display.getDefault()
.getActiveShell(), message) {
@Override
public boolean validate(String input) {
return validateLong(input);
}
};
if (dialog.open() == Window.OK) {
result = Long.valueOf(dialog.getValue());
} else {
result = null;
}
}
};
Display.getDefault().syncExec(runnable);
if (runnable.getResult() != null) {
return runnable.getResult();
} else {
throw new IllegalStateException(M2DOC_DIALOG_CANCELLED);
}
}
private boolean validateLong(String input) {
boolean res;
try {
Long.valueOf(input);
res = true;
// CHECKSTYLE:OFF
} catch (Exception e) {
// CHECKSTYLE:ON
res = false;
}
return res;
}
// @formatter:off
@Documentation(value = "Prompts for a Float value with the given message.", params = {
@Param(name = "message", value = "The message displayed to the user"), }, result = "Prompts the user and return the input of the user as a Float.", examples = {
@Example(expression = "'Enter your weight: '.promptFloat()", result = "prompts the user"), })
// @formatter:on
public Float promptFloat(final String message) {
final RunnableWithResult<Float> runnable = new RunnableWithResult<Float>() {
@Override
public void run() {
final AbstractPromptDialog dialog = new AbstractPromptDialog(Display.getDefault()
.getActiveShell(), message) {
@Override
public boolean validate(String input) {
return validateFloat(input);
}
};
if (dialog.open() == Window.OK) {
result = Float.valueOf(dialog.getValue());
} else {
result = null;
}
}
};
Display.getDefault().syncExec(runnable);
if (runnable.getResult() != null) {
return runnable.getResult();
} else {
throw new IllegalStateException(M2DOC_DIALOG_CANCELLED);
}
}
private boolean validateFloat(String input) {
boolean res;
try {
Float.valueOf(input);
res = true;
// CHECKSTYLE:OFF
} catch (Exception e) {
// CHECKSTYLE:ON
res = false;
}
return res;
}
// @formatter:off
@Documentation(value = "Prompts for a Double value with the given message.", params = {
@Param(name = "message", value = "The message displayed to the user"), }, result = "Prompts the user and return the input of the user as a Double.", examples = {
@Example(expression = "'Enter your weight: '.promptDouble()", result = "prompts the user"), })
// @formatter:on
public Double promptDouble(final String message) {
final RunnableWithResult<Double> runnable = new RunnableWithResult<Double>() {
@Override
public void run() {
final AbstractPromptDialog dialog = new AbstractPromptDialog(Display.getDefault()
.getActiveShell(), message) {
@Override
public boolean validate(String input) {
return validateDouble(input);
}
};
if (dialog.open() == Window.OK) {
result = Double.valueOf(dialog.getValue());
} else {
result = null;
}
}
};
Display.getDefault().syncExec(runnable);
if (runnable.getResult() != null) {
return runnable.getResult();
} else {
throw new IllegalStateException(M2DOC_DIALOG_CANCELLED);
}
}
private boolean validateDouble(String input) {
boolean res;
try {
Double.valueOf(input);
res = true;
// CHECKSTYLE:OFF
} catch (Exception e) {
// CHECKSTYLE:ON
res = false;
}
return res;
}
public EObject promptEObject(String message, EClass eCls) {
final Set<EClass> acceptedEClasses = new HashSet<>();
acceptedEClasses.add(eCls);
return promptEObject(message, acceptedEClasses);
}
public EObject promptEObject(final String message, final Set<EClass> acceptedEClasses) {
final RunnableWithResult<EObject> runnable = new RunnableWithResult<EObject>() {
@Override
public void run() {
final EObjectSelectionDialog dialog = new EObjectSelectionDialog(Display.getDefault()
.getActiveShell(), adapterFactory, "M2Doc", message, queryEnvironment,
resourceSetForModels, acceptedEClasses);
if (dialog.open() == Window.OK) {
result = (EObject)dialog.getValue();
} else {
result = null;
}
}
};
Display.getDefault().syncExec(runnable);
if (runnable.getResult() != null) {
return runnable.getResult();
} else {
throw new IllegalStateException(M2DOC_DIALOG_CANCELLED);
}
}
/**
* Disposes this instance.
*/
public void dispose() {
adapterFactory.dispose();
}
@Override
protected IService<Method> getService(Method method, boolean forWorkspace) {
final IService<Method> result;
if ("promptEObject".equals(method.getName())) {
result = new EOBjectPromptService(method, this, forWorkspace);
} else if (!"dispose".equals(method.getName())) {
result = new JavaMethodService(method, this, forWorkspace);
} else {
result = null;
}
return result;
}
}