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 @ 2755

History | View | Annotate | Download (12.9 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
        this.progressBar.setMinimum(0);
120
        this.progressBar.setMaximum(100);
121
        this.progressBar.setValue(0);
122

    
123
        this.cancelRequestButton.setIcon(getIcon("cancelRequestButton.png"));
124
        this.cancelRequestButton.setDisabledIcon(getIcon("disabledCancelRequestButton.png"));
125
        if( this.cancelRequestButton.getText().equals("...") ) {
126
            this.cancelRequestButton.setText("");
127
        }
128
        if (!this.showCancelButton) {
129
            this.cancelRequestButton.setVisible(this.showCancelButton);
130
        }
131
        this.cancelRequestButton.addActionListener((ActionEvent arg0) -> {
132
            cancelRequestButton.setEnabled(false);
133
            bindedTaskStatus.cancelRequest();
134
        });
135
        this.removeTaskButton.setIcon(getIcon("removeTaskButton.png"));
136
        this.removeTaskButton.setDisabledIcon(getIcon("disabledRemoveTaskButton.png"));
137
        if( this.removeTaskButton.getText().equals("...") ) {
138
            this.removeTaskButton.setText("");
139
        }
140
        this.removeTaskButton.setEnabled(true);
141
        this.removeTaskButton.addActionListener((ActionEvent arg0) -> {
142
            if( bindedTaskStatus==null ) {
143
                return;
144
            }
145
            if (bindedTaskStatus.isRunning()) {
146
                if ((arg0.getModifiers() & ActionEvent.CTRL_MASK) == ActionEvent.CTRL_MASK) {
147
                    bindedTaskStatus.getManager().remove(bindedTaskStatus);
148
                }
149
            } else {
150
                bindedTaskStatus.getManager().remove(bindedTaskStatus);
151
            }
152
        });
153
    }
154

    
155
    @Override
156
    public void setVisible(boolean visible) {
157
        if( !SwingUtilities.isEventDispatchThread() ) {
158
            SwingUtilities.invokeLater(() -> {
159
                setVisible(visible);
160
            });
161
            return;
162
        }
163
        this.titlelabel.setVisible(visible);
164
        this.messagelabel.setVisible(visible);
165
        this.progressBar.setVisible(visible);
166
        this.cancelRequestButton.setVisible(visible);
167
        this.removeTaskButton.setVisible(visible);
168
    }
169
    
170

    
171
    @Override
172
    public boolean getShowCancelButton() {
173
        return this.showCancelButton;
174
    }
175

    
176
    @Override
177
    public void setShowCancelButton(boolean showCancelButton) {
178
        this.showCancelButton = showCancelButton;
179
        if (this.cancelRequestButton != null) {
180
            this.cancelRequestButton.setVisible(this.showCancelButton);
181
        }
182
    }
183

    
184
    @Override
185
    public boolean getShowRemoveTaskButton() {
186
        return this.showRemoveTaskButton;
187
    }
188

    
189
    @Override
190
    public void setShowRemoveTaskButton(boolean showRemoveTaskButton) {
191
        this.showRemoveTaskButton = showRemoveTaskButton;
192
        if (this.removeTaskButton != null) {
193
            this.removeTaskButton.setVisible(this.showRemoveTaskButton);
194
        }
195
    }
196

    
197
    @Override
198
    public void bind(TaskStatus taskStatus) {
199
        if (cancelRequestButton!=null ) {
200
            this.cancelRequestButton.setEnabled(this.showCancelButton);
201
            this.cancelRequestButton.setVisible(this.showCancelButton);
202
        }
203
        if (this.bindedTaskStatus != null) {
204
            this.bindedTaskStatus.deleteObserver(this);
205
        }
206
        if (this.bindedTaskStatusManager != null) {
207
            this.bindedTaskStatusManager.deleteObserver(this);
208
        }
209
        this.bindedTaskStatus = taskStatus;
210
        this.bindedTaskStatusManager = null;
211
        if (this.bindedTaskStatus != null) {
212
            this.bindedTaskStatus.addObserver(this);
213
        }
214
    }
215
    
216
    @Override
217
    public void bind(TaskStatusManager taskStatusManager) {
218
        if (this.bindedTaskStatus != null) {
219
            this.bindedTaskStatus.deleteObserver(this);
220
        }
221
        if (this.bindedTaskStatusManager != null) {
222
            this.bindedTaskStatusManager.deleteObserver(this);
223
        }
224
        this.bindedTaskStatus = null;
225
        this.bindedTaskStatusManager = taskStatusManager;
226
        if (this.bindedTaskStatusManager != null) {
227
            this.bindedTaskStatusManager.addObserver(this);
228
        }
229
    }
230

    
231
    private ImageIcon getIcon(String name) {
232
        URL iconurl = this.getClass().getResource(name);
233
        if (iconurl == null) {
234
            return new ImageIcon();
235
        }
236
        return new ImageIcon(iconurl);
237
    }
238

    
239
    @Override
240
    public void update(final Observable observable, final Object notification) {
241

    
242
        if( observable==null ) {
243
            return;
244
        }
245
        TaskStatus theTaskStatus;
246
        if( observable instanceof TaskStatus ) {
247
            theTaskStatus = (TaskStatus) observable;
248
        } else if( observable instanceof TaskStatusManager ) {
249
            theTaskStatus = (TaskStatus) notification;
250
            if( theTaskStatus == null ) {
251
                TaskStatusManager manager = (TaskStatusManager)observable;
252
                theTaskStatus = manager.getRunningTaskStatusMostRecent();
253
            }
254
        } else {
255
            return;
256
        }
257
        if (!SwingUtilities.isEventDispatchThread()) {
258
            SwingUtilities.invokeLater(() -> {
259
                update(observable, notification);
260
            });
261
            return;
262
        }
263
//        System.out.println("-----------");
264
        if (theTaskStatus == null ) {
265
//            System.out.println("controller:"+this.hashCode());
266
//            System.out.println("Task: null");
267
            this.titlelabel.setText("");
268
            this.messagelabel.setText("");
269
            this.progressBar.setIndeterminate(false);
270
            this.progressBar.setValue(100);
271
            this.cancelRequestButton.setEnabled(false);
272
            this.removeTaskButton.setEnabled(true);
273
            return;
274
        }
275
//        System.out.println("controller:"+this.hashCode());
276
//        System.out.println("Code:"+theTaskStatus.getCode());
277
//        System.out.println("isAborted:"+theTaskStatus.isAborted());
278
//        System.out.println("isCancelled:"+theTaskStatus.isCancelled());
279
//        System.out.println("isRunning():"+theTaskStatus.isRunning());
280
//        System.out.println("Title:"+theTaskStatus.getTitle());
281
//        System.out.println("Label:"+theTaskStatus.getLabel());
282
//        System.out.println("isIndeterminate:"+theTaskStatus.isIndeterminate());
283
//        System.out.println("Completed:"+theTaskStatus.getCompleted());
284
        if ( theTaskStatus.isAborted()) {
285
            this.titlelabel.setText(theTaskStatus.getTitle());
286
            this.messagelabel.setText(theTaskStatus.getLabel());
287
            if( this.progressBar.isIndeterminate() ) {
288
                this.progressBar.setIndeterminate(false);
289
                this.progressBar.setValue(0);
290
            }
291
            this.cancelRequestButton.setEnabled(false);
292
            this.removeTaskButton.setEnabled(true);
293
            return;
294
        }
295
        if ( theTaskStatus.isCancelled()) {
296
            this.titlelabel.setText(theTaskStatus.getTitle());
297
            this.messagelabel.setText("");
298
            if( this.progressBar.isIndeterminate() ) {
299
                this.progressBar.setIndeterminate(false);
300
                this.progressBar.setValue(100);
301
            }
302
            this.cancelRequestButton.setEnabled(false);
303
            this.removeTaskButton.setEnabled(true);
304
            return;
305
        }
306
        if ( theTaskStatus.isRunning()) {
307
            if (theTaskStatus.getTitle() == null) {
308
                this.titlelabel.setText("Unknown");
309
            } else {
310
                this.titlelabel.setText(theTaskStatus.getTitle());
311
            }
312
            this.messagelabel.setText(theTaskStatus.getLabel());
313
            if( theTaskStatus.isIndeterminate()!=this.progressBar.isIndeterminate() ) {
314
                this.progressBar.setIndeterminate(theTaskStatus.isIndeterminate());
315
            }
316
            this.progressBar.setValue(theTaskStatus.getCompleted());
317
            if (!this.cancelRequestButton.isEnabled()) {
318
                this.cancelRequestButton.setEnabled(true);
319
            }
320
            return;
321
        }
322
        this.titlelabel.setText(theTaskStatus.getTitle());
323
        this.messagelabel.setText("");
324
        if( this.progressBar.isIndeterminate() ) {
325
            this.progressBar.setIndeterminate(false);
326
            this.progressBar.setValue(100);
327
        }
328
        this.cancelRequestButton.setEnabled(false);
329
        this.removeTaskButton.setEnabled(true);
330
    }
331

    
332
    @Override
333
    public TaskStatus getTaskStatus() {
334
        return this.bindedTaskStatus;
335
    }
336

    
337
    @Override
338
    public SimpleTaskStatus getSimpleTaskStatus() {
339
        return (SimpleTaskStatus) this.bindedTaskStatus;
340
    }
341

    
342
    @Override
343
    public void setTitle(String title) {
344
        if (this.bindedTaskStatus instanceof SimpleTaskStatus) {
345
            ((SimpleTaskStatus) this.bindedTaskStatus).setTitle(title);
346
        } else {
347
            this.titlelabel.setText(title);
348
        }
349
    }
350

    
351
    @Override
352
    public void message(String message) {
353
        if (this.bindedTaskStatus instanceof SimpleTaskStatus) {
354
            ((SimpleTaskStatus) this.bindedTaskStatus).message(message);
355
        } else {
356
            this.messagelabel.setText(message);
357
        }
358
    }
359

    
360
    @Override
361
    public void setCurValue(long value) {
362
        if (this.bindedTaskStatus instanceof SimpleTaskStatus) {
363
            ((SimpleTaskStatus) this.bindedTaskStatus).setCurValue(value);
364
        }
365
    }
366

    
367
}