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