File: ProgressDialog.java

package info (click to toggle)
gpsprune 26.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,824 kB
  • sloc: java: 52,154; sh: 25; makefile: 21; python: 15
file content (154 lines) | stat: -rw-r--r-- 4,016 bytes parent folder | download | duplicates (4)
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package tim.prune.gui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;

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;
import tim.prune.function.Cancellable;

/**
 * Class to show a progress dialog for various time-consuming functions
 */
public class ProgressDialog implements ProgressIndicator
{
	private JDialog _dialog = null;
	private final String _titleKey;
	private final String _labelKey;
	private JProgressBar _progressBar = null;
	private final JFrame _parentFrame;
	private final Cancellable _function;
	private boolean _cancelled = false;
	private int _maxValue = -1;

	/**
	 * Constructor
	 * @param inParentFrame parent frame for creating dialog
	 * @param inTitleKey key for dialog title text
	 * @param inLabelKey key for label text
	 * @param inFunction function which can be cancelled
	 */
	public ProgressDialog(JFrame inParentFrame, String inTitleKey,
		String inLabelKey, Cancellable inFunction)
	{
		_titleKey = inTitleKey;
		_labelKey = inLabelKey == null ? "confirm.running" : inLabelKey;
		_parentFrame = inParentFrame;
		_function = inFunction;
	}

	/**
	 * Constructor without cancel button
	 * @param inParentFrame parent frame
	 * @param inTitleKey key for dialog title
	 */
	public ProgressDialog(JFrame inParentFrame, String inTitleKey) {
		this(inParentFrame, inTitleKey, null, null);
	}

	/**
	 * Create the dialog to show the progress
	 */
	private JDialog createProgressDialog()
	{
		JDialog dialog = new JDialog(_parentFrame, I18nManager.getText(_titleKey));
		dialog.setLocationRelativeTo(_parentFrame);
		JPanel panel = new JPanel();
		panel.setLayout(new BorderLayout());
		panel.add(new JLabel(I18nManager.getText(_labelKey)), 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(300, 30));
		_progressBar.setValue(0);
		_progressBar.setStringPainted(true);
		_progressBar.setString("");
		centrePanel.add(_progressBar, BorderLayout.CENTER);
		panel.add(centrePanel, BorderLayout.CENTER);
		if (_function != null)
		{
			// 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(e -> {_cancelled = true; _function.cancel();});
			buttonPanel.add(cancelButton);
			panel.add(buttonPanel, BorderLayout.SOUTH);
		}
		dialog.getContentPane().add(panel);
		dialog.pack();
		return dialog;
	}

	/**
	 * Show the dialog in indeterminate mode, before limits are calculated
	 */
	public void show()
	{
		_cancelled = false;
		if (_dialog == null) {
			_dialog = createProgressDialog();
		}
		_dialog.setVisible(true);
		setMaximumValue(-1);
	}

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

	/** Set the current value of the progress bar */
	public void setValue(int inValue)
	{
		if (_maxValue > 0)
		{
			_progressBar.setString("" + inValue + " / " + _maxValue);
			_progressBar.setValue(inValue);
		}
		else {
			_progressBar.setString("" + inValue);
		}
	}

	/**
	 * Close the dialog
	 */
	public void close()
	{
		if (_dialog != null) {
			_dialog.dispose();
		}
	}

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

	@Override
	public void showProgress(int inCurrent, int inMax)
	{
		if (inMax != _maxValue) {
			setMaximumValue(inMax);
		}
		setValue(inCurrent);
	}
}