1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package p3j.gui.dialogs;
17
18 import java.awt.Component;
19 import java.awt.Frame;
20 import java.awt.event.ActionEvent;
21 import java.awt.event.ActionListener;
22 import java.util.ArrayList;
23 import java.util.Enumeration;
24 import java.util.List;
25
26 import javax.swing.AbstractButton;
27 import javax.swing.ButtonGroup;
28 import javax.swing.JButton;
29 import javax.swing.JDialog;
30 import javax.swing.JPanel;
31 import javax.swing.JRadioButton;
32 import javax.swing.JTextField;
33
34 import p3j.gui.P3J;
35 import p3j.gui.misc.P3JConfigFile;
36 import p3j.gui.panels.PropertiesShowPanelFactory;
37 import p3j.misc.Misc;
38 import p3j.misc.gui.GUI;
39 import p3j.simulation.ExecutionMode;
40
41
42
43
44
45
46
47 public class ExecutionPreferencesDialog extends JDialog {
48
49
50 private static final long serialVersionUID = 3246519924977747183L;
51
52
53 public static final int DIALOG_WIDTH = 700;
54
55
56 public static final int DIALOG_HEIGHT = 200;
57
58
59 private static final int FORM_KEY_WIDTH = 110;
60
61
62 private final JPanel contentPanel;
63
64
65 private final JTextField numOfTrials = new JTextField();
66
67
68 private final JTextField numOfParallelThreads = new JTextField();
69
70
71 private final P3JConfigFile p3jConfiguration;
72
73
74 private final ButtonGroup execModeButtonGroup = new ButtonGroup();
75
76
77 private final JButton apply = new JButton("Apply");
78 {
79 apply.addActionListener(new ApplyAction(this));
80 }
81
82
83 private final JButton cancel = new JButton("Cancel");
84 {
85 cancel.addActionListener(new ActionListener() {
86 @Override
87 public void actionPerformed(ActionEvent e) {
88 setVisible(false);
89 }
90 });
91 }
92
93
94 private final JButton resetToDefaults = new JButton("Reset Defaults");
95 {
96 resetToDefaults.addActionListener(new ActionListener() {
97 @Override
98 public void actionPerformed(ActionEvent e) {
99
100 numOfTrials.setText("" + Misc.DEFAULT_NUM_TRIALS);
101 numOfParallelThreads.setText("" + Misc.DEFAULT_NUM_PARALLEL_THREADS);
102
103 ExecutionMode defaultMode = Misc.DEFAULT_EXEC_MODE;
104 Enumeration<AbstractButton> execModeButtons = execModeButtonGroup
105 .getElements();
106 while (execModeButtons.hasMoreElements()) {
107 AbstractButton execModeButton = execModeButtons.nextElement();
108 if (execModeButton.getText().equals(defaultMode.toString())) {
109 execModeButton.setSelected(true);
110 break;
111 }
112 }
113 contentPanel.repaint();
114 }
115 });
116 }
117
118
119 private final List<JButton> buttons = new ArrayList<JButton>();
120 {
121 buttons.add(resetToDefaults);
122 buttons.add(cancel);
123 buttons.add(apply);
124 }
125
126
127
128
129
130
131
132 public ExecutionPreferencesDialog(Frame owner) {
133 super(owner, "Edit Execution Preferences", true);
134 p3jConfiguration = P3J.getInstance().getConfigFile();
135 setSize(DIALOG_WIDTH, DIALOG_HEIGHT);
136 GUI.centerOnScreen(this);
137
138 PropertiesShowPanelFactory pspf = new PropertiesShowPanelFactory(
139 FORM_KEY_WIDTH, buttons, 2);
140 numOfTrials.setText("" + p3jConfiguration.get(Misc.PREF_NUM_TRIALS));
141 pspf.app(Misc.PREF_NUM_TRIALS + ":", numOfTrials);
142
143 numOfParallelThreads.setText(""
144 + p3jConfiguration.get(Misc.PREF_NUM_PARALLEL_THREADS));
145 pspf.app(Misc.PREF_NUM_PARALLEL_THREADS + ":", numOfParallelThreads);
146
147 pspf.app(Misc.PREF_EXECUTION_MODE + ":", createExecutionModePanel());
148 contentPanel = pspf.constructPanel();
149 this.getContentPane().add(contentPanel);
150 }
151
152
153
154
155
156
157 private JPanel createExecutionModePanel() {
158 JPanel execModePanel = new JPanel();
159 ExecutionMode currentMode = (ExecutionMode) p3jConfiguration
160 .get(Misc.PREF_EXECUTION_MODE);
161
162 for (ExecutionMode mode : ExecutionMode.values()) {
163 JRadioButton button = new JRadioButton(mode.toString());
164 execModePanel.add(button);
165 execModeButtonGroup.add(button);
166 if (mode == currentMode) {
167 button.setSelected(true);
168 }
169 }
170 return execModePanel;
171 }
172
173
174
175
176 class ApplyAction implements ActionListener {
177
178
179 private Component owner;
180
181
182
183
184
185
186
187 ApplyAction(Component own) {
188 owner = own;
189 }
190
191
192
193
194
195
196
197 @Override
198 public void actionPerformed(ActionEvent e) {
199 try {
200 Integer numTrials = Integer.parseInt(numOfTrials.getText());
201 Integer numParallelThreads = Integer.parseInt(numOfParallelThreads
202 .getText());
203
204 Enumeration<AbstractButton> execModeButtons = execModeButtonGroup
205 .getElements();
206 ExecutionMode execMode = Misc.DEFAULT_EXEC_MODE;
207 while (execModeButtons.hasMoreElements()) {
208 AbstractButton execModeButton = execModeButtons.nextElement();
209 if (execModeButton.isSelected()) {
210 execMode = ExecutionMode.forString(execModeButton.getText());
211 break;
212 }
213 }
214
215 p3jConfiguration.put(Misc.PREF_NUM_TRIALS, numTrials);
216 p3jConfiguration
217 .put(Misc.PREF_NUM_PARALLEL_THREADS, numParallelThreads);
218 p3jConfiguration.put(Misc.PREF_EXECUTION_MODE, execMode);
219 setVisible(false);
220 } catch (Exception ex) {
221 GUI.printErrorMessage(owner, "Error applying new settings.",
222 "The new settings cannot be applied:" + ex.getMessage(), ex);
223 }
224 }
225 }
226 }