Statistics
| Revision:

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 / LoadedGroup.java @ 866

History | View | Annotate | Download (3.19 KB)

1
package org.gvsig.proj.swing.impl.tree;
2

    
3
import java.util.ArrayList;
4
import java.util.Enumeration;
5
import java.util.List;
6

    
7
import javax.swing.Icon;
8
import javax.swing.tree.TreeNode;
9

    
10
import org.gvsig.tools.ToolsLocator;
11
import org.gvsig.tools.i18n.I18nManager;
12

    
13
public class LoadedGroup extends BranchNode {
14
        private boolean loaded = false;
15
        private boolean loading = false;
16
        private GroupLoader loader = null;
17
        final protected I18nManager i18nManager = ToolsLocator.getI18nManager(); 
18
        
19
        public LoadedGroup(TreeNode parent, TreeContainer container,  String message, Icon icon) {
20
                super(parent, container, message, icon);
21
        }
22
        
23
        public LoadedGroup(TreeNode parent, TreeContainer container, String message, Icon expandedIcon, Icon collapsedIcon) {
24
                super(parent, container, message, expandedIcon, collapsedIcon);
25
        }
26
        
27
        /**
28
         * Requests children to be loaded, if they have not been previously loaded
29
         * 
30
         * @return true if children were previously loaded (so load was not needed), false
31
         * otherwise.
32
         */
33
        public void loadChildren() {
34
                if (loaded == false && loading==false) {
35
                        reloadChildren();
36
                }
37
        }
38
        
39
        public void reloadChildren() {
40
                if (!loading) {
41
                        loading = true;
42
                        children.clear();
43
                        doLoad();
44
                        TreeNode node = loader.createLoadingNode();
45
                        if (node!=null) {
46
                                children.add(node);
47
                        }
48
                }
49
                else {
50
                        doLoad();
51
                }
52
        }
53
        
54
        public void doLoad() {
55
                if (loader!=null) {
56
                        loader.cancel();
57
                }
58
                loader = getLoader();
59
                loader.execute();
60
        }
61
        
62
        protected boolean isLoading() {
63
                return loading;
64
        }
65
        
66
        protected boolean isLoaded() {
67
                return loaded;
68
        }
69
        
70
        protected GroupLoader getLoader() {
71
                return this.loader;
72
        }
73
        
74
        public void setLoader(GroupLoader loader) {
75
                this.loader = loader;
76
        }
77
        
78
        /**
79
         * This method is called by the loader in the Event Dispatch Thread
80
         * when children loading has been cancelled
81
         */
82
        protected void cancelled(GroupLoader loader) {
83
                if (this.loader == loader) {
84
                        loading = false;
85
                        this.loader = null;
86
                        cancelled();
87
                }
88
        }
89

    
90
        /**
91
         * This method is called by the loader in the Event Dispatch Thread
92
         * when children loading is completed
93
         */
94
        public void loaded(ArrayList<TreeNode> loadedData, GroupLoader loader) {
95
                if (this.loader == loader) { // ignore if results are coming from an older loader
96
                        this.loader = null;
97
                        this.loading = false;
98
                        if (loader.isCancelled()) {
99
                                cancelled();
100
                        }
101
                        else {
102
                                loaded = true;
103
                                loaded(loadedData);
104
                        }
105
                }
106
        }
107
        
108
        public TreeContainer geTreeContainer() {
109
                return this.container;
110
        }
111
        
112
        /**
113
         * This method is called in the Event Dispatch Thread
114
         * when children loading is completed
115
         */
116
        public void loaded(List<TreeNode> loadedData) {
117
                this.children.clear();
118
                this.children.addAll(loadedData);
119
                this.container.getModel().reload(this);
120
        }
121
        
122
        /**
123
         * This method is called the Event Dispatch Thread
124
         * when children loading has been cancelled
125
         */
126
        protected void cancelled() {
127
                this.children.clear();
128
        }
129
        
130
        @Override
131
        public TreeNode getChildAt(int childIndex) {
132
                loadChildren();
133
                return super.getChildAt(childIndex);
134
        }
135
        
136
        @Override
137
        public int getChildCount() {
138
                loadChildren();
139
                return super.getChildCount();
140
        }
141
        
142
        @Override
143
        @SuppressWarnings("rawtypes")
144
        public Enumeration children() {
145
                loadChildren();
146
                return super.children();
147
        }
148
}