]> git.argeo.org Git - lgpl/argeo-commons.git/blob - model/SingleJcrNode.java
Prepare next development cycle
[lgpl/argeo-commons.git] / model / SingleJcrNode.java
1 package org.argeo.jcr.ui.explorer.model;
2
3 import javax.jcr.Node;
4 import javax.jcr.NodeIterator;
5 import javax.jcr.RepositoryException;
6 import javax.jcr.Workspace;
7
8 import org.argeo.ArgeoException;
9 import org.argeo.eclipse.ui.TreeParent;
10
11 /**
12 * UI Tree component. Wraps a node of a JCR {@link Workspace}. It also keeps a
13 * reference to its parent node that can either be a {@link WorkspaceNode}, a
14 * {@link SingleJcrNode} or null if the node is "mounted" as the root of the UI
15 * tree.
16 */
17
18 public class SingleJcrNode extends TreeParent implements UiNode {
19
20 private final Node node;
21 private String alias = null;
22
23 // keeps a local reference to the node's name to avoid exception when the
24 // session is lost
25 // private final String name;
26
27 /** Creates a new UiNode in the UI Tree */
28 public SingleJcrNode(TreeParent parent, Node node, String name) {
29 super(name);
30 setParent(parent);
31 this.node = node;
32 }
33
34 /**
35 * Creates a new UiNode in the UI Tree, keeping a reference to the alias of
36 * the corresponding repository in the current UI environment. It is useful
37 * to be able to mount nodes as roots of the UI tree.
38 */
39 public SingleJcrNode(TreeParent parent, Node node, String name, String alias) {
40 super(name);
41 setParent(parent);
42 this.node = node;
43 this.alias = alias;
44 }
45
46 /** returns the node wrapped by the current Ui object */
47 public Node getNode() {
48 return node;
49 }
50
51 /**
52 * Returns the alias corresponding to the repository abstraction that
53 * contains current node. If the current object is mounted as root of the UI
54 * tree, the alias is stored in the object. Otherwise, we must browse the
55 * tree backward to the RepositoryNode.
56 *
57 * Alias is then cached in the current object so that next time it will be
58 * here.
59 */
60 public String getAlias() {
61 if (alias == null) {
62 alias = ((UiNode) getParent()).getAlias();
63 }
64 return alias;
65 }
66
67 /**
68 * Override normal behaviour to initialize children only when first
69 * requested
70 */
71 @Override
72 public synchronized Object[] getChildren() {
73 if (isLoaded()) {
74 return super.getChildren();
75 } else {
76 // initialize current object
77 try {
78 NodeIterator ni = node.getNodes();
79 while (ni.hasNext()) {
80 Node curNode = ni.nextNode();
81 addChild(new SingleJcrNode(this, curNode, curNode.getName()));
82 }
83 return super.getChildren();
84 } catch (RepositoryException re) {
85 throw new ArgeoException(
86 "Unexcpected error while initializing children SingleJcrNode",
87 re);
88 }
89 }
90 }
91
92 @Override
93 public boolean hasChildren() {
94 try {
95 return node.hasNodes();
96 } catch (RepositoryException re) {
97 throw new ArgeoException(
98 "Unexpected error while checking children node existence",
99 re);
100 }
101 }
102
103 }