]> git.argeo.org Git - gpl/argeo-slc.git/blob - eclipse/plugins/org.argeo.slc.client.ui/src/org/argeo/slc/client/ui/views/ExecutionModulesContentProvider.java
-> Paging update.
[gpl/argeo-slc.git] / eclipse / plugins / org.argeo.slc.client.ui / src / org / argeo / slc / client / ui / views / ExecutionModulesContentProvider.java
1 package org.argeo.slc.client.ui.views;
2
3 import java.util.ArrayList;
4 import java.util.HashMap;
5 import java.util.List;
6 import java.util.Map;
7 import java.util.SortedMap;
8 import java.util.TreeMap;
9
10 import org.apache.commons.logging.Log;
11 import org.apache.commons.logging.LogFactory;
12 import org.argeo.eclipse.ui.TreeObject;
13 import org.argeo.eclipse.ui.TreeParent;
14 import org.argeo.slc.execution.ExecutionFlowDescriptor;
15 import org.argeo.slc.execution.ExecutionModuleDescriptor;
16 import org.argeo.slc.runtime.SlcAgent;
17 import org.eclipse.jface.viewers.ITreeContentProvider;
18 import org.eclipse.jface.viewers.Viewer;
19
20 public class ExecutionModulesContentProvider implements ITreeContentProvider {
21 private final static Log log = LogFactory
22 .getLog(ExecutionModulesContentProvider.class);
23
24 private List<SlcAgent> slcAgents;
25
26 public Object[] getChildren(Object parent) {
27 if (parent instanceof ExecutionModuleNode) {
28 ExecutionModuleNode executionModuleNode = (ExecutionModuleNode) parent;
29 ExecutionModuleDescriptor emd = executionModuleNode.getDescriptor();
30 emd = executionModuleNode.getAgentNode().getAgent()
31 .getExecutionModuleDescriptor(emd.getName(),
32 emd.getVersion());
33 executionModuleNode.cacheDescriptor(emd);
34 // for (String flowName : executionModuleNode.getFlowDescriptors()
35 // .keySet()) {
36 // executionModuleNode.addChild(new FlowNode(flowName,
37 // executionModuleNode));
38 // }
39 return executionModuleNode.getChildren();
40 } else if (parent instanceof AgentNode) {
41 AgentNode agentNode = (AgentNode) parent;
42
43 if (log.isTraceEnabled())
44 log.trace("Scan agent " + agentNode);
45
46 agentNode.clearChildren();
47 for (ExecutionModuleDescriptor desc : agentNode.getAgent()
48 .listExecutionModuleDescriptors()) {
49 agentNode.addChild(new ExecutionModuleNode(agentNode, desc));
50 }
51
52 return agentNode.getChildren();
53 } else if (parent instanceof TreeParent) {
54 return ((TreeParent) parent).getChildren();
55 } else if (parent instanceof FlowNode) {
56 return new Object[0];
57 } else {
58 List<AgentNode> agentNodes = new ArrayList<AgentNode>();
59 for (SlcAgent slcAgent : slcAgents) {
60 agentNodes.add(new AgentNode(slcAgent));
61 }
62 return agentNodes.toArray();
63 }
64 }
65
66 public Object getParent(Object node) {
67 // if (node instanceof TreeObject) {
68 // return ((TreeObject) node).getParent();
69 // }
70 return null;
71 }
72
73 public boolean hasChildren(Object parent) {
74 if (parent instanceof TreeParent && ((TreeParent) parent).isLoaded()) {
75 return ((TreeParent) parent).hasChildren();
76 } else if (parent instanceof AgentNode) {
77 return true;
78 } else if (parent instanceof ExecutionModuleNode) {
79 return true;
80 }
81 return false;
82 }
83
84 public void inputChanged(Viewer v, Object oldInput, Object newInput) {
85 }
86
87 public void dispose() {
88 }
89
90 public Object[] getElements(Object parent) {
91 return getChildren(parent);
92 }
93
94 public void setSlcAgents(List<SlcAgent> slcAgents) {
95 this.slcAgents = slcAgents;
96 }
97
98 public class AgentNode extends TreeParent {
99 private final SlcAgent agent;
100
101 public AgentNode(SlcAgent agent) {
102 super(agent.toString());
103 this.agent = agent;
104 }
105
106 public SlcAgent getAgent() {
107 return agent;
108 }
109 }
110
111 public class ExecutionModuleNode extends TreeParent {
112 private final AgentNode agentNode;
113 private ExecutionModuleDescriptor descriptor;
114 private Map<String, ExecutionFlowDescriptor> flowDescriptors;
115
116 public ExecutionModuleNode(AgentNode agentNode,
117 ExecutionModuleDescriptor descriptor) {
118 super(descriptor.toString());
119 this.agentNode = agentNode;
120 this.descriptor = descriptor;
121
122 }
123
124 public AgentNode getAgentNode() {
125 return agentNode;
126 }
127
128 public ExecutionModuleDescriptor getDescriptor() {
129 return descriptor;
130 }
131
132 public void cacheDescriptor(ExecutionModuleDescriptor descriptor) {
133 this.descriptor = descriptor;
134
135 SortedMap<String, FolderNode> folderNodes = new TreeMap<String, FolderNode>();
136
137 flowDescriptors = new HashMap<String, ExecutionFlowDescriptor>();
138 for (ExecutionFlowDescriptor fd : descriptor.getExecutionFlows()) {
139 // if (log.isTraceEnabled())
140 // log.trace("fd.path=" + fd.getPath() + ", fd.name="
141 // + fd.getName());
142
143 // find path and label
144 String path;
145 String label;
146 int lastSlash = fd.getName().lastIndexOf('/');
147 if ((fd.getPath() == null || fd.getPath().trim().equals(""))
148 && lastSlash >= 0) {
149 path = fd.getName().substring(0, lastSlash);
150 label = fd.getName().substring(lastSlash + 1);
151 } else {
152 path = fd.getPath();
153 label = fd.getName();
154 }
155 // if (log.isTraceEnabled())
156 // log.trace("path=" + path + ", label=" + label);
157
158 if (path == null || path.trim().equals("")
159 || path.trim().equals("/")) {
160 // directChildren.put(name, new FlowNode(name, this));
161 addChild(new FlowNode(label, fd.getName(), this));
162 } else {
163 FolderNode folderNode = mkdirs(this, path, folderNodes);
164 folderNode
165 .addChild(new FlowNode(label, fd.getName(), this));
166 }
167
168 flowDescriptors.put(fd.getName(), fd);
169 }
170 // TODO: make it readonly
171 }
172
173 protected FolderNode mkdirs(TreeParent root, String path,
174 SortedMap<String, FolderNode> folderNodes) {
175 // Normalize
176 if (path.charAt(0) != '/')
177 path = '/' + path;
178 if (path.charAt(path.length() - 1) == '/')
179 path = path.substring(0, path.length() - 1);
180
181 if (folderNodes.containsKey(path))
182 return folderNodes.get(path);
183
184 int lastIndx = path.lastIndexOf('/');
185 String folderName;
186 String parentPath;
187 if (lastIndx >= 0) {
188 folderName = path.substring(lastIndx + 1);
189 parentPath = path.substring(0, lastIndx);
190 } else {
191 folderName = path;
192 parentPath = "";
193 }
194
195 TreeParent parent;
196 if (parentPath.equals(""))
197 parent = root;
198 else
199 parent = mkdirs(root, parentPath, folderNodes);
200 FolderNode newFolder = new FolderNode(folderName);
201 parent.addChild(newFolder);
202 folderNodes.put(path, newFolder);
203 return newFolder;
204 }
205
206 public Map<String, ExecutionFlowDescriptor> getFlowDescriptors() {
207 return flowDescriptors;
208 }
209
210 }
211
212 public class FlowNode extends TreeObject {
213 private final String flowName;
214 private final ExecutionModuleNode executionModuleNode;
215
216 public FlowNode(String label, String flowName,
217 ExecutionModuleNode executionModuleNode) {
218 super(label);
219 this.flowName = flowName;
220 this.executionModuleNode = executionModuleNode;
221 }
222
223 public String getFlowName() {
224 return flowName;
225 }
226
227 public ExecutionModuleNode getExecutionModuleNode() {
228 return executionModuleNode;
229 }
230
231 }
232
233 public class FolderNode extends TreeParent {
234 public FolderNode(String name) {
235 super(name);
236 }
237
238 }
239 }