root / org.gvsig.proj / branches / refactor2018 / org.gvsig.proj / org.gvsig.proj.swing / org.gvsig.proj.swing.impl / src / main / java / org / gvsig / proj / swing / impl / tree / AsyncLoadedGroup.java @ 852
History | View | Annotate | Download (2.82 KB)
1 |
package org.gvsig.proj.swing.impl.tree; |
---|---|
2 |
|
3 |
import java.util.ArrayList; |
4 |
import java.util.Enumeration; |
5 |
|
6 |
import javax.swing.tree.TreeNode; |
7 |
|
8 |
public abstract class AsyncLoadedGroup extends BranchNode { |
9 |
private boolean loaded = false; |
10 |
private boolean loading = false; |
11 |
private GroupLoader loader = null; |
12 |
final protected TreeContainer container; |
13 |
|
14 |
public AsyncLoadedGroup(TreeNode parent, TreeContainer container, String message) { |
15 |
super(parent, container, message);
|
16 |
this.container = container;
|
17 |
} |
18 |
|
19 |
/**
|
20 |
* Requests children to be loaded, if they have not been previously loaded
|
21 |
*
|
22 |
* @return true if children were previously loaded (so load was not needed), false
|
23 |
* otherwise.
|
24 |
*/
|
25 |
public void loadChildren() { |
26 |
if (loaded == false && loading==false) { |
27 |
reloadChildren(); |
28 |
} |
29 |
} |
30 |
|
31 |
public void reloadChildren() { |
32 |
if (!loading) {
|
33 |
loading = true;
|
34 |
doLoad(); |
35 |
children.clear(); |
36 |
children.add(new LoadingNode(this, container, container.getManager().getTranslation("Loading..."))); |
37 |
} |
38 |
else {
|
39 |
doLoad(); |
40 |
} |
41 |
} |
42 |
|
43 |
public void doLoad() { |
44 |
if (loader!=null) { |
45 |
loader.cancel(false);
|
46 |
} |
47 |
loader = createLoader(); |
48 |
loader.execute(); |
49 |
} |
50 |
|
51 |
protected boolean isLoading() { |
52 |
return loading;
|
53 |
} |
54 |
|
55 |
protected boolean isLoaded() { |
56 |
return loaded;
|
57 |
} |
58 |
|
59 |
protected GroupLoader createLoader() {
|
60 |
this.loader = doCreateLoader();
|
61 |
return this.loader; |
62 |
} |
63 |
|
64 |
protected abstract GroupLoader doCreateLoader(); |
65 |
|
66 |
/**
|
67 |
* This method is called by the loader in the Event Dispatch Thread
|
68 |
* when children loading has been cancelled
|
69 |
*/
|
70 |
protected void cancelled(GroupLoader loader) { |
71 |
if (this.loader == loader) { |
72 |
loading = false;
|
73 |
this.loader = null; |
74 |
cancelled(); |
75 |
} |
76 |
} |
77 |
|
78 |
/**
|
79 |
* This method is called by the loader in the Event Dispatch Thread
|
80 |
* when children loading is completed
|
81 |
*/
|
82 |
public void loaded(ArrayList<TreeNode> loadedData, GroupLoader loader) { |
83 |
if (this.loader == loader) { // ignore if results are coming from an older loader |
84 |
this.loader = null; |
85 |
this.loading = false; |
86 |
if (loader.isCancelled()) {
|
87 |
cancelled(); |
88 |
} |
89 |
else {
|
90 |
loaded = true;
|
91 |
loaded(loadedData); |
92 |
} |
93 |
} |
94 |
} |
95 |
|
96 |
/**
|
97 |
* This method is called in the Event Dispatch Thread
|
98 |
* when children loading is completed
|
99 |
*/
|
100 |
public void loaded(ArrayList<TreeNode> loadedData) { |
101 |
this.children.clear();
|
102 |
this.children.addAll(loadedData);
|
103 |
this.container.getModel().reload(this); |
104 |
} |
105 |
|
106 |
/**
|
107 |
* This method is called the Event Dispatch Thread
|
108 |
* when children loading has been cancelled
|
109 |
*/
|
110 |
protected abstract void cancelled(); |
111 |
|
112 |
@Override
|
113 |
public TreeNode getChildAt(int childIndex) { |
114 |
loadChildren(); |
115 |
return super.getChildAt(childIndex); |
116 |
} |
117 |
|
118 |
@Override
|
119 |
public int getChildCount() { |
120 |
loadChildren(); |
121 |
return super.getChildCount(); |
122 |
} |
123 |
|
124 |
@Override
|
125 |
@SuppressWarnings("rawtypes") |
126 |
public Enumeration children() { |
127 |
loadChildren(); |
128 |
return super.children(); |
129 |
} |
130 |
} |