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

History | View | Annotate | Download (6.58 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 java.awt.Dimension;
27
import java.awt.GridBagConstraints;
28
import java.awt.GridBagLayout;
29
import java.awt.LayoutManager;
30
import java.awt.event.ActionEvent;
31
import java.awt.event.ActionListener;
32
import java.net.URL;
33

    
34
import javax.swing.ImageIcon;
35
import javax.swing.JButton;
36
import javax.swing.JComponent;
37
import javax.swing.JLabel;
38
import javax.swing.JProgressBar;
39
import javax.swing.SwingUtilities;
40
import org.apache.commons.lang3.StringUtils;
41

    
42
import org.gvsig.tools.ToolsLocator;
43
import org.gvsig.tools.observer.Observable;
44
import org.gvsig.tools.observer.Observer;
45
import org.gvsig.tools.swing.api.ToolsSwingLocator;
46
import org.gvsig.tools.swing.api.task.JTasksStatus;
47
import org.gvsig.tools.swing.api.task.JTasksStatusList;
48
import org.gvsig.tools.swing.api.windowmanager.WindowManager;
49
import org.gvsig.tools.task.TaskStatus;
50
import org.gvsig.tools.task.TaskStatusManager;
51

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

    
60
        private static final long serialVersionUID = 245436792193991920L;
61

    
62
        private TaskStatusManager manager = null;
63
        private JLabel label = null;
64
        private JProgressBar progressBar = null;
65
        private JButton showAllTaskStatusButton = null;
66
        private int maxLabelSize = 25;
67
        
68
        public DefaultJTasksStatus() {
69
                this(ToolsLocator.getTaskStatusManager());
70
        }
71
        
72
        public DefaultJTasksStatus(TaskStatusManager manager) {
73
                this.bind(manager);
74
                this.createComponents();
75
        }
76
        
77
        public void setMaxLabelSize(int maxLabelSize) {
78
            this.maxLabelSize = maxLabelSize;
79
        }
80
        
81
        public int getMaxLabelSize() {
82
            return this.maxLabelSize;
83
        }
84
        
85
        public JComponent asJComponent() {
86
                return this;
87
        }
88
        
89
        public void bind(TaskStatusManager manager) {
90
                if( this.manager!=null ) {
91
                        this.manager.deleteObserver(this);
92
                }
93
                this.manager = manager;
94
                if( this.manager!= null ) {
95
                        this.manager.addObserver(this);
96
                }
97
        }
98

    
99
        public Dimension getPreferredSize() {
100
                Dimension d = super.getPreferredSize();
101
                return new Dimension( 270, d.height);
102
        }
103
        
104
        private void createComponents() {
105
                this.label = new JLabel();
106
                
107
                this.progressBar = new JProgressBar(1,100);
108
        this.progressBar.setPreferredSize(new Dimension(40, 10));
109
                this.progressBar.setIndeterminate(true);
110
                this.progressBar.setBorderPainted(true);
111
                this.progressBar.setVisible(false);
112
                
113
                this.showAllTaskStatusButton = new JButton();
114
                this.showAllTaskStatusButton.setPreferredSize( new Dimension(16, 16));
115
                this.showAllTaskStatusButton.setBorderPainted(false);
116
                this.showAllTaskStatusButton.setIcon( this.getIcon("showAllTaskStatusButton.png"));
117
                this.showAllTaskStatusButton.setVisible(false);
118
                this.showAllTaskStatusButton.addActionListener(new ActionListener() {
119
                        public void actionPerformed(ActionEvent arg0) {
120
                                JTasksStatusList dialog = ToolsSwingLocator.getTaskStatusSwingManager().createJTasksStatusList(manager);
121
                                WindowManager wm = ToolsSwingLocator.getWindowManager();
122
                                wm.showWindow(dialog, "Tasks",WindowManager.MODE.TOOL);
123
                        }
124
                });
125
                
126
        LayoutManager layout = new GridBagLayout();
127
                this.setLayout(layout);
128
        GridBagConstraints c = new GridBagConstraints();
129

    
130
        c.gridx = 0;
131
        c.fill = GridBagConstraints.VERTICAL;
132
        c.weightx = 0.0d;
133
        c.ipadx = 1;
134
        this.add(this.label, c);
135

    
136
        c.gridx = 1;
137
        c.fill = GridBagConstraints.BOTH;
138
        c.weightx = 1.0d;
139
        this.add(this.progressBar, c);
140
                
141
        c.gridx = 2;
142
        c.fill = GridBagConstraints.VERTICAL;
143
        c.weightx = 0.0d;
144
        this.add(this.showAllTaskStatusButton);
145
        }
146
        
147
        private ImageIcon getIcon(String name) {
148
                URL iconurl = this.getClass().getResource(name);
149
                if( iconurl == null ) {
150
                        return new ImageIcon();
151
                }
152
                return new ImageIcon(iconurl);
153
        }
154
        public void update(final Observable observable, final Object notification) {
155

    
156
                if(notification != null && !(notification instanceof TaskStatus) ) {
157
                        return;
158
                }
159
                if( !SwingUtilities.isEventDispatchThread()) {
160
                        SwingUtilities.invokeLater( new Runnable() {
161
                                public void run() {
162
                                        update(observable, notification);
163
                                }
164
                        });
165
                        return;
166
                }
167
                TaskStatus taskStatus = (TaskStatus) notification;
168
                if( taskStatus == null ) {
169
                        taskStatus = this.manager.getRunningTaskStatusMostRecent();
170
                        if( taskStatus == null ) {
171
                                this.label.setText("");
172
                                this.label.setToolTipText("");
173
                                this.progressBar.setVisible(false);
174
                                this.showAllTaskStatusButton.setVisible(false);
175
                                return;
176
                        }
177
                }
178
                if( !taskStatus.isRunning() ) {
179
                        TaskStatus taskStatus2 = this.manager.getRunningTaskStatusMostRecent();
180
                        if( taskStatus2 == null ) {
181
                                // Not running task
182
                                this.label.setText("");
183
                                this.label.setToolTipText("");
184
                                this.progressBar.setVisible(false);
185
                                this.showAllTaskStatusButton.setVisible(false);
186
                                return;
187
                        } else {
188
                                taskStatus = taskStatus2;
189
                        }
190
                 }
191
                 String label = taskStatus.getLabel();
192
                 String title = taskStatus.getTitle();                  
193
         
194
                 if (title == null) {
195
                         title = "Progress";
196
                 }
197
                if( label == null || label.trim().equals("") ) {
198
                        label = title;
199
                } else {
200
                        label = title + ": " + label;
201
                }
202
                
203
                this.label.setText( StringUtils.abbreviateMiddle(label, "...", this.maxLabelSize) );
204
                this.label.setToolTipText(label );
205
                this.progressBar.setIndeterminate( taskStatus.isIndeterminate() );
206
                this.progressBar.setValue( taskStatus.getCompleted() );
207
                if( !this.progressBar.isVisible() ) {
208
                        this.progressBar.setVisible(true);
209
                }
210
                if( !this.showAllTaskStatusButton.isVisible() ) {
211
                        this.showAllTaskStatusButton.setVisible(true);
212
                }
213
                
214
        }
215

    
216
}