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 /** Canonical implementation of tree content provider manipulating JCR nodes. */
32 public abstract class AbstractNodeContentProvider
extends
33 AbstractTreeContentProvider
{
34 private static final long serialVersionUID
= -4905836490027272569L;
36 private final static Log log
= LogFactory
37 .getLog(AbstractNodeContentProvider
.class);
39 private Session session
;
41 public AbstractNodeContentProvider(Session session
) {
42 this.session
= session
;
46 * Whether this path is a base path (and thus has no parent). By default it
47 * returns true if path is '/' (root node)
49 protected Boolean
isBasePath(String path
) {
51 return path
.equals("/");
55 public Object
[] getChildren(Object element
) {
57 if (element
instanceof Node
) {
59 Node node
= (Node
) element
;
60 children
= getChildren(node
);
61 } catch (RepositoryException e
) {
62 throw new ArgeoException("Cannot get children of " + element
, e
);
64 } else if (element
instanceof WrappedNode
) {
65 WrappedNode wrappedNode
= (WrappedNode
) element
;
67 children
= getChildren(wrappedNode
.getNode());
68 } catch (RepositoryException e
) {
69 throw new ArgeoException("Cannot get children of "
72 } else if (element
instanceof NodesWrapper
) {
73 NodesWrapper node
= (NodesWrapper
) element
;
74 children
= node
.getChildren();
76 children
= super.getChildren(element
);
79 children
= sort(element
, children
);
83 /** Do not sort by default. To be overidden to provide custom sort. */
84 protected Object
[] sort(Object parent
, Object
[] children
) {
89 * To be overridden in order to filter out some nodes. Does nothing by
90 * default. The provided list is a temporary one and can thus be modified
91 * directly . (e.g. via an iterator)
93 protected List
<Node
> filterChildren(List
<Node
> children
)
94 throws RepositoryException
{
98 protected Object
[] getChildren(Node node
) throws RepositoryException
{
99 List
<Node
> nodes
= new ArrayList
<Node
>();
100 for (NodeIterator nit
= node
.getNodes(); nit
.hasNext();)
101 nodes
.add(nit
.nextNode());
102 nodes
= filterChildren(nodes
);
103 return nodes
.toArray();
107 public Object
getParent(Object element
) {
108 if (element
instanceof Node
) {
109 Node node
= (Node
) element
;
111 String path
= node
.getPath();
112 if (isBasePath(path
))
115 return node
.getParent();
116 } catch (RepositoryException e
) {
117 log
.warn("Cannot get parent of " + element
+ ": " + e
);
120 } else if (element
instanceof WrappedNode
) {
121 WrappedNode wrappedNode
= (WrappedNode
) element
;
122 return wrappedNode
.getParent();
123 } else if (element
instanceof NodesWrapper
) {
124 NodesWrapper nodesWrapper
= (NodesWrapper
) element
;
125 return this.getParent(nodesWrapper
.getNode());
127 return super.getParent(element
);
131 public boolean hasChildren(Object element
) {
133 if (element
instanceof Node
) {
134 Node node
= (Node
) element
;
135 return node
.hasNodes();
136 } else if (element
instanceof WrappedNode
) {
137 WrappedNode wrappedNode
= (WrappedNode
) element
;
138 return wrappedNode
.getNode().hasNodes();
139 } else if (element
instanceof NodesWrapper
) {
140 NodesWrapper nodesWrapper
= (NodesWrapper
) element
;
141 return nodesWrapper
.hasChildren();
144 } catch (RepositoryException e
) {
145 throw new ArgeoException("Cannot check whether " + element
146 + " has children", e
);
148 return super.hasChildren(element
);
151 public Session
getSession() {