]> git.argeo.org Git - lgpl/argeo-commons.git/blob - jcr/AbstractNodeContentProvider.java
Prepare next development cycle
[lgpl/argeo-commons.git] / jcr / AbstractNodeContentProvider.java
1 /*
2 * Copyright (C) 2007-2012 Argeo GmbH
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16 package org.argeo.eclipse.ui.jcr;
17
18 import java.util.ArrayList;
19 import java.util.List;
20
21 import javax.jcr.Node;
22 import javax.jcr.NodeIterator;
23 import javax.jcr.RepositoryException;
24 import javax.jcr.Session;
25
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;
30
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);
36
37 private Session session;
38
39 public AbstractNodeContentProvider(Session session) {
40 this.session = session;
41 }
42
43 /**
44 * Whether this path is a base path (and thus has no parent). By default it
45 * returns true if path is '/' (root node)
46 */
47 protected Boolean isBasePath(String path) {
48 // root node
49 return path.equals("/");
50 }
51
52 @Override
53 public Object[] getChildren(Object element) {
54 Object[] children;
55 if (element instanceof Node) {
56 try {
57 Node node = (Node) element;
58 children = getChildren(node);
59 } catch (RepositoryException e) {
60 throw new ArgeoException("Cannot get children of " + element, e);
61 }
62 } else if (element instanceof WrappedNode) {
63 WrappedNode wrappedNode = (WrappedNode) element;
64 try {
65 children = getChildren(wrappedNode.getNode());
66 } catch (RepositoryException e) {
67 throw new ArgeoException("Cannot get children of "
68 + wrappedNode, e);
69 }
70 } else if (element instanceof NodesWrapper) {
71 NodesWrapper node = (NodesWrapper) element;
72 children = node.getChildren();
73 } else {
74 children = super.getChildren(element);
75 }
76
77 children = sort(element, children);
78 return children;
79 }
80
81 /** Do not sort by default. To be overidden to provide custom sort. */
82 protected Object[] sort(Object parent, Object[] children) {
83 return children;
84 }
85
86 /**
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)
90 */
91 protected List<Node> filterChildren(List<Node> children)
92 throws RepositoryException {
93 return children;
94 }
95
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();
102 }
103
104 @Override
105 public Object getParent(Object element) {
106 if (element instanceof Node) {
107 Node node = (Node) element;
108 try {
109 String path = node.getPath();
110 if (isBasePath(path))
111 return null;
112 else
113 return node.getParent();
114 } catch (RepositoryException e) {
115 log.warn("Cannot get parent of " + element + ": " + e);
116 return null;
117 }
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());
124 }
125 return super.getParent(element);
126 }
127
128 @Override
129 public boolean hasChildren(Object element) {
130 try {
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();
140 }
141
142 } catch (RepositoryException e) {
143 throw new ArgeoException("Cannot check whether " + element
144 + " has children", e);
145 }
146 return super.hasChildren(element);
147 }
148
149 public Session getSession() {
150 return session;
151 }
152 }