]> 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
Introduce SLC RCP
[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.HashMap;
4 import java.util.Map;
5 import java.util.SortedMap;
6 import java.util.TreeMap;
7
8 import org.apache.commons.logging.Log;
9 import org.apache.commons.logging.LogFactory;
10 import org.argeo.eclipse.ui.TreeObject;
11 import org.argeo.eclipse.ui.TreeParent;
12 import org.argeo.slc.execution.ExecutionFlowDescriptor;
13 import org.argeo.slc.execution.ExecutionModuleDescriptor;
14 import org.argeo.slc.runtime.SlcAgent;
15 import org.eclipse.jface.viewers.ITreeContentProvider;
16 import org.eclipse.jface.viewers.Viewer;
17
18 public class ExecutionModulesContentProvider implements ITreeContentProvider {
19 private final static Log log = LogFactory
20 .getLog(ExecutionModulesContentProvider.class);
21
22 private SlcAgent slcAgent;
23
24 @Override
25 public Object[] getChildren(Object parent) {
26 if (parent instanceof ExecutionModuleNode) {
27 ExecutionModuleNode executionModuleNode = (ExecutionModuleNode) parent;
28 ExecutionModuleDescriptor emd = executionModuleNode.getDescriptor();
29 emd = executionModuleNode.getAgentNode().getAgent()
30 .getExecutionModuleDescriptor(emd.getName(),
31 emd.getVersion());
32 executionModuleNode.cacheDescriptor(emd);
33 // for (String flowName : executionModuleNode.getFlowDescriptors()
34 // .keySet()) {
35 // executionModuleNode.addChild(new FlowNode(flowName,
36 // executionModuleNode));
37 // }
38 return executionModuleNode.getChildren();
39 } else if (parent instanceof AgentNode) {
40 AgentNode agentNode = (AgentNode) parent;
41
42 if (log.isTraceEnabled())
43 log.trace("Scan agent " + agentNode);
44
45 agentNode.clearChildren();
46 for (ExecutionModuleDescriptor desc : agentNode.getAgent()
47 .listExecutionModuleDescriptors()) {
48 agentNode.addChild(new ExecutionModuleNode(agentNode, desc));
49 }
50
51 return agentNode.getChildren();
52 } else if (parent instanceof TreeParent) {
53 return ((TreeParent) parent).getChildren();
54 } else if (parent instanceof FlowNode) {
55 return new Object[0];
56 } else {
57 log.trace(parent);
58 Object[] arr = { new AgentNode(slcAgent) };
59 return arr;
60 }
61 }
62
63 @Override
64 public Object getParent(Object node) {
65 // if (node instanceof TreeObject) {
66 // return ((TreeObject) node).getParent();
67 // }
68 return null;
69 }
70
71 @Override
72 public boolean hasChildren(Object parent) {
73 if (parent instanceof TreeParent && ((TreeParent) parent).isLoaded()) {
74 return ((TreeParent) parent).hasChildren();
75 } else if (parent instanceof AgentNode) {
76 return true;
77 } else if (parent instanceof ExecutionModuleNode) {
78 return true;
79 }
80 return false;
81 }
82
83 @Override
84 public void inputChanged(Viewer v, Object oldInput, Object newInput) {
85 }
86
87 @Override
88 public void dispose() {
89 }
90
91 @Override
92 public Object[] getElements(Object parent) {
93 return getChildren(parent);
94 }
95
96 public void setSlcAgent(SlcAgent slcAgent) {
97 this.slcAgent = slcAgent;
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
139 // SortedMap<String, FlowNode> directChildren = new TreeMap<String,
140 // FlowNode>();
141
142 flowDescriptors = new HashMap<String, ExecutionFlowDescriptor>();
143 for (ExecutionFlowDescriptor fd : descriptor.getExecutionFlows()) {
144 if (log.isTraceEnabled())
145 log
146 .trace("path=" + fd.getPath() + ", name="
147 + fd.getName());
148
149 String path = fd.getPath();
150 String name = fd.getName();
151
152 if (path == null || path.trim().equals("")) {
153 // directChildren.put(name, new FlowNode(name, this));
154 addChild(new FlowNode(name, this));
155 } else {
156 FolderNode folderNode = mkdirs(this, path, folderNodes);
157 folderNode.addChild(new FlowNode(name, this));
158 }
159
160 flowDescriptors.put(fd.getName(), fd);
161 }
162 // TODO: make it readonly
163 }
164
165 protected FolderNode mkdirs(TreeParent root, String path,
166 SortedMap<String, FolderNode> folderNodes) {
167 if (path.charAt(path.length() - 1) == '/')
168 path = path.substring(0, path.length() - 1);
169
170 if (folderNodes.containsKey(path))
171 return folderNodes.get(path);
172
173 int lastIndx = path.lastIndexOf('/');
174 String folderName = path.substring(lastIndx + 1);
175 String parentPath = path.substring(0, lastIndx);
176
177 TreeParent parent;
178 if (parentPath.equals(""))
179 parent = root;
180 else
181 parent = mkdirs(root, parentPath, folderNodes);
182 FolderNode newFolder = new FolderNode(folderName);
183 parent.addChild(newFolder);
184 folderNodes.put(path, newFolder);
185 return newFolder;
186 }
187
188 public Map<String, ExecutionFlowDescriptor> getFlowDescriptors() {
189 return flowDescriptors;
190 }
191
192 }
193
194 public class FlowNode extends TreeObject {
195 private final String flowName;
196 private final ExecutionModuleNode executionModuleNode;
197
198 public FlowNode(String flowName, ExecutionModuleNode executionModuleNode) {
199 super(flowName);
200 this.flowName = flowName;
201 this.executionModuleNode = executionModuleNode;
202 }
203
204 public String getFlowName() {
205 return flowName;
206 }
207
208 public ExecutionModuleNode getExecutionModuleNode() {
209 return executionModuleNode;
210 }
211
212 }
213
214 public class FolderNode extends TreeParent {
215 public FolderNode(String name) {
216 super(name);
217 }
218
219 }
220 }