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