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;
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 p3j.gui.dialogs.execstatus.ExecutionProgressDialog;
32  import p3j.misc.gui.GUI;
33  
34  /**
35   * Simple progress bar dialog to show progress of generic tasks that may take
36   * longer. Typical examples would be loading and storing projections, as this
37   * depends on the size of the projections (and also on the type of data storage
38   * and the hardware).
39   * 
40   * @see p3j.gui.dialogs.execstatus.ExecutionProgressDialog for a more
41   *      sophisticated, task-specific implementation
42   * 
43   *      Created on 14.10.2012
44   * 
45   * @author Christina Bohk
46   * @author Roland Ewald
47   */
48  public class SimpleProgressDialog extends JDialog {
49  
50    /** The Constant serialVersionUID. */
51    private static final long serialVersionUID = 6832108832051608722L;
52  
53    /** Width of the dialog. */
54    private static final int DIALOG_WIDTH = 400;
55  
56    /** Height of the dialog. */
57    private static final int DIALOG_HEIGHT = 130;
58  
59    final int waypoints;
60  
61    final JProgressBar progressBar;
62  
63    /** Button to close the dialog. */
64    private JButton okButton = new JButton("OK");
65    {
66      okButton.addActionListener(new ActionListener() {
67        @Override
68        public void actionPerformed(ActionEvent e) {
69          setVisible(false);
70        }
71      });
72    }
73  
74    public SimpleProgressDialog(Frame owner, String processName,
75        String detailedDescription, int numOfWaypoints) {
76      super(owner, processName, false);
77      setSize(DIALOG_WIDTH, DIALOG_HEIGHT);
78      GUI.centerOnScreen(this);
79  
80      waypoints = numOfWaypoints;
81      progressBar = new JProgressBar(0, numOfWaypoints);
82      okButton.setEnabled(false);
83  
84      JPanel buttonPanel = new JPanel();
85      buttonPanel.add(okButton);
86  
87      JPanel content = new JPanel(new BorderLayout(GUI.STD_LAYOUT_GAP,
88          GUI.STD_LAYOUT_GAP));
89      JLabel executionSummary = new JLabel(detailedDescription,
90          SwingConstants.CENTER);
91      content.add(executionSummary, BorderLayout.NORTH);
92      content.add(progressBar, BorderLayout.CENTER);
93      content.add(buttonPanel, BorderLayout.SOUTH);
94  
95      progressBar.setStringPainted(true);
96      progressBar.setPreferredSize(new Dimension(0,
97          ExecutionProgressDialog.PREFERRED_HEIGHT_PROGBAR));
98      getContentPane().add(content);
99    }
100 
101   public void taskFinished() {
102     updateProgress(waypoints, "Done");
103     okButton.setEnabled(true);
104   }
105 
106   public void updateProgress(int waypoint, String status) {
107     progressBar.setValue(waypoint);
108     progressBar.setString(status);
109   }
110 
111   public void incrementProgress(String status) {
112     updateProgress(progressBar.getValue() + 1, status);
113   }
114 
115 }