/
GamaPreferencesView.java
530 lines (459 loc) · 17.1 KB
/
GamaPreferencesView.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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
/*********************************************************************************************
*
* 'GamaPreferencesView.java, in plugin ummisco.gama.ui.shared, is part of the source code of the GAMA modeling and
* simulation platform. (v. 1.8.1)
*
* (c) 2007-2020 UMI 209 UMMISCO IRD/UPMC & Partners
*
* Visit https://github.com/gama-platform/gama for license information and developers contact.
*
*
**********************************************************************************************/
package ummisco.gama.ui.views;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.eclipse.jface.layout.GridDataFactory;
import org.eclipse.jface.layout.GridLayoutFactory;
import org.eclipse.jface.preference.PreferenceDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CTabFolder;
import org.eclipse.swt.custom.CTabItem;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.events.ControlListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.FillLayout;
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.FileDialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.internal.dialogs.WorkbenchPreferenceDialog;
import msi.gama.application.workbench.ThemeHelper;
import msi.gama.common.preferences.GamaPreferences;
import msi.gama.common.preferences.Pref;
import msi.gama.metamodel.shape.GamaPoint;
import msi.gama.runtime.GAMA;
import msi.gaml.types.IType;
import ummisco.gama.dev.utils.DEBUG;
import ummisco.gama.ui.controls.ParameterExpandBar;
import ummisco.gama.ui.controls.ParameterExpandItem;
import ummisco.gama.ui.dialogs.Messages;
import ummisco.gama.ui.interfaces.IParameterEditor;
import ummisco.gama.ui.parameters.AbstractEditor;
import ummisco.gama.ui.parameters.EditorFactory;
import ummisco.gama.ui.resources.GamaFonts;
import ummisco.gama.ui.resources.GamaIcons;
import ummisco.gama.ui.resources.IGamaColors;
import ummisco.gama.ui.resources.IGamaIcons;
import ummisco.gama.ui.utils.WorkbenchHelper;
import ummisco.gama.ui.views.toolbar.Selector;
/**
* Class GamaPreferencesView.
*
* @author drogoul
* @since 31 août 2013
*
*/
@SuppressWarnings ({ "unchecked", "rawtypes" })
public class GamaPreferencesView {
static Pref<GamaPoint> DIALOG_LOCATION = GamaPreferences.create("dialog_location",
"Location of the preferences dialog on screen", new GamaPoint(-1, -1), IType.POINT, false).hidden();
static Pref<GamaPoint> DIALOG_SIZE = GamaPreferences.create("dialog_size",
"Size of the preferences dialog on screen", new GamaPoint(-1, -1), IType.POINT, false).hidden();
static Pref<Integer> DIALOG_TAB = GamaPreferences
.create("dialog_tab", "Tab selected in the preferences dialog", -1, IType.INT, false).hidden();
public static Map<String, Image> prefs_images = new LinkedHashMap();
public static int NB_DIVISIONS = 2;
static GamaPreferencesView instance;
static boolean restartRequired;
public static void show() {
if (instance == null || instance.shell == null || instance.shell.isDisposed()) {
instance = new GamaPreferencesView(WorkbenchHelper.getShell());
}
for (final IParameterEditor ed : instance.editors.values()) {
ed.updateValue(true);
}
instance.open();
}
static {
prefs_images.put(GamaPreferences.Interface.NAME, GamaIcons.create(IGamaIcons.PREFS_GENERAL).image());
prefs_images.put(GamaPreferences.Modeling.NAME, GamaIcons.create(IGamaIcons.PREFS_EDITOR).image());
prefs_images.put(GamaPreferences.Runtime.NAME, GamaIcons.create("prefs/prefs.simulations2").image());
prefs_images.put(GamaPreferences.Simulations.NAME, GamaIcons.create("prefs/prefs.runtime2").image());
prefs_images.put(GamaPreferences.Displays.NAME, GamaIcons.create("prefs/prefs.ui2").image());
prefs_images.put(GamaPreferences.External.NAME, GamaIcons.create(IGamaIcons.PREFS_LIBS).image());
}
Shell parentShell, shell;
CTabFolder tabFolder;
List<ParameterExpandBar> contents = new ArrayList();
final Map<String, IParameterEditor> editors = new LinkedHashMap();
final Map<String, Object> modelValues = new LinkedHashMap();
private GamaPreferencesView(final Shell parent) {
parentShell = parent;
shell = new Shell(parentShell, SWT.TITLE | SWT.RESIZE | SWT.APPLICATION_MODAL);
final var gridLayout = new GridLayout(2, false);
gridLayout.marginWidth = gridLayout.marginHeight = 5;
gridLayout.horizontalSpacing = gridLayout.verticalSpacing = 5;
shell.setLayout(gridLayout);
// shell.setBackground(ThemeHelper.isDark() ? IGamaColors.WHITE.color() : IGamaColors.BLACK.color());
buildContents();
}
private void buildContents() {
tabFolder = new CTabFolder(shell, SWT.TOP | SWT.NO_TRIM);
tabFolder.setBorderVisible(true);
// tabFolder.setBackgroundMode(SWT.INHERIT_DEFAULT);
tabFolder.setMRUVisible(true);
tabFolder.setSimple(false); // rounded tabs
tabFolder.setLayoutData(new GridData(GridData.FILL, GridData.FILL, true, true, 2, 1));
final var prefs = GamaPreferences.organizePrefs();
for (final String tabName : prefs.keySet()) {
final var item = new CTabItem(tabFolder, SWT.NONE);
item.setFont(GamaFonts.getNavigHeaderFont());
item.setText(tabName);
item.setImage(prefs_images.get(tabName));
item.setShowClose(false);
buildContentsFor(item, prefs.get(tabName));
}
buildButtons();
shell.layout();
}
private void buildContentsFor(final CTabItem tab, final Map<String, List<Pref>> entries) {
final var viewer = new ParameterExpandBar(tab.getParent(), SWT.V_SCROLL);
contents.add(viewer);
final var data = new GridData(SWT.FILL, SWT.FILL, true, true);
// viewer.setBackgroundMode(SWT.INHERIT_DEFAULT);
viewer.setBackground(
!ThemeHelper.isDark() ? IGamaColors.VERY_LIGHT_GRAY.color() : IGamaColors.DARK_GRAY.darker());
viewer.setLayoutData(data);
// ?
viewer.computeSize(tab.getBounds().x, SWT.DEFAULT);
//
viewer.setSpacing(5);
tab.setControl(viewer);
for (final String groupName : entries.keySet()) {
final var item = new ParameterExpandItem(viewer, entries.get(groupName), SWT.NONE, null);
item.setText(groupName);
// item.setColor(new GamaColor(230, 230, 230, 255));
final var compo = new Composite(viewer, SWT.NONE);
// compo.setBackground(viewer.getBackground());
buildGroupContents(compo, entries.get(groupName), NB_DIVISIONS);
item.setControl(compo);
item.setHeight(compo.computeSize(SWT.DEFAULT, SWT.DEFAULT).y);
item.setExpanded(true);
}
}
final Map<String, Boolean> activations = new HashMap();
void checkActivables(final Pref e, final Object value) {
if (e.getEnablement() != null) {
for (final String activable : e.getEnablement()) {
final var ed = editors.get(activable);
if (ed == null) {
if (value instanceof Boolean) {
activations.put(activable, (Boolean) value);
} else {
activations.put(activable, true);
}
} else {
if (value instanceof Boolean) {
ed.setActive((Boolean) value);
} else {
ed.setActive(true);
}
}
}
}
if (e.getDisablement() != null && value instanceof Boolean) {
for (final String deactivable : e.getDisablement()) {
final var ed = editors.get(deactivable);
if (ed == null) {
activations.put(deactivable, !(Boolean) value);
} else {
ed.setActive(!(Boolean) value);
}
}
}
}
void checkRefreshables(final Pref e, final Object value) {
if (e.getRefreshment() != null) {
for (final String activable : e.getRefreshment()) {
final var ed = editors.get(activable);
if (ed != null) {
ed.updateValue(true);
}
}
}
}
private void buildGroupContents(final Composite compo, final List<Pref> list, final int nbColumns) {
GridLayoutFactory.fillDefaults().numColumns(NB_DIVISIONS).spacing(0, 0).equalWidth(true).applyTo(compo);
final var comps = new Composite[nbColumns];
for (var i = 0; i < nbColumns; i++) {
comps[i] = new Composite(compo, SWT.BORDER);
// comps[i].setBackground(compo.getBackground());
GridLayoutFactory.fillDefaults().numColumns(2).spacing(0, 0).equalWidth(false).applyTo(comps[i]);
GridDataFactory.fillDefaults().grab(true, true).applyTo(comps[i]);
}
// final int compositeIndex = 0;
var i = 0;
for (final Pref e : list) {
modelValues.put(e.getKey(), e.getValue());
// Initial activations of editors
checkActivables(e, e.getValue());
e.onChange(value -> {
if (e.acceptChange(value)) {
modelValues.put(e.getKey(), value);
checkActivables(e, value);
checkRefreshables(e, value);
if (e.isRestartRequired()) {
setRestartRequired();
}
} else {
GamaPreferencesView.this.showError("" + value + " is not accepted for parameter " + e.getKey());
}
});
final var isSubParameter = activations.containsKey(e.getKey());
final var ed = EditorFactory.create(null, comps[(int) (i * ((double) nbColumns / list.size()))], e,
isSubParameter, true);
if (e.isDisabled()) {
ed.setActive(false);
} else {
final var m = getMenuFor(e, ed);
final var l = ed.getLabel();
l.setMenu(m);
}
editors.put(e.getKey(), ed);
i++;
}
// Initial activations of editors
for (final String s : activations.keySet()) {
final var ed = editors.get(s);
if (ed != null) {
ed.setActive(activations.get(s));
}
}
activations.clear();
compo.layout();
compo.pack(true);
}
private static Menu getMenuFor(final Pref e, final AbstractEditor ed) {
final var m = new Menu(ed.getLabel());
final var title = new MenuItem(m, SWT.PUSH);
title.setEnabled(false);
if (e.inGaml()) {
title.setText("Use gama." + e.getKey() + " in GAML");
@SuppressWarnings ("unused") final var sep = new MenuItem(m, SWT.SEPARATOR);
final var i = new MenuItem(m, SWT.PUSH);
i.setText("Copy name to clipboard");
i.addSelectionListener((Selector) se -> WorkbenchHelper.copy("gama." + e.getKey()));
} else {
title.setText("Not assignable from GAML");
@SuppressWarnings ("unused") final var sep = new MenuItem(m, SWT.SEPARATOR);
}
final var i2 = new MenuItem(m, SWT.PUSH);
i2.setText("Revert to default value");
i2.addSelectionListener((Selector) se -> {
e.set(e.getInitialValue(GAMA.getRuntimeScope()));
ed.forceUpdateValueAsynchronously();
});
return m;
}
/**
* @param string
*/
protected void showError(final String string) {
// TODO make it a proper component of the view
DEBUG.LOG("Error in preferences : " + string);
}
private void buildButtons() {
final var doc = new Label(shell, SWT.NONE);
doc.setLayoutData(new GridData(GridData.FILL, GridData.FILL, true, true, 2, 1));
doc.setFont(GamaFonts.boldHelpFont);
doc.setText(
"Some preferences can also be set in GAML, using 'gama.pref_name <- new_value;'. 'pref_name' is displayed in the contextual menu of each preference");
final var group1 = new Composite(shell, SWT.NONE);
group1.setLayout(new FillLayout());
final var gridDataGroup1 = new GridData(GridData.BEGINNING, GridData.END, true, false);
// gridDataGroup1.widthHint = 300;
group1.setLayoutData(gridDataGroup1);
final var buttonRevert = new Button(group1, SWT.PUSH | SWT.FLAT);
buttonRevert.setText("Revert to defaults");
buttonRevert.setImage(GamaIcons.create(IGamaIcons.ACTION_REVERT).image());
buttonRevert.setToolTipText("Restore default values for all preferences");
final var buttonAdvanced = new Button(group1, SWT.PUSH | SWT.FLAT);
buttonAdvanced.setText("Advanced...");
buttonAdvanced.setToolTipText("Access to advanced preferences");
final var buttonImport = new Button(group1, SWT.PUSH | SWT.FLAT);
buttonImport.setText("Import...");
buttonImport.setToolTipText("Import preferences from a file...");
buttonImport.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(final SelectionEvent e) {
final var fd = new FileDialog(shell, SWT.OPEN);
fd.setFilterExtensions(new String[] { "*.prefs" });
final var path = fd.open();
if (path == null)
return;
GamaPreferences.applyPreferencesFrom(path, modelValues);
for (final IParameterEditor ed : editors.values()) {
ed.updateValue(true);
}
}
});
final var buttonExport = new Button(group1, SWT.PUSH | SWT.FLAT);
buttonExport.setText("Export...");
buttonExport.setToolTipText("Export preferences to a model that can be run to restore or share them...");
buttonExport.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(final SelectionEvent e) {
final var fd = new FileDialog(shell, SWT.SAVE);
fd.setFileName("Preferences.gaml");
fd.setFilterExtensions(new String[] { "*.gaml" });
fd.setOverwrite(false);
final var path = fd.open();
if (path == null)
return;
GamaPreferences.savePreferencesTo(path);
}
});
final var group2 = new Composite(shell, SWT.NONE);
group2.setLayout(new FillLayout());
final var gridDataGroup2 = new GridData(GridData.END, GridData.END, true, false);
gridDataGroup2.widthHint = 200;
group2.setLayoutData(gridDataGroup2);
final var buttonCancel = new Button(group2, SWT.PUSH);
buttonCancel.setText("Cancel");
buttonCancel.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(final SelectionEvent e) {
close();
}
});
final var buttonOK = new Button(group2, SWT.PUSH);
buttonOK.setText("Save");
buttonOK.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(final SelectionEvent e) {
GamaPreferences.setNewPreferences(modelValues);
if (restartRequired) {
restartRequired = false;
final var restart = Messages.confirm("Restart GAMA",
"It is advised to restart GAMA after these changes. Restart now ?");
if (restart) {
close();
PlatformUI.getWorkbench().restart(true);
}
} else {
close();
}
}
});
this.shell.setDefaultButton(buttonOK);
buttonRevert.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(final SelectionEvent e) {
if (!Messages.question("Revert to default",
"Do you want to revert all preferences to their default values ? A restart of the platform will be performed immediately"))
return;
GamaPreferences.revertToDefaultValues(modelValues);
// for (final IParameterEditor ed : editors.values()) {
// // ed.forceUpdateValueAsynchronously();
// ed.updateValue(true);
// }
PlatformUI.getWorkbench().restart(true);
}
});
buttonAdvanced.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(final SelectionEvent e) {
close();
WorkbenchHelper.asyncRun(() -> {
final PreferenceDialog pd = WorkbenchPreferenceDialog.createDialogOn(parentShell, null);
pd.open();
shell.setVisible(true);
});
}
});
shell.addDisposeListener(e -> {
saveDialogProperties();
});
shell.addControlListener(new ControlListener() {
@Override
public void controlResized(final ControlEvent e) {
for (final IParameterEditor ed : editors.values()) {
((AbstractEditor) ed).resizeLabel(shell.getSize().x / (NB_DIVISIONS * 2));
((AbstractEditor) ed).getLabel().update();
}
for (final ParameterExpandBar bar : contents) {
for (final ParameterExpandItem item : bar.getItems()) {
item.setHeight(item.getControl().computeSize(SWT.DEFAULT, SWT.DEFAULT).y);
}
bar.redraw();
}
shell.layout(true, true);
}
@Override
public void controlMoved(final ControlEvent e) {}
});
}
void close() {
shell.setVisible(false);
}
private void saveLocation() {
final var p = shell.getLocation();
DIALOG_LOCATION.set(new GamaPoint(p.x, p.y)).save();
}
private void saveSize() {
final var s = shell.getSize();
DIALOG_SIZE.set(new GamaPoint(s.x, s.y)).save();
}
private void saveTab() {
final var index = tabFolder.getSelectionIndex();
DIALOG_TAB.set(index).save();
}
private void saveDialogProperties() {
if (shell.isDisposed())
return;
saveLocation();
saveSize();
saveTab();
}
public void open() {
final var loc = DIALOG_LOCATION.getValue();
final var size = DIALOG_SIZE.getValue();
final int tab = DIALOG_TAB.getValue();
var x = (int) loc.x;
var y = (int) loc.y;
var width = (int) size.x;
var height = (int) size.y;
if (loc.x == -1 || loc.y == -1 || size.x == -1 || size.y == -1) {
final var p = shell.computeSize(SWT.DEFAULT, SWT.DEFAULT, true);
final var bounds = WorkbenchHelper.getDisplay().getBounds();
width = Math.min(p.x, bounds.width - 200);
height = Math.min(p.y, bounds.height - 200);
x = (bounds.width - width) / 2;
y = (bounds.height - height) / 2;
}
tabFolder.setSelection(tab);
shell.setLocation(x, y);
shell.setSize(width, height);
shell.open();
while (!this.shell.isDisposed() && this.shell.isVisible()) {
if (!this.shell.getDisplay().readAndDispatch()) {
this.shell.getDisplay().sleep();
}
}
saveDialogProperties();
}
public static void setRestartRequired() {
restartRequired = true;
}
}