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 / DefaultJTaskStatus.java @ 673

History | View | Annotate | Download (7.41 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
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
 */
22
package org.gvsig.tools.swing.impl.task;
23

    
24
import java.awt.Component;
25
import java.awt.Dimension;
26
import java.awt.GridBagConstraints;
27
import java.awt.GridBagLayout;
28
import java.awt.event.ActionEvent;
29
import java.awt.event.ActionListener;
30
import java.net.URL;
31

    
32
import javax.swing.ImageIcon;
33
import javax.swing.JButton;
34
import javax.swing.JComponent;
35
import javax.swing.JLabel;
36
import javax.swing.JProgressBar;
37
import javax.swing.SwingUtilities;
38

    
39
import org.gvsig.tools.observer.Observable;
40
import org.gvsig.tools.observer.Observer;
41
import org.gvsig.tools.swing.api.task.JTaskStatus;
42
import org.gvsig.tools.task.SimpleTaskStatus;
43
import org.gvsig.tools.task.TaskStatus;
44

    
45
/**
46
 * Default implementation of the {@link JTaskStatus}.
47
 * 
48
 * @author gvSIG Team
49
 * @version $Id$
50
 */
51
public class DefaultJTaskStatus extends JTaskStatus implements Observer {
52

    
53
        private static final long serialVersionUID = -1908456747637477552L;
54

    
55
        private JLabel titlelabel = null;
56
        private JLabel messagelabel = null;
57
        private JProgressBar progressBar = null;
58
        private JButton cancelRequestButton = null;
59
        private JButton removeTaskButton; 
60

    
61
        private boolean showCancelButton;
62
        private boolean showRemoveTaskButton;
63

    
64
        private TaskStatus taskStatus;
65

    
66
        public DefaultJTaskStatus() {
67
                this.taskStatus = null;
68
                this.showCancelButton = true;
69
                this.createComponents();
70
        }
71
        
72
        public DefaultJTaskStatus(TaskStatus taskStatus) {
73
                this();
74
                this.bind(taskStatus);
75
        }
76
        
77
        public boolean getShowCancelButton() {
78
                return this.showCancelButton;
79
        }
80
        
81
        public void setShowCancelButton(boolean showCancelButton) {
82
                this.showCancelButton = showCancelButton;
83
                if( this.cancelRequestButton != null ) {
84
                    this.cancelRequestButton.setVisible(this.showCancelButton);
85
            }
86
        }
87
        
88
        public boolean getShowRemoveTaskButton() {
89
                return this.showRemoveTaskButton;
90
        }
91
        
92
        public void setShowRemoveTaskButton(boolean showRemoveTaskButton) {
93
                this.showRemoveTaskButton = showRemoveTaskButton;
94
                if( this.removeTaskButton != null ) {
95
                    this.removeTaskButton.setVisible(this.showRemoveTaskButton);
96
            }
97
        }
98
        
99
        public JComponent asJComponent() {
100
                return this;
101
        }
102

    
103
        public void bind(TaskStatus taskStatus) {
104
                if( this.taskStatus!=null ) {
105
                        this.taskStatus.deleteObserver(this);
106
                }
107
                this.taskStatus = taskStatus;
108
                if( this.taskStatus!=null ) {
109
                        this.taskStatus.addObserver(this);
110
                }
111
        }
112

    
113
        private void createComponents() {
114
                this.titlelabel = new JLabel();
115
                this.titlelabel.setPreferredSize( new Dimension( 200, 16));
116
                
117
                this.progressBar = new JProgressBar(1,100);
118
                this.progressBar.setPreferredSize(new Dimension( 200, 10));
119
                this.progressBar.setIndeterminate(false);
120
                this.progressBar.setBorderPainted(true);
121
                
122
                this.cancelRequestButton = new JButton();
123
                this.cancelRequestButton.setPreferredSize( new Dimension(16, 16));
124
                this.cancelRequestButton.setBorderPainted(false);
125
            this.cancelRequestButton.setIcon( getIcon("cancelRequestButton.png"));
126
            this.cancelRequestButton.setDisabledIcon( getIcon("disabledCancelRequestButton.png") );
127
            if( !this.showCancelButton ) {
128
                    this.cancelRequestButton.setVisible(this.showCancelButton);
129
            }
130
            this.cancelRequestButton.addActionListener(new ActionListener() {
131
                        public void actionPerformed(ActionEvent arg0) {
132
                                cancelRequestButton.setEnabled(false);
133
                                taskStatus.cancelRequest();
134
                        }
135
                });
136
                this.removeTaskButton = new JButton();
137
                this.removeTaskButton.setPreferredSize( new Dimension(16, 16));
138
                this.removeTaskButton.setBorderPainted(false);
139
            this.removeTaskButton.setIcon( getIcon("removeTaskButton.png"));
140
            this.removeTaskButton.setDisabledIcon( getIcon("disabledRemoveTaskButton.png") );
141
            this.removeTaskButton.setEnabled(false);
142
            this.removeTaskButton.addActionListener(new ActionListener() {
143
                        public void actionPerformed(ActionEvent arg0) {
144
                                if( !taskStatus.isRunning() ) {
145
                                        taskStatus.getManager().remove(taskStatus);
146
                                }
147
                        }
148
                });
149
            
150
                this.messagelabel = new JLabel();
151
                this.messagelabel.setPreferredSize( new Dimension( 200, 16));
152

    
153
                this.setLayout(new GridBagLayout());
154
                
155
                this.add(this.titlelabel, 0, 0, GridBagConstraints.HORIZONTAL, 0.98, 0);
156
                this.add(this.progressBar, 0, 1, GridBagConstraints.HORIZONTAL, 0.98, 0);
157
                this.add(this.messagelabel, 0, 2, GridBagConstraints.HORIZONTAL, 0.98, 0);
158
                this.add(this.cancelRequestButton, 1, 1, GridBagConstraints.NONE, 0.01, 0);
159
                this.add(this.removeTaskButton, 2, 1, GridBagConstraints.NONE, 0.01, 0);
160
        }
161

    
162
        private ImageIcon getIcon(String name) {
163
                URL iconurl = this.getClass().getResource(name);
164
                if( iconurl == null ) {
165
                        return new ImageIcon();
166
                }
167
                return new ImageIcon(iconurl);
168
        }
169
        
170
        private void add(Component comp, int gridx, int gridy, int fill, double weightx, double weighty) {
171
                GridBagConstraints c = new GridBagConstraints();
172
                c.fill = fill;
173
                c.gridx = gridx;
174
                c.gridy = gridy;
175
                c.weightx = weightx;
176
                c.weighty = weighty;
177
                this.add(comp, c);
178
        }
179

    
180
        
181
        public void update(final Observable observable, final Object notification) {
182

    
183
                if(observable != null && !(observable instanceof TaskStatus) ) {
184
                        return;
185
                }
186
                if( !SwingUtilities.isEventDispatchThread()) {
187
                        SwingUtilities.invokeLater( new Runnable() {
188
                                public void run() {
189
                                        update(observable, notification);
190
                                }
191
                        });
192
                        return;
193
                }
194
                TaskStatus taskStatus = (TaskStatus) observable;
195
                if( taskStatus == null || !taskStatus.isRunning() ) {
196
                        this.messagelabel.setText("");
197
                        this.progressBar.setIndeterminate(false);
198
                        this.progressBar.setValue(100);
199
                        this.cancelRequestButton.setEnabled(false);
200
                        this.removeTaskButton.setEnabled(true);
201
                        return;
202
                }
203
                if (taskStatus.getTitle() == null) {
204
                        this.titlelabel.setText("Progress");
205
                } else {
206
                        this.titlelabel.setText(taskStatus.getTitle());
207
                }
208
                this.messagelabel.setText(taskStatus.getLabel());
209
                this.progressBar.setIndeterminate( taskStatus.isIndeterminate() );
210
                this.progressBar.setValue( taskStatus.getCompleted() );
211
                if( !this.cancelRequestButton.isEnabled() ) {
212
                        this.cancelRequestButton.setEnabled(true);
213
                }
214
                
215
        }
216

    
217
        public TaskStatus getTaskStatus() {
218
                return this.taskStatus;
219
        }
220

    
221
        public void setTittle(String tittle) {
222
        setTitle(tittle);
223
    }
224

    
225
    @Override
226
    public void setTitle(String title) {
227
        if (this.taskStatus instanceof SimpleTaskStatus) {
228
            ((SimpleTaskStatus) this.taskStatus).setTitle(title);
229
        }
230
        }
231

    
232
        public void message(String message) {
233
                if( this.taskStatus instanceof SimpleTaskStatus ) {
234
                        ((SimpleTaskStatus)this.taskStatus).message(message);
235
                }
236
        }
237

    
238
        public void setCurValue(long value) {
239
                if( this.taskStatus instanceof SimpleTaskStatus ) {
240
                        ((SimpleTaskStatus)this.taskStatus).setCurValue(value);
241
                }
242
        }
243

    
244
        
245
}