]> 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/WorkspaceNode.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 / WorkspaceNode.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.Session;
22 import javax.jcr.Workspace;
23 import javax.jcr.observation.EventIterator;
24 import javax.jcr.observation.EventListener;
25
26 import org.argeo.ArgeoException;
27 import org.argeo.eclipse.ui.TreeParent;
28
29 /**
30 * UI Tree component. Wraps the root node of a JCR {@link Workspace}. It also
31 * keeps a reference to its parent {@link RepositoryNode}, to be able to
32 * retrieve alias of the current used repository
33 */
34 public class WorkspaceNode extends TreeParent implements EventListener, UiNode {
35 private Session session = null;
36
37 public WorkspaceNode(RepositoryNode parent, String name) {
38 this(parent, name, null);
39 }
40
41 public WorkspaceNode(RepositoryNode parent, String name, Session session) {
42 super(name);
43 this.session = session;
44 if (session != null)
45 processNewSession(session);
46 setParent(parent);
47 }
48
49 public Session getSession() {
50 return session;
51 }
52
53 public Node getRootNode() {
54 try {
55 if (session != null)
56 return session.getRootNode();
57 else
58 return null;
59 } catch (RepositoryException e) {
60 throw new ArgeoException("Cannot get root node of workspace "
61 + getName(), e);
62 }
63 }
64
65 public void login() {
66 try {
67 logout();
68 session = ((RepositoryNode) getParent()).repositoryLogin(getName());
69 processNewSession(session);
70
71 } catch (RepositoryException e) {
72 throw new ArgeoException("Cannot connect to repository "
73 + getName(), e);
74 }
75 }
76
77 @Override
78 public synchronized void dispose() {
79 logout();
80 super.dispose();
81 }
82
83 /** Logouts the session, does not nothing if there is no live session. */
84 public void logout() {
85 try {
86 if (session != null && session.isLive()) {
87 session.getWorkspace().getObservationManager()
88 .removeEventListener(this);
89 session.logout();
90 }
91 } catch (RepositoryException e) {
92 throw new ArgeoException("Cannot connect to repository "
93 + getName(), e);
94 }
95 }
96
97 /** Returns the alias of the parent Repository */
98 public String getAlias() {
99 return ((UiNode) getParent()).getAlias();
100 }
101
102 @Override
103 public boolean hasChildren() {
104 try {
105 if (session == null)
106 return false;
107 else
108 return session.getRootNode().hasNodes();
109 } catch (RepositoryException re) {
110 throw new ArgeoException(
111 "Unexpected error while checking children node existence",
112 re);
113 }
114 }
115
116 /** Override normal behaviour to initialize display of the workspace */
117 @Override
118 public synchronized Object[] getChildren() {
119 if (isLoaded()) {
120 return super.getChildren();
121 } else {
122 // initialize current object
123 try {
124 Node rootNode;
125 if (session == null)
126 return null;
127 else
128 rootNode = session.getRootNode();
129 NodeIterator ni = rootNode.getNodes();
130 while (ni.hasNext()) {
131 Node node = ni.nextNode();
132 addChild(new SingleJcrNode(this, node, node.getName()));
133 }
134 return super.getChildren();
135 } catch (RepositoryException e) {
136 throw new ArgeoException(
137 "Cannot initialize WorkspaceNode UI object."
138 + getName(), e);
139 }
140 }
141 }
142
143 public void onEvent(final EventIterator events) {
144 // if (session == null)
145 // return;
146 // Display.getDefault().syncExec(new Runnable() {
147 // public void run() {
148 // while (events.hasNext()) {
149 // Event event = events.nextEvent();
150 // try {
151 // String path = event.getPath();
152 // String parentPath = path.substring(0,
153 // path.lastIndexOf('/'));
154 // final Object parent;
155 // if (parentPath.equals("/") || parentPath.equals(""))
156 // parent = this;
157 // else if (session.itemExists(parentPath)){
158 // parent = session.getItem(parentPath);
159 // ((Item)parent).refresh(false);
160 // }
161 // else
162 // parent = null;
163 // if (parent != null) {
164 // nodesViewer.refresh(parent);
165 // }
166 //
167 // } catch (RepositoryException e) {
168 // log.warn("Error processing event " + event, e);
169 // }
170 // }
171 // }
172 // });
173 }
174
175 protected void processNewSession(Session session) {
176 // try {
177 // ObservationManager observationManager = session.getWorkspace()
178 // .getObservationManager();
179 // observationManager.addEventListener(this, Event.NODE_ADDED
180 // | Event.NODE_REMOVED, "/", true, null, null, false);
181 // } catch (RepositoryException e) {
182 // throw new ArgeoException("Cannot process new session "
183 // + session, e);
184 // }
185 }
186
187 }