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 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   * 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
50      IObserver<PPPMProcessor> {
51  
52    /** Serialization ID. */
53    private static final long serialVersionUID = 6990212654769873627L;
54  
55    /** The preferred height of the progress bar. */
56    public static final int PREFERRED_HEIGHT_PROGBAR = 40;
57  
58    /** Width of the dialog. */
59    private static final int DIALOG_WIDTH = 400;
60  
61    /** Height of the dialog. */
62    private static final int DIALOG_HEIGHT = 130;
63  
64    /** The initial name of the button to pause execution. */
65    private static final String PAUSE_COMMAND = "Pause";
66  
67    /** The font size of the status message. */
68    private static final int FONT_SIZE_STATUS_MSG = 20;
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(true);
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     GUI.showModalDialog(this);
174   }
175 
176   @Override
177   public void update(PPPMProcessor entity) {
178     // Don't do anything here...
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    * Add computation task to the set of observed ones.
195    * 
196    * @param computationTask
197    *          the computation task to be observed
198    */
199   public void addSimulationRun(IComputationTask computationTask) {
200     computationTasks.add(computationTask);
201   }
202 
203   /**
204    * Event handler called when execution is done
205    */
206   private void executionDone() {
207     P3J.getInstance().switchNavigationTreeTab(
208         NavigationTreeTab.RESULTS_OVERVIEW);
209     P3J.getInstance().refreshNavigationTree();
210     setVisible(false);
211   }
212 
213 }