1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package p3j.gui.dialogs.execstatus;
17
18 import james.core.base.IEntity;
19 import james.core.experiments.tasks.IComputationTask;
20 import james.core.observe.IObserver;
21 import james.gui.utils.BasicUtilities;
22
23 import java.awt.BorderLayout;
24 import java.awt.Dimension;
25 import java.awt.Font;
26 import java.awt.event.ActionEvent;
27 import java.awt.event.ActionListener;
28 import java.util.HashSet;
29 import java.util.Set;
30
31 import javax.swing.JButton;
32 import javax.swing.JDialog;
33 import javax.swing.JLabel;
34 import javax.swing.JPanel;
35 import javax.swing.JProgressBar;
36 import javax.swing.SwingConstants;
37
38 import p3j.gui.P3J;
39 import p3j.gui.misc.NavigationTreeTab;
40 import p3j.misc.gui.GUI;
41
42
43
44
45
46
47
48
49
50 public class ExecutionProgressDialog extends JDialog implements IObserver {
51
52
53 private static final long serialVersionUID = 6990212654769873627L;
54
55
56 private static final int DIALOG_WIDTH = 400;
57
58
59 private static final int DIALOG_HEIGHT = 130;
60
61
62 private static final String PAUSE_COMMAND = "Pause";
63
64
65 private static final int FONT_SIZE_STATUS_MSG = 20;
66
67
68 private static final int PREFERRED_HEIGHT_PROGBAR = 40;
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(false);
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 setVisible(true);
174 BasicUtilities.repaintOnEDT(this);
175 }
176
177 @Override
178 public void update(IEntity entity) {
179
180 }
181
182 @Override
183 public void update(IEntity entity, Object hint) {
184 trialCounter++;
185 if (trialCounter == numberOfTrials) {
186 okButton.setEnabled(true);
187 pauseButton.setEnabled(false);
188 cancelButton.setEnabled(false);
189 }
190 progressBar.setValue(trialCounter);
191 progressBar.setString("Trial #" + trialCounter);
192 }
193
194
195
196
197
198
199
200 public void addSimulationRun(IComputationTask computationTask) {
201 computationTasks.add(computationTask);
202 }
203
204
205
206
207 private void executionDone() {
208 P3J.getInstance().switchNavigationTreeTab(
209 NavigationTreeTab.RESULTS_OVERVIEW);
210 P3J.getInstance().refreshNavigationTree();
211 setVisible(false);
212 }
213
214 }