gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.swing / org.gvsig.tools.swing.impl / src / main / java / org / gvsig / tools / swing / impl / task / DefaultJTaskStatusController.java @ 2632
History | View | Annotate | Download (10.3 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2021 gvSIG Association.
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 2
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
* For any additional information, do not hesitate to contact us
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
23 |
*/
|
24 |
package org.gvsig.tools.swing.impl.task; |
25 |
|
26 |
import org.gvsig.tools.swing.api.task.TaskStatusController; |
27 |
import java.awt.event.ActionEvent; |
28 |
import java.net.URL; |
29 |
|
30 |
import javax.swing.ImageIcon; |
31 |
import javax.swing.JButton; |
32 |
import javax.swing.JLabel; |
33 |
import javax.swing.JProgressBar; |
34 |
import javax.swing.SwingUtilities; |
35 |
|
36 |
import org.gvsig.tools.observer.Observable; |
37 |
import org.gvsig.tools.observer.Observer; |
38 |
import org.gvsig.tools.task.SimpleTaskStatus; |
39 |
import org.gvsig.tools.task.TaskStatus; |
40 |
import org.gvsig.tools.task.TaskStatusManager; |
41 |
|
42 |
/**
|
43 |
* Default implementation of the {@link TaskStatusController}.
|
44 |
*
|
45 |
* @author gvSIG Team
|
46 |
*/
|
47 |
public class DefaultJTaskStatusController implements TaskStatusController, Observer { |
48 |
|
49 |
private JLabel titlelabel = null; |
50 |
private JLabel messagelabel = null; |
51 |
private JProgressBar progressBar = null; |
52 |
private JButton cancelRequestButton = null; |
53 |
private JButton removeTaskButton; |
54 |
|
55 |
private boolean showCancelButton; |
56 |
private boolean showRemoveTaskButton; |
57 |
|
58 |
private TaskStatus bindedTaskStatus;
|
59 |
private TaskStatusManager bindedTaskStatusManager;
|
60 |
|
61 |
@SuppressWarnings("OverridableMethodCallInConstructor") |
62 |
public DefaultJTaskStatusController(
|
63 |
TaskStatus taskStatus, |
64 |
JLabel titlelabel,
|
65 |
JLabel messagelabel,
|
66 |
JProgressBar progressBar,
|
67 |
JButton cancelRequestButton,
|
68 |
JButton removeTaskButton
|
69 |
) { |
70 |
this.titlelabel = titlelabel;
|
71 |
this.messagelabel = messagelabel;
|
72 |
this.progressBar = progressBar;
|
73 |
this.cancelRequestButton = cancelRequestButton;
|
74 |
this.removeTaskButton = removeTaskButton;
|
75 |
|
76 |
this.bindedTaskStatus = null; |
77 |
this.showCancelButton = true; |
78 |
|
79 |
this.bind(taskStatus);
|
80 |
|
81 |
this.initComponents();
|
82 |
} |
83 |
|
84 |
public DefaultJTaskStatusController(
|
85 |
TaskStatus taskStatus, |
86 |
JLabel titlelabel,
|
87 |
JLabel messagelabel,
|
88 |
JProgressBar progressBar
|
89 |
) { |
90 |
this(taskStatus, titlelabel, messagelabel, progressBar, null, null); |
91 |
} |
92 |
|
93 |
public DefaultJTaskStatusController(
|
94 |
JLabel titlelabel,
|
95 |
JLabel messagelabel,
|
96 |
JProgressBar progressBar
|
97 |
) { |
98 |
this(null, titlelabel, messagelabel, progressBar, null, null); |
99 |
} |
100 |
|
101 |
private void initComponents() { |
102 |
if( this.messagelabel==null ) { |
103 |
this.messagelabel = new JLabel(); |
104 |
} |
105 |
if( this.titlelabel==null ) { |
106 |
this.titlelabel = new JLabel(); |
107 |
} |
108 |
if( this.progressBar==null ) { |
109 |
this.progressBar = new JProgressBar(); |
110 |
} |
111 |
if( this.cancelRequestButton==null ) { |
112 |
this.cancelRequestButton = new JButton(); |
113 |
} |
114 |
if( this.removeTaskButton==null ) { |
115 |
this.removeTaskButton = new JButton(); |
116 |
} |
117 |
this.progressBar.setIndeterminate(false); |
118 |
this.progressBar.setBorderPainted(true); |
119 |
|
120 |
this.cancelRequestButton.setIcon(getIcon("cancelRequestButton.png")); |
121 |
this.cancelRequestButton.setDisabledIcon(getIcon("disabledCancelRequestButton.png")); |
122 |
if (!this.showCancelButton) { |
123 |
this.cancelRequestButton.setVisible(this.showCancelButton); |
124 |
} |
125 |
this.cancelRequestButton.addActionListener((ActionEvent arg0) -> { |
126 |
cancelRequestButton.setEnabled(false);
|
127 |
bindedTaskStatus.cancelRequest(); |
128 |
}); |
129 |
this.removeTaskButton.setIcon(getIcon("removeTaskButton.png")); |
130 |
this.removeTaskButton.setDisabledIcon(getIcon("disabledRemoveTaskButton.png")); |
131 |
this.removeTaskButton.setEnabled(true); |
132 |
this.removeTaskButton.addActionListener((ActionEvent arg0) -> { |
133 |
if( bindedTaskStatus==null ) { |
134 |
return;
|
135 |
} |
136 |
if (bindedTaskStatus.isRunning()) {
|
137 |
if ((arg0.getModifiers() & ActionEvent.CTRL_MASK) == ActionEvent.CTRL_MASK) { |
138 |
bindedTaskStatus.getManager().remove(bindedTaskStatus); |
139 |
} |
140 |
} else {
|
141 |
bindedTaskStatus.getManager().remove(bindedTaskStatus); |
142 |
} |
143 |
}); |
144 |
} |
145 |
|
146 |
@Override
|
147 |
public void setVisible(boolean visible) { |
148 |
if( !SwingUtilities.isEventDispatchThread() ) { |
149 |
SwingUtilities.invokeLater(() -> {
|
150 |
setVisible(visible); |
151 |
}); |
152 |
return;
|
153 |
} |
154 |
this.titlelabel.setVisible(visible);
|
155 |
this.messagelabel.setVisible(visible);
|
156 |
this.progressBar.setVisible(visible);
|
157 |
this.cancelRequestButton.setVisible(visible);
|
158 |
this.removeTaskButton.setVisible(visible);
|
159 |
} |
160 |
|
161 |
|
162 |
@Override
|
163 |
public boolean getShowCancelButton() { |
164 |
return this.showCancelButton; |
165 |
} |
166 |
|
167 |
@Override
|
168 |
public void setShowCancelButton(boolean showCancelButton) { |
169 |
this.showCancelButton = showCancelButton;
|
170 |
if (this.cancelRequestButton != null) { |
171 |
this.cancelRequestButton.setVisible(this.showCancelButton); |
172 |
} |
173 |
} |
174 |
|
175 |
@Override
|
176 |
public boolean getShowRemoveTaskButton() { |
177 |
return this.showRemoveTaskButton; |
178 |
} |
179 |
|
180 |
@Override
|
181 |
public void setShowRemoveTaskButton(boolean showRemoveTaskButton) { |
182 |
this.showRemoveTaskButton = showRemoveTaskButton;
|
183 |
if (this.removeTaskButton != null) { |
184 |
this.removeTaskButton.setVisible(this.showRemoveTaskButton); |
185 |
} |
186 |
} |
187 |
|
188 |
@Override
|
189 |
public void bind(TaskStatus taskStatus) { |
190 |
if (cancelRequestButton!=null ) { |
191 |
this.cancelRequestButton.setEnabled(this.showCancelButton); |
192 |
this.cancelRequestButton.setVisible(this.showCancelButton); |
193 |
} |
194 |
if (this.bindedTaskStatus != null) { |
195 |
this.bindedTaskStatus.deleteObserver(this); |
196 |
} |
197 |
if (this.bindedTaskStatusManager != null) { |
198 |
this.bindedTaskStatusManager.deleteObserver(this); |
199 |
} |
200 |
this.bindedTaskStatus = taskStatus;
|
201 |
this.bindedTaskStatusManager = null; |
202 |
if (this.bindedTaskStatus != null) { |
203 |
this.bindedTaskStatus.addObserver(this); |
204 |
} |
205 |
} |
206 |
|
207 |
@Override
|
208 |
public void bind(TaskStatusManager taskStatusManager) { |
209 |
if (this.bindedTaskStatus != null) { |
210 |
this.bindedTaskStatus.deleteObserver(this); |
211 |
} |
212 |
if (this.bindedTaskStatusManager != null) { |
213 |
this.bindedTaskStatusManager.deleteObserver(this); |
214 |
} |
215 |
this.bindedTaskStatus = null; |
216 |
this.bindedTaskStatusManager = taskStatusManager;
|
217 |
if (this.bindedTaskStatusManager != null) { |
218 |
this.bindedTaskStatusManager.addObserver(this); |
219 |
} |
220 |
} |
221 |
|
222 |
private ImageIcon getIcon(String name) { |
223 |
URL iconurl = this.getClass().getResource(name); |
224 |
if (iconurl == null) { |
225 |
return new ImageIcon(); |
226 |
} |
227 |
return new ImageIcon(iconurl); |
228 |
} |
229 |
|
230 |
@Override
|
231 |
public void update(final Observable observable, final Object notification) { |
232 |
|
233 |
if( observable==null ) { |
234 |
return;
|
235 |
} |
236 |
TaskStatus theTaskStatus; |
237 |
if( observable instanceof TaskStatus ) { |
238 |
theTaskStatus = (TaskStatus) observable; |
239 |
} else if( observable instanceof TaskStatusManager ) { |
240 |
theTaskStatus = (TaskStatus) notification; |
241 |
if( theTaskStatus == null ) { |
242 |
TaskStatusManager manager = (TaskStatusManager)observable; |
243 |
theTaskStatus = manager.getRunningTaskStatusMostRecent(); |
244 |
} |
245 |
} else {
|
246 |
return;
|
247 |
} |
248 |
if (!SwingUtilities.isEventDispatchThread()) { |
249 |
SwingUtilities.invokeLater(() -> {
|
250 |
update(observable, notification); |
251 |
}); |
252 |
return;
|
253 |
} |
254 |
if (theTaskStatus == null || !theTaskStatus.isRunning()) { |
255 |
this.messagelabel.setText(theTaskStatus==null?"":theTaskStatus.getLabel()); |
256 |
|
257 |
this.progressBar.setIndeterminate(false); |
258 |
this.progressBar.setValue(100); |
259 |
this.cancelRequestButton.setEnabled(false); |
260 |
this.removeTaskButton.setEnabled(true); |
261 |
return;
|
262 |
} |
263 |
|
264 |
if (theTaskStatus.getTitle() == null) { |
265 |
this.titlelabel.setText("Unknown"); |
266 |
} else {
|
267 |
this.titlelabel.setText(theTaskStatus.getTitle());
|
268 |
} |
269 |
this.messagelabel.setText(theTaskStatus.getLabel());
|
270 |
this.progressBar.setIndeterminate(theTaskStatus.isIndeterminate());
|
271 |
this.progressBar.setValue(theTaskStatus.getCompleted());
|
272 |
|
273 |
if (!this.cancelRequestButton.isEnabled()) { |
274 |
this.cancelRequestButton.setEnabled(true); |
275 |
} |
276 |
|
277 |
} |
278 |
|
279 |
@Override
|
280 |
public TaskStatus getTaskStatus() {
|
281 |
return this.bindedTaskStatus; |
282 |
} |
283 |
|
284 |
@Override
|
285 |
public SimpleTaskStatus getSimpleTaskStatus() {
|
286 |
return (SimpleTaskStatus) this.bindedTaskStatus; |
287 |
} |
288 |
|
289 |
@Override
|
290 |
public void setTitle(String title) { |
291 |
if (this.bindedTaskStatus instanceof SimpleTaskStatus) { |
292 |
((SimpleTaskStatus) this.bindedTaskStatus).setTitle(title);
|
293 |
} else {
|
294 |
this.titlelabel.setText(title);
|
295 |
} |
296 |
} |
297 |
|
298 |
@Override
|
299 |
public void message(String message) { |
300 |
if (this.bindedTaskStatus instanceof SimpleTaskStatus) { |
301 |
((SimpleTaskStatus) this.bindedTaskStatus).message(message);
|
302 |
} else {
|
303 |
this.messagelabel.setText(message);
|
304 |
} |
305 |
} |
306 |
|
307 |
@Override
|
308 |
public void setCurValue(long value) { |
309 |
if (this.bindedTaskStatus instanceof SimpleTaskStatus) { |
310 |
((SimpleTaskStatus) this.bindedTaskStatus).setCurValue(value);
|
311 |
} |
312 |
} |
313 |
|
314 |
} |