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