File: ProgressDialog.java

package info (click to toggle)
gpsprune 17-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 3,984 kB
  • ctags: 5,218
  • sloc: java: 39,403; sh: 25; makefile: 17; python: 15
file content (121 lines) | stat: -rw-r--r-- 3,232 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package tim.prune.gui;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;

import tim.prune.I18nManager;

/**
 * Class to show a simple progress dialog
 * similar to swing's ProgressMonitor but with a few
 * modifications
 */
public class ProgressDialog
{
	/** Parent frame */
	private JFrame _parentFrame = null;
	/** Key for title text */
	private String _titleKey = null;
	/** function dialog */
	private JDialog _dialog = null;
	/** Progress bar for function */
	private JProgressBar _progressBar = null;
	/** Cancel flag */
	private boolean _cancelled = false;


	/**
	 * Constructor
	 * @param inParentFrame parent frame
	 * @param inNameKey key for title
	 */
	public ProgressDialog(JFrame inParentFrame, String inNameKey)
	{
		_parentFrame = inParentFrame;
		_titleKey = inNameKey;
	}

	public void show()
	{
		if (_dialog == null)
		{
			_dialog = new JDialog(_parentFrame, I18nManager.getText(_titleKey), false);
			_dialog.setLocationRelativeTo(_parentFrame);
			_dialog.getContentPane().add(makeDialogComponents());
			_dialog.pack();
		}
		_progressBar.setMinimum(0);
		_progressBar.setMaximum(100);
		_progressBar.setValue(0);
		_progressBar.setIndeterminate(true);
		_cancelled = false;
		_dialog.setVisible(true);
	}

	/**
	 * Make the dialog components
	 * @return the GUI components for the dialog
	 */
	private Component makeDialogComponents()
	{
		JPanel dialogPanel = new JPanel();
		dialogPanel.setLayout(new BorderLayout());
		dialogPanel.add(new JLabel(I18nManager.getText("confirm.running")), BorderLayout.NORTH);
		// Centre panel with an empty border
		JPanel centrePanel = new JPanel();
		centrePanel.setBorder(BorderFactory.createEmptyBorder(8, 8, 8, 8));
		centrePanel.setLayout(new BorderLayout());
		_progressBar = new JProgressBar();
		_progressBar.setPreferredSize(new Dimension(250, 30));
		centrePanel.add(_progressBar, BorderLayout.CENTER);
		dialogPanel.add(centrePanel, BorderLayout.CENTER);
		// Cancel button at the bottom
		JPanel buttonPanel = new JPanel();
		buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
		JButton cancelButton = new JButton(I18nManager.getText("button.cancel"));
		cancelButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				_cancelled = true;
				_dialog.dispose();
			}
		});
		buttonPanel.add(cancelButton);
		dialogPanel.add(buttonPanel, BorderLayout.SOUTH);
		return dialogPanel;
	}

	/** Set the maximum value of the progress bar */
	public void setMaximum(int inMax) {
		_progressBar.setMaximum(inMax);
		_progressBar.setIndeterminate(inMax <= 1);
	}

	/** Set the current value of the progress bar */
	public void setValue(int inValue) {
		_progressBar.setValue(inValue);
	}

	/** Close the dialog */
	public void dispose() {
		_dialog.dispose();
	}

	/**
	 * @return true if cancel button was pressed
	 */
	public boolean isCancelled() {
		return _cancelled;
	}
}