]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.cms.ui.workbench/src/org/argeo/cms/ui/workbench/internal/jcr/model/SingleJcrNodeElem.java
Clean and refactor cms.ui.workbench bundle
[lgpl/argeo-commons.git] / org.argeo.cms.ui.workbench / src / org / argeo / cms / ui / workbench / internal / jcr / model / SingleJcrNodeElem.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.cms.ui.workbench.internal.jcr.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.eclipse.ui.EclipseUiException;
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 WorkspaceElem}, a
29 * {@link SingleJcrNodeElem} or null if the node is "mounted" as the root of the UI
30 * tree.
31 */
32
33 public class SingleJcrNodeElem extends TreeParent {
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 SingleJcrNodeElem(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 SingleJcrNodeElem(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 protected String getRepositoryAlias() {
67 return alias;
68 }
69
70 /**
71 * Override normal behavior to initialize children only when first requested
72 */
73 @Override
74 public synchronized Object[] getChildren() {
75 if (isLoaded()) {
76 return super.getChildren();
77 } else {
78 // initialize current object
79 try {
80 NodeIterator ni = node.getNodes();
81 while (ni.hasNext()) {
82 Node curNode = ni.nextNode();
83 addChild(new SingleJcrNodeElem(this, curNode, curNode.getName()));
84 }
85 return super.getChildren();
86 } catch (RepositoryException re) {
87 throw new EclipseUiException(
88 "Unexcpected error while initializing children SingleJcrNode",
89 re);
90 }
91 }
92 }
93
94 @Override
95 public boolean hasChildren() {
96 try {
97 if (node.getSession().isLive())
98 return node.hasNodes();
99 else
100 return false;
101 } catch (RepositoryException re) {
102 throw new EclipseUiException(
103 "Unexpected error while checking children node existence",
104 re);
105 }
106 }
107
108 }