Statistics
| Revision:

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 802 cordinyana
/**
2
 * gvSIG. Desktop Geographic Information System.
3 673 cordinyana
 *
4 2632 jjdelcerro
 * Copyright (C) 2007-2021 gvSIG Association.
5 802 cordinyana
 *
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 447 jjdelcerro
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 448 jjdelcerro
import org.gvsig.tools.task.SimpleTaskStatus;
39 447 jjdelcerro
import org.gvsig.tools.task.TaskStatus;
40 2411 jjdelcerro
import org.gvsig.tools.task.TaskStatusManager;
41 447 jjdelcerro
42 673 cordinyana
/**
43 1820 jjdelcerro
 * Default implementation of the {@link TaskStatusController}.
44 1723 jjdelcerro
 *
45 673 cordinyana
 * @author gvSIG Team
46
 */
47 1820 jjdelcerro
public class DefaultJTaskStatusController implements TaskStatusController, Observer {
48 447 jjdelcerro
49 1723 jjdelcerro
    private JLabel titlelabel = null;
50
    private JLabel messagelabel = null;
51
    private JProgressBar progressBar = null;
52
    private JButton cancelRequestButton = null;
53
    private JButton removeTaskButton;
54 477 jjdelcerro
55 1723 jjdelcerro
    private boolean showCancelButton;
56
    private boolean showRemoveTaskButton;
57 447 jjdelcerro
58 2411 jjdelcerro
    private TaskStatus bindedTaskStatus;
59
    private TaskStatusManager bindedTaskStatusManager;
60
61 2632 jjdelcerro
    @SuppressWarnings("OverridableMethodCallInConstructor")
62 1820 jjdelcerro
    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 2411 jjdelcerro
        this.bindedTaskStatus = null;
77 1723 jjdelcerro
        this.showCancelButton = true;
78 447 jjdelcerro
79 1723 jjdelcerro
        this.bind(taskStatus);
80 447 jjdelcerro
81 1820 jjdelcerro
        this.initComponents();
82 1723 jjdelcerro
    }
83
84 1820 jjdelcerro
    public DefaultJTaskStatusController(
85
            TaskStatus taskStatus,
86
            JLabel titlelabel,
87
            JLabel messagelabel,
88
            JProgressBar progressBar
89
    ) {
90
        this(taskStatus, titlelabel, messagelabel, progressBar, null, null);
91 1723 jjdelcerro
    }
92
93 1820 jjdelcerro
    public DefaultJTaskStatusController(
94
            JLabel titlelabel,
95
            JLabel messagelabel,
96
            JProgressBar progressBar
97
    ) {
98
        this(null, titlelabel, messagelabel, progressBar, null, null);
99 1723 jjdelcerro
    }
100
101 1820 jjdelcerro
    private void initComponents() {
102 1840 jjdelcerro
        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 1820 jjdelcerro
        if( this.cancelRequestButton==null ) {
112
            this.cancelRequestButton = new JButton();
113 1723 jjdelcerro
        }
114 1820 jjdelcerro
        if( this.removeTaskButton==null ) {
115
            this.removeTaskButton = new JButton();
116 1723 jjdelcerro
        }
117
        this.progressBar.setIndeterminate(false);
118
        this.progressBar.setBorderPainted(true);
119 447 jjdelcerro
120 1723 jjdelcerro
        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 2411 jjdelcerro
        this.cancelRequestButton.addActionListener((ActionEvent arg0) -> {
126
            cancelRequestButton.setEnabled(false);
127
            bindedTaskStatus.cancelRequest();
128 1723 jjdelcerro
        });
129
        this.removeTaskButton.setIcon(getIcon("removeTaskButton.png"));
130
        this.removeTaskButton.setDisabledIcon(getIcon("disabledRemoveTaskButton.png"));
131
        this.removeTaskButton.setEnabled(true);
132 2411 jjdelcerro
        this.removeTaskButton.addActionListener((ActionEvent arg0) -> {
133 2632 jjdelcerro
            if( bindedTaskStatus==null ) {
134
                return;
135
            }
136 2411 jjdelcerro
            if (bindedTaskStatus.isRunning()) {
137
                if ((arg0.getModifiers() & ActionEvent.CTRL_MASK) == ActionEvent.CTRL_MASK) {
138
                    bindedTaskStatus.getManager().remove(bindedTaskStatus);
139 1723 jjdelcerro
                }
140 2411 jjdelcerro
            } else {
141
                bindedTaskStatus.getManager().remove(bindedTaskStatus);
142 1723 jjdelcerro
            }
143
        });
144 1820 jjdelcerro
    }
145 447 jjdelcerro
146 2411 jjdelcerro
    @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 1820 jjdelcerro
    public boolean getShowCancelButton() {
164
        return this.showCancelButton;
165
    }
166 447 jjdelcerro
167 2411 jjdelcerro
    @Override
168 1820 jjdelcerro
    public void setShowCancelButton(boolean showCancelButton) {
169
        this.showCancelButton = showCancelButton;
170
        if (this.cancelRequestButton != null) {
171
            this.cancelRequestButton.setVisible(this.showCancelButton);
172
        }
173
    }
174 447 jjdelcerro
175 2411 jjdelcerro
    @Override
176 1820 jjdelcerro
    public boolean getShowRemoveTaskButton() {
177
        return this.showRemoveTaskButton;
178 1723 jjdelcerro
    }
179
180 2411 jjdelcerro
    @Override
181 1820 jjdelcerro
    public void setShowRemoveTaskButton(boolean showRemoveTaskButton) {
182
        this.showRemoveTaskButton = showRemoveTaskButton;
183
        if (this.removeTaskButton != null) {
184
            this.removeTaskButton.setVisible(this.showRemoveTaskButton);
185
        }
186
    }
187
188 2411 jjdelcerro
    @Override
189 1820 jjdelcerro
    public void bind(TaskStatus taskStatus) {
190 2632 jjdelcerro
        if (cancelRequestButton!=null ) {
191
            this.cancelRequestButton.setEnabled(this.showCancelButton);
192
            this.cancelRequestButton.setVisible(this.showCancelButton);
193
        }
194 2411 jjdelcerro
        if (this.bindedTaskStatus != null) {
195
            this.bindedTaskStatus.deleteObserver(this);
196 1820 jjdelcerro
        }
197 2411 jjdelcerro
        if (this.bindedTaskStatusManager != null) {
198
            this.bindedTaskStatusManager.deleteObserver(this);
199 1820 jjdelcerro
        }
200 2411 jjdelcerro
        this.bindedTaskStatus = taskStatus;
201
        this.bindedTaskStatusManager = null;
202
        if (this.bindedTaskStatus != null) {
203
            this.bindedTaskStatus.addObserver(this);
204
        }
205 1820 jjdelcerro
    }
206 2411 jjdelcerro
207 2632 jjdelcerro
    @Override
208 2411 jjdelcerro
    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 1820 jjdelcerro
222 1723 jjdelcerro
    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 2411 jjdelcerro
230
    @Override
231 1723 jjdelcerro
    public void update(final Observable observable, final Object notification) {
232
233 2411 jjdelcerro
        if( observable==null ) {
234 1723 jjdelcerro
            return;
235
        }
236 2411 jjdelcerro
        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 1723 jjdelcerro
        if (!SwingUtilities.isEventDispatchThread()) {
249 2411 jjdelcerro
            SwingUtilities.invokeLater(() -> {
250
                update(observable, notification);
251 1723 jjdelcerro
            });
252
            return;
253
        }
254 1824 jjdelcerro
        if (theTaskStatus == null || !theTaskStatus.isRunning()) {
255
            this.messagelabel.setText(theTaskStatus==null?"":theTaskStatus.getLabel());
256 1723 jjdelcerro
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 1824 jjdelcerro
        if (theTaskStatus.getTitle() == null) {
265 709 jldominguez
            this.titlelabel.setText("Unknown");
266
        } else {
267 1824 jjdelcerro
            this.titlelabel.setText(theTaskStatus.getTitle());
268 709 jldominguez
        }
269 1824 jjdelcerro
        this.messagelabel.setText(theTaskStatus.getLabel());
270
        this.progressBar.setIndeterminate(theTaskStatus.isIndeterminate());
271
        this.progressBar.setValue(theTaskStatus.getCompleted());
272 447 jjdelcerro
273 1723 jjdelcerro
        if (!this.cancelRequestButton.isEnabled()) {
274
            this.cancelRequestButton.setEnabled(true);
275
        }
276 448 jjdelcerro
277 1723 jjdelcerro
    }
278
279 2411 jjdelcerro
    @Override
280 1723 jjdelcerro
    public TaskStatus getTaskStatus() {
281 2411 jjdelcerro
        return this.bindedTaskStatus;
282 1723 jjdelcerro
    }
283
284 597 cordinyana
    @Override
285 2411 jjdelcerro
    public SimpleTaskStatus getSimpleTaskStatus() {
286
        return (SimpleTaskStatus) this.bindedTaskStatus;
287
    }
288
289
    @Override
290 597 cordinyana
    public void setTitle(String title) {
291 2411 jjdelcerro
        if (this.bindedTaskStatus instanceof SimpleTaskStatus) {
292
            ((SimpleTaskStatus) this.bindedTaskStatus).setTitle(title);
293 2632 jjdelcerro
        } else {
294
            this.titlelabel.setText(title);
295 597 cordinyana
        }
296 1723 jjdelcerro
    }
297 448 jjdelcerro
298 2411 jjdelcerro
    @Override
299 1723 jjdelcerro
    public void message(String message) {
300 2411 jjdelcerro
        if (this.bindedTaskStatus instanceof SimpleTaskStatus) {
301
            ((SimpleTaskStatus) this.bindedTaskStatus).message(message);
302 2632 jjdelcerro
        } else {
303
            this.messagelabel.setText(message);
304 1723 jjdelcerro
        }
305
    }
306 448 jjdelcerro
307 2411 jjdelcerro
    @Override
308 1723 jjdelcerro
    public void setCurValue(long value) {
309 2411 jjdelcerro
        if (this.bindedTaskStatus instanceof SimpleTaskStatus) {
310
            ((SimpleTaskStatus) this.bindedTaskStatus).setCurValue(value);
311 1723 jjdelcerro
        }
312
    }
313 448 jjdelcerro
314 447 jjdelcerro
}