]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/gui/ProgressDialog.java
d09087e7daa433f73f5d143a8d5cda14df979c64
[GpsPrune.git] / tim / prune / gui / ProgressDialog.java
1 package tim.prune.gui;
2
3 import java.awt.BorderLayout;
4 import java.awt.Component;
5 import java.awt.Dimension;
6 import java.awt.FlowLayout;
7 import java.awt.event.ActionEvent;
8 import java.awt.event.ActionListener;
9
10 import javax.swing.JButton;
11 import javax.swing.JDialog;
12 import javax.swing.JFrame;
13 import javax.swing.JLabel;
14 import javax.swing.JPanel;
15 import javax.swing.JProgressBar;
16
17 import tim.prune.I18nManager;
18
19 /**
20  * Class to show a simple progress dialog
21  * similar to swing's ProgressMonitor but with a few
22  * modifications
23  */
24 public class ProgressDialog
25 {
26         /** Parent frame */
27         private JFrame _parentFrame = null;
28         /** Key for title text */
29         private String _titleKey = null;
30         /** function dialog */
31         private JDialog _dialog = null;
32         /** Progress bar for function */
33         private JProgressBar _progressBar = null;
34         /** Cancel flag */
35         private boolean _cancelled = false;
36
37
38         /**
39          * Constructor
40          * @param inParentFrame parent frame
41          * @param inNameKey key for title
42          */
43         public ProgressDialog(JFrame inParentFrame, String inNameKey)
44         {
45                 _parentFrame = inParentFrame;
46                 _titleKey = inNameKey;
47         }
48
49         public void show()
50         {
51                 if (_dialog == null)
52                 {
53                         _dialog = new JDialog(_parentFrame, I18nManager.getText(_titleKey), false);
54                         _dialog.setLocationRelativeTo(_parentFrame);
55                         _dialog.getContentPane().add(makeDialogComponents());
56                         _dialog.pack();
57                 }
58                 _progressBar.setMinimum(0);
59                 _progressBar.setMaximum(100);
60                 _progressBar.setValue(0);
61                 _progressBar.setIndeterminate(true);
62                 _cancelled = false;
63                 _dialog.setVisible(true);
64         }
65
66         /**
67          * Make the dialog components
68          * @return the GUI components for the dialog
69          */
70         private Component makeDialogComponents()
71         {
72                 JPanel dialogPanel = new JPanel();
73                 dialogPanel.setLayout(new BorderLayout());
74                 dialogPanel.add(new JLabel(I18nManager.getText("confirm.running")), BorderLayout.NORTH);
75                 _progressBar = new JProgressBar();
76                 _progressBar.setPreferredSize(new Dimension(250, 30));
77                 dialogPanel.add(_progressBar, BorderLayout.CENTER);
78                 // Cancel button at the bottom
79                 JPanel buttonPanel = new JPanel();
80                 buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
81                 JButton cancelButton = new JButton(I18nManager.getText("button.cancel"));
82                 cancelButton.addActionListener(new ActionListener() {
83                         public void actionPerformed(ActionEvent e) {
84                                 _cancelled = true;
85                                 _dialog.dispose();
86                         }
87                 });
88                 buttonPanel.add(cancelButton);
89                 dialogPanel.add(buttonPanel, BorderLayout.SOUTH);
90                 return dialogPanel;
91         }
92
93         /** Set the maximum value of the progress bar */
94         public void setMaximum(int inMax) {
95                 _progressBar.setMaximum(inMax);
96                 _progressBar.setIndeterminate(inMax <= 1);
97         }
98
99         /** Set the current value of the progress bar */
100         public void setValue(int inValue) {
101                 _progressBar.setValue(inValue);
102         }
103
104         /** Close the dialog */
105         public void dispose() {
106                 _dialog.dispose();
107         }
108
109         /**
110          * @return true if cancel button was pressed
111          */
112         public boolean isCancelled() {
113                 return _cancelled;
114         }
115 }