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
.apache
.commons
.logging
.Log
;
12 import org
.apache
.commons
.logging
.LogFactory
;
13 import org
.argeo
.ArgeoException
;
14 import org
.argeo
.eclipse
.ui
.AbstractTreeContentProvider
;
16 /** Canonic implementation of tree content provider manipulating JCR nodes. */
17 public abstract class AbstractNodeContentProvider
extends
18 AbstractTreeContentProvider
{
19 private final static Log log
= LogFactory
20 .getLog(AbstractNodeContentProvider
.class);
22 private Session session
;
24 public AbstractNodeContentProvider(Session session
) {
25 this.session
= session
;
29 * Whether this path is a base path (and thus has no parent). By default it
30 * returns true if path is '/' (root node)
32 protected Boolean
isBasePath(String path
) {
34 return path
.equals("/");
38 public Object
[] getChildren(Object element
) {
40 if (element
instanceof Node
) {
42 Node node
= (Node
) element
;
43 children
= getChildren(node
);
44 } catch (RepositoryException e
) {
45 throw new ArgeoException("Cannot get children of " + element
, e
);
47 } else if (element
instanceof WrappedNode
) {
48 WrappedNode wrappedNode
= (WrappedNode
) element
;
50 children
= getChildren(wrappedNode
.getNode());
51 } catch (RepositoryException e
) {
52 throw new ArgeoException("Cannot get children of "
55 } else if (element
instanceof NodesWrapper
) {
56 NodesWrapper node
= (NodesWrapper
) element
;
57 children
= node
.getChildren();
59 children
= super.getChildren(element
);
62 children
= sort(element
, children
);
66 /** Do not sort by default. To be overidden to provide custom sort. */
67 protected Object
[] sort(Object parent
, Object
[] children
) {
72 * To be overridden in order to filter out some nodes. Does nothing by
73 * default. The provided list is a temporary one and can thus be modified
74 * directly . (e.g. via an iterator)
76 protected List
<Node
> filterChildren(List
<Node
> children
)
77 throws RepositoryException
{
81 protected Object
[] getChildren(Node node
) throws RepositoryException
{
82 List
<Node
> nodes
= new ArrayList
<Node
>();
83 for (NodeIterator nit
= node
.getNodes(); nit
.hasNext();)
84 nodes
.add(nit
.nextNode());
85 nodes
= filterChildren(nodes
);
86 return nodes
.toArray();
90 public Object
getParent(Object element
) {
91 if (element
instanceof Node
) {
92 Node node
= (Node
) element
;
94 String path
= node
.getPath();
98 return node
.getParent();
99 } catch (RepositoryException e
) {
100 log
.warn("Cannot get parent of " + element
+ ": " + e
);
103 } else if (element
instanceof WrappedNode
) {
104 WrappedNode wrappedNode
= (WrappedNode
) element
;
105 return wrappedNode
.getParent();
106 } else if (element
instanceof NodesWrapper
) {
107 NodesWrapper nodesWrapper
= (NodesWrapper
) element
;
108 return this.getParent(nodesWrapper
.getNode());
110 return super.getParent(element
);
114 public boolean hasChildren(Object element
) {
116 if (element
instanceof Node
) {
117 Node node
= (Node
) element
;
118 return node
.hasNodes();
119 } else if (element
instanceof WrappedNode
) {
120 WrappedNode wrappedNode
= (WrappedNode
) element
;
121 return wrappedNode
.getNode().hasNodes();
122 } else if (element
instanceof NodesWrapper
) {
123 NodesWrapper nodesWrapper
= (NodesWrapper
) element
;
124 return nodesWrapper
.hasChildren();
127 } catch (RepositoryException e
) {
128 throw new ArgeoException("Cannot check whether " + element
129 + " has children", e
);
131 return super.hasChildren(element
);
134 public Session
getSession() {