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