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

History | View | Annotate | Download (8.13 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 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.awt.event.ActionListener;
29
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
import org.gvsig.tools.task.SimpleTaskStatus;
40
import org.gvsig.tools.task.TaskStatus;
41

    
42
/**
43
 * Default implementation of the {@link TaskStatusController}.
44
 *
45
 * @author gvSIG Team
46
 * @version $Id$
47
 */
48
public class DefaultJTaskStatusController implements TaskStatusController, Observer {
49

    
50
    private JLabel titlelabel = null;
51
    private JLabel messagelabel = null;
52
    private JProgressBar progressBar = null;
53
    private JButton cancelRequestButton = null;
54
    private JButton removeTaskButton;
55

    
56
    private boolean showCancelButton;
57
    private boolean showRemoveTaskButton;
58

    
59
    private TaskStatus taskStatus;
60

    
61
    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
        this.taskStatus = null;
76
        this.showCancelButton = true;
77

    
78
        this.bind(taskStatus);
79

    
80
        this.initComponents();
81
    }
82

    
83
    public DefaultJTaskStatusController(
84
            TaskStatus taskStatus,
85
            JLabel titlelabel,
86
            JLabel messagelabel,
87
            JProgressBar progressBar
88
    ) {
89
        this(taskStatus, titlelabel, messagelabel, progressBar, null, null);
90
    }
91

    
92
    public DefaultJTaskStatusController(
93
            JLabel titlelabel,
94
            JLabel messagelabel,
95
            JProgressBar progressBar
96
    ) {
97
        this(null, titlelabel, messagelabel, progressBar, null, null);
98
    }
99

    
100
    private void initComponents() {
101
        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
        if( this.cancelRequestButton==null ) {
111
            this.cancelRequestButton = new JButton();
112
        }
113
        if( this.removeTaskButton==null ) {
114
            this.removeTaskButton = new JButton();
115
        }
116
        this.progressBar.setIndeterminate(false);
117
        this.progressBar.setBorderPainted(true);
118

    
119
        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
                    if ((arg0.getModifiers() & ActionEvent.CTRL_MASK) == ActionEvent.CTRL_MASK) {
137
                        taskStatus.getManager().remove(taskStatus);
138
                    }
139
                } else {
140
                    taskStatus.getManager().remove(taskStatus);
141
                }
142
            }
143
        });
144
    }
145

    
146
    public boolean getShowCancelButton() {
147
        return this.showCancelButton;
148
    }
149

    
150
    public void setShowCancelButton(boolean showCancelButton) {
151
        this.showCancelButton = showCancelButton;
152
        if (this.cancelRequestButton != null) {
153
            this.cancelRequestButton.setVisible(this.showCancelButton);
154
        }
155
    }
156

    
157
    public boolean getShowRemoveTaskButton() {
158
        return this.showRemoveTaskButton;
159
    }
160

    
161
    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
    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
        TaskStatus theTaskStatus = (TaskStatus) observable;
199
        if (theTaskStatus == null || !theTaskStatus.isRunning()) {
200
            this.messagelabel.setText(theTaskStatus==null?"":theTaskStatus.getLabel());
201

    
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
        if (theTaskStatus.getTitle() == null) {
210
            this.titlelabel.setText("Unknown");
211
        } else {
212
            this.titlelabel.setText(theTaskStatus.getTitle());
213
        }
214
        this.messagelabel.setText(theTaskStatus.getLabel());
215
        this.progressBar.setIndeterminate(theTaskStatus.isIndeterminate());
216
        this.progressBar.setValue(theTaskStatus.getCompleted());
217

    
218
        if (!this.cancelRequestButton.isEnabled()) {
219
            this.cancelRequestButton.setEnabled(true);
220
        }
221

    
222
    }
223

    
224
    public TaskStatus getTaskStatus() {
225
        return this.taskStatus;
226
    }
227

    
228
    @Override
229
    public void setTitle(String title) {
230
        if (this.taskStatus instanceof SimpleTaskStatus) {
231
            ((SimpleTaskStatus) this.taskStatus).setTitle(title);
232
        }
233
    }
234

    
235
    public void message(String message) {
236
        if (this.taskStatus instanceof SimpleTaskStatus) {
237
            ((SimpleTaskStatus) this.taskStatus).message(message);
238
        }
239
    }
240

    
241
    public void setCurValue(long value) {
242
        if (this.taskStatus instanceof SimpleTaskStatus) {
243
            ((SimpleTaskStatus) this.taskStatus).setCurValue(value);
244
        }
245
    }
246

    
247
}