1 package org
.argeo
.slc
.client
.ui
.views
;
3 import java
.util
.ArrayList
;
4 import java
.util
.Arrays
;
5 import java
.util
.Comparator
;
6 import java
.util
.Iterator
;
8 import java
.util
.SortedSet
;
9 import java
.util
.TreeSet
;
11 import javax
.jcr
.Node
;
12 import javax
.jcr
.NodeIterator
;
13 import javax
.jcr
.Property
;
14 import javax
.jcr
.RepositoryException
;
15 import javax
.jcr
.Session
;
16 import javax
.jcr
.nodetype
.NodeType
;
17 import javax
.jcr
.observation
.Event
;
18 import javax
.jcr
.query
.Query
;
19 import javax
.jcr
.query
.QueryManager
;
21 import org
.apache
.commons
.logging
.Log
;
22 import org
.apache
.commons
.logging
.LogFactory
;
23 import org
.argeo
.ArgeoException
;
24 import org
.argeo
.eclipse
.ui
.jcr
.AsyncUiEventListener
;
25 import org
.argeo
.eclipse
.ui
.jcr
.DefaultNodeLabelProvider
;
26 import org
.argeo
.eclipse
.ui
.jcr
.NodeElementComparer
;
27 import org
.argeo
.eclipse
.ui
.jcr
.SimpleNodeContentProvider
;
28 import org
.argeo
.eclipse
.ui
.specific
.EclipseUiSpecificUtils
;
29 import org
.argeo
.slc
.BasicNameVersion
;
30 import org
.argeo
.slc
.NameVersion
;
31 import org
.argeo
.slc
.SlcException
;
32 import org
.argeo
.slc
.client
.ui
.SlcImages
;
33 import org
.argeo
.slc
.client
.ui
.editors
.ProcessEditor
;
34 import org
.argeo
.slc
.client
.ui
.editors
.ProcessEditorInput
;
35 import org
.argeo
.slc
.execution
.ExecutionModulesManager
;
36 import org
.argeo
.slc
.jcr
.SlcJcrConstants
;
37 import org
.argeo
.slc
.jcr
.SlcNames
;
38 import org
.argeo
.slc
.jcr
.SlcTypes
;
39 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
40 import org
.eclipse
.core
.runtime
.IStatus
;
41 import org
.eclipse
.core
.runtime
.Status
;
42 import org
.eclipse
.core
.runtime
.jobs
.Job
;
43 import org
.eclipse
.jface
.viewers
.DoubleClickEvent
;
44 import org
.eclipse
.jface
.viewers
.IDoubleClickListener
;
45 import org
.eclipse
.jface
.viewers
.IStructuredSelection
;
46 import org
.eclipse
.jface
.viewers
.ITableLabelProvider
;
47 import org
.eclipse
.jface
.viewers
.TreeViewer
;
48 import org
.eclipse
.swt
.SWT
;
49 import org
.eclipse
.swt
.dnd
.DND
;
50 import org
.eclipse
.swt
.dnd
.DragSourceAdapter
;
51 import org
.eclipse
.swt
.dnd
.DragSourceEvent
;
52 import org
.eclipse
.swt
.dnd
.TextTransfer
;
53 import org
.eclipse
.swt
.dnd
.Transfer
;
54 import org
.eclipse
.swt
.graphics
.Image
;
55 import org
.eclipse
.swt
.widgets
.Composite
;
56 import org
.eclipse
.swt
.widgets
.Display
;
57 import org
.eclipse
.ui
.IWorkbenchPage
;
58 import org
.eclipse
.ui
.PlatformUI
;
59 import org
.eclipse
.ui
.part
.ViewPart
;
61 /** JCR based view of the execution modules. */
62 public class JcrExecutionModulesView
extends ViewPart
implements SlcTypes
,
64 private final static Log log
= LogFactory
65 .getLog(JcrExecutionModulesView
.class);
67 public static final String ID
= "org.argeo.slc.client.ui.jcrExecutionModulesView";
69 private TreeViewer viewer
;
71 private Session session
;
73 private ExecutionModulesManager modulesManager
;
75 public void createPartControl(Composite parent
) {
76 viewer
= new TreeViewer(parent
, SWT
.MULTI
| SWT
.H_SCROLL
| SWT
.V_SCROLL
);
77 EclipseUiSpecificUtils
.enableToolTipSupport(viewer
);
79 ViewContentProvider contentProvider
= new ViewContentProvider(session
);
80 viewer
.setContentProvider(contentProvider
);
81 viewer
.setComparer(new NodeElementComparer());
82 final ViewLabelProvider viewLabelProvider
= new ViewLabelProvider();
83 viewer
.setLabelProvider(viewLabelProvider
);
84 viewer
.setInput(getViewSite());
85 viewer
.addDoubleClickListener(new ViewDoubleClickListener());
86 getViewSite().setSelectionProvider(viewer
);
88 Transfer
[] tt
= new Transfer
[] { TextTransfer
.getInstance() };
89 // Transfer[] tt = new Transfer[] { EditorInputTransfer.getInstance() };
90 int operations
= DND
.DROP_COPY
| DND
.DROP_MOVE
;
91 viewer
.addDragSupport(operations
, tt
, new ViewDragListener());
94 String
[] nodeTypes
= { SlcTypes
.SLC_AGENT
,
95 SlcTypes
.SLC_AGENT_FACTORY
, SlcTypes
.SLC_EXECUTION_MODULE
};
96 session
.getWorkspace()
97 .getObservationManager()
99 new VmAgentObserver(viewer
.getTree().getDisplay()),
100 Event
.NODE_ADDED
| Event
.NODE_REMOVED
102 SlcJcrConstants
.VM_AGENT_FACTORY_PATH
, true, null,
104 } catch (RepositoryException e
) {
105 throw new SlcException("Cannot add observer", e
);
109 public void setFocus() {
110 viewer
.getControl().setFocus();
113 public TreeViewer
getViewer() {
117 public void refreshView() {
118 viewer
.setInput(getViewSite());
121 class ViewContentProvider
extends SimpleNodeContentProvider
{
123 public ViewContentProvider(Session session
) {
125 new String
[] { SlcJcrConstants
.VM_AGENT_FACTORY_PATH
});
129 // protected Object[] getChildren(Node node) throws RepositoryException
131 // if (node.isNodeType(SlcTypes.SLC_AGENT_FACTORY)) {
132 // List<AgentNodesWrapper> wrappers = new
133 // ArrayList<AgentNodesWrapper>();
134 // for (NodeIterator nit = node.getNodes(); nit.hasNext();) {
135 // wrappers.add(new AgentNodesWrapper(nit.nextNode()));
137 // return wrappers.toArray();
139 // return super.getChildren(node);
143 protected Object
[] sort(Object parent
, Object
[] children
) {
144 Object
[] sorted
= new Object
[children
.length
];
145 System
.arraycopy(children
, 0, sorted
, 0, children
.length
);
146 Arrays
.sort(sorted
, new ViewComparator());
151 protected List
<Node
> filterChildren(List
<Node
> children
)
152 throws RepositoryException
{
153 for (Iterator
<Node
> it
= children
.iterator(); it
.hasNext();) {
154 Node node
= it
.next();
155 // execution spec definitions
156 if (node
.getName().equals(SLC_EXECUTION_SPECS
))
159 else if (node
.getParent().isNodeType(
160 SlcTypes
.SLC_EXECUTION_FLOW
))
163 return super.filterChildren(children
);
167 public boolean hasChildren(Object element
) {
168 if (element
instanceof Node
) {
169 Node node
= (Node
) element
;
171 if (node
.isNodeType(SlcTypes
.SLC_EXECUTION_FLOW
))
173 } catch (RepositoryException e
) {
174 throw new SlcException("Cannot check has children", e
);
177 return super.hasChildren(element
);
181 static class ViewComparator
implements Comparator
<Object
> {
183 public int compare(Object o1
, Object o2
) {
185 if (o1
instanceof Node
&& o2
instanceof Node
) {
186 Node node1
= (Node
) o1
;
187 Node node2
= (Node
) o2
;
189 if (node1
.getName().equals(SLC_EXECUTION_SPECS
))
191 if (node2
.getName().equals(SLC_EXECUTION_SPECS
))
194 if (node1
.isNodeType(SLC_EXECUTION_FLOW
)
195 && node2
.isNodeType(SLC_EXECUTION_FLOW
)) {
196 return node1
.getName().compareTo(node2
.getName());
197 } else if (node1
.isNodeType(SLC_EXECUTION_FLOW
)
198 && !node2
.isNodeType(SLC_EXECUTION_FLOW
)) {
200 } else if (!node1
.isNodeType(SLC_EXECUTION_FLOW
)
201 && node2
.isNodeType(SLC_EXECUTION_FLOW
)) {
205 return node1
.getName().compareTo(node2
.getName());
208 } catch (RepositoryException e
) {
209 throw new ArgeoException("Cannot compare " + o1
+ " and " + o2
,
217 // /** Wraps the execution modules of an agent. */
218 // static class AgentNodesWrapper extends NodesWrapper {
220 // public AgentNodesWrapper(Node node) {
224 // protected List<WrappedNode> getWrappedNodes()
225 // throws RepositoryException {
226 // List<WrappedNode> children = new ArrayList<WrappedNode>();
227 // Node executionModules = getNode();
228 // for (NodeIterator nit = executionModules.getNodes(); nit.hasNext();) {
229 // for (NodeIterator nitVersions = nit.nextNode().getNodes(); nitVersions
231 // children.add(new WrappedNode(this, nitVersions.nextNode()));
239 class VmAgentObserver
extends AsyncUiEventListener
{
241 public VmAgentObserver(Display display
) {
245 protected void onEventInUiThread(List
<Event
> events
) {
246 for (Event event
: events
) {
248 String path
= event
.getPath();
250 if (session
.itemExists(path
)) {
251 Node parentNode
= session
.getNode(path
);// .getParent();
252 if (log
.isTraceEnabled())
253 log
.trace("Refresh " + parentNode
+ " after event "
255 viewer
.refresh(parentNode
);
257 } catch (RepositoryException e
) {
258 log
.warn("Cannot process event " + event
+ ": " + e
);
263 // Node vmAgentNode = session
264 // .getNode(SlcJcrConstants.VM_AGENT_FACTORY_PATH);
265 // viewer.refresh(vmAgentNode);
266 // } catch (RepositoryException e) {
267 // log.warn("Cannot process event : " + e);
269 // TODO: optimize based on event
274 class ViewLabelProvider
extends DefaultNodeLabelProvider
implements
275 ITableLabelProvider
{
278 protected String
getText(Node node
) throws RepositoryException
{
279 if (node
.isNodeType(NodeType
.MIX_TITLE
)
280 && node
.hasProperty(Property
.JCR_TITLE
))
281 return node
.getProperty(Property
.JCR_TITLE
).getString();
282 else if (node
.getName().equals(SLC_EXECUTION_SPECS
))
283 return "Execution Specifications";
284 else if (node
.getPath().equals(
285 SlcJcrConstants
.VM_AGENT_FACTORY_PATH
))
286 return "Internal Agents";
287 return super.getText(node
);
291 public Image
getImage(Node node
) throws RepositoryException
{
292 // we try to optimize a bit by putting deeper nodes first
293 if (node
.getParent().isNodeType(
294 SlcTypes
.SLC_EXECUTION_SPEC_ATTRIBUTE
))
295 return SlcImages
.CHOICES
;
296 else if (node
.isNodeType(SlcTypes
.SLC_EXECUTION_SPEC_ATTRIBUTE
))
297 return SlcImages
.EXECUTION_SPEC_ATTRIBUTE
;
298 else if (node
.isNodeType(SlcTypes
.SLC_EXECUTION_SPEC
))
299 return SlcImages
.EXECUTION_SPEC
;
300 else if (node
.getName().equals(SLC_EXECUTION_SPECS
))
301 return SlcImages
.EXECUTION_SPECS
;
302 else if (node
.isNodeType(SlcTypes
.SLC_EXECUTION_FLOW
))
303 return SlcImages
.FLOW
;
304 else if (node
.isNodeType(SlcTypes
.SLC_MODULE
)) {
305 if (node
.getProperty(SLC_STARTED
).getBoolean())
306 return SlcImages
.MODULE
;
308 return SlcImages
.MODULE_STOPPED
;
309 } else if (node
.isNodeType(SlcTypes
.SLC_AGENT
))
310 return SlcImages
.AGENT
;
311 else if (node
.isNodeType(SlcTypes
.SLC_AGENT_FACTORY
))
312 return SlcImages
.AGENT_FACTORY
;
314 return SlcImages
.FOLDER
;
317 public String
getToolTipText(Node node
) throws RepositoryException
{
318 if (node
.isNodeType(NodeType
.MIX_TITLE
)
319 && node
.hasProperty(Property
.JCR_DESCRIPTION
))
320 return node
.getProperty(Property
.JCR_DESCRIPTION
).getString();
321 return super.getToolTipText(node
);
324 public String
getColumnText(Object obj
, int index
) {
328 public Image
getColumnImage(Object obj
, int index
) {
329 return getImage(obj
);
334 class ViewDoubleClickListener
implements IDoubleClickListener
{
335 public void doubleClick(DoubleClickEvent evt
) {
336 Object obj
= ((IStructuredSelection
) evt
.getSelection())
339 if (obj
instanceof Node
) {
340 Node node
= (Node
) obj
;
341 if (node
.isNodeType(SLC_EXECUTION_MODULE
)) {
342 String name
= node
.getProperty(SLC_NAME
).getString();
343 String version
= node
.getProperty(SLC_VERSION
)
345 final NameVersion nameVersion
= new BasicNameVersion(
347 Boolean started
= node
.getProperty(SLC_STARTED
)
352 job
= new Job("Stop " + nameVersion
) {
353 protected IStatus
run(IProgressMonitor monitor
) {
354 monitor
.beginTask("Stop " + nameVersion
, 1);
355 modulesManager
.stop(nameVersion
);
357 return Status
.OK_STATUS
;
361 job
= new Job("Start " + nameVersion
) {
362 protected IStatus
run(IProgressMonitor monitor
) {
363 monitor
.beginTask("Start " + nameVersion
, 1);
364 modulesManager
.start(nameVersion
);
366 return Status
.OK_STATUS
;
373 String path
= node
.getPath();
374 // TODO factorize with editor
375 QueryManager qm
= node
.getSession().getWorkspace()
377 String statement
= "SELECT * FROM ["
378 + SlcTypes
.SLC_EXECUTION_FLOW
379 + "] WHERE ISDESCENDANTNODE(['" + path
380 + "']) OR ISSAMENODE(['" + path
+ "'])";
381 // log.debug(statement);
382 Query query
= qm
.createQuery(statement
, Query
.JCR_SQL2
);
385 SortedSet
<String
> paths
= new TreeSet
<String
>();
386 for (NodeIterator nit
= query
.execute().getNodes(); nit
388 paths
.add(nit
.nextNode().getPath());
391 // List<String> paths = new ArrayList<String>();
392 // paths.add(node.getPath());
393 IWorkbenchPage activePage
= PlatformUI
.getWorkbench()
394 .getActiveWorkbenchWindow().getActivePage();
395 activePage
.openEditor(new ProcessEditorInput(
396 new ArrayList
<String
>(paths
), true),
400 } catch (Exception e
) {
401 throw new SlcException("Cannot open " + obj
, e
);
407 class ViewDragListener
extends DragSourceAdapter
{
408 public void dragSetData(DragSourceEvent event
) {
409 IStructuredSelection selection
= (IStructuredSelection
) viewer
411 if (selection
.getFirstElement() instanceof Node
) {
412 Node node
= (Node
) selection
.getFirstElement();
414 // if (node.isNodeType(SLC_EXECUTION_FLOW)) {
415 // if (EditorInputTransfer.getInstance().isSupportedType(
416 // event.dataType)) {
417 // ProcessEditorInput pei = new ProcessEditorInput(
419 // EditorInputData eid = EditorInputTransfer
420 // .createEditorInputData(ProcessEditor.ID,
422 // event.data = new EditorInputTransfer.EditorInputData[] { eid
427 // } catch (RepositoryException e1) {
428 // throw new SlcException("Cannot drag " + node, e1);
431 if (TextTransfer
.getInstance().isSupportedType(event
.dataType
)) {
433 event
.data
= node
.getPath();
434 } catch (RepositoryException e1
) {
435 // TODO Auto-generated catch block
436 e1
.printStackTrace();
443 public void setSession(Session session
) {
444 this.session
= session
;
447 public void setModulesManager(ExecutionModulesManager modulesManager
) {
448 this.modulesManager
= modulesManager
;