]> 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
ré-organisation des plugins de SLC Eclipse.
[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.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 // IoC
25 private List<SlcAgent> slcAgents;
26
27 public Object[] getChildren(Object parent) {
28 if (parent instanceof ExecutionModuleNode) {
29 ExecutionModuleNode executionModuleNode = (ExecutionModuleNode) parent;
30 ExecutionModuleDescriptor emd = executionModuleNode.getDescriptor();
31
32 // Terminate the building of UI specific object emd
33 emd = executionModuleNode
34 .getAgentNode()
35 .getAgent()
36 .getExecutionModuleDescriptor(emd.getName(),
37 emd.getVersion());
38 executionModuleNode.cacheDescriptor(emd);
39
40 // This is not recursive, e.g. ExecutionModuleNode build a Tree of
41 // specific
42 // treeObject and cache it in the cacheDescriptor.
43 // Then we only have TreeObjects
44 return executionModuleNode.getChildren();
45 } else if (parent instanceof AgentNode) {
46 AgentNode agentNode = (AgentNode) parent;
47
48 if (log.isTraceEnabled())
49 log.trace("Scan agent " + agentNode);
50
51 agentNode.clearChildren();
52 for (ExecutionModuleDescriptor desc : agentNode.getAgent()
53 .listExecutionModuleDescriptors()) {
54 agentNode.addChild(new ExecutionModuleNode(agentNode, desc));
55 }
56
57 return agentNode.getChildren();
58 } else if (parent instanceof TreeParent) {
59 return ((TreeParent) parent).getChildren();
60 } else if (parent instanceof FlowNode) {
61 return new Object[0];
62 } else {
63 List<AgentNode> agentNodes = new ArrayList<AgentNode>();
64 for (SlcAgent slcAgent : slcAgents) {
65 agentNodes.add(new AgentNode(slcAgent));
66 }
67 return agentNodes.toArray();
68 }
69 }
70
71 public Object getParent(Object node) {
72 // if (node instanceof TreeObject) {
73 // return ((TreeObject) node).getParent();
74 // }
75 return null;
76 }
77
78 public boolean hasChildren(Object parent) {
79 if (parent instanceof TreeParent && ((TreeParent) parent).isLoaded()) {
80 return ((TreeParent) parent).hasChildren();
81 } else if (parent instanceof AgentNode) {
82 return true;
83 } else if (parent instanceof ExecutionModuleNode) {
84 return true;
85 }
86 return false;
87 }
88
89 public void inputChanged(Viewer v, Object oldInput, Object newInput) {
90 }
91
92 public void dispose() {
93 }
94
95 public Object[] getElements(Object parent) {
96 return getChildren(parent);
97 }
98
99
100
101 public class AgentNode extends TreeParent {
102 private final SlcAgent agent;
103
104 public AgentNode(SlcAgent agent) {
105 super(agent.toString());
106 this.agent = agent;
107 }
108
109 public SlcAgent getAgent() {
110 return agent;
111 }
112 }
113
114 public class ExecutionModuleNode extends TreeParent {
115 private final AgentNode agentNode;
116 private ExecutionModuleDescriptor descriptor;
117 private Map<String, ExecutionFlowDescriptor> flowDescriptors;
118
119 public ExecutionModuleNode(AgentNode agentNode,
120 ExecutionModuleDescriptor descriptor) {
121 super(descriptor.toString());
122 this.agentNode = agentNode;
123 this.descriptor = descriptor;
124
125 }
126
127 public AgentNode getAgentNode() {
128 return agentNode;
129 }
130
131 public ExecutionModuleDescriptor getDescriptor() {
132 return descriptor;
133 }
134
135 public void cacheDescriptor(ExecutionModuleDescriptor descriptor) {
136 this.descriptor = descriptor;
137
138 SortedMap<String, FolderNode> folderNodes = new TreeMap<String, FolderNode>();
139 flowDescriptors = new HashMap<String, ExecutionFlowDescriptor>();
140
141 for (ExecutionFlowDescriptor fd : descriptor.getExecutionFlows()) {
142 // Find, format and store path and label values for each flow
143 // descritor:
144
145 // we format name of type path="" & name="path/toTest/Test" to :
146 // path="path/toTest/" name="Test"
147 String path;
148 String label;
149 int lastSlash = fd.getName().lastIndexOf('/');
150 if ((fd.getPath() == null || fd.getPath().trim().equals(""))
151 && lastSlash >= 0) {
152 path = fd.getName().substring(0, lastSlash);
153 label = fd.getName().substring(lastSlash + 1);
154 } else {
155 path = fd.getPath();
156 label = fd.getName();
157 }
158
159 if (path == null || path.trim().equals("")
160 || path.trim().equals("/")) {
161 // directChildren.put(name, new FlowNode(name, this));
162 addChild(new FlowNode(label, fd.getName(), fd, this));
163 } else {
164 FolderNode folderNode = mkdirs(this, path, folderNodes);
165 folderNode.addChild(new FlowNode(label, fd.getName(), fd,
166 this));
167 }
168
169 flowDescriptors.put(fd.getName(), fd);
170 }
171 // TODO: make it readonly
172 }
173
174 protected FolderNode mkdirs(TreeParent root, String path,
175 SortedMap<String, FolderNode> folderNodes) {
176 // Normalize
177 if (path.charAt(0) != '/')
178 path = '/' + path;
179 if (path.charAt(path.length() - 1) == '/')
180 path = path.substring(0, path.length() - 1);
181
182 if (folderNodes.containsKey(path))
183 return folderNodes.get(path);
184
185 int lastIndx = path.lastIndexOf('/');
186 String folderName;
187 String parentPath;
188 if (lastIndx >= 0) {
189 folderName = path.substring(lastIndx + 1);
190 parentPath = path.substring(0, lastIndx);
191 } else {
192 folderName = path;
193 parentPath = "";
194 }
195
196 TreeParent parent;
197 if (parentPath.equals(""))
198 parent = root;
199 else
200 parent = mkdirs(root, parentPath, folderNodes);
201 FolderNode newFolder = new FolderNode(folderName);
202 parent.addChild(newFolder);
203 folderNodes.put(path, newFolder);
204 return newFolder;
205 }
206
207 public Map<String, ExecutionFlowDescriptor> getFlowDescriptors() {
208 return flowDescriptors;
209 }
210
211 }
212
213 /**
214 *
215 * @author bsinou
216 *
217 * The implementation of a vernice of a given slc process. Note that
218 * we store the parent node (execution module node) & the
219 * ExecutionFlowDescriptor.
220 */
221 public class FlowNode extends TreeObject {
222
223 private final String flowName;
224 private final ExecutionModuleNode executionModuleNode;
225 private final ExecutionFlowDescriptor executionFlowDescriptor;
226
227 public FlowNode(String label, String flowName,
228 ExecutionFlowDescriptor executionFlowDescriptor,
229 ExecutionModuleNode parent) {
230 super(label);
231 this.flowName = flowName;
232 this.executionFlowDescriptor = executionFlowDescriptor;
233 this.executionModuleNode = parent;
234 }
235
236 public String getFlowName() {
237 return flowName;
238 }
239
240 public ExecutionModuleNode getExecutionModuleNode() {
241 return executionModuleNode;
242 }
243
244 public ExecutionFlowDescriptor getExecutionFlowDescriptor() {
245 return executionFlowDescriptor;
246 }
247
248 }
249
250 public class FolderNode extends TreeParent {
251 public FolderNode(String name) {
252 super(name);
253 }
254
255 }
256
257 // IoC
258 public void setSlcAgents(List<SlcAgent> slcAgents) {
259 this.slcAgents = slcAgents;
260 }
261 }