-
Notifications
You must be signed in to change notification settings - Fork 3
/
AcceleoMainTab.java
449 lines (405 loc) · 13.3 KB
/
AcceleoMainTab.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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
/*******************************************************************************
* Copyright (c) 2017, 2024 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.aql.ls.debug.ide.ui.launch;
import com.google.gson.Gson;
import java.util.LinkedHashMap;
import java.util.Map;
import org.eclipse.acceleo.aql.AcceleoUtil;
import org.eclipse.acceleo.aql.ide.AcceleoPlugin;
import org.eclipse.acceleo.aql.ls.debug.AcceleoDebugger;
import org.eclipse.acceleo.aql.ls.debug.ide.AcceleoDebugPlugin;
import org.eclipse.acceleo.aql.ls.debug.ide.ui.dialog.AbstractResourceSelectionDialog;
import org.eclipse.acceleo.aql.ls.debug.ide.ui.dialog.FileSelectionDialog;
import org.eclipse.acceleo.aql.ls.debug.ide.ui.dialog.FolderSelectionDialog;
import org.eclipse.acceleo.aql.parser.AcceleoParser;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Status;
import org.eclipse.debug.core.ILaunchConfiguration;
import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
import org.eclipse.debug.ui.AbstractLaunchConfigurationTab;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
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.Event;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.dialogs.FilteredResourcesSelectionDialog;
public class AcceleoMainTab extends AbstractLaunchConfigurationTab {
/**
* The Browse button text.
*/
protected static final String BROWSE = "Browse...";
/**
* New line.
*/
protected static final String NEW_LINE = "\n";
/**
* New line.
*/
protected static final String WINDOWS_NEW_LINE = "\r\n";
/**
* The {@link Gson} instance.
*/
protected static final Gson GSON = new Gson();
/**
* The module resource.
*/
protected String module;
/**
* The model resource.
*/
protected String model;
/**
* The destination.
*/
protected String destination;
/**
* The options {@link Map}.
*/
protected Map<String, String> options;
/**
* The options.
*/
private String optionsString;
/**
* The initial module.
*/
private String initialModule;
/**
* The initial model.
*/
private String initialModel;
/**
* The initial destination.
*/
private String initialDestination;
/**
* The initial options.
*/
private String initialOptionsString;
/**
* The module {@link Text}.
*/
private Text moduleText;
/**
* The model {@link Text}.
*/
private Text modelText;
/**
* The Unix end line radio {@link Button}.
*/
private Button unixEndLineButton;
/**
* The Windows end line radio {@link Button}.
*/
private Button windowsEndLineButton;
/**
* The destination {@link Text}.
*/
private Text destinationText;
@Override
public void setDefaults(ILaunchConfigurationWorkingCopy configuration) {
if (module != null) {
configuration.setMappedResources(new IResource[] {ResourcesPlugin.getWorkspace().getRoot()
.getFile(new Path(module)) });
}
configuration.setAttribute(AcceleoDebugger.MODULE, module);
configuration.setAttribute(AcceleoDebugger.MODEL, model);
configuration.setAttribute(AcceleoDebugger.DESTINATION, destination);
configuration.setAttribute(AcceleoDebugger.OPTIONS, optionsString);
}
@Override
public void initializeFrom(ILaunchConfiguration configuration) {
try {
if (configuration.hasAttribute(AcceleoDebugger.MODULE)) {
moduleText.setText(configuration.getAttribute(AcceleoDebugger.MODULE, ""));
initialModule = module;
}
if (configuration.hasAttribute(AcceleoDebugger.MODEL)) {
modelText.setText(configuration.getAttribute(AcceleoDebugger.MODEL, ""));
initialModel = model;
}
if (configuration.hasAttribute(AcceleoDebugger.DESTINATION)) {
destinationText.setText(configuration.getAttribute(AcceleoDebugger.DESTINATION, ""));
initialDestination = destination;
}
if (configuration.hasAttribute(AcceleoDebugger.OPTIONS)) {
options = GSON.fromJson(configuration.getAttribute(AcceleoDebugger.OPTIONS, ""),
AcceleoDebugger.OPTION_MAP_TYPE);
} else {
options = new LinkedHashMap<>();
if (!options.containsKey(AcceleoUtil.NEW_LINE_OPTION)) {
options.put(AcceleoUtil.NEW_LINE_OPTION, System.lineSeparator());
}
}
optionsString = GSON.toJson(options);
initialOptionsString = optionsString;
final String newLine = options.get(AcceleoUtil.NEW_LINE_OPTION);
unixEndLineButton.setSelection(NEW_LINE.equals(newLine));
windowsEndLineButton.setSelection(WINDOWS_NEW_LINE.equals(newLine));
} catch (CoreException e) {
AcceleoDebugPlugin.getPlugin().log(new Status(IStatus.ERROR, AcceleoDebugPlugin.ID,
"couldn't initialize from launch configuration", e));
}
}
@Override
public void performApply(ILaunchConfigurationWorkingCopy configuration) {
setDefaults(configuration);
initialModule = module;
initialModel = model;
initialDestination = destination;
initialOptionsString = optionsString;
setDirty(isDirty());
}
@Override
public boolean isValid(ILaunchConfiguration launchConfig) {
boolean res = true;
try {
if (launchConfig.hasAttribute(AcceleoDebugger.MODULE)) {
final IFile resource = ResourcesPlugin.getWorkspace().getRoot().getFile(new Path(launchConfig
.getAttribute(AcceleoDebugger.MODULE, "")));
if (!resource.exists()) {
setErrorMessage("The selected Acceleo module doesn't exists.");
res = false;
} else if (!AcceleoPlugin.isAcceleoMain(resource)) {
setErrorMessage("The selected Acceleo module doesn't contain a main template.");
res = false;
}
} else {
setErrorMessage("Select an Acceleo module with main template.");
res = false;
}
if (res) {
if (!launchConfig.hasAttribute(AcceleoDebugger.MODEL)) {
setErrorMessage("Select a model file.");
res = false;
}
}
if (res) {
if (!launchConfig.hasAttribute(AcceleoDebugger.DESTINATION)) {
setErrorMessage("Select a destination folder.");
res = false;
}
}
} catch (CoreException e) {
AcceleoDebugPlugin.getPlugin().log(new Status(IStatus.ERROR, AcceleoDebugPlugin.ID,
"couldn't validate launch configuration", e));
}
if (res) {
setErrorMessage(null);
}
return res;
}
@Override
public String getName() {
return "Acceleo 4";
}
/**
* Creates the {@link #setControl(org.eclipse.swt.widgets.Control) control}.
*
* @wbp.parser.entryPoint
*/
@Override
public void createControl(Composite parent) {
final Composite control = new Composite(parent, parent.getStyle() ^ (SWT.H_SCROLL | SWT.V_SCROLL));
control.setLayout(new GridLayout(1, false));
moduleText = createModuleComposite(control);
modelText = createModelComposite(control);
destinationText = createDestinationComposite(control);
createNewLine(control);
setControl(control);
}
/**
* Creates the module {@link Composite}.
*
* @param parent
* the parent
* @return the created {@link Text}
*/
private Text createModuleComposite(final Composite parent) {
final Group group = new Group(parent, parent.getStyle());
group.setLayout(new GridLayout(2, false));
group.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
group.setText("Module file:");
final Text res = new Text(group, SWT.BORDER);
res.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
res.addModifyListener(new ModifyListener() {
@Override
public void modifyText(ModifyEvent e) {
module = res.getText();
setDirty(isDirty());
updateLaunchConfigurationDialog();
}
});
Button browseButton = createPushButton(group, BROWSE, null);
browseButton.setText(BROWSE);
browseButton.addListener(SWT.Selection, new Listener() {
@Override
public void handleEvent(Event event) {
handleBrowseModuleButton();
}
});
return res;
}
/**
* Creates the model {@link Composite}.
*
* @param parent
* the parent
* @return the created {@link Text}
*/
private Text createModelComposite(final Composite parent) {
final Group group = new Group(parent, parent.getStyle());
group.setLayout(new GridLayout(2, false));
group.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
group.setText("Model file:");
final Text res = new Text(group, SWT.BORDER);
res.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
res.addModifyListener(new ModifyListener() {
@Override
public void modifyText(ModifyEvent e) {
model = res.getText();
setDirty(isDirty());
updateLaunchConfigurationDialog();
}
});
Button browseButton = createPushButton(group, BROWSE, null);
browseButton.addListener(SWT.Selection, new Listener() {
@Override
public void handleEvent(Event event) {
handleBrowseModelButton();
}
});
return res;
}
/**
* Creates the destination {@link Composite}.
*
* @param parent
* the parent
* @return the created {@link Text}
*/
private Text createDestinationComposite(final Composite parent) {
final Group group = new Group(parent, parent.getStyle());
group.setLayout(new GridLayout(2, false));
group.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
group.setText("Destination folder:");
final Text res = new Text(group, SWT.BORDER);
res.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
res.addModifyListener(new ModifyListener() {
@Override
public void modifyText(ModifyEvent e) {
destination = res.getText();
setDirty(isDirty());
updateLaunchConfigurationDialog();
}
});
Button browseButton = createPushButton(group, BROWSE, null);
browseButton.addListener(SWT.Selection, new Listener() {
@Override
public void handleEvent(Event event) {
handleBrowseDestinationButton();
}
});
return res;
}
/**
* Creates the new line selection {@link Composite}.
*
* @param parent
* the parent
*/
private void createNewLine(final Composite parent) {
final Group group = new Group(parent, parent.getStyle());
group.setLayout(new GridLayout(2, false));
group.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
group.setText("Enf of line:");
unixEndLineButton = new Button(group, SWT.RADIO);
unixEndLineButton.setText("Unix");
unixEndLineButton.addListener(SWT.Selection, e -> {
if (unixEndLineButton.getSelection()) {
putOption(AcceleoUtil.NEW_LINE_OPTION, NEW_LINE);
}
});
windowsEndLineButton = new Button(group, SWT.RADIO);
windowsEndLineButton.setText("Windows");
windowsEndLineButton.addListener(SWT.Selection, e -> {
if (windowsEndLineButton.getSelection()) {
putOption(AcceleoUtil.NEW_LINE_OPTION, WINDOWS_NEW_LINE);
}
});
}
protected boolean isDirty() {
return module != initialModule || model != initialModel || destination != initialDestination
|| !optionsString.equals(initialOptionsString);
}
private void handleBrowseModuleButton() {
final AbstractResourceSelectionDialog dialog = new FileSelectionDialog(getShell(),
"Select a module file", module, AcceleoParser.MODULE_FILE_EXTENSION, true);
final int dialogResult = dialog.open();
if ((dialogResult == IDialogConstants.OK_ID) && !dialog.getFileName().isEmpty()) {
moduleText.setText(dialog.getFileName());
setDirty(isDirty());
}
}
/**
* Puts the given option.
*
* @param name
* the option name
* @param value
* the option value
*/
protected void putOption(String name, String value) {
options.put(name, value);
optionsString = GSON.toJson(options);
setDirty(isDirty());
updateLaunchConfigurationDialog();
}
private void handleBrowseModelButton() {
FilteredResourcesSelectionDialog dialog = new FilteredResourcesSelectionDialog(getShell(), false,
ResourcesPlugin.getWorkspace().getRoot(), IResource.FILE);
dialog.setTitle("Select the model file");
String path = modelText.getText();
if (path != null && path.length() > 0 && new Path(path).lastSegment().length() > 0) {
dialog.setInitialPattern(new Path(path).lastSegment());
} else {
String initial = "*.xmi"; //$NON-NLS-1$
dialog.setInitialPattern(initial);
}
dialog.open();
if (dialog.getResult() != null && dialog.getResult().length > 0 && dialog
.getResult()[0] instanceof IFile) {
modelText.setText(((IFile)dialog.getResult()[0]).getFullPath().toString());
}
}
private void handleBrowseDestinationButton() {
final AbstractResourceSelectionDialog dialog = new FolderSelectionDialog(getShell(),
"Select the destination folder", destination);
final int dialogResult = dialog.open();
if ((dialogResult == IDialogConstants.OK_ID) && dialog.getFileName() != null && !dialog.getFileName()
.isEmpty()) {
destinationText.setText(dialog.getFileName());
setDirty(isDirty());
}
}
}