]> 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
365f046503bfe3e312420d33e639216caaa6e54c
[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.SlcException;
15 import org.argeo.slc.execution.ExecutionFlowDescriptor;
16 import org.argeo.slc.execution.ExecutionModuleDescriptor;
17 import org.argeo.slc.runtime.SlcAgent;
18 import org.eclipse.jface.viewers.ITreeContentProvider;
19 import org.eclipse.jface.viewers.Viewer;
20
21 public class ExecutionModulesContentProvider implements ITreeContentProvider {
22 private final static Log log = LogFactory
23 .getLog(ExecutionModulesContentProvider.class);
24
25 private List<SlcAgent> slcAgents;
26
27 public Object[] getChildren(Object parent) {
28
29 if (parent instanceof ExecutionModuleNode) {
30 ExecutionModuleNode executionModuleNode = (ExecutionModuleNode) parent;
31 ExecutionModuleDescriptor emd = executionModuleNode.getDescriptor();
32
33 // Terminate the building of UI specific object emd
34 emd = executionModuleNode
35 .getAgentNode()
36 .getAgent()
37 .getExecutionModuleDescriptor(emd.getName(),
38 emd.getVersion());
39 executionModuleNode.cacheDescriptor(emd);
40
41 // This is not recursive, e.g. ExecutionModuleNode build a Tree of
42 // specific
43 // treeObject and cache it in the cacheDescriptor.
44 // Then we only have TreeObjects
45 return executionModuleNode.getChildren();
46 } else if (parent instanceof AgentNode) {
47 AgentNode agentNode = (AgentNode) parent;
48
49 if (log.isTraceEnabled())
50 log.trace("Scan agent " + agentNode);
51
52 agentNode.clearChildren();
53 for (ExecutionModuleDescriptor desc : agentNode.getAgent()
54 .listExecutionModuleDescriptors()) {
55 agentNode.addChild(new ExecutionModuleNode(agentNode, desc));
56 }
57
58 return agentNode.getChildren();
59 } else if (parent instanceof TreeParent) {
60 return ((TreeParent) parent).getChildren();
61 } else if (parent instanceof FlowNode) {
62 return new Object[0];
63 } else {
64 List<AgentNode> agentNodes = new ArrayList<AgentNode>();
65 for (SlcAgent slcAgent : slcAgents) {
66 agentNodes.add(new AgentNode(slcAgent));
67 }
68 return agentNodes.toArray();
69 }
70 }
71
72 public Object getParent(Object node) {
73 // if (node instanceof TreeObject) {
74 // return ((TreeObject) node).getParent();
75 // }
76 return null;
77 }
78
79 public boolean hasChildren(Object parent) {
80 if (parent instanceof TreeParent && ((TreeParent) parent).isLoaded()) {
81 return ((TreeParent) parent).hasChildren();
82 } else if (parent instanceof AgentNode) {
83 return true;
84 } else if (parent instanceof ExecutionModuleNode) {
85 return true;
86 }
87 return false;
88 }
89
90 public void inputChanged(Viewer v, Object oldInput, Object newInput) {
91 }
92
93 public void dispose() {
94 }
95
96 public Object[] getElements(Object parent) {
97 return getChildren(parent);
98 }
99
100 public void setSlcAgents(List<SlcAgent> slcAgents) {
101 this.slcAgents = slcAgents// for (String flowName :
102 // executionModuleNode.getFlowDescriptors()
103 // .keySet()) {
104 // executionModuleNode.addChild(new FlowNode(flowName,
105 // executionModuleNode));
106 // }
107 ;
108 }
109
110 public class AgentNode extends TreeParent {
111 private final SlcAgent agent;
112
113 public AgentNode(SlcAgent agent) {
114 super(agent.toString());
115 this.agent = agent;
116 }
117
118 public SlcAgent getAgent() {
119 return agent;
120 }
121 }
122
123 public class ExecutionModuleNode extends TreeParent {
124 private final AgentNode agentNode;
125 private ExecutionModuleDescriptor descriptor;
126 private Map<String, ExecutionFlowDescriptor> flowDescriptors;
127
128 public ExecutionModuleNode(AgentNode agentNode,
129 ExecutionModuleDescriptor descriptor) {
130 super(descriptor.toString());
131 this.agentNode = agentNode;
132 this.descriptor = descriptor;
133
134 }
135
136 public AgentNode getAgentNode() {
137 return agentNode;
138 }
139
140 public ExecutionModuleDescriptor getDescriptor() {
141 return descriptor;
142 }
143
144 public void cacheDescriptor(ExecutionModuleDescriptor descriptor) {
145 this.descriptor = descriptor;
146
147 SortedMap<String, FolderNode> folderNodes = new TreeMap<String, FolderNode>();
148
149 flowDescriptors = new HashMap<String, ExecutionFlowDescriptor>();
150 for (ExecutionFlowDescriptor fd : descriptor.getExecutionFlows()) {
151 if (log.isTraceEnabled())
152 log.trace("fd.path=" + fd.getPath() + ", fd.name="
153 + fd.getName());
154 Map<String, Object> values = fd.getValues();
155
156 if (values == null)
157 log.debug("No attribute for " + fd.getName());
158 else
159 for (String key : values.keySet())
160 log.debug(key + " - " + values.get(key));
161
162 // find path and label
163 String path;
164 String label;
165 int lastSlash = fd.getName().lastIndexOf('/');
166 if ((fd.getPath() == null || fd.getPath().trim().equals(""))
167 && lastSlash >= 0) {
168 path = fd.getName().substring(0, lastSlash);
169 label = fd.getName().substring(lastSlash + 1);
170 } else {
171 path = fd.getPath();
172 label = fd.getName();
173 }
174 // if (log.isTraceEnabled())
175 // log.trace("path=" + path + ", label=" + label);
176
177 if (path == null || path.trim().equals("")
178 || path.trim().equals("/")) {
179 // directChildren.put(name, new FlowNode(name, this));
180 addChild(new FlowNode(label, fd.getName(), fd.getValues(),
181 this));
182 } else {
183 FolderNode folderNode = mkdirs(this, path, folderNodes);
184 // TODO : why do we add a reference to the parent ?
185 // Probably to differentiate 2 flow nodes with same name but
186 // distinct execution Node. TBC
187 folderNode.addChild(new FlowNode(label, fd.getName(), fd
188 .getValues(), this));
189
190 }
191
192 flowDescriptors.put(fd.getName(), fd);
193 }
194 // TODO: make it readonly
195 }
196
197 protected FolderNode mkdirs(TreeParent root, String path,
198 SortedMap<String, FolderNode> folderNodes) {
199 // Normalize
200 if (path.charAt(0) != '/')
201 path = '/' + path;
202 if (path.charAt(path.length() - 1) == '/')
203 path = path.substring(0, path.length() - 1);
204
205 if (folderNodes.containsKey(path))
206 return folderNodes.get(path);
207
208 int lastIndx = path.lastIndexOf('/');
209 String folderName;
210 String parentPath;
211 if (lastIndx >= 0) {
212 folderName = path.substring(lastIndx + 1);
213 parentPath = path.substring(0, lastIndx);
214 } else {
215 folderName = path;
216 parentPath = "";
217 }
218
219 TreeParent parent;
220 if (parentPath.equals(""))
221 parent = root;
222 else
223 parent = mkdirs(root, parentPath, folderNodes);
224 FolderNode newFolder = new FolderNode(folderName);
225 parent.addChild(newFolder);
226 folderNodes.put(path, newFolder);
227 return newFolder;
228 }
229
230 public Map<String, ExecutionFlowDescriptor> getFlowDescriptors() {
231 return flowDescriptors;
232 }
233
234 }
235
236 public class FlowNode extends TreeObject {
237 private final String flowName;
238 private final ExecutionModuleNode executionModuleNode;
239
240 // TODO : handle casting from various object type to String and reverse.
241 private final Map<String, Object> values;
242
243 public FlowNode(String label, String flowName,
244 Map<String, Object> values, ExecutionModuleNode parent) {
245 super(label);
246 this.flowName = flowName;
247 this.values = values;
248 this.executionModuleNode = parent;
249 }
250
251 public Map<String, Object> getValues() {
252 return values;
253 }
254
255 public Object getValueByKey(String key) {
256 return values.get(key);
257 }
258
259 public void setValueByKey(String key, Object value) {
260 if (values.get(key) == null)
261 throw new SlcException("Unsupported Parameter " + key
262 + " for FlowNode " + flowName);
263 else {
264 values.remove(key);
265 values.put(key, value);
266 }
267 }
268
269 public String getFlowName() {
270 return flowName;
271 }
272
273 public ExecutionModuleNode getExecutionModuleNode() {
274 return executionModuleNode;
275 }
276
277 }
278
279 public class FolderNode extends TreeParent {
280 public FolderNode(String name) {
281 super(name);
282 }
283
284 }
285 }