1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package p3j.gui.dialogs.execstatus;
17
18 import java.awt.BorderLayout;
19 import java.awt.Dimension;
20 import java.awt.Font;
21 import java.awt.event.ActionEvent;
22 import java.awt.event.ActionListener;
23 import java.util.HashSet;
24 import java.util.Set;
25
26 import javax.swing.JButton;
27 import javax.swing.JDialog;
28 import javax.swing.JLabel;
29 import javax.swing.JPanel;
30 import javax.swing.JProgressBar;
31 import javax.swing.SwingConstants;
32
33 import org.jamesii.core.experiments.tasks.IComputationTask;
34 import org.jamesii.core.observe.IObserver;
35
36 import p3j.gui.P3J;
37 import p3j.gui.misc.NavigationTreeTab;
38 import p3j.misc.gui.GUI;
39 import p3j.simulation.PPPMProcessor;
40
41
42
43
44
45
46
47
48
49 public class ExecutionProgressDialog extends JDialog implements
50 IObserver<PPPMProcessor> {
51
52
53 private static final long serialVersionUID = 6990212654769873627L;
54
55
56 public static final int PREFERRED_HEIGHT_PROGBAR = 40;
57
58
59 private static final int DIALOG_WIDTH = 400;
60
61
62 private static final int DIALOG_HEIGHT = 130;
63
64
65 private static final String PAUSE_COMMAND = "Pause";
66
67
68 private static final int FONT_SIZE_STATUS_MSG = 20;
69
70
71 private final JProgressBar progressBar;
72
73
74 private final int numberOfTrials;
75
76
77 private Set<IComputationTask> computationTasks = new HashSet<IComputationTask>();
78
79
80 private int trialCounter;
81
82
83 private JButton cancelButton = new JButton("Cancel");
84 {
85 cancelButton.addActionListener(new ActionListener() {
86 @Override
87 public synchronized void actionPerformed(ActionEvent e) {
88 if (GUI.printQuestion(P3J.getInstance(), "Really cancel execution?",
89 "Do you really want to cancel the execution?")) {
90 for (IComputationTask simulationRun : computationTasks) {
91 simulationRun.stopProcessor();
92 }
93 executionDone();
94 }
95 }
96 });
97 }
98
99
100 private JButton pauseButton = new JButton(PAUSE_COMMAND);
101 {
102 pauseButton.addActionListener(new ActionListener() {
103 @Override
104 public synchronized void actionPerformed(ActionEvent e) {
105 boolean pausing = false;
106 for (IComputationTask simulationRun : computationTasks) {
107 simulationRun.pauseProcessor();
108 pausing = simulationRun.isPausing();
109 }
110 if (pausing) {
111 pauseButton.setText("Resume");
112 } else {
113 pauseButton.setText(PAUSE_COMMAND);
114 }
115 }
116 });
117 }
118
119
120 private JButton okButton = new JButton("OK");
121 {
122 okButton.addActionListener(new ActionListener() {
123 @Override
124 public void actionPerformed(ActionEvent e) {
125 executionDone();
126 }
127 });
128 }
129
130
131 private JPanel buttonPanel = new JPanel();
132 {
133 buttonPanel.add(okButton);
134 buttonPanel.add(pauseButton);
135 buttonPanel.add(cancelButton);
136 }
137
138
139
140
141
142
143
144 public ExecutionProgressDialog(int numOfTrials) {
145 setModal(true);
146 numberOfTrials = numOfTrials;
147 progressBar = new JProgressBar(0, numberOfTrials);
148 initUI();
149 }
150
151
152
153
154 private void initUI() {
155 setTitle("Execution Status");
156 setSize(DIALOG_WIDTH, DIALOG_HEIGHT);
157 GUI.centerOnScreen(this);
158 okButton.setEnabled(false);
159
160 JPanel content = new JPanel(new BorderLayout(GUI.STD_LAYOUT_GAP,
161 GUI.STD_LAYOUT_GAP));
162 JLabel executionSummary = new JLabel("Number of Trials to execute: "
163 + numberOfTrials, SwingConstants.CENTER);
164 executionSummary.setFont(new Font("Sans", Font.BOLD, FONT_SIZE_STATUS_MSG));
165 content.add(executionSummary, BorderLayout.NORTH);
166 content.add(progressBar, BorderLayout.CENTER);
167 content.add(buttonPanel, BorderLayout.SOUTH);
168
169 progressBar.setStringPainted(true);
170 progressBar.setPreferredSize(new Dimension(0, PREFERRED_HEIGHT_PROGBAR));
171 getContentPane().add(content);
172
173 GUI.showModalDialog(this);
174 }
175
176 @Override
177 public void update(PPPMProcessor entity) {
178
179 }
180
181 @Override
182 public void update(PPPMProcessor entity, Object hint) {
183 trialCounter++;
184 if (trialCounter == numberOfTrials) {
185 okButton.setEnabled(true);
186 pauseButton.setEnabled(false);
187 cancelButton.setEnabled(false);
188 }
189 progressBar.setValue(trialCounter);
190 progressBar.setString("Trial #" + trialCounter);
191 }
192
193
194
195
196
197
198
199 public void addSimulationRun(IComputationTask computationTask) {
200 computationTasks.add(computationTask);
201 }
202
203
204
205
206 private void executionDone() {
207 P3J.getInstance().switchNavigationTreeTab(
208 NavigationTreeTab.RESULTS_OVERVIEW);
209 P3J.getInstance().refreshNavigationTree();
210 setVisible(false);
211 }
212
213 }