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