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 / DefaultJTasksStatus.java @ 674

History | View | Annotate | Download (6.03 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.Dimension;
25
import java.awt.GridBagConstraints;
26
import java.awt.GridBagLayout;
27
import java.awt.LayoutManager;
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.ToolsLocator;
40
import org.gvsig.tools.observer.Observable;
41
import org.gvsig.tools.observer.Observer;
42
import org.gvsig.tools.swing.api.ToolsSwingLocator;
43
import org.gvsig.tools.swing.api.task.JTasksStatus;
44
import org.gvsig.tools.swing.api.task.JTasksStatusList;
45
import org.gvsig.tools.swing.api.windowmanager.WindowManager;
46
import org.gvsig.tools.task.TaskStatus;
47
import org.gvsig.tools.task.TaskStatusManager;
48

    
49
/**
50
 * Default implementation of the {@link JTasksStatus}.
51
 * 
52
 * @author gvSIG Team
53
 * @version $Id$
54
 */
55
public class DefaultJTasksStatus extends JTasksStatus implements Observer {
56

    
57
        private static final long serialVersionUID = 245436792193991920L;
58

    
59
        private TaskStatusManager manager = null;
60
        private JLabel label = null;
61
        private JProgressBar progressBar = null;
62
        private JButton showAllTaskStatusButton = null;
63
        
64
        public DefaultJTasksStatus() {
65
                this(ToolsLocator.getTaskStatusManager());
66
        }
67
        
68
        public DefaultJTasksStatus(TaskStatusManager manager) {
69
                this.bind(manager);
70
                this.createComponents();
71
        }
72
        
73
        public JComponent asJComponent() {
74
                return this;
75
        }
76
        
77
        public void bind(TaskStatusManager manager) {
78
                if( this.manager!=null ) {
79
                        this.manager.deleteObserver(this);
80
                }
81
                this.manager = manager;
82
                if( this.manager!= null ) {
83
                        this.manager.addObserver(this);
84
                }
85
        }
86

    
87
        public Dimension getPreferredSize() {
88
                Dimension d = super.getPreferredSize();
89
                return new Dimension( 270, d.height);
90
        }
91
        
92
        private void createComponents() {
93
                this.label = new JLabel();
94
                
95
                this.progressBar = new JProgressBar(1,100);
96
        this.progressBar.setPreferredSize(new Dimension(40, 10));
97
                this.progressBar.setIndeterminate(true);
98
                this.progressBar.setBorderPainted(true);
99
                this.progressBar.setVisible(false);
100
                
101
                this.showAllTaskStatusButton = new JButton();
102
                this.showAllTaskStatusButton.setPreferredSize( new Dimension(16, 16));
103
                this.showAllTaskStatusButton.setBorderPainted(false);
104
                this.showAllTaskStatusButton.setIcon( this.getIcon("showAllTaskStatusButton.png"));
105
                this.showAllTaskStatusButton.setVisible(false);
106
                this.showAllTaskStatusButton.addActionListener(new ActionListener() {
107
                        public void actionPerformed(ActionEvent arg0) {
108
                                JTasksStatusList dialog = ToolsSwingLocator.getTaskStatusSwingManager().createJTasksStatusList(manager);
109
                                WindowManager wm = ToolsSwingLocator.getWindowManager();
110
                                wm.showWindow(dialog, "Tasks",WindowManager.MODE.TOOL);
111
                        }
112
                });
113
                
114
        LayoutManager layout = new GridBagLayout();
115
                this.setLayout(layout);
116
        GridBagConstraints c = new GridBagConstraints();
117

    
118
        c.gridx = 0;
119
        c.fill = GridBagConstraints.VERTICAL;
120
        c.weightx = 0.0d;
121
        c.ipadx = 1;
122
        this.add(this.label, c);
123

    
124
        c.gridx = 1;
125
        c.fill = GridBagConstraints.BOTH;
126
        c.weightx = 1.0d;
127
        this.add(this.progressBar, c);
128
                
129
        c.gridx = 2;
130
        c.fill = GridBagConstraints.VERTICAL;
131
        c.weightx = 0.0d;
132
        this.add(this.showAllTaskStatusButton);
133
        }
134
        
135
        private ImageIcon getIcon(String name) {
136
                URL iconurl = this.getClass().getResource(name);
137
                if( iconurl == null ) {
138
                        return new ImageIcon();
139
                }
140
                return new ImageIcon(iconurl);
141
        }
142
        public void update(final Observable observable, final Object notification) {
143

    
144
                if(notification != null && !(notification instanceof TaskStatus) ) {
145
                        return;
146
                }
147
                if( !SwingUtilities.isEventDispatchThread()) {
148
                        SwingUtilities.invokeLater( new Runnable() {
149
                                public void run() {
150
                                        update(observable, notification);
151
                                }
152
                        });
153
                        return;
154
                }
155
                TaskStatus taskStatus = (TaskStatus) notification;
156
                if( taskStatus == null ) {
157
                        taskStatus = this.manager.getRunningTaskStatusMostRecent();
158
                        if( taskStatus == null ) {
159
                                this.label.setText("");
160
                                this.progressBar.setVisible(false);
161
                                this.showAllTaskStatusButton.setVisible(false);
162
                                return;
163
                        }
164
                }
165
                if( !taskStatus.isRunning() ) {
166
                        TaskStatus taskStatus2 = this.manager.getRunningTaskStatusMostRecent();
167
                        if( taskStatus2 == null ) {
168
                                // Not running task
169
                                this.label.setText("");
170
                                this.progressBar.setVisible(false);
171
                                this.showAllTaskStatusButton.setVisible(false);
172
                                return;
173
                        } else {
174
                                taskStatus = taskStatus2;
175
                        }
176
                 }
177
                 String label = taskStatus.getLabel();
178
                 String title = taskStatus.getTitle();                  
179
         
180
                 if (title == null) {
181
                         title = "Progress";
182
                 }
183
                if( label == null || label.trim().equals("") ) {
184
                        label = title;
185
                } else {
186
                        label = title + ": " + label;
187
                }
188
                
189
                this.label.setText( label );
190
                this.progressBar.setIndeterminate( taskStatus.isIndeterminate() );
191
                this.progressBar.setValue( taskStatus.getCompleted() );
192
                if( !this.progressBar.isVisible() ) {
193
                        this.progressBar.setVisible(true);
194
                }
195
                if( !this.showAllTaskStatusButton.isVisible() ) {
196
                        this.showAllTaskStatusButton.setVisible(true);
197
                }
198
                
199
        }
200

    
201
}