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

History | View | Annotate | Download (4.17 KB)

1
package org.gvsig.tools.swing.impl.task;
2

    
3
import java.awt.Component;
4
import java.awt.Dimension;
5
import java.awt.GridBagConstraints;
6
import java.awt.GridBagLayout;
7
import java.net.URL;
8

    
9
import javax.swing.ImageIcon;
10
import javax.swing.JButton;
11
import javax.swing.JComponent;
12
import javax.swing.JLabel;
13
import javax.swing.JProgressBar;
14
import javax.swing.SwingUtilities;
15

    
16
import org.gvsig.tools.observer.Observable;
17
import org.gvsig.tools.observer.Observer;
18
import org.gvsig.tools.swing.api.task.JTaskStatus;
19
import org.gvsig.tools.task.TaskStatus;
20

    
21
public class DefaultJTaskStatus extends JTaskStatus implements Observer {
22

    
23
        /**
24
         * 
25
         */
26
        private static final long serialVersionUID = -1908456747637477552L;
27

    
28
        private JLabel tittlelabel = null;
29
        private JLabel messagelabel = null;
30
        private JProgressBar progressBar = null;
31
        private JButton cancelRequestButton = null;
32
        private boolean showCancelButton;
33

    
34
        private TaskStatus taskStatus; 
35
        
36
        public DefaultJTaskStatus() {
37
                this.taskStatus = null;
38
                this.showCancelButton = true;
39
                this.createComponents();
40
        }
41
        
42
        public DefaultJTaskStatus(TaskStatus taskStatus) {
43
                this();
44
                this.bind(taskStatus);
45
        }
46
        
47
        public boolean getShowCancelButton() {
48
                return this.showCancelButton;
49
        }
50
        
51
        public void setShowCancelButton(boolean showCancelButton) {
52
                this.showCancelButton = showCancelButton;
53
                if( this.cancelRequestButton != null ) {
54
                    this.cancelRequestButton.setVisible(this.showCancelButton);
55
            }
56
        }
57
        
58
        public JComponent asJComponent() {
59
                return this;
60
        }
61

    
62
        public void bind(TaskStatus taskStatus) {
63
                if( this.taskStatus!=null ) {
64
                        this.taskStatus.deleteObserver(this);
65
                }
66
                this.taskStatus = taskStatus;
67
                if( this.taskStatus!=null ) {
68
                        this.taskStatus.addObserver(this);
69
                }
70
        }
71

    
72
        private void createComponents() {
73
                this.tittlelabel = new JLabel();
74
                this.tittlelabel.setPreferredSize( new Dimension( 200, 16));
75
                
76
                this.progressBar = new JProgressBar(1,100);
77
                this.progressBar.setPreferredSize(new Dimension( 200, 10));
78
                this.progressBar.setIndeterminate(true);
79
                this.progressBar.setBorderPainted(true);
80
                
81
                this.cancelRequestButton = new JButton();
82
                this.cancelRequestButton.setPreferredSize( new Dimension(16, 16));
83
                this.cancelRequestButton.setBorderPainted(false);
84
            this.cancelRequestButton.setIcon( getIcon("cancelRequestButton.png"));
85
            if( !this.showCancelButton ) {
86
                    this.cancelRequestButton.setVisible(this.showCancelButton);
87
            }
88
                
89
                this.messagelabel = new JLabel();
90
                this.messagelabel.setPreferredSize( new Dimension( 200, 16));
91

    
92
                this.setLayout(new GridBagLayout());
93
                
94
                this.add(this.tittlelabel, 0, 0, GridBagConstraints.HORIZONTAL, 0.9, 0);
95
                this.add(this.progressBar, 0, 1, GridBagConstraints.HORIZONTAL, 0.9, 0);
96
                this.add(this.messagelabel, 0, 2, GridBagConstraints.HORIZONTAL, 0.9, 0);
97
                this.add(this.cancelRequestButton, 1, 1, GridBagConstraints.NONE, 0.1, 0);
98
        }
99

    
100
        private ImageIcon getIcon(String name) {
101
                URL iconurl = this.getClass().getResource(name);
102
                if( iconurl == null ) {
103
                        return new ImageIcon();
104
                }
105
                return new ImageIcon(iconurl);
106
        }
107
        
108
        private void add(Component comp, int gridx, int gridy, int fill, double weightx, double weighty) {
109
                GridBagConstraints c = new GridBagConstraints();
110
                c.fill = fill;
111
                c.gridx = gridx;
112
                c.gridy = gridy;
113
                c.weightx = weightx;
114
                c.weighty = weighty;
115
                this.add(comp, c);
116
        }
117

    
118
        
119
        public void update(final Observable observable, final Object notification) {
120

    
121
                if(observable != null && !(observable instanceof TaskStatus) ) {
122
                        return;
123
                }
124
                if( !SwingUtilities.isEventDispatchThread()) {
125
                        SwingUtilities.invokeLater( new Runnable() {
126
                                public void run() {
127
                                        update(observable, notification);
128
                                }
129
                        });
130
                        return;
131
                }
132
                if( observable == null ) {
133
                        this.tittlelabel.setText("");
134
                        this.messagelabel.setText("");
135
                        this.progressBar.setValue(100);
136
                        this.cancelRequestButton.setEnabled(false);
137
                        return;
138
                }
139
                TaskStatus taskStatus = (TaskStatus) observable;
140
                this.tittlelabel.setText(taskStatus.getTitle());
141
                this.messagelabel.setText(taskStatus.getLabel());
142
                this.progressBar.setIndeterminate( taskStatus.isIndeterminate() );
143
                this.progressBar.setValue( taskStatus.getCompleted() );
144
                if( !this.cancelRequestButton.isEnabled() ) {
145
                        this.cancelRequestButton.setEnabled(true);
146
                }
147
        }
148

    
149
        
150
}