]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/gui/GenericProgressDialog.java
acef147fb8906d60eef8fefaa0ee125eb653f5f1
[GpsPrune.git] / tim / prune / gui / GenericProgressDialog.java
1 package tim.prune.gui;
2
3 import java.awt.event.ActionEvent;
4 import java.awt.event.ActionListener;
5
6 import javax.swing.BorderFactory;
7 import javax.swing.Box;
8 import javax.swing.BoxLayout;
9 import javax.swing.JButton;
10 import javax.swing.JDialog;
11 import javax.swing.JFrame;
12 import javax.swing.JLabel;
13 import javax.swing.JPanel;
14 import javax.swing.JProgressBar;
15
16 import tim.prune.I18nManager;
17 import tim.prune.function.Cancellable;
18
19 /**
20  * Class to show a progress dialog for various time-consuming functions
21  */
22 public class GenericProgressDialog
23 {
24         private JDialog _progressDialog   = null;
25         private String _dialogTitleKey    = null;
26         private String _labelKey          = null;
27         private JProgressBar _progressBar = null;
28         private JFrame _parentFrame       = null;
29         private Cancellable _function     = null;
30
31         /**
32          * Constructor
33          * @param inTitleKey key for dialog title text
34          * @param inLabelKey key for label text
35          * @param inParentFrame parent frame for creating dialog
36          * @param inFunction function which can be cancelled
37          */
38         public GenericProgressDialog(String inTitleKey, String inLabelKey,
39                 JFrame inParentFrame, Cancellable inFunction)
40         {
41                 _dialogTitleKey = inTitleKey;
42                 _labelKey = inLabelKey;
43                 if (_labelKey == null) {
44                         _labelKey = "confirm.running";
45                 }
46                 _parentFrame = inParentFrame;
47                 _function = inFunction;
48         }
49
50         /**
51          * Create the dialog to show the progress
52          */
53         private void createProgressDialog()
54         {
55                 _progressDialog = new JDialog(_parentFrame, I18nManager.getText(_dialogTitleKey));
56                 _progressDialog.setLocationRelativeTo(_parentFrame);
57                 _progressBar = new JProgressBar(0, 100);
58                 _progressBar.setValue(0);
59                 _progressBar.setStringPainted(true);
60                 _progressBar.setString("");
61                 JPanel panel = new JPanel();
62                 panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
63                 panel.setBorder(BorderFactory.createEmptyBorder(6, 6, 6, 6));
64                 panel.add(new JLabel(I18nManager.getText(_labelKey)));
65                 panel.add(_progressBar);
66                 panel.add(Box.createVerticalStrut(6)); // spacer
67                 JButton cancelButton = new JButton(I18nManager.getText("button.cancel"));
68                 cancelButton.addActionListener(new ActionListener() {
69                         public void actionPerformed(ActionEvent e)
70                         {
71                                 _function.cancel();
72                         }
73                 });
74                 panel.add(cancelButton);
75                 _progressDialog.getContentPane().add(panel);
76                 _progressDialog.pack();
77                 _progressDialog.setVisible(true);
78         }
79
80         /**
81          * Show the dialog in indeterminate mode, before limits are calculated
82          */
83         public void show()
84         {
85                 if (_progressDialog == null)
86                 {
87                         createProgressDialog();
88                         _progressBar.setIndeterminate(true);
89                 }
90         }
91
92         /**
93          * Update the progress bar
94          * @param inCurrent current value
95          * @param inMax maximum value
96          */
97         public void showProgress(int inCurrent, int inMax)
98         {
99                 if (_progressDialog == null)
100                         createProgressDialog();
101                 if (_progressBar.isIndeterminate())
102                         _progressBar.setIndeterminate(false);
103                 if (inMax > 0)
104                         _progressBar.setMaximum(inMax);
105                 _progressBar.setValue(inCurrent);
106                 _progressBar.setString("" + inCurrent + " / " + _progressBar.getMaximum());
107         }
108
109         /**
110          * Close the dialog
111          */
112         public void close()
113         {
114                 if (_progressDialog != null)
115                         _progressDialog.dispose();
116         }
117 }