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 = 600;
54
55
56 public static final int DIALOG_HEIGHT = 200;
57
58
59 private static final int FORM_KEY_WIDTH = 200;
60
61
62 private static final int FORM_VALUE_WIDTH = 100;
63
64
65 private final JPanel contentPanel;
66
67
68 private final JTextField numOfTrials = new JTextField();
69
70
71 private final JTextField numOfParallelThreads = new JTextField();
72
73
74 private final P3JConfigFile p3jConfiguration;
75
76
77 private final ButtonGroup execModeButtonGroup = new ButtonGroup();
78
79
80 private final JButton apply = new JButton("Apply");
81 {
82 apply.addActionListener(new ApplyAction(this));
83 }
84
85
86 private final JButton cancel = new JButton("Cancel");
87 {
88 cancel.addActionListener(new ActionListener() {
89 @Override
90 public void actionPerformed(ActionEvent e) {
91 setVisible(false);
92 }
93 });
94 }
95
96
97 private final JButton resetToDefaults = new JButton("Reset Defaults");
98 {
99 resetToDefaults.addActionListener(new ActionListener() {
100 @Override
101 public void actionPerformed(ActionEvent e) {
102
103 numOfTrials.setText("" + Misc.DEFAULT_NUM_TRIALS);
104 numOfParallelThreads.setText("" + Misc.DEFAULT_NUM_PARALLEL_THREADS);
105
106 ExecutionMode defaultMode = Misc.DEFAULT_EXEC_MODE;
107 Enumeration<AbstractButton> execModeButtons = execModeButtonGroup
108 .getElements();
109 while (execModeButtons.hasMoreElements()) {
110 AbstractButton execModeButton = execModeButtons.nextElement();
111 if (execModeButton.getText().equals(defaultMode.toString())) {
112 execModeButton.setSelected(true);
113 break;
114 }
115 }
116 contentPanel.repaint();
117 }
118 });
119 }
120
121
122 private final List<JButton> buttons = new ArrayList<JButton>();
123 {
124 buttons.add(resetToDefaults);
125 buttons.add(cancel);
126 buttons.add(apply);
127 }
128
129
130
131
132
133
134
135 public ExecutionPreferencesDialog(Frame owner) {
136 super(owner, "Edit Execution Preferences", true);
137 p3jConfiguration = P3J.getInstance().getConfigFile();
138 setSize(DIALOG_WIDTH, DIALOG_HEIGHT);
139 GUI.centerOnScreen(this);
140
141 PropertiesShowPanelFactory pspf = new PropertiesShowPanelFactory(
142 FORM_KEY_WIDTH, FORM_VALUE_WIDTH, buttons, 2);
143 numOfTrials.setText("" + p3jConfiguration.get(Misc.PREF_NUM_TRIALS));
144 pspf.app(Misc.PREF_NUM_TRIALS + ":", numOfTrials);
145
146 numOfParallelThreads.setText(""
147 + p3jConfiguration.get(Misc.PREF_NUM_PARALLEL_THREADS));
148 pspf.app(Misc.PREF_NUM_PARALLEL_THREADS + ":", numOfParallelThreads);
149
150 pspf.app(Misc.PREF_EXECUTION_MODE + ":", createExecutionModePanel());
151 contentPanel = pspf.constructPanel();
152 this.getContentPane().add(contentPanel);
153 }
154
155
156
157
158
159
160 private JPanel createExecutionModePanel() {
161 JPanel execModePanel = new JPanel();
162 ExecutionMode currentMode = (ExecutionMode) p3jConfiguration
163 .get(Misc.PREF_EXECUTION_MODE);
164
165 for (ExecutionMode mode : ExecutionMode.values()) {
166 JRadioButton button = new JRadioButton(mode.toString());
167 execModePanel.add(button);
168 execModeButtonGroup.add(button);
169 if (mode == currentMode) {
170 button.setSelected(true);
171 }
172 }
173 return execModePanel;
174 }
175
176
177
178
179 class ApplyAction implements ActionListener {
180
181
182 private Component owner;
183
184
185
186
187
188
189
190 ApplyAction(Component own) {
191 owner = own;
192 }
193
194
195
196
197
198
199
200 @Override
201 public void actionPerformed(ActionEvent e) {
202 try {
203 Integer numTrials = Integer.parseInt(numOfTrials.getText());
204 Integer numParallelThreads = Integer.parseInt(numOfParallelThreads
205 .getText());
206
207 Enumeration<AbstractButton> execModeButtons = execModeButtonGroup
208 .getElements();
209 ExecutionMode execMode = Misc.DEFAULT_EXEC_MODE;
210 while (execModeButtons.hasMoreElements()) {
211 AbstractButton execModeButton = execModeButtons.nextElement();
212 if (execModeButton.isSelected()) {
213 execMode = ExecutionMode.forString(execModeButton.getText());
214 break;
215 }
216 }
217
218
219
220 if (numTrials % numParallelThreads != 0)
221 numTrials += (numParallelThreads - numTrials % numParallelThreads);
222
223 p3jConfiguration.put(Misc.PREF_NUM_TRIALS, numTrials);
224 p3jConfiguration
225 .put(Misc.PREF_NUM_PARALLEL_THREADS, numParallelThreads);
226 p3jConfiguration.put(Misc.PREF_EXECUTION_MODE, execMode);
227 setVisible(false);
228 } catch (Exception ex) {
229 GUI.printErrorMessage(owner, "Error applying new settings.",
230 "The new settings cannot be applied:" + ex.getMessage(), ex);
231 }
232 }
233 }
234 }