]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.eclipse.ui.workbench/src/org/argeo/eclipse/ui/workbench/jcr/internal/NodeContentProvider.java
Adapt file upload/download to latest RWT version.
[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 TreeParent[] arr = new TreeParent[tmpArr.length];
111 for (int i = 0; i < tmpArr.length; i++)
112 arr[i] = (TreeParent) tmpArr[i];
113 Arrays.sort(arr, itemComparator);
114 return arr;
115 } else
116 return ((TreeParent) parentElement).getChildren();
117 } else
118 return new Object[0];
119 }
120
121 /**
122 * Sets whether the content provider should order the children nodes or not.
123 * It is user duty to call a full refresh of the tree after changing this
124 * parameter.
125 */
126 public void setSortChildren(boolean sortChildren) {
127 this.sortChildren = sortChildren;
128 }
129
130 public Object getParent(Object element) {
131 if (element instanceof TreeParent) {
132 return ((TreeParent) element).getParent();
133 } else
134 return null;
135 }
136
137 public boolean hasChildren(Object element) {
138 if (element instanceof RepositoriesElem) {
139 RepositoryRegister rr = ((RepositoriesElem) element)
140 .getRepositoryRegister();
141 return rr.getRepositories().size() > 0;
142 } else if (element instanceof TreeParent) {
143 TreeParent tp = (TreeParent) element;
144 return tp.hasChildren();
145 }
146 return false;
147 }
148
149 public void dispose() {
150 if (homeNode != null)
151 homeNode.dispose();
152 if (repositoriesNode != null) {
153 // logs out open sessions
154 // see https://bugzilla.argeo.org/show_bug.cgi?id=23
155 repositoriesNode.dispose();
156 }
157 }
158
159 /**
160 * Specific comparator for this view. See specification here:
161 * https://www.argeo.org/bugzilla/show_bug.cgi?id=139
162 */
163 private class TreeBrowserComparator implements Comparator<TreeParent> {
164
165 public int category(TreeParent element) {
166 if (element instanceof SingleJcrNodeElem) {
167 Node node = ((SingleJcrNodeElem) element).getNode();
168 try {
169 if (node.isNodeType(NodeType.NT_FOLDER))
170 return 5;
171 } catch (RepositoryException e) {
172 // TODO Auto-generated catch block
173 e.printStackTrace();
174 }
175 }
176 return 10;
177 }
178
179 public int compare(TreeParent o1, TreeParent o2) {
180 int cat1 = category(o1);
181 int cat2 = category(o2);
182
183 if (cat1 != cat2) {
184 return cat1 - cat2;
185 }
186 return o1.getName().compareTo(o2.getName());
187 }
188 }
189 }