1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package de.mindcrimeilab.swing.util;
19
20 import java.awt.Component;
21 import java.awt.Cursor;
22 import java.awt.GridBagConstraints;
23 import java.awt.GridBagLayout;
24 import java.awt.Insets;
25 import java.awt.Window;
26
27 import javax.swing.BorderFactory;
28 import javax.swing.JComponent;
29 import javax.swing.JDialog;
30 import javax.swing.JLabel;
31 import javax.swing.JProgressBar;
32 import javax.swing.SwingConstants;
33 import javax.swing.SwingUtilities;
34 import javax.swing.SwingWorker;
35 import javax.swing.WindowConstants;
36
37
38
39
40
41
42
43
44
45 public abstract class VisualProgressWorker<T, V> extends SwingWorker<T, V> {
46
47 private final InternalProgressDialog dialog;
48
49 private volatile boolean stopWorker;
50
51 public VisualProgressWorker(Component component) {
52 super();
53 dialog = new InternalProgressDialog(SwingUtilities.windowForComponent(component));
54 stopWorker = false;
55 }
56
57 public void setStopWorker(boolean stop) {
58 stopWorker = stop;
59 }
60
61 public boolean hasStopWorker() {
62 return stopWorker;
63 }
64
65 public void showProgressDialog() {
66
67 dialog.setLocationRelativeTo(dialog.getParent());
68 dialog.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
69 dialog.pack();
70 dialog.setVisible(true);
71 dialog.toFront();
72 dialog.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
73 }
74
75 public void hideProgressDialog() {
76 dialog.setVisible(false);
77 dialog.dispose();
78 dialog.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
79 }
80
81 public void setDetailMessage(String message) {
82 dialog.setDetailMessage(message);
83 dialog.pack();
84 }
85
86 public void setActionText(String text) {
87 dialog.setActionText(text);
88 dialog.pack();
89 }
90
91 public void setTitle(String title) {
92 dialog.setTitle(title);
93 dialog.pack();
94 }
95
96 @Override
97 protected void done() {
98 super.done();
99 if (dialog.isVisible()) {
100 this.hideProgressDialog();
101 }
102 }
103 }
104
105
106
107
108
109
110
111
112
113 final class InternalProgressDialog extends JDialog {
114
115
116
117
118 private static final long serialVersionUID = -418787593671991025L;
119
120 private JProgressBar jpbProgessBar = null;
121
122 private JLabel jlDetailMessage = null;
123
124 private JLabel jlAction = null;
125
126 public InternalProgressDialog(Window owner) {
127 super(owner);
128 initializeGui();
129 }
130
131 public void setDetailMessage(String name) {
132 getJlDetailMessage().setText(name);
133 }
134
135 public void setActionText(String text) {
136 getJlAction().setText(text);
137 }
138
139 private void initializeGui() {
140 setTitle("Operation in progress...");
141 setModal(false);
142 setResizable(false);
143
144 final JComponent contentPane = (JComponent) getContentPane();
145 contentPane.setLayout(new GridBagLayout());
146 contentPane.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
147
148 addActionLabelTo(contentPane);
149 addActionTextTo(contentPane);
150 addLabelProgressTo(contentPane);
151 addProgressBarTo(contentPane);
152 addDetailMessageTo(contentPane);
153
154 pack();
155 getJlAction().setText("");
156 getJlDetailMessage().setText("");
157 }
158
159 private void addActionLabelTo(JComponent component) {
160 GridBagConstraints gbc = new GridBagConstraints();
161 gbc.anchor = GridBagConstraints.WEST;
162 gbc.gridx = 0;
163 gbc.gridy = 0;
164 gbc.gridwidth = 1;
165 gbc.gridheight = 1;
166 gbc.fill = GridBagConstraints.NONE;
167 gbc.weightx = 0.0;
168 gbc.weighty = 0.0;
169 gbc.insets = new Insets(0, 0, 5, 10);
170 component.add(new JLabel("Action:"), gbc);
171 }
172
173 private void addActionTextTo(JComponent component) {
174 GridBagConstraints gbc = new GridBagConstraints();
175 gbc.anchor = GridBagConstraints.WEST;
176 gbc.gridx = 1;
177 gbc.gridy = 0;
178 gbc.gridwidth = 1;
179 gbc.gridheight = 1;
180 gbc.fill = GridBagConstraints.HORIZONTAL;
181 gbc.weightx = 1.0;
182 gbc.weighty = 0.0;
183 gbc.insets = new Insets(0, 0, 5, 10);
184 component.add(getJlAction(), gbc);
185 }
186
187 private void addLabelProgressTo(JComponent component) {
188 GridBagConstraints gbc = new GridBagConstraints();
189 gbc.anchor = GridBagConstraints.WEST;
190 gbc.gridx = 0;
191 gbc.gridy = 1;
192 gbc.gridwidth = 1;
193 gbc.gridheight = 1;
194 gbc.fill = GridBagConstraints.NONE;
195 gbc.weightx = 0.0;
196 gbc.weighty = 0.0;
197 gbc.insets = new Insets(0, 0, 5, 0);
198 component.add(new JLabel("Progress:"), gbc);
199 }
200
201 private void addProgressBarTo(JComponent component) {
202 GridBagConstraints gbcJpbProgressBar = new GridBagConstraints();
203 gbcJpbProgressBar.anchor = GridBagConstraints.WEST;
204 gbcJpbProgressBar.gridx = 1;
205 gbcJpbProgressBar.gridy = 1;
206 gbcJpbProgressBar.gridwidth = 1;
207 gbcJpbProgressBar.gridheight = 1;
208 gbcJpbProgressBar.fill = GridBagConstraints.BOTH;
209 gbcJpbProgressBar.weightx = 1.0;
210 gbcJpbProgressBar.weighty = 1.0;
211 gbcJpbProgressBar.insets = new Insets(0, 0, 5, 0);
212 component.add(getJpbProgressBar(), gbcJpbProgressBar);
213 }
214
215 private void addDetailMessageTo(JComponent component) {
216 GridBagConstraints gbc = new GridBagConstraints();
217 gbc.anchor = GridBagConstraints.WEST;
218 gbc.gridx = 0;
219 gbc.gridy = 2;
220 gbc.gridwidth = 2;
221 gbc.gridheight = 1;
222 gbc.fill = GridBagConstraints.HORIZONTAL;
223 gbc.weightx = 1.0;
224 gbc.weighty = 0.0;
225 gbc.insets = new Insets(0, 0, 5, 10);
226 component.add(getJlDetailMessage(), gbc);
227 }
228
229 private JProgressBar getJpbProgressBar() {
230 if (null == jpbProgessBar) {
231 jpbProgessBar = new JProgressBar(SwingConstants.HORIZONTAL);
232 jpbProgessBar.setIndeterminate(true);
233 }
234 return jpbProgessBar;
235 }
236
237 private JLabel getJlDetailMessage() {
238 if (null == jlDetailMessage) {
239 jlDetailMessage = new JLabel("X");
240 }
241 return jlDetailMessage;
242 }
243
244 private JLabel getJlAction() {
245 if (null == jlAction) {
246 jlAction = new JLabel("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
247 }
248 return jlAction;
249 }
250
251 }