1 package org
.argeo
.slc
.client
.ui
.views
;
3 import java
.util
.ArrayList
;
4 import java
.util
.HashMap
;
7 import java
.util
.SortedMap
;
8 import java
.util
.TreeMap
;
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
;
21 public class ExecutionModulesContentProvider
implements ITreeContentProvider
{
22 private final static Log log
= LogFactory
23 .getLog(ExecutionModulesContentProvider
.class);
25 private List
<SlcAgent
> slcAgents
;
27 public Object
[] getChildren(Object parent
) {
29 if (parent
instanceof ExecutionModuleNode
) {
30 ExecutionModuleNode executionModuleNode
= (ExecutionModuleNode
) parent
;
31 ExecutionModuleDescriptor emd
= executionModuleNode
.getDescriptor();
33 // Terminate the building of UI specific object emd
34 emd
= executionModuleNode
37 .getExecutionModuleDescriptor(emd
.getName(),
39 executionModuleNode
.cacheDescriptor(emd
);
41 // This is not recursive, e.g. ExecutionModuleNode build a Tree of
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
;
49 if (log
.isTraceEnabled())
50 log
.trace("Scan agent " + agentNode
);
52 agentNode
.clearChildren();
53 for (ExecutionModuleDescriptor desc
: agentNode
.getAgent()
54 .listExecutionModuleDescriptors()) {
55 agentNode
.addChild(new ExecutionModuleNode(agentNode
, desc
));
58 return agentNode
.getChildren();
59 } else if (parent
instanceof TreeParent
) {
60 return ((TreeParent
) parent
).getChildren();
61 } else if (parent
instanceof FlowNode
) {
64 List
<AgentNode
> agentNodes
= new ArrayList
<AgentNode
>();
65 for (SlcAgent slcAgent
: slcAgents
) {
66 agentNodes
.add(new AgentNode(slcAgent
));
68 return agentNodes
.toArray();
72 public Object
getParent(Object node
) {
73 // if (node instanceof TreeObject) {
74 // return ((TreeObject) node).getParent();
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
) {
84 } else if (parent
instanceof ExecutionModuleNode
) {
90 public void inputChanged(Viewer v
, Object oldInput
, Object newInput
) {
93 public void dispose() {
96 public Object
[] getElements(Object parent
) {
97 return getChildren(parent
);
100 public void setSlcAgents(List
<SlcAgent
> slcAgents
) {
101 this.slcAgents
= slcAgents
// for (String flowName :
102 // executionModuleNode.getFlowDescriptors()
104 // executionModuleNode.addChild(new FlowNode(flowName,
105 // executionModuleNode));
110 public class AgentNode
extends TreeParent
{
111 private final SlcAgent agent
;
113 public AgentNode(SlcAgent agent
) {
114 super(agent
.toString());
118 public SlcAgent
getAgent() {
123 public class ExecutionModuleNode
extends TreeParent
{
124 private final AgentNode agentNode
;
125 private ExecutionModuleDescriptor descriptor
;
126 private Map
<String
, ExecutionFlowDescriptor
> flowDescriptors
;
128 public ExecutionModuleNode(AgentNode agentNode
,
129 ExecutionModuleDescriptor descriptor
) {
130 super(descriptor
.toString());
131 this.agentNode
= agentNode
;
132 this.descriptor
= descriptor
;
136 public AgentNode
getAgentNode() {
140 public ExecutionModuleDescriptor
getDescriptor() {
144 public void cacheDescriptor(ExecutionModuleDescriptor descriptor
) {
145 this.descriptor
= descriptor
;
147 SortedMap
<String
, FolderNode
> folderNodes
= new TreeMap
<String
, FolderNode
>();
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="
154 Map
<String
, Object
> values
= fd
.getValues();
157 log
.debug("No attribute for " + fd
.getName());
159 for (String key
: values
.keySet())
160 log
.debug(key
+ " - " + values
.get(key
));
162 // find path and label
165 int lastSlash
= fd
.getName().lastIndexOf('/');
166 if ((fd
.getPath() == null || fd
.getPath().trim().equals(""))
168 path
= fd
.getName().substring(0, lastSlash
);
169 label
= fd
.getName().substring(lastSlash
+ 1);
172 label
= fd
.getName();
174 // if (log.isTraceEnabled())
175 // log.trace("path=" + path + ", label=" + label);
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(),
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));
192 flowDescriptors
.put(fd
.getName(), fd
);
194 // TODO: make it readonly
197 protected FolderNode
mkdirs(TreeParent root
, String path
,
198 SortedMap
<String
, FolderNode
> folderNodes
) {
200 if (path
.charAt(0) != '/')
202 if (path
.charAt(path
.length() - 1) == '/')
203 path
= path
.substring(0, path
.length() - 1);
205 if (folderNodes
.containsKey(path
))
206 return folderNodes
.get(path
);
208 int lastIndx
= path
.lastIndexOf('/');
212 folderName
= path
.substring(lastIndx
+ 1);
213 parentPath
= path
.substring(0, lastIndx
);
220 if (parentPath
.equals(""))
223 parent
= mkdirs(root
, parentPath
, folderNodes
);
224 FolderNode newFolder
= new FolderNode(folderName
);
225 parent
.addChild(newFolder
);
226 folderNodes
.put(path
, newFolder
);
230 public Map
<String
, ExecutionFlowDescriptor
> getFlowDescriptors() {
231 return flowDescriptors
;
236 public class FlowNode
extends TreeObject
{
237 private final String flowName
;
238 private final ExecutionModuleNode executionModuleNode
;
240 // TODO : handle casting from various object type to String and reverse.
241 private final Map
<String
, Object
> values
;
243 public FlowNode(String label
, String flowName
,
244 Map
<String
, Object
> values
, ExecutionModuleNode parent
) {
246 this.flowName
= flowName
;
247 this.values
= values
;
248 this.executionModuleNode
= parent
;
251 public Map
<String
, Object
> getValues() {
255 public Object
getValueByKey(String key
) {
256 return values
.get(key
);
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
);
265 values
.put(key
, value
);
269 public String
getFlowName() {
273 public ExecutionModuleNode
getExecutionModuleNode() {
274 return executionModuleNode
;
279 public class FolderNode
extends TreeParent
{
280 public FolderNode(String name
) {