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 @ 462
History | View | Annotate | Download (4.61 KB)
1 |
package org.gvsig.tools.swing.impl.task; |
---|---|
2 |
|
3 |
import java.awt.Dimension; |
4 |
import java.awt.FlowLayout; |
5 |
import java.awt.LayoutManager; |
6 |
import java.awt.event.ActionEvent; |
7 |
import java.awt.event.ActionListener; |
8 |
import java.net.URL; |
9 |
|
10 |
import javax.swing.ImageIcon; |
11 |
import javax.swing.JButton; |
12 |
import javax.swing.JComponent; |
13 |
import javax.swing.JLabel; |
14 |
import javax.swing.JProgressBar; |
15 |
import javax.swing.SwingUtilities; |
16 |
|
17 |
import org.gvsig.tools.ToolsLocator; |
18 |
import org.gvsig.tools.observer.Observable; |
19 |
import org.gvsig.tools.observer.Observer; |
20 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
21 |
import org.gvsig.tools.swing.api.task.JTasksStatus; |
22 |
import org.gvsig.tools.swing.api.task.JTasksStatusList; |
23 |
import org.gvsig.tools.swing.api.windowmanager.WindowManager; |
24 |
import org.gvsig.tools.task.TaskStatus; |
25 |
import org.gvsig.tools.task.TaskStatusManager; |
26 |
|
27 |
public class DefaultJTasksStatus extends JTasksStatus implements Observer { |
28 |
|
29 |
/**
|
30 |
*
|
31 |
*/
|
32 |
private static final long serialVersionUID = 245436792193991920L; |
33 |
|
34 |
private TaskStatusManager manager = null; |
35 |
private JLabel label = null; |
36 |
private JProgressBar progressBar = null; |
37 |
private JButton showAllTaskStatusButton = null; |
38 |
|
39 |
public DefaultJTasksStatus() {
|
40 |
this(ToolsLocator.getTaskStatusManager());
|
41 |
} |
42 |
|
43 |
public DefaultJTasksStatus(TaskStatusManager manager) {
|
44 |
this.bind(manager);
|
45 |
this.createComponents();
|
46 |
} |
47 |
|
48 |
public JComponent asJComponent() { |
49 |
return this; |
50 |
} |
51 |
|
52 |
public void bind(TaskStatusManager manager) { |
53 |
if( this.manager!=null ) { |
54 |
this.manager.deleteObserver(this); |
55 |
} |
56 |
this.manager = manager;
|
57 |
if( this.manager!= null ) { |
58 |
this.manager.addObserver(this); |
59 |
} |
60 |
} |
61 |
|
62 |
public Dimension getPreferredSize() { |
63 |
Dimension d = super.getPreferredSize(); |
64 |
return new Dimension( 270, d.height); |
65 |
} |
66 |
|
67 |
private void createComponents() { |
68 |
this.label = new JLabel(); |
69 |
this.label.setPreferredSize( new Dimension( 200, 16)); |
70 |
|
71 |
this.progressBar = new JProgressBar(1,100); |
72 |
this.progressBar.setPreferredSize(new Dimension( 40, 10)); |
73 |
this.progressBar.setIndeterminate(true); |
74 |
this.progressBar.setBorderPainted(true); |
75 |
this.progressBar.setVisible(false); |
76 |
|
77 |
this.showAllTaskStatusButton = new JButton(); |
78 |
this.showAllTaskStatusButton.setPreferredSize( new Dimension(16, 16)); |
79 |
this.showAllTaskStatusButton.setBorderPainted(false); |
80 |
this.showAllTaskStatusButton.setIcon( this.getIcon("showAllTaskStatusButton.png")); |
81 |
this.showAllTaskStatusButton.setVisible(false); |
82 |
this.showAllTaskStatusButton.addActionListener(new ActionListener() { |
83 |
public void actionPerformed(ActionEvent arg0) { |
84 |
JTasksStatusList dialog = ToolsSwingLocator.getTaskStatusSwingManager().createJTasksStatusList(manager); |
85 |
WindowManager wm = ToolsSwingLocator.getWindowManager(); |
86 |
wm.showWindow(dialog, "Tasks",WindowManager.MODE.TOOL);
|
87 |
} |
88 |
}); |
89 |
|
90 |
LayoutManager layout = new FlowLayout(); |
91 |
this.setLayout(layout);
|
92 |
|
93 |
this.add(this.label); |
94 |
this.add(this.progressBar); |
95 |
this.add(this.showAllTaskStatusButton); |
96 |
} |
97 |
|
98 |
private ImageIcon getIcon(String name) { |
99 |
URL iconurl = this.getClass().getResource(name); |
100 |
if( iconurl == null ) { |
101 |
return new ImageIcon(); |
102 |
} |
103 |
return new ImageIcon(iconurl); |
104 |
} |
105 |
public void update(final Observable observable, final Object notification) { |
106 |
|
107 |
if(notification != null && !(notification instanceof TaskStatus) ) { |
108 |
return;
|
109 |
} |
110 |
if( !SwingUtilities.isEventDispatchThread()) { |
111 |
SwingUtilities.invokeLater( new Runnable() { |
112 |
public void run() { |
113 |
update(observable, notification); |
114 |
} |
115 |
}); |
116 |
return;
|
117 |
} |
118 |
TaskStatus taskStatus = (TaskStatus) notification; |
119 |
if( taskStatus == null ) { |
120 |
taskStatus = this.manager.getRunningTaskStatusMostRecent();
|
121 |
if( taskStatus == null ) { |
122 |
this.label.setText(""); |
123 |
this.progressBar.setVisible(false); |
124 |
this.showAllTaskStatusButton.setVisible(false); |
125 |
return;
|
126 |
} |
127 |
} |
128 |
if( !taskStatus.isRunning() ) {
|
129 |
TaskStatus taskStatus2 = this.manager.getRunningTaskStatusMostRecent();
|
130 |
if( taskStatus2 == null ) { |
131 |
// Not running task
|
132 |
this.label.setText(""); |
133 |
this.progressBar.setVisible(false); |
134 |
this.showAllTaskStatusButton.setVisible(false); |
135 |
return;
|
136 |
} else {
|
137 |
taskStatus = taskStatus2; |
138 |
} |
139 |
} |
140 |
String label = taskStatus.getLabel();
|
141 |
if( label == null || label.trim().equals("") ) { |
142 |
label = taskStatus.getTitle(); |
143 |
} else {
|
144 |
label = taskStatus.getTitle() + ": " + label;
|
145 |
} |
146 |
this.label.setText( label );
|
147 |
this.progressBar.setIndeterminate( taskStatus.isIndeterminate() );
|
148 |
this.progressBar.setValue( taskStatus.getCompleted() );
|
149 |
if( !this.progressBar.isVisible() ) { |
150 |
this.progressBar.setVisible(true); |
151 |
} |
152 |
if( !this.showAllTaskStatusButton.isVisible() ) { |
153 |
this.showAllTaskStatusButton.setVisible(true); |
154 |
} |
155 |
|
156 |
} |
157 |
|
158 |
} |