]> git.argeo.org Git - gpl/argeo-slc.git/blob - eclipse/plugins/org.argeo.slc.client.ui/src/main/java/org/argeo/slc/client/ui/providers/ExecutionModulesContentProvider.java
Fix erros in SLC
[gpl/argeo-slc.git] / eclipse / plugins / org.argeo.slc.client.ui / src / main / java / org / argeo / slc / client / ui / providers / ExecutionModulesContentProvider.java
1 package org.argeo.slc.client.ui.providers;
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.TreeParent;
13 import org.argeo.slc.execution.ExecutionFlowDescriptor;
14 import org.argeo.slc.execution.ExecutionModuleDescriptor;
15 import org.argeo.slc.runtime.SlcAgent;
16 import org.eclipse.jface.viewers.ITreeContentProvider;
17 import org.eclipse.jface.viewers.Viewer;
18
19 public class ExecutionModulesContentProvider implements ITreeContentProvider {
20 private final static Log log = LogFactory
21 .getLog(ExecutionModulesContentProvider.class);
22
23 // IoC
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
31 // Terminate the building of UI specific object emd
32 emd = executionModuleNode
33 .getAgentNode()
34 .getAgent()
35 .getExecutionModuleDescriptor(emd.getName(),
36 emd.getVersion());
37 executionModuleNode.cacheDescriptor(emd);
38
39 // This is not recursive, e.g. ExecutionModuleNode build a Tree of
40 // specific
41 // treeObject and cache it in the cacheDescriptor.
42 // Then we only have TreeObjects
43 return executionModuleNode.getChildren();
44 } else if (parent instanceof AgentNode) {
45 AgentNode agentNode = (AgentNode) parent;
46
47 if (log.isTraceEnabled())
48 log.trace("Scan agent " + agentNode);
49
50 agentNode.clearChildren();
51 for (ExecutionModuleDescriptor desc : agentNode.getAgent()
52 .listExecutionModuleDescriptors()) {
53 agentNode.addChild(new ExecutionModuleNode(agentNode, desc));
54 }
55
56 return agentNode.getChildren();
57 } else if (parent instanceof TreeParent) {
58 return ((TreeParent) parent).getChildren();
59 } else if (parent instanceof FlowNode) {
60 return new Object[0];
61 } else {
62 List<AgentNode> agentNodes = new ArrayList<AgentNode>();
63 for (SlcAgent slcAgent : slcAgents) {
64 agentNodes.add(new AgentNode(slcAgent));
65 }
66 return agentNodes.toArray();
67 }
68 }
69
70 public Object getParent(Object node) {
71 // if (node instanceof TreeObject) {
72 // return ((TreeObject) node).getParent();
73 // }
74 return null;
75 }
76
77 public boolean hasChildren(Object parent) {
78 if (parent instanceof TreeParent && ((TreeParent) parent).isLoaded()) {
79 return ((TreeParent) parent).hasChildren();
80 } else if (parent instanceof AgentNode) {
81 return true;
82 } else if (parent instanceof ExecutionModuleNode) {
83 return true;
84 }
85 return false;
86 }
87
88 public void inputChanged(Viewer v, Object oldInput, Object newInput) {
89 }
90
91 public void dispose() {
92 }
93
94 public Object[] getElements(Object parent) {
95 return getChildren(parent);
96 }
97
98
99
100 public class AgentNode extends TreeParent {
101 private final SlcAgent agent;
102
103 public AgentNode(SlcAgent agent) {
104 super(agent.toString());
105 this.agent = agent;
106 }
107
108 public SlcAgent getAgent() {
109 return agent;
110 }
111 }
112
113 public class ExecutionModuleNode extends TreeParent {
114 private final AgentNode agentNode;
115 private ExecutionModuleDescriptor descriptor;
116 private Map<String, ExecutionFlowDescriptor> flowDescriptors;
117
118 public ExecutionModuleNode(AgentNode agentNode,
119 ExecutionModuleDescriptor descriptor) {
120 super(descriptor.toString());
121 this.agentNode = agentNode;
122 this.descriptor = descriptor;
123
124 }
125
126 public AgentNode getAgentNode() {
127 return agentNode;
128 }
129
130 public ExecutionModuleDescriptor getDescriptor() {
131 return descriptor;
132 }
133
134 public void cacheDescriptor(ExecutionModuleDescriptor descriptor) {
135 this.descriptor = descriptor;
136
137 SortedMap<String, FolderNode> folderNodes = new TreeMap<String, FolderNode>();
138 flowDescriptors = new HashMap<String, ExecutionFlowDescriptor>();
139
140 for (ExecutionFlowDescriptor fd : descriptor.getExecutionFlows()) {
141 // Find, format and store path and label values for each flow
142 // descritor:
143
144 // we format name of type path="" & name="path/toTest/Test" to :
145 // path="path/toTest/" name="Test"
146 String path;
147 String label;
148 int lastSlash = fd.getName().lastIndexOf('/');
149 if ((fd.getPath() == null || fd.getPath().trim().equals(""))
150 && lastSlash >= 0) {
151 path = fd.getName().substring(0, lastSlash);
152 label = fd.getName().substring(lastSlash + 1);
153 } else {
154 path = fd.getPath();
155 label = fd.getName();
156 }
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(), fd, this));
162 } else {
163 FolderNode folderNode = mkdirs(this, path, folderNodes);
164 folderNode.addChild(new FlowNode(label, fd.getName(), fd,
165 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 /**
213 *
214 * @author bsinou
215 *
216 * The implementation of a vernice of a given slc process. Note that
217 * we store the parent node (execution module node) & the
218 * ExecutionFlowDescriptor.
219 */
220 public class FlowNode extends TreeParent {
221
222 private final String flowName;
223 private final ExecutionModuleNode executionModuleNode;
224 private final ExecutionFlowDescriptor executionFlowDescriptor;
225
226 public FlowNode(String label, String flowName,
227 ExecutionFlowDescriptor executionFlowDescriptor,
228 ExecutionModuleNode parent) {
229 super(label);
230 this.flowName = flowName;
231 this.executionFlowDescriptor = executionFlowDescriptor;
232 this.executionModuleNode = parent;
233 }
234
235 public String getFlowName() {
236 return flowName;
237 }
238
239 public ExecutionModuleNode getExecutionModuleNode() {
240 return executionModuleNode;
241 }
242
243 public ExecutionFlowDescriptor getExecutionFlowDescriptor() {
244 return executionFlowDescriptor;
245 }
246
247 }
248
249 public class FolderNode extends TreeParent {
250 public FolderNode(String name) {
251 super(name);
252 }
253
254 }
255
256 // IoC
257 public void setSlcAgents(List<SlcAgent> slcAgents) {
258 this.slcAgents = slcAgents;
259 }
260 }