]> git.argeo.org Git - lgpl/argeo-commons.git/blob - eclipse/runtime/org.argeo.eclipse.ui.jcr/src/main/java/org/argeo/eclipse/ui/jcr/AbstractNodeContentProvider.java
Add filtering of children nodes
[lgpl/argeo-commons.git] / eclipse / runtime / org.argeo.eclipse.ui.jcr / src / main / java / org / argeo / eclipse / ui / jcr / AbstractNodeContentProvider.java
1 package org.argeo.eclipse.ui.jcr;
2
3 import java.util.ArrayList;
4 import java.util.List;
5
6 import javax.jcr.Node;
7 import javax.jcr.NodeIterator;
8 import javax.jcr.RepositoryException;
9 import javax.jcr.Session;
10
11 import org.argeo.ArgeoException;
12 import org.argeo.eclipse.ui.AbstractTreeContentProvider;
13
14 /** Canonic implementation of tree content provider manipulating JCR nodes. */
15 public abstract class AbstractNodeContentProvider extends
16 AbstractTreeContentProvider {
17 private Session session;
18
19 public AbstractNodeContentProvider(Session session) {
20 this.session = session;
21 }
22
23 /**
24 * Whether this path is a base path (and thus has no parent). By default it
25 * returns true if path is '/' (root node)
26 */
27 protected Boolean isBasePath(String path) {
28 // root node
29 return path.equals("/");
30 }
31
32 @Override
33 public Object[] getChildren(Object element) {
34 Object[] children;
35 if (element instanceof Node) {
36 try {
37 Node node = (Node) element;
38 children = getChildren(node);
39 } catch (RepositoryException e) {
40 throw new ArgeoException("Cannot get children of " + element, e);
41 }
42 } else if (element instanceof WrappedNode) {
43 WrappedNode wrappedNode = (WrappedNode) element;
44 try {
45 children = getChildren(wrappedNode.getNode());
46 } catch (RepositoryException e) {
47 throw new ArgeoException("Cannot get children of "
48 + wrappedNode, e);
49 }
50 } else if (element instanceof NodesWrapper) {
51 NodesWrapper node = (NodesWrapper) element;
52 children = node.getChildren();
53 } else {
54 children = super.getChildren(element);
55 }
56
57 children = sort(element, children);
58 return children;
59 }
60
61 /** Do not sort by default. To be overidden to provide custom sort. */
62 protected Object[] sort(Object parent, Object[] children) {
63 return children;
64 }
65
66 /**
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)
70 */
71 protected List<Node> filterChildren(List<Node> children)
72 throws RepositoryException {
73 return children;
74 }
75
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();
82 }
83
84 @Override
85 public Object getParent(Object element) {
86 if (element instanceof Node) {
87 Node node = (Node) element;
88 try {
89 String path = node.getPath();
90 if (isBasePath(path))
91 return null;
92 else
93 return node.getParent();
94 } catch (RepositoryException e) {
95 throw new ArgeoException("Cannot get parent of " + element, e);
96 }
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());
103 }
104 return super.getParent(element);
105 }
106
107 @Override
108 public boolean hasChildren(Object element) {
109 try {
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();
119 }
120
121 } catch (RepositoryException e) {
122 throw new ArgeoException("Cannot check whether " + element
123 + " has children", e);
124 }
125 return super.hasChildren(element);
126 }
127
128 public Session getSession() {
129 return session;
130 }
131 }