]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.eclipse.ui.workbench/src/org/argeo/eclipse/ui/workbench/jcr/internal/NodeContentProvider.java
Do not use latest EclipseUiUtils to enable using the new command as hot fix on existi...
[lgpl/argeo-commons.git] / org.argeo.eclipse.ui.workbench / src / org / argeo / eclipse / ui / workbench / jcr / internal / NodeContentProvider.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.eclipse.ui.workbench.jcr.internal;
17
18 import java.util.ArrayList;
19 import java.util.Arrays;
20 import java.util.Comparator;
21 import java.util.List;
22
23 import javax.jcr.Node;
24 import javax.jcr.RepositoryException;
25 import javax.jcr.RepositoryFactory;
26 import javax.jcr.Session;
27 import javax.jcr.nodetype.NodeType;
28
29 import org.argeo.eclipse.ui.TreeParent;
30 import org.argeo.jcr.ArgeoJcrConstants;
31 import org.argeo.jcr.RepositoryRegister;
32 import org.argeo.jcr.UserJcrUtils;
33 import org.argeo.eclipse.ui.workbench.jcr.internal.model.RepositoriesElem;
34 import org.argeo.eclipse.ui.workbench.jcr.internal.model.SingleJcrNodeElem;
35 import org.argeo.util.security.Keyring;
36 import org.eclipse.jface.viewers.ITreeContentProvider;
37 import org.eclipse.jface.viewers.Viewer;
38
39 /**
40 * Implementation of the {@code ITreeContentProvider} to display multiple
41 * repository environment in a tree like structure
42 */
43 public class NodeContentProvider implements ITreeContentProvider {
44 private static final long serialVersionUID = -4083809398848374403L;
45 final private RepositoryRegister repositoryRegister;
46 final private RepositoryFactory repositoryFactory;
47
48 // Current user session on the default workspace of the argeo Node
49 final private Session userSession;
50 final private Keyring keyring;
51 private boolean sortChildren;
52
53 // Reference for cleaning
54 private SingleJcrNodeElem homeNode = null;
55 private RepositoriesElem repositoriesNode = null;
56
57 // Utils
58 private TreeBrowserComparator itemComparator = new TreeBrowserComparator();
59
60 public NodeContentProvider(Session userSession, Keyring keyring,
61 RepositoryRegister repositoryRegister,
62 RepositoryFactory repositoryFactory, Boolean sortChildren) {
63 this.userSession = userSession;
64 this.keyring = keyring;
65 this.repositoryRegister = repositoryRegister;
66 this.repositoryFactory = repositoryFactory;
67 this.sortChildren = sortChildren;
68 }
69
70 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
71 if (newInput == null)// dispose
72 return;
73
74 if (userSession != null) {
75 Node userHome = UserJcrUtils.getUserHome(userSession);
76 if (userHome != null) {
77 // TODO : find a way to dynamically get alias for the node
78 if (homeNode != null)
79 homeNode.dispose();
80 homeNode = new SingleJcrNodeElem(null, userHome,
81 userSession.getUserID(), ArgeoJcrConstants.ALIAS_NODE);
82 }
83 }
84 if (repositoryRegister != null) {
85 if (repositoriesNode != null)
86 repositoriesNode.dispose();
87 repositoriesNode = new RepositoriesElem("Repositories",
88 repositoryRegister, repositoryFactory, null, userSession,
89 keyring);
90 }
91 }
92
93 /**
94 * Sends back the first level of the Tree. Independent from inputElement
95 * that can be null
96 */
97 public Object[] getElements(Object inputElement) {
98 List<Object> objs = new ArrayList<Object>();
99 if (homeNode != null)
100 objs.add(homeNode);
101 if (repositoriesNode != null)
102 objs.add(repositoriesNode);
103 return objs.toArray();
104 }
105
106 public Object[] getChildren(Object parentElement) {
107 if (parentElement instanceof TreeParent) {
108 if (sortChildren) {
109 Object[] tmpArr = ((TreeParent) parentElement).getChildren();
110 if (tmpArr == null)
111 return new Object[0];
112 TreeParent[] arr = new TreeParent[tmpArr.length];
113 for (int i = 0; i < tmpArr.length; i++)
114 arr[i] = (TreeParent) tmpArr[i];
115 Arrays.sort(arr, itemComparator);
116 return arr;
117 } else
118 return ((TreeParent) parentElement).getChildren();
119 } else
120 return new Object[0];
121 }
122
123 /**
124 * Sets whether the content provider should order the children nodes or not.
125 * It is user duty to call a full refresh of the tree after changing this
126 * parameter.
127 */
128 public void setSortChildren(boolean sortChildren) {
129 this.sortChildren = sortChildren;
130 }
131
132 public Object getParent(Object element) {
133 if (element instanceof TreeParent) {
134 return ((TreeParent) element).getParent();
135 } else
136 return null;
137 }
138
139 public boolean hasChildren(Object element) {
140 if (element instanceof RepositoriesElem) {
141 RepositoryRegister rr = ((RepositoriesElem) element)
142 .getRepositoryRegister();
143 return rr.getRepositories().size() > 0;
144 } else if (element instanceof TreeParent) {
145 TreeParent tp = (TreeParent) element;
146 return tp.hasChildren();
147 }
148 return false;
149 }
150
151 public void dispose() {
152 if (homeNode != null)
153 homeNode.dispose();
154 if (repositoriesNode != null) {
155 // logs out open sessions
156 // see https://bugzilla.argeo.org/show_bug.cgi?id=23
157 repositoriesNode.dispose();
158 }
159 }
160
161 /**
162 * Specific comparator for this view. See specification here:
163 * https://www.argeo.org/bugzilla/show_bug.cgi?id=139
164 */
165 private class TreeBrowserComparator implements Comparator<TreeParent> {
166
167 public int category(TreeParent element) {
168 if (element instanceof SingleJcrNodeElem) {
169 Node node = ((SingleJcrNodeElem) element).getNode();
170 try {
171 if (node.isNodeType(NodeType.NT_FOLDER))
172 return 5;
173 } catch (RepositoryException e) {
174 // TODO Auto-generated catch block
175 e.printStackTrace();
176 }
177 }
178 return 10;
179 }
180
181 public int compare(TreeParent o1, TreeParent o2) {
182 int cat1 = category(o1);
183 int cat2 = category(o2);
184
185 if (cat1 != cat2) {
186 return cat1 - cat2;
187 }
188 return o1.getName().compareTo(o2.getName());
189 }
190 }
191 }