View Javadoc

1   /*
2    * Copyright 2006 - 2012 Christina Bohk and Roland Ewald
3    *  
4    * Licensed under the Apache License, Version 2.0 (the "License"); 
5    * you may not use this file except in compliance with the License. 
6    * You may obtain a copy of the License at 
7    *  
8    *  http://www.apache.org/licenses/LICENSE-2.0
9    *  
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
13   * See the License for the specific language governing permissions and 
14   * limitations under the License. 
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   * Dialog to show during execution of trials.
45   * 
46   * @author Christina Bohk
47   * @author Roland Ewald
48   * 
49   */
50  public class ExecutionProgressDialog extends JDialog implements IObserver {
51  
52  	/** Serialization ID. */
53  	private static final long serialVersionUID = 6990212654769873627L;
54  
55  	/** Width of the dialog. */
56  	private static final int DIALOG_WIDTH = 400;
57  
58  	/** Height of the dialog. */
59  	private static final int DIALOG_HEIGHT = 130;
60  
61  	/** The initial name of the button to pause execution. */
62  	private static final String PAUSE_COMMAND = "Pause";
63  
64  	/** The font size of the status message. */
65  	private static final int FONT_SIZE_STATUS_MSG = 20;
66  
67  	/** The preffered height of the progress bar. */
68  	private static final int PREFERRED_HEIGHT_PROGBAR = 40;
69  
70  	/** The progress bar. */
71  	private final JProgressBar progressBar;
72  
73  	/** Overall number of trials. */
74  	private final int numberOfTrials;
75  
76  	/** Set of the computation tasks that are observed. */
77  	private Set<IComputationTask> computationTasks = new HashSet<IComputationTask>();
78  
79  	/** The trial counter. */
80  	private int trialCounter;
81  
82  	/** Button to cancel execution. */
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  	/** Button to pause/resume the execution. */
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 	/** Button to close the dialog. */
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 	/** Button panel. */
131 	private JPanel buttonPanel = new JPanel();
132 	{
133 		buttonPanel.add(okButton);
134 		buttonPanel.add(pauseButton);
135 		buttonPanel.add(cancelButton);
136 	}
137 
138 	/**
139 	 * Instantiates a new execution progress dialog.
140 	 * 
141 	 * @param numOfTrials
142 	 *          the number of trials
143 	 */
144 	public ExecutionProgressDialog(int numOfTrials) {
145 		setModal(false);
146 		numberOfTrials = numOfTrials;
147 		progressBar = new JProgressBar(0, numberOfTrials);
148 		initUI();
149 	}
150 
151 	/**
152 	 * Initializes the user interface.
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 		// Don't do anything here...
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 	 * Add computation task to the set of observed ones.
196 	 * 
197 	 * @param computationTask
198 	 *          the computation task to be observed
199 	 */
200 	public void addSimulationRun(IComputationTask computationTask) {
201 		computationTasks.add(computationTask);
202 	}
203 
204 	/**
205 	 * Event handler called when execution is done
206 	 */
207 	private void executionDone() {
208 		P3J.getInstance().switchNavigationTreeTab(
209 		    NavigationTreeTab.RESULTS_OVERVIEW);
210 		P3J.getInstance().refreshNavigationTree();
211 		setVisible(false);
212 	}
213 
214 }