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  
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   * Dialog to show during execution of trials.
44   * 
45   * @author Christina Bohk
46   * @author Roland Ewald
47   * 
48   */
49  public class ExecutionProgressDialog extends JDialog implements IObserver {
50  
51    /** Serialization ID. */
52    private static final long serialVersionUID = 6990212654769873627L;
53  
54    /** The preferred height of the progress bar. */
55    public static final int PREFERRED_HEIGHT_PROGBAR = 40;
56  
57    /** Width of the dialog. */
58    private static final int DIALOG_WIDTH = 400;
59  
60    /** Height of the dialog. */
61    private static final int DIALOG_HEIGHT = 130;
62  
63    /** The initial name of the button to pause execution. */
64    private static final String PAUSE_COMMAND = "Pause";
65  
66    /** The font size of the status message. */
67    private static final int FONT_SIZE_STATUS_MSG = 20;
68  
69    /** The progress bar. */
70    private final JProgressBar progressBar;
71  
72    /** Overall number of trials. */
73    private final int numberOfTrials;
74  
75    /** Set of the computation tasks that are observed. */
76    private Set<IComputationTask> computationTasks = new HashSet<IComputationTask>();
77  
78    /** The trial counter. */
79    private int trialCounter;
80  
81    /** Button to cancel execution. */
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    /** Button to pause/resume the execution. */
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   /** Button to close the dialog. */
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   /** Button panel. */
130   private JPanel buttonPanel = new JPanel();
131   {
132     buttonPanel.add(okButton);
133     buttonPanel.add(pauseButton);
134     buttonPanel.add(cancelButton);
135   }
136 
137   /**
138    * Instantiates a new execution progress dialog.
139    * 
140    * @param numOfTrials
141    *          the number of trials
142    */
143   public ExecutionProgressDialog(int numOfTrials) {
144     setModal(true);
145     numberOfTrials = numOfTrials;
146     progressBar = new JProgressBar(0, numberOfTrials);
147     initUI();
148   }
149 
150   /**
151    * Initializes the user interface.
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     // Don't do anything here...
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    * Add computation task to the set of observed ones.
194    * 
195    * @param computationTask
196    *          the computation task to be observed
197    */
198   public void addSimulationRun(IComputationTask computationTask) {
199     computationTasks.add(computationTask);
200   }
201 
202   /**
203    * Event handler called when execution is done
204    */
205   private void executionDone() {
206     P3J.getInstance().switchNavigationTreeTab(
207         NavigationTreeTab.RESULTS_OVERVIEW);
208     P3J.getInstance().refreshNavigationTree();
209     setVisible(false);
210   }
211 
212 }