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 @ 600
History | View | Annotate | Download (6.43 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.awt.event.ActionEvent; |
8 |
import java.awt.event.ActionListener; |
9 |
import java.net.URL; |
10 |
|
11 |
import javax.swing.ImageIcon; |
12 |
import javax.swing.JButton; |
13 |
import javax.swing.JComponent; |
14 |
import javax.swing.JLabel; |
15 |
import javax.swing.JProgressBar; |
16 |
import javax.swing.SwingUtilities; |
17 |
|
18 |
import org.gvsig.tools.observer.Observable; |
19 |
import org.gvsig.tools.observer.Observer; |
20 |
import org.gvsig.tools.swing.api.task.JTaskStatus; |
21 |
import org.gvsig.tools.task.SimpleTaskStatus; |
22 |
import org.gvsig.tools.task.TaskStatus; |
23 |
|
24 |
public class DefaultJTaskStatus extends JTaskStatus implements Observer { |
25 |
|
26 |
/**
|
27 |
*
|
28 |
*/
|
29 |
private static final long serialVersionUID = -1908456747637477552L; |
30 |
|
31 |
private JLabel titlelabel = null; |
32 |
private JLabel messagelabel = null; |
33 |
private JProgressBar progressBar = null; |
34 |
private JButton cancelRequestButton = null; |
35 |
private JButton removeTaskButton; |
36 |
|
37 |
private boolean showCancelButton; |
38 |
private boolean showRemoveTaskButton; |
39 |
|
40 |
private TaskStatus taskStatus;
|
41 |
|
42 |
|
43 |
public DefaultJTaskStatus() {
|
44 |
this.taskStatus = null; |
45 |
this.showCancelButton = true; |
46 |
this.createComponents();
|
47 |
} |
48 |
|
49 |
public DefaultJTaskStatus(TaskStatus taskStatus) {
|
50 |
this();
|
51 |
this.bind(taskStatus);
|
52 |
} |
53 |
|
54 |
public boolean getShowCancelButton() { |
55 |
return this.showCancelButton; |
56 |
} |
57 |
|
58 |
public void setShowCancelButton(boolean showCancelButton) { |
59 |
this.showCancelButton = showCancelButton;
|
60 |
if( this.cancelRequestButton != null ) { |
61 |
this.cancelRequestButton.setVisible(this.showCancelButton); |
62 |
} |
63 |
} |
64 |
|
65 |
public boolean getShowRemoveTaskButton() { |
66 |
return this.showRemoveTaskButton; |
67 |
} |
68 |
|
69 |
public void setShowRemoveTaskButton(boolean showRemoveTaskButton) { |
70 |
this.showRemoveTaskButton = showRemoveTaskButton;
|
71 |
if( this.removeTaskButton != null ) { |
72 |
this.removeTaskButton.setVisible(this.showRemoveTaskButton); |
73 |
} |
74 |
} |
75 |
|
76 |
public JComponent asJComponent() { |
77 |
return this; |
78 |
} |
79 |
|
80 |
public void bind(TaskStatus taskStatus) { |
81 |
if( this.taskStatus!=null ) { |
82 |
this.taskStatus.deleteObserver(this); |
83 |
} |
84 |
this.taskStatus = taskStatus;
|
85 |
if( this.taskStatus!=null ) { |
86 |
this.taskStatus.addObserver(this); |
87 |
} |
88 |
} |
89 |
|
90 |
private void createComponents() { |
91 |
this.titlelabel = new JLabel(); |
92 |
this.titlelabel.setPreferredSize( new Dimension( 200, 16)); |
93 |
|
94 |
this.progressBar = new JProgressBar(1,100); |
95 |
this.progressBar.setPreferredSize(new Dimension( 200, 10)); |
96 |
this.progressBar.setIndeterminate(false); |
97 |
this.progressBar.setBorderPainted(true); |
98 |
|
99 |
this.cancelRequestButton = new JButton(); |
100 |
this.cancelRequestButton.setPreferredSize( new Dimension(16, 16)); |
101 |
this.cancelRequestButton.setBorderPainted(false); |
102 |
this.cancelRequestButton.setIcon( getIcon("cancelRequestButton.png")); |
103 |
this.cancelRequestButton.setDisabledIcon( getIcon("disabledCancelRequestButton.png") ); |
104 |
if( !this.showCancelButton ) { |
105 |
this.cancelRequestButton.setVisible(this.showCancelButton); |
106 |
} |
107 |
this.cancelRequestButton.addActionListener(new ActionListener() { |
108 |
public void actionPerformed(ActionEvent arg0) { |
109 |
cancelRequestButton.setEnabled(false);
|
110 |
taskStatus.cancelRequest(); |
111 |
} |
112 |
}); |
113 |
this.removeTaskButton = new JButton(); |
114 |
this.removeTaskButton.setPreferredSize( new Dimension(16, 16)); |
115 |
this.removeTaskButton.setBorderPainted(false); |
116 |
this.removeTaskButton.setIcon( getIcon("removeTaskButton.png")); |
117 |
this.removeTaskButton.setDisabledIcon( getIcon("disabledRemoveTaskButton.png") ); |
118 |
this.removeTaskButton.setEnabled(false); |
119 |
this.removeTaskButton.addActionListener(new ActionListener() { |
120 |
public void actionPerformed(ActionEvent arg0) { |
121 |
if( !taskStatus.isRunning() ) {
|
122 |
taskStatus.getManager().remove(taskStatus); |
123 |
} |
124 |
} |
125 |
}); |
126 |
|
127 |
this.messagelabel = new JLabel(); |
128 |
this.messagelabel.setPreferredSize( new Dimension( 200, 16)); |
129 |
|
130 |
this.setLayout(new GridBagLayout()); |
131 |
|
132 |
this.add(this.titlelabel, 0, 0, GridBagConstraints.HORIZONTAL, 0.98, 0); |
133 |
this.add(this.progressBar, 0, 1, GridBagConstraints.HORIZONTAL, 0.98, 0); |
134 |
this.add(this.messagelabel, 0, 2, GridBagConstraints.HORIZONTAL, 0.98, 0); |
135 |
this.add(this.cancelRequestButton, 1, 1, GridBagConstraints.NONE, 0.01, 0); |
136 |
this.add(this.removeTaskButton, 2, 1, GridBagConstraints.NONE, 0.01, 0); |
137 |
} |
138 |
|
139 |
private ImageIcon getIcon(String name) { |
140 |
URL iconurl = this.getClass().getResource(name); |
141 |
if( iconurl == null ) { |
142 |
return new ImageIcon(); |
143 |
} |
144 |
return new ImageIcon(iconurl); |
145 |
} |
146 |
|
147 |
private void add(Component comp, int gridx, int gridy, int fill, double weightx, double weighty) { |
148 |
GridBagConstraints c = new GridBagConstraints(); |
149 |
c.fill = fill; |
150 |
c.gridx = gridx; |
151 |
c.gridy = gridy; |
152 |
c.weightx = weightx; |
153 |
c.weighty = weighty; |
154 |
this.add(comp, c);
|
155 |
} |
156 |
|
157 |
|
158 |
public void update(final Observable observable, final Object notification) { |
159 |
|
160 |
if(observable != null && !(observable instanceof TaskStatus) ) { |
161 |
return;
|
162 |
} |
163 |
if( !SwingUtilities.isEventDispatchThread()) { |
164 |
SwingUtilities.invokeLater( new Runnable() { |
165 |
public void run() { |
166 |
update(observable, notification); |
167 |
} |
168 |
}); |
169 |
return;
|
170 |
} |
171 |
TaskStatus taskStatus = (TaskStatus) observable; |
172 |
if( taskStatus == null || !taskStatus.isRunning() ) { |
173 |
this.messagelabel.setText(""); |
174 |
this.progressBar.setIndeterminate(false); |
175 |
this.progressBar.setValue(100); |
176 |
this.cancelRequestButton.setEnabled(false); |
177 |
this.removeTaskButton.setEnabled(true); |
178 |
return;
|
179 |
} |
180 |
if (taskStatus.getTitle() == null) { |
181 |
this.titlelabel.setText("Progress"); |
182 |
} else {
|
183 |
this.titlelabel.setText(taskStatus.getTitle());
|
184 |
} |
185 |
this.messagelabel.setText(taskStatus.getLabel());
|
186 |
this.progressBar.setIndeterminate( taskStatus.isIndeterminate() );
|
187 |
this.progressBar.setValue( taskStatus.getCompleted() );
|
188 |
if( !this.cancelRequestButton.isEnabled() ) { |
189 |
this.cancelRequestButton.setEnabled(true); |
190 |
} |
191 |
|
192 |
} |
193 |
|
194 |
public TaskStatus getTaskStatus() {
|
195 |
return this.taskStatus; |
196 |
} |
197 |
|
198 |
public void setTittle(String tittle) { |
199 |
setTitle(tittle); |
200 |
} |
201 |
|
202 |
@Override
|
203 |
public void setTitle(String title) { |
204 |
if (this.taskStatus instanceof SimpleTaskStatus) { |
205 |
((SimpleTaskStatus) this.taskStatus).setTitle(title);
|
206 |
} |
207 |
} |
208 |
|
209 |
public void message(String message) { |
210 |
if( this.taskStatus instanceof SimpleTaskStatus ) { |
211 |
((SimpleTaskStatus)this.taskStatus).message(message);
|
212 |
} |
213 |
} |
214 |
|
215 |
public void setCurValue(long value) { |
216 |
if( this.taskStatus instanceof SimpleTaskStatus ) { |
217 |
((SimpleTaskStatus)this.taskStatus).setCurValue(value);
|
218 |
} |
219 |
} |
220 |
|
221 |
|
222 |
} |