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 @ 1840
History | View | Annotate | Download (8.13 KB)
1 | 802 | cordinyana | /**
|
---|---|---|---|
2 | * gvSIG. Desktop Geographic Information System.
|
||
3 | 673 | cordinyana | *
|
4 | 802 | cordinyana | * Copyright (C) 2007-2013 gvSIG Association.
|
5 | *
|
||
6 | 673 | cordinyana | * 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 | 802 | cordinyana | *
|
11 | 673 | cordinyana | * 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 | 802 | cordinyana | *
|
16 | 673 | cordinyana | * 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 | 802 | cordinyana | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 | 673 | cordinyana | * MA 02110-1301, USA.
|
20 | 802 | cordinyana | *
|
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 | 673 | cordinyana | */
|
24 | 447 | jjdelcerro | package org.gvsig.tools.swing.impl.task; |
25 | |||
26 | 1820 | jjdelcerro | import org.gvsig.tools.swing.api.task.TaskStatusController; |
27 | 449 | jjdelcerro | import java.awt.event.ActionEvent; |
28 | import java.awt.event.ActionListener; |
||
29 | 447 | jjdelcerro | import java.net.URL; |
30 | |||
31 | import javax.swing.ImageIcon; |
||
32 | import javax.swing.JButton; |
||
33 | import javax.swing.JLabel; |
||
34 | import javax.swing.JProgressBar; |
||
35 | import javax.swing.SwingUtilities; |
||
36 | |||
37 | import org.gvsig.tools.observer.Observable; |
||
38 | import org.gvsig.tools.observer.Observer; |
||
39 | 448 | jjdelcerro | import org.gvsig.tools.task.SimpleTaskStatus; |
40 | 447 | jjdelcerro | import org.gvsig.tools.task.TaskStatus; |
41 | |||
42 | 673 | cordinyana | /**
|
43 | 1820 | jjdelcerro | * Default implementation of the {@link TaskStatusController}.
|
44 | 1723 | jjdelcerro | *
|
45 | 673 | cordinyana | * @author gvSIG Team
|
46 | * @version $Id$
|
||
47 | */
|
||
48 | 1820 | jjdelcerro | public class DefaultJTaskStatusController implements TaskStatusController, Observer { |
49 | 447 | jjdelcerro | |
50 | 1723 | jjdelcerro | private JLabel titlelabel = null; |
51 | private JLabel messagelabel = null; |
||
52 | private JProgressBar progressBar = null; |
||
53 | private JButton cancelRequestButton = null; |
||
54 | private JButton removeTaskButton; |
||
55 | 477 | jjdelcerro | |
56 | 1723 | jjdelcerro | private boolean showCancelButton; |
57 | private boolean showRemoveTaskButton; |
||
58 | 447 | jjdelcerro | |
59 | 1723 | jjdelcerro | private TaskStatus taskStatus;
|
60 | 454 | jjdelcerro | |
61 | 1820 | jjdelcerro | public DefaultJTaskStatusController(
|
62 | TaskStatus taskStatus, |
||
63 | JLabel titlelabel,
|
||
64 | JLabel messagelabel,
|
||
65 | JProgressBar progressBar,
|
||
66 | JButton cancelRequestButton,
|
||
67 | JButton removeTaskButton
|
||
68 | ) { |
||
69 | this.titlelabel = titlelabel;
|
||
70 | this.messagelabel = messagelabel;
|
||
71 | this.progressBar = progressBar;
|
||
72 | this.cancelRequestButton = cancelRequestButton;
|
||
73 | this.removeTaskButton = removeTaskButton;
|
||
74 | |||
75 | 1723 | jjdelcerro | this.taskStatus = null; |
76 | this.showCancelButton = true; |
||
77 | 447 | jjdelcerro | |
78 | 1723 | jjdelcerro | this.bind(taskStatus);
|
79 | 447 | jjdelcerro | |
80 | 1820 | jjdelcerro | this.initComponents();
|
81 | 1723 | jjdelcerro | } |
82 | |||
83 | 1820 | jjdelcerro | public DefaultJTaskStatusController(
|
84 | TaskStatus taskStatus, |
||
85 | JLabel titlelabel,
|
||
86 | JLabel messagelabel,
|
||
87 | JProgressBar progressBar
|
||
88 | ) { |
||
89 | this(taskStatus, titlelabel, messagelabel, progressBar, null, null); |
||
90 | 1723 | jjdelcerro | } |
91 | |||
92 | 1820 | jjdelcerro | public DefaultJTaskStatusController(
|
93 | JLabel titlelabel,
|
||
94 | JLabel messagelabel,
|
||
95 | JProgressBar progressBar
|
||
96 | ) { |
||
97 | this(null, titlelabel, messagelabel, progressBar, null, null); |
||
98 | 1723 | jjdelcerro | } |
99 | |||
100 | 1820 | jjdelcerro | private void initComponents() { |
101 | 1840 | jjdelcerro | if( this.messagelabel==null ) { |
102 | this.messagelabel = new JLabel(); |
||
103 | } |
||
104 | if( this.titlelabel==null ) { |
||
105 | this.titlelabel = new JLabel(); |
||
106 | } |
||
107 | if( this.progressBar==null ) { |
||
108 | this.progressBar = new JProgressBar(); |
||
109 | } |
||
110 | 1820 | jjdelcerro | if( this.cancelRequestButton==null ) { |
111 | this.cancelRequestButton = new JButton(); |
||
112 | 1723 | jjdelcerro | } |
113 | 1820 | jjdelcerro | if( this.removeTaskButton==null ) { |
114 | this.removeTaskButton = new JButton(); |
||
115 | 1723 | jjdelcerro | } |
116 | this.progressBar.setIndeterminate(false); |
||
117 | this.progressBar.setBorderPainted(true); |
||
118 | 447 | jjdelcerro | |
119 | 1723 | jjdelcerro | this.cancelRequestButton.setIcon(getIcon("cancelRequestButton.png")); |
120 | this.cancelRequestButton.setDisabledIcon(getIcon("disabledCancelRequestButton.png")); |
||
121 | if (!this.showCancelButton) { |
||
122 | this.cancelRequestButton.setVisible(this.showCancelButton); |
||
123 | } |
||
124 | this.cancelRequestButton.addActionListener(new ActionListener() { |
||
125 | public void actionPerformed(ActionEvent arg0) { |
||
126 | cancelRequestButton.setEnabled(false);
|
||
127 | taskStatus.cancelRequest(); |
||
128 | } |
||
129 | }); |
||
130 | this.removeTaskButton.setIcon(getIcon("removeTaskButton.png")); |
||
131 | this.removeTaskButton.setDisabledIcon(getIcon("disabledRemoveTaskButton.png")); |
||
132 | this.removeTaskButton.setEnabled(true); |
||
133 | this.removeTaskButton.addActionListener(new ActionListener() { |
||
134 | public void actionPerformed(ActionEvent arg0) { |
||
135 | if (taskStatus.isRunning()) {
|
||
136 | 1820 | jjdelcerro | if ((arg0.getModifiers() & ActionEvent.CTRL_MASK) == ActionEvent.CTRL_MASK) { |
137 | 1723 | jjdelcerro | taskStatus.getManager().remove(taskStatus); |
138 | } |
||
139 | } else {
|
||
140 | taskStatus.getManager().remove(taskStatus); |
||
141 | } |
||
142 | } |
||
143 | }); |
||
144 | 1820 | jjdelcerro | } |
145 | 447 | jjdelcerro | |
146 | 1820 | jjdelcerro | public boolean getShowCancelButton() { |
147 | return this.showCancelButton; |
||
148 | } |
||
149 | 447 | jjdelcerro | |
150 | 1820 | jjdelcerro | public void setShowCancelButton(boolean showCancelButton) { |
151 | this.showCancelButton = showCancelButton;
|
||
152 | if (this.cancelRequestButton != null) { |
||
153 | this.cancelRequestButton.setVisible(this.showCancelButton); |
||
154 | } |
||
155 | } |
||
156 | 447 | jjdelcerro | |
157 | 1820 | jjdelcerro | public boolean getShowRemoveTaskButton() { |
158 | return this.showRemoveTaskButton; |
||
159 | 1723 | jjdelcerro | } |
160 | |||
161 | 1820 | jjdelcerro | public void setShowRemoveTaskButton(boolean showRemoveTaskButton) { |
162 | this.showRemoveTaskButton = showRemoveTaskButton;
|
||
163 | if (this.removeTaskButton != null) { |
||
164 | this.removeTaskButton.setVisible(this.showRemoveTaskButton); |
||
165 | } |
||
166 | } |
||
167 | |||
168 | public void bind(TaskStatus taskStatus) { |
||
169 | if (this.taskStatus != null) { |
||
170 | this.taskStatus.deleteObserver(this); |
||
171 | } |
||
172 | this.taskStatus = taskStatus;
|
||
173 | if (this.taskStatus != null) { |
||
174 | this.taskStatus.addObserver(this); |
||
175 | } |
||
176 | } |
||
177 | |||
178 | 1723 | jjdelcerro | private ImageIcon getIcon(String name) { |
179 | URL iconurl = this.getClass().getResource(name); |
||
180 | if (iconurl == null) { |
||
181 | return new ImageIcon(); |
||
182 | } |
||
183 | return new ImageIcon(iconurl); |
||
184 | } |
||
185 | public void update(final Observable observable, final Object notification) { |
||
186 | |||
187 | if (observable != null && !(observable instanceof TaskStatus)) { |
||
188 | return;
|
||
189 | } |
||
190 | if (!SwingUtilities.isEventDispatchThread()) { |
||
191 | SwingUtilities.invokeLater(new Runnable() { |
||
192 | public void run() { |
||
193 | update(observable, notification); |
||
194 | } |
||
195 | }); |
||
196 | return;
|
||
197 | } |
||
198 | 1824 | jjdelcerro | TaskStatus theTaskStatus = (TaskStatus) observable; |
199 | if (theTaskStatus == null || !theTaskStatus.isRunning()) { |
||
200 | this.messagelabel.setText(theTaskStatus==null?"":theTaskStatus.getLabel()); |
||
201 | 1723 | jjdelcerro | |
202 | this.progressBar.setIndeterminate(false); |
||
203 | this.progressBar.setValue(100); |
||
204 | this.cancelRequestButton.setEnabled(false); |
||
205 | this.removeTaskButton.setEnabled(true); |
||
206 | return;
|
||
207 | } |
||
208 | |||
209 | 1824 | jjdelcerro | if (theTaskStatus.getTitle() == null) { |
210 | 709 | jldominguez | this.titlelabel.setText("Unknown"); |
211 | } else {
|
||
212 | 1824 | jjdelcerro | this.titlelabel.setText(theTaskStatus.getTitle());
|
213 | 709 | jldominguez | } |
214 | 1824 | jjdelcerro | this.messagelabel.setText(theTaskStatus.getLabel());
|
215 | this.progressBar.setIndeterminate(theTaskStatus.isIndeterminate());
|
||
216 | this.progressBar.setValue(theTaskStatus.getCompleted());
|
||
217 | 447 | jjdelcerro | |
218 | 1723 | jjdelcerro | if (!this.cancelRequestButton.isEnabled()) { |
219 | this.cancelRequestButton.setEnabled(true); |
||
220 | } |
||
221 | 448 | jjdelcerro | |
222 | 1723 | jjdelcerro | } |
223 | |||
224 | public TaskStatus getTaskStatus() {
|
||
225 | return this.taskStatus; |
||
226 | } |
||
227 | |||
228 | 597 | cordinyana | @Override
|
229 | public void setTitle(String title) { |
||
230 | if (this.taskStatus instanceof SimpleTaskStatus) { |
||
231 | ((SimpleTaskStatus) this.taskStatus).setTitle(title);
|
||
232 | } |
||
233 | 1723 | jjdelcerro | } |
234 | 448 | jjdelcerro | |
235 | 1723 | jjdelcerro | public void message(String message) { |
236 | if (this.taskStatus instanceof SimpleTaskStatus) { |
||
237 | ((SimpleTaskStatus) this.taskStatus).message(message);
|
||
238 | } |
||
239 | } |
||
240 | 448 | jjdelcerro | |
241 | 1723 | jjdelcerro | public void setCurValue(long value) { |
242 | if (this.taskStatus instanceof SimpleTaskStatus) { |
||
243 | ((SimpleTaskStatus) this.taskStatus).setCurValue(value);
|
||
244 | } |
||
245 | } |
||
246 | 448 | jjdelcerro | |
247 | 447 | jjdelcerro | } |