1 package org
.argeo
.eclipse
.ui
.jcr
;
3 import java
.util
.ArrayList
;
7 import javax
.jcr
.NodeIterator
;
8 import javax
.jcr
.RepositoryException
;
9 import javax
.jcr
.Session
;
11 import org
.argeo
.ArgeoException
;
12 import org
.argeo
.eclipse
.ui
.AbstractTreeContentProvider
;
14 /** Canonic implementation of tree content provider manipulating JCR nodes. */
15 public abstract class AbstractNodeContentProvider
extends
16 AbstractTreeContentProvider
{
17 private Session session
;
19 public AbstractNodeContentProvider(Session session
) {
20 this.session
= session
;
24 * Whether this path is a base path (and thus has no parent). By default it
25 * returns true if path is '/' (root node)
27 protected Boolean
isBasePath(String path
) {
29 return path
.equals("/");
33 public Object
[] getChildren(Object element
) {
35 if (element
instanceof Node
) {
37 Node node
= (Node
) element
;
38 children
= getChildren(node
);
39 } catch (RepositoryException e
) {
40 throw new ArgeoException("Cannot get children of " + element
, e
);
42 } else if (element
instanceof WrappedNode
) {
43 WrappedNode wrappedNode
= (WrappedNode
) element
;
45 children
= getChildren(wrappedNode
.getNode());
46 } catch (RepositoryException e
) {
47 throw new ArgeoException("Cannot get children of "
50 } else if (element
instanceof NodesWrapper
) {
51 NodesWrapper node
= (NodesWrapper
) element
;
52 children
= node
.getChildren();
54 children
= super.getChildren(element
);
57 children
= sort(element
, children
);
61 /** Do not sort by default. To be overidden to provide custom sort. */
62 protected Object
[] sort(Object parent
, Object
[] children
) {
67 * To be overridden in order to filter out some nodes. Does nothing by
68 * default. The provided list is a temporary one and can thus be modified
69 * directly . (e.g. via an iterator)
71 protected List
<Node
> filterChildren(List
<Node
> children
)
72 throws RepositoryException
{
76 protected Object
[] getChildren(Node node
) throws RepositoryException
{
77 List
<Node
> nodes
= new ArrayList
<Node
>();
78 for (NodeIterator nit
= node
.getNodes(); nit
.hasNext();)
79 nodes
.add(nit
.nextNode());
80 nodes
= filterChildren(nodes
);
81 return nodes
.toArray();
85 public Object
getParent(Object element
) {
86 if (element
instanceof Node
) {
87 Node node
= (Node
) element
;
89 String path
= node
.getPath();
93 return node
.getParent();
94 } catch (RepositoryException e
) {
95 throw new ArgeoException("Cannot get parent of " + element
, e
);
97 } else if (element
instanceof WrappedNode
) {
98 WrappedNode wrappedNode
= (WrappedNode
) element
;
99 return wrappedNode
.getParent();
100 } else if (element
instanceof NodesWrapper
) {
101 NodesWrapper nodesWrapper
= (NodesWrapper
) element
;
102 return this.getParent(nodesWrapper
.getNode());
104 return super.getParent(element
);
108 public boolean hasChildren(Object element
) {
110 if (element
instanceof Node
) {
111 Node node
= (Node
) element
;
112 return node
.hasNodes();
113 } else if (element
instanceof WrappedNode
) {
114 WrappedNode wrappedNode
= (WrappedNode
) element
;
115 return wrappedNode
.getNode().hasNodes();
116 } else if (element
instanceof NodesWrapper
) {
117 NodesWrapper nodesWrapper
= (NodesWrapper
) element
;
118 return nodesWrapper
.hasChildren();
121 } catch (RepositoryException e
) {
122 throw new ArgeoException("Cannot check whether " + element
123 + " has children", e
);
125 return super.hasChildren(element
);
128 public Session
getSession() {