svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.xml2db / org.gvsig.xml2db.swing / org.gvsig.xml2db.swing.impl / src / main / java / org / gvsig / xml2db / swing / impl / Task.java @ 47334
History | View | Annotate | Download (4.25 KB)
1 |
/*
|
---|---|
2 |
* To change this license header, choose License Headers in Project Properties.
|
3 |
* To change this template file, choose Tools | Templates
|
4 |
* and open the template in the editor.
|
5 |
*/
|
6 |
package org.gvsig.xml2db.swing.impl; |
7 |
|
8 |
import java.awt.Color; |
9 |
import javax.swing.SwingUtilities; |
10 |
import org.apache.commons.lang3.mutable.MutableBoolean; |
11 |
import org.gvsig.tools.ToolsLocator; |
12 |
import org.gvsig.tools.exception.BaseException; |
13 |
import org.gvsig.tools.swing.api.task.TaskStatusController; |
14 |
import org.gvsig.tools.task.SimpleTaskStatus; |
15 |
|
16 |
/**
|
17 |
*
|
18 |
* @author jjdelcerro
|
19 |
*/
|
20 |
public abstract class Task implements Runnable { |
21 |
|
22 |
private final String title; |
23 |
private final String name; |
24 |
private final TaskStatusController taskStatusController; |
25 |
private final MutableBoolean processing; |
26 |
private final Runnable updateStateComponents; |
27 |
private SimpleTaskStatus taskStatus;
|
28 |
|
29 |
public static final Task FAKE_TASK = new Task("None", "None") { |
30 |
@Override
|
31 |
protected void task(SimpleTaskStatus taskStatus) throws Throwable { |
32 |
} |
33 |
|
34 |
@Override
|
35 |
public void start() { |
36 |
} |
37 |
}; |
38 |
|
39 |
public static final Task get(Task task) { |
40 |
if( task==null ) { |
41 |
return FAKE_TASK;
|
42 |
} |
43 |
return task;
|
44 |
} |
45 |
|
46 |
public Task(String name, String title, MutableBoolean processing, Runnable updateStateComponents, TaskStatusController taskStatusController) { |
47 |
this.name = name;
|
48 |
this.title = title;
|
49 |
this.processing = processing;
|
50 |
this.updateStateComponents = updateStateComponents;
|
51 |
this.taskStatusController = taskStatusController;
|
52 |
this.taskStatus = null; |
53 |
} |
54 |
|
55 |
public Task(String name, String title, Runnable updateStateComponents, TaskStatusController taskStatusController) { |
56 |
this(name, title, new MutableBoolean(false), updateStateComponents, taskStatusController); |
57 |
} |
58 |
|
59 |
public Task(String name, String title, MutableBoolean processing, Runnable updateStateComponents) { |
60 |
this(name, title, processing, updateStateComponents, null); |
61 |
} |
62 |
|
63 |
public Task(String name, String title, MutableBoolean processing) { |
64 |
this(name, title, processing, null, null); |
65 |
} |
66 |
|
67 |
public Task(String name, String title) { |
68 |
this(name, title, new MutableBoolean(false), null, null); |
69 |
} |
70 |
|
71 |
private void updateStateComponents() { |
72 |
if (this.updateStateComponents != null) { |
73 |
SwingUtilities.invokeLater(updateStateComponents);
|
74 |
} |
75 |
} |
76 |
|
77 |
@Override
|
78 |
public final void run() { |
79 |
this.processing.setTrue();
|
80 |
this.taskStatus = ToolsLocator.getTaskStatusManager().createDefaultSimpleTaskStatus(this.title); |
81 |
try {
|
82 |
updateStateComponents(); |
83 |
if (this.taskStatusController != null) { |
84 |
this.taskStatusController.bind(taskStatus);
|
85 |
this.taskStatusController.setVisible(processing.booleanValue());
|
86 |
} |
87 |
this.taskStatus.add();
|
88 |
this.task(this.taskStatus); |
89 |
this.taskStatus.terminate();
|
90 |
} catch (Throwable ex) { |
91 |
this.taskStatus.abort();
|
92 |
// String s = Xml2dbSwingCommons.getHTMLColorTag(Color.RED.darker(), BaseException.getMessageStack(ex, 0));
|
93 |
String s = BaseException.getMessageStack(ex, 0); |
94 |
this.taskStatus.message(s);
|
95 |
} finally {
|
96 |
this.processing.setFalse();
|
97 |
if (!(this.taskStatus.isAborted() || this.taskStatus.isCancelled())) { |
98 |
this.taskStatus.remove();
|
99 |
this.taskStatusController.setVisible(processing.booleanValue());
|
100 |
} |
101 |
updateStateComponents(); |
102 |
this.postTask();
|
103 |
} |
104 |
} |
105 |
|
106 |
public void start() { |
107 |
Thread th = new Thread(this, this.name); |
108 |
th.start(); |
109 |
} |
110 |
|
111 |
public boolean isProcessing() { |
112 |
return this.processing.booleanValue(); |
113 |
} |
114 |
|
115 |
public boolean needToShowTheStatus() { |
116 |
if( taskStatus == null ) { |
117 |
return false; |
118 |
} |
119 |
if( this.processing.isTrue() ) { |
120 |
return true; |
121 |
} |
122 |
return this.taskStatus.isAborted() || this.taskStatus.isCancelled(); |
123 |
} |
124 |
|
125 |
protected abstract void task(SimpleTaskStatus taskStatus) throws Throwable; |
126 |
|
127 |
protected void postTask() { |
128 |
|
129 |
} |
130 |
} |