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