]> 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
+ Fix a bug on file download for the generic JCR view
[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 if (element instanceof Node) {
35 try {
36 List<Node> nodes = new ArrayList<Node>();
37 for (NodeIterator nit = ((Node) element).getNodes(); nit
38 .hasNext();)
39 nodes.add(nit.nextNode());
40 return nodes.toArray();
41 } catch (RepositoryException e) {
42 throw new ArgeoException("Cannot get children of " + element, e);
43 }
44 } else {
45 return super.getChildren(element);
46 }
47 }
48
49 @Override
50 public Object getParent(Object element) {
51 if (element instanceof Node) {
52 Node node = (Node) element;
53 try {
54 String path = node.getPath();
55 if (isBasePath(path))
56 return null;
57 else
58 return node.getParent();
59 } catch (RepositoryException e) {
60 throw new ArgeoException("Cannot get parent of " + element, e);
61 }
62 }
63 return super.getParent(element);
64 }
65
66 @Override
67 public boolean hasChildren(Object element) {
68 if (element instanceof Node) {
69 Node node = (Node) element;
70 try {
71 return node.hasNodes();
72 } catch (RepositoryException e) {
73 throw new ArgeoException("Cannot check whether " + element
74 + " has children", e);
75 }
76 }
77 return super.hasChildren(element);
78 }
79
80 public Session getSession() {
81 return session;
82 }
83 }