2 * Copyright (C) 2007-2012 Argeo GmbH
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org
.argeo
.eclipse
.ui
.jcr
;
18 import java
.util
.ArrayList
;
19 import java
.util
.List
;
21 import javax
.jcr
.Node
;
22 import javax
.jcr
.NodeIterator
;
23 import javax
.jcr
.RepositoryException
;
24 import javax
.jcr
.Session
;
26 import org
.apache
.commons
.logging
.Log
;
27 import org
.apache
.commons
.logging
.LogFactory
;
28 import org
.argeo
.ArgeoException
;
29 import org
.argeo
.eclipse
.ui
.AbstractTreeContentProvider
;
31 /** Canonic implementation of tree content provider manipulating JCR nodes. */
32 public abstract class AbstractNodeContentProvider
extends
33 AbstractTreeContentProvider
{
34 private final static Log log
= LogFactory
35 .getLog(AbstractNodeContentProvider
.class);
37 private Session session
;
39 public AbstractNodeContentProvider(Session session
) {
40 this.session
= session
;
44 * Whether this path is a base path (and thus has no parent). By default it
45 * returns true if path is '/' (root node)
47 protected Boolean
isBasePath(String path
) {
49 return path
.equals("/");
53 public Object
[] getChildren(Object element
) {
55 if (element
instanceof Node
) {
57 Node node
= (Node
) element
;
58 children
= getChildren(node
);
59 } catch (RepositoryException e
) {
60 throw new ArgeoException("Cannot get children of " + element
, e
);
62 } else if (element
instanceof WrappedNode
) {
63 WrappedNode wrappedNode
= (WrappedNode
) element
;
65 children
= getChildren(wrappedNode
.getNode());
66 } catch (RepositoryException e
) {
67 throw new ArgeoException("Cannot get children of "
70 } else if (element
instanceof NodesWrapper
) {
71 NodesWrapper node
= (NodesWrapper
) element
;
72 children
= node
.getChildren();
74 children
= super.getChildren(element
);
77 children
= sort(element
, children
);
81 /** Do not sort by default. To be overidden to provide custom sort. */
82 protected Object
[] sort(Object parent
, Object
[] children
) {
87 * To be overridden in order to filter out some nodes. Does nothing by
88 * default. The provided list is a temporary one and can thus be modified
89 * directly . (e.g. via an iterator)
91 protected List
<Node
> filterChildren(List
<Node
> children
)
92 throws RepositoryException
{
96 protected Object
[] getChildren(Node node
) throws RepositoryException
{
97 List
<Node
> nodes
= new ArrayList
<Node
>();
98 for (NodeIterator nit
= node
.getNodes(); nit
.hasNext();)
99 nodes
.add(nit
.nextNode());
100 nodes
= filterChildren(nodes
);
101 return nodes
.toArray();
105 public Object
getParent(Object element
) {
106 if (element
instanceof Node
) {
107 Node node
= (Node
) element
;
109 String path
= node
.getPath();
110 if (isBasePath(path
))
113 return node
.getParent();
114 } catch (RepositoryException e
) {
115 log
.warn("Cannot get parent of " + element
+ ": " + e
);
118 } else if (element
instanceof WrappedNode
) {
119 WrappedNode wrappedNode
= (WrappedNode
) element
;
120 return wrappedNode
.getParent();
121 } else if (element
instanceof NodesWrapper
) {
122 NodesWrapper nodesWrapper
= (NodesWrapper
) element
;
123 return this.getParent(nodesWrapper
.getNode());
125 return super.getParent(element
);
129 public boolean hasChildren(Object element
) {
131 if (element
instanceof Node
) {
132 Node node
= (Node
) element
;
133 return node
.hasNodes();
134 } else if (element
instanceof WrappedNode
) {
135 WrappedNode wrappedNode
= (WrappedNode
) element
;
136 return wrappedNode
.getNode().hasNodes();
137 } else if (element
instanceof NodesWrapper
) {
138 NodesWrapper nodesWrapper
= (NodesWrapper
) element
;
139 return nodesWrapper
.hasChildren();
142 } catch (RepositoryException e
) {
143 throw new ArgeoException("Cannot check whether " + element
144 + " has children", e
);
146 return super.hasChildren(element
);
149 public Session
getSession() {