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.Frame;
21  import java.awt.event.ActionEvent;
22  import java.awt.event.ActionListener;
23  
24  import javax.swing.JButton;
25  import javax.swing.JDialog;
26  import javax.swing.JLabel;
27  import javax.swing.JPanel;
28  import javax.swing.JProgressBar;
29  import javax.swing.SwingConstants;
30  
31  import org.jamesii.gui.utils.BasicUtilities;
32  
33  import p3j.misc.IProgressObserver;
34  import p3j.misc.gui.GUI;
35  
36  /**
37   * Simple progress bar dialog to show progress of generic tasks that may take
38   * longer. Typical examples would be loading and storing projections, as this
39   * depends on the size of the projections (and also on the type of data storage
40   * and the hardware).
41   * 
42   * @see p3j.gui.dialogs.execstatus.ExecutionProgressDialog for a more
43   *      sophisticated, task-specific implementation
44   * 
45   *      Created on 14.10.2012
46   * 
47   * @author Christina Bohk
48   * @author Roland Ewald
49   */
50  public class SimpleProgressDialog extends JDialog implements IProgressObserver {
51  
52    /** The Constant serialVersionUID. */
53    private static final long serialVersionUID = 6832108832051608722L;
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 actual progress bar. */
62    final JProgressBar progressBar;
63  
64    /** Flag to determine whether this process can be cancelled. */
65    final boolean cancellationAllowed;
66  
67    /** Flag to signal cancellation. */
68    private boolean cancelled = false;
69  
70    /** Button to close the dialog. */
71    private JButton okButton = new JButton("OK");
72    {
73      okButton.addActionListener(new ActionListener() {
74        @Override
75        public void actionPerformed(ActionEvent e) {
76          setVisible(false);
77        }
78      });
79    }
80  
81    /** Button to cancel the task. */
82    private JButton cancelButton = new JButton("Cancel");
83    {
84      cancelButton.addActionListener(new ActionListener() {
85        @Override
86        public void actionPerformed(ActionEvent e) {
87          cancelled = true;
88        }
89      });
90    }
91  
92    /**
93     * Instantiates a new simple progress dialog.
94     * 
95     * @param owner
96     *          the owner of the dialog
97     * @param processName
98     *          the process name
99     * @param detailedDescription
100    *          the detailed description
101    * @param numOfWaypoints
102    *          the number of waypoints
103    * @param cancellationAllowed
104    *          flag to allow cancellation
105    */
106   public SimpleProgressDialog(Frame owner, String processName,
107       String detailedDescription, int numOfWaypoints,
108       boolean cancellationAllowed) {
109     super(owner, processName, false);
110     this.cancellationAllowed = cancellationAllowed;
111     setSize(DIALOG_WIDTH, DIALOG_HEIGHT
112         - (detailedDescription.isEmpty() ? 20 : 0));
113     GUI.centerOnScreen(this);
114 
115     progressBar = new JProgressBar(0, numOfWaypoints);
116     okButton.setEnabled(false);
117 
118     JPanel buttonPanel = new JPanel();
119     buttonPanel.add(okButton);
120     if (cancellationAllowed)
121       buttonPanel.add(cancelButton);
122 
123     JPanel content = new JPanel(new BorderLayout(GUI.STD_LAYOUT_GAP,
124         GUI.STD_LAYOUT_GAP));
125     JLabel executionSummary = new JLabel(detailedDescription,
126         SwingConstants.CENTER);
127     content.add(executionSummary, BorderLayout.NORTH);
128     content.add(progressBar, BorderLayout.CENTER);
129     content.add(buttonPanel, BorderLayout.SOUTH);
130 
131     progressBar.setStringPainted(true);
132     progressBar.setPreferredSize(new Dimension(0,
133         ExecutionProgressDialog.PREFERRED_HEIGHT_PROGBAR));
134     getContentPane().add(content);
135   }
136 
137   @Override
138   public void taskFinished() {
139     updateProgress(progressBar.getMaximum(), "Done");
140     okButton.setEnabled(true);
141     cancelButton.setEnabled(false);
142   }
143 
144   @Override
145   public void taskCanceled() {
146     setVisible(false);
147   }
148 
149   @Override
150   public void updateProgress(final int waypoint, final String status) {
151     BasicUtilities.invokeLaterOnEDT(new Runnable() {
152       @Override
153       public void run() {
154         progressBar.setValue(waypoint);
155         progressBar.setString(status);
156       }
157     });
158   }
159 
160   @Override
161   public int getCurrentWaypoint() {
162     return progressBar.getValue();
163   }
164 
165   @Override
166   public void addWaypoints(int additionalWayPoints) {
167     progressBar.setMaximum(progressBar.getMaximum() + additionalWayPoints);
168   }
169 
170   @Override
171   public void incrementProgress(String status) {
172     updateProgress(progressBar.getValue() + 1, status);
173   }
174 
175   @Override
176   public boolean isCancelled() {
177     return cancelled;
178   }
179 
180   /**
181    * Shows the progress dialog.
182    * 
183    * @param owner
184    *          the owner of the dialog
185    * @param processName
186    *          the process name
187    * @param detailedDescription
188    *          the detailed description
189    * @param numOfWaypoints
190    *          the number of waypoints to be shown
191    * @param cancellationAllowed
192    *          the flag to allow cancellation
193    * @return the simple progress dialog
194    */
195   public static IProgressObserver showDialog(Frame owner, String processName,
196       String detailedDescription, int numOfWaypoints,
197       boolean cancellationAllowed) {
198     final SimpleProgressDialog theDialog = new SimpleProgressDialog(owner,
199         processName, detailedDescription, numOfWaypoints, cancellationAllowed);
200     BasicUtilities.invokeLaterOnEDT(new Runnable() {
201       @Override
202       public void run() {
203         theDialog.setVisible(true);
204       }
205     });
206     return theDialog;
207   }
208 
209 }