Repository navigation
Expand file tree
/
Copy pathOptionsDialog.java
More file actions
578 lines (494 loc) · 20.5 KB
/
Copy pathOptionsDialog.java
File metadata and controls
578 lines (494 loc) · 20.5 KB
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
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
/*
* Copyright (c) 2007 Sun Microsystems, Inc. All rights reserved.
*
* Sun Microsystems, Inc. has intellectual property rights relating to technology embodied in the product
* that is described in this document. In particular, and without limitation, these intellectual property
* rights may include one or more of the U.S. patents listed at http://www.sun.com/patents and one or
* more additional patents or pending patent applications in the U.S. and in other countries.
*
* U.S. Government Rights - Commercial software. Government users are subject to the Sun
* Microsystems, Inc. standard license agreement and applicable provisions of the FAR and its
* supplements.
*
* Use is subject to license terms. Sun, Sun Microsystems, the Sun logo, Java and Solaris are trademarks or
* registered trademarks of Sun Microsystems, Inc. in the U.S. and other countries. All SPARC trademarks
* are used under license and are trademarks or registered trademarks of SPARC International, Inc. in the
* U.S. and other countries.
*
* UNIX is a registered trademark in the U.S. and other countries, exclusively licensed through X/Open
* Company, Ltd.
*/
package com.sun.max.program.option.gui;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import java.util.*;
import java.util.Arrays;
import java.util.List;
import javax.swing.*;
import com.sun.max.*;
import com.sun.max.gui.*;
import com.sun.max.program.*;
import com.sun.max.program.option.*;
/**
* A dialog used to present the {@linkplain Option options} defined by a {@link OptionSet}.
*
* @author Doug Simon
* @author Ben L. Titzer
*/
public class OptionsDialog extends JDialog {
public static final int TEXT_FIELD_COLUMNS = 40;
protected abstract static class GUIOption<T> implements ItemListener {
protected final Option<T> option;
protected final JCheckBox guard;
protected final JLabel label;
protected JComponent input;
protected GUIOption(Option<T> opt) {
this.option = opt;
guard = new JCheckBox();
label = new JLabel(option.getName());
guard.addItemListener(this);
}
public JCheckBox getGuard() {
return guard;
}
public JLabel getLabel() {
return label;
}
public JComponent getInputComponent() {
return input;
}
public void itemStateChanged(ItemEvent e) {
setInputAndLabelEnabled(guard.isSelected());
}
protected void setEnabledOfContainedComponents(Container container, boolean enabled) {
for (Component component : container.getComponents()) {
component.setEnabled(enabled);
if (component instanceof Container) {
setEnabledOfContainedComponents((Container) component, enabled);
}
}
}
protected void setInputAndLabelEnabled(boolean enabled) {
input.setEnabled(enabled);
setEnabledOfContainedComponents(input, enabled);
label.setEnabled(enabled);
}
public abstract T getValue() throws Option.Error;
public abstract void setValue(T value);
public void commitOption() {
if (guard.isSelected()) {
option.setValue(getValue());
}
}
public String unparse() {
return option.getType().unparseValue(getValue());
}
public void parse(String str) {
guard.setEnabled(true);
setValue(option.getType().parseValue(str));
}
}
protected static class IntegerGUIOption extends GUIOption<Integer> {
private final JTextField textField;
protected IntegerGUIOption(Option<Integer> option) {
super(option);
textField = new JTextField();
input = textField;
textField.setColumns(TEXT_FIELD_COLUMNS);
setValue(option.getValue());
}
@Override
public void setValue(Integer i) {
textField.setText(String.valueOf(i));
guard.setSelected(i != null);
setInputAndLabelEnabled(i != null);
}
@Override
public Integer getValue() {
if (guard.isSelected()) {
return option.getType().parseValue(textField.getText());
}
return null;
}
}
protected static class BooleanGUIOption extends GUIOption<Boolean> {
private final JLabel helpText;
protected BooleanGUIOption(Option<Boolean> option) {
super(option);
helpText = new JLabel("<html>" + option.getHelp() + "</html>");
input = helpText;
setValue(option.getValue());
}
@Override
public void commitOption() {
if (guard.isSelected()) {
option.setValue(true);
} else {
option.setValue(false);
}
}
@Override
public void setValue(Boolean b) {
guard.setSelected(b);
helpText.setEnabled(b);
setInputAndLabelEnabled(b);
}
@Override
public Boolean getValue() {
return guard.isSelected();
}
}
protected static class StringGUIOption extends GUIOption<String> {
private final JTextField textField;
protected StringGUIOption(Option<String> option) {
super(option);
textField = new JTextField(TEXT_FIELD_COLUMNS);
input = textField;
setValue(option.getValue());
}
@Override
public void setValue(String i) {
textField.setText(String.valueOf(i));
guard.setSelected(i != null);
setInputAndLabelEnabled(i != null);
}
@Override
public String getValue() {
if (guard.isSelected()) {
return textField.getText();
}
return null;
}
}
protected static class ListGUIOption extends GUIOption<List<Object>> {
private final JTextField textField;
private final JList list;
protected ListGUIOption(Option<List<Object>> option) {
super(option);
if (option.getType() instanceof OptionTypes.EnumListType) {
textField = null;
final OptionTypes.EnumListType elt = (OptionTypes.EnumListType) option.getType();
final OptionTypes.EnumType et = (OptionTypes.EnumType) elt.elementOptionType;
list = new JList(et.values);
list.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
list.setVisibleRowCount(Math.min(10, et.values.length));
list.setLayoutOrientation(JList.VERTICAL);
final JScrollPane scrollPane = new JScrollPane(list);
input = scrollPane;
} else {
list = null;
textField = new JTextField(TEXT_FIELD_COLUMNS);
input = textField;
}
setValue(option.getValue());
}
@Override
public void setValue(List<Object> objects) {
if (objects != null) {
if (textField == null) {
this.list.clearSelection();
final int[] selectedIndices = new int[objects.size()];
int i = 0;
for (Object value : objects) {
final Enum enumValue = (Enum) value;
selectedIndices[i++] = enumValue.ordinal();
}
this.list.setSelectedIndices(selectedIndices);
} else {
textField.setText(String.valueOf(option.getType().unparseValue(objects)));
}
}
guard.setSelected(objects != null);
setInputAndLabelEnabled(objects != null);
}
@Override
public List<Object> getValue() {
if (guard.isSelected()) {
if (textField == null) {
final List<Object> result = new LinkedList<Object>();
for (Object value : this.list.getSelectedValues()) {
result.add(value);
}
return result;
}
return option.getType().parseValue(textField.getText());
}
return null;
}
}
protected static class URLGUIOption extends GUIOption<URL> {
private final JTextField textField;
protected URLGUIOption(Option<URL> option) {
super(option);
textField = new JTextField(TEXT_FIELD_COLUMNS);
input = textField;
setValue(option.getValue());
}
@Override
public void setValue(URL i) {
textField.setText(String.valueOf(i));
guard.setSelected(i != null);
setInputAndLabelEnabled(i != null);
}
@Override
public URL getValue() {
if (guard.isSelected()) {
try {
return new URL(textField.getText());
} catch (MalformedURLException e) {
return null;
}
}
return null;
}
}
protected static class EnumGUIOption extends GUIOption<Object> {
private final JComboBox comboBox;
protected EnumGUIOption(Option<Object> option) {
super(option);
final OptionTypes.EnumType et = (OptionTypes.EnumType) option.getType();
comboBox = new JComboBox(et.values);
input = comboBox;
setValue(option.getValue());
}
@Override
public void setValue(Object value) {
comboBox.setSelectedItem(value);
guard.setSelected(true);
}
@Override
public Object getValue() {
return comboBox.getSelectedItem();
}
}
protected static class FileGUIOption extends GUIOption<File> {
private final JTextField textField;
private final JButton fileChooserButton;
private JFileChooser fileChooser;
protected FileGUIOption(Option<File> option) {
super(option);
textField = new JTextField(TEXT_FIELD_COLUMNS);
final JPanel inputPanel = new JPanel();
input = inputPanel;
fileChooserButton = new JButton(new AbstractAction("Select") {
public void actionPerformed(ActionEvent e) {
if (fileChooser == null) {
fileChooser = new JFileChooser(new File("."));
fileChooser.setCurrentDirectory(new File(System.getProperty("user.home")));
fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
}
final File file = new File(textField.getText());
if (file.exists()) {
final File parent = file.getParentFile();
if (parent != null && parent.isDirectory()) {
fileChooser.setCurrentDirectory(parent);
}
fileChooser.ensureFileIsVisible(file);
fileChooser.setSelectedFile(file);
}
if (fileChooser.showDialog(fileChooserButton, "Select") == JFileChooser.APPROVE_OPTION) {
textField.setText(fileChooser.getSelectedFile().getAbsolutePath());
}
}
});
inputPanel.add(textField);
inputPanel.add(fileChooserButton);
setValue(option.getValue());
}
@Override
public void setValue(File i) {
if (i != null) {
if (fileChooser != null) {
fileChooser.setSelectedFile(i);
}
textField.setText(i.getAbsolutePath());
}
guard.setSelected(i != null);
setInputAndLabelEnabled(i != null);
}
@Override
public File getValue() {
return new File(textField.getText());
}
}
protected static class PackageGUIOption extends GUIOption<MaxPackage> {
private final JComboBox values;
protected PackageGUIOption(Option<MaxPackage> option) {
super(option);
final MaxPackageOptionType type = (MaxPackageOptionType) option.getType();
final List<MaxPackage> maxPackages = new LinkedList<MaxPackage>();
for (MaxPackage maxPackage : type.superPackage.getTransitiveSubPackages(Classpath.fromSystem())) {
final Class<Scheme> schemeClass = Utils.cast(type.classType);
if (maxPackage.schemeTypeToImplementation(schemeClass) != null) {
maxPackages.add(maxPackage);
}
}
final MaxPackage[] pkgs = maxPackages.toArray(new MaxPackage[maxPackages.size()]);
Arrays.sort(pkgs, new Comparator<MaxPackage>() {
public int compare(MaxPackage o1, MaxPackage o2) {
return o1.name().compareTo(o2.name());
}
});
this.values = new JComboBox(pkgs);
input = this.values;
// The combo box must be editable as the prepopulated items are just those packages found from the super package
this.values.setEditable(true);
setValue(option.getValue());
}
@Override
public MaxPackage getValue() {
return option.getType().parseValue(values.getSelectedItem().toString());
}
@Override
public void setValue(MaxPackage p) {
if (p != null) {
values.setSelectedItem(p);
guard.setSelected(true);
} else {
values.setSelectedIndex(-1);
guard.setSelected(false);
}
}
}
private static GUIOption createGUIOption(Option<?> option) {
final Option.Type<?> type = option.getType();
if (type == OptionTypes.BOOLEAN_TYPE) {
final Option<Boolean> opt = OptionTypes.BOOLEAN_TYPE.cast(option);
return new BooleanGUIOption(opt);
}
if (type == OptionTypes.INT_TYPE) {
final Option<Integer> opt = OptionTypes.INT_TYPE.cast(option);
return new IntegerGUIOption(opt);
}
if (type == OptionTypes.STRING_TYPE) {
final Option<String> opt = OptionTypes.STRING_TYPE.cast(option);
return new StringGUIOption(opt);
}
if (type == OptionTypes.URL_TYPE) {
final Option<URL> opt = OptionTypes.URL_TYPE.cast(option);
return new URLGUIOption(opt);
}
if (type == OptionTypes.FILE_TYPE) {
final Option<File> opt = OptionTypes.FILE_TYPE.cast(option);
return new FileGUIOption(opt);
}
if (type instanceof OptionTypes.EnumType) {
final Option<Object> opt = Utils.cast(option);
return new EnumGUIOption(opt);
}
if (type instanceof OptionTypes.ListType) {
final Option<List<Object>> opt = Utils.cast(option);
return new ListGUIOption(opt);
}
if (type instanceof MaxPackageOptionType) {
final Option<MaxPackage> opt = Utils.cast(option);
return new PackageGUIOption(opt);
}
return null;
}
private boolean cancelled;
public OptionsDialog(JFrame owner, final OptionSet optionSet) {
super(owner, "Options Dialog", true);
final LinkedList<GUIOption<?>> guiOptions = new LinkedList<GUIOption<?>>();
final Container contentPane = getContentPane();
contentPane.setLayout(new BorderLayout());
// Configure panel of options
final JPanel optionsPanel = new JPanel();
optionsPanel.setLayout(new SpringLayout());
optionsPanel.setBorder(BorderFactory.createTitledBorder(""));
for (final Option<?> programOption : optionSet.getOptions()) {
final GUIOption guiOption = createGUIOption(programOption);
if (guiOption != null) {
guiOptions.add(guiOption);
final JCheckBox enabled = guiOption.getGuard();
final JLabel label = guiOption.getLabel();
label.setToolTipText(programOption.getHelp());
optionsPanel.add(enabled);
optionsPanel.add(label);
optionsPanel.add(guiOption.getInputComponent());
}
}
SpringUtilities.makeCompactGrid(optionsPanel, guiOptions.size(), 3, 0, 0, 5, 5);
contentPane.add(optionsPanel, BorderLayout.CENTER);
// Configure buttons
final JButton ok = new JButton(new AbstractAction("OK") {
public void actionPerformed(ActionEvent e) {
for (GUIOption guiOption : guiOptions) {
try {
guiOption.commitOption();
} catch (Option.Error e1) {
showErrorDialog(guiOption, e1);
return;
}
}
OptionsDialog.this.setVisible(false);
}
});
final JButton cancel = new JButton(new AbstractAction("Use Defaults") {
public void actionPerformed(ActionEvent e) {
OptionsDialog.this.setVisible(false);
OptionsDialog.this.cancelled = true;
}
});
final JButton exit = new JButton(new AbstractAction("Cancel") {
public void actionPerformed(ActionEvent e) {
System.exit(1);
}
});
ok.setToolTipText("Apply options as configured in this dialog and ignore the command line arguments.");
cancel.setToolTipText("Ignore options as configured in this dialog and use the command line arguments instead.");
exit.setToolTipText("Stop running the program [calls System.exit()].");
final JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
buttonPanel.add(ok);
buttonPanel.add(cancel);
buttonPanel.add(exit);
contentPane.add(buttonPanel, BorderLayout.SOUTH);
pack();
// Places dialog in the middle of the screen if 'owner == null'
setLocationRelativeTo(owner);
}
private class ErrorDialog extends JDialog {
ErrorDialog(JDialog owner, GUIOption guiOption, Option.Error error) {
super(owner, "Option Error", true);
final JPanel errorPanel = new JPanel();
errorPanel.setLayout(new SpringLayout());
errorPanel.setBorder(BorderFactory.createTitledBorder(""));
final Container contentPane = getContentPane();
contentPane.setLayout(new BorderLayout());
// Configure buttons
final JButton ok = new JButton(new AbstractAction("OK") {
public void actionPerformed(ActionEvent e) {
errorPanel.setVisible(false);
}
});
final JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
buttonPanel.add(ok);
final JLabel textPane = new JLabel();
textPane.setText("Invalid value for " + guiOption.option.getType().getTypeName() + " option \"-" + guiOption.option.getName() + "\"");
contentPane.add(textPane);
contentPane.add(buttonPanel, BorderLayout.SOUTH);
pack();
// Places dialog in the middle of the screen if 'owner == null'
setLocationRelativeTo(owner);
}
}
public void showErrorDialog(GUIOption guiOption, Option.Error error) {
final String typeName = guiOption.option.getType().getTypeName();
JOptionPane.showMessageDialog(this, "Error in option \"-" + guiOption.option.getName() + "\" of type " + typeName + "\n" + error.getMessage());
// new ErrorDialog(this, guiOption, error).setVisible(true);
}
/**
* Creates a dialog for displaying a GUI for a given option set.
*
* @return true if the user wants to use the options as configured by the dialog, false if the caller of this method
* should defer to parsing an argument array instead
*/
public static boolean show(JFrame owner, OptionSet optionSet) {
final OptionsDialog programOptionDialog = new OptionsDialog(owner, optionSet);
programOptionDialog.setVisible(true);
programOptionDialog.dispose();
return !programOptionDialog.cancelled;
}
}