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