3 import java.awt.event.ActionEvent;
4 import java.awt.event.ActionListener;
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;
16 import tim.prune.I18nManager;
17 import tim.prune.function.Cancellable;
20 * Class to show a progress dialog for various time-consuming functions
22 public class GenericProgressDialog
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;
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
38 public GenericProgressDialog(String inTitleKey, String inLabelKey,
39 JFrame inParentFrame, Cancellable inFunction)
41 _dialogTitleKey = inTitleKey;
42 _labelKey = inLabelKey;
43 if (_labelKey == null) {
44 _labelKey = "confirm.running";
46 _parentFrame = inParentFrame;
47 _function = inFunction;
51 * Create the dialog to show the progress
53 private void createProgressDialog()
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)
74 panel.add(cancelButton);
75 _progressDialog.getContentPane().add(panel);
76 _progressDialog.pack();
77 _progressDialog.setVisible(true);
81 * Show the dialog in indeterminate mode, before limits are calculated
85 if (_progressDialog == null)
87 createProgressDialog();
88 _progressBar.setIndeterminate(true);
93 * Update the progress bar
94 * @param inCurrent current value
95 * @param inMax maximum value
97 public void showProgress(int inCurrent, int inMax)
99 if (_progressDialog == null)
100 createProgressDialog();
101 if (_progressBar.isIndeterminate())
102 _progressBar.setIndeterminate(false);
104 _progressBar.setMaximum(inMax);
105 _progressBar.setValue(inCurrent);
106 _progressBar.setString("" + inCurrent + " / " + _progressBar.getMaximum());
114 if (_progressDialog != null)
115 _progressDialog.dispose();