2 * Copyright (C) 2007-2012 Argeo GmbH
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org
.argeo
.jcr
.ui
.explorer
.browser
;
18 import java
.util
.ArrayList
;
19 import java
.util
.Arrays
;
20 import java
.util
.Comparator
;
21 import java
.util
.List
;
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
;
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
.jcr
.ui
.explorer
.model
.RepositoriesElem
;
34 import org
.argeo
.jcr
.ui
.explorer
.model
.SingleJcrNodeElem
;
35 import org
.argeo
.util
.security
.Keyring
;
36 import org
.eclipse
.jface
.viewers
.ITreeContentProvider
;
37 import org
.eclipse
.jface
.viewers
.Viewer
;
40 * Implementation of the {@code ITreeContentProvider} to display multiple
41 * repository environment in a tree like structure
44 public class NodeContentProvider
implements ITreeContentProvider
{
45 final private RepositoryRegister repositoryRegister
;
46 final private RepositoryFactory repositoryFactory
;
48 * A session of the logged in user on the default workspace of the node
51 final private Session userSession
;
52 final private Keyring keyring
;
53 private boolean sortChildren
;
55 // reference for cleaning
56 private SingleJcrNodeElem homeNode
= null;
57 private RepositoriesElem repositoriesNode
= null;
60 private TreeBrowserComparator itemComparator
= new TreeBrowserComparator();
62 public NodeContentProvider(Session userSession
, Keyring keyring
,
63 RepositoryRegister repositoryRegister
,
64 RepositoryFactory repositoryFactory
, Boolean sortChildren
) {
65 this.userSession
= userSession
;
66 this.keyring
= keyring
;
67 this.repositoryRegister
= repositoryRegister
;
68 this.repositoryFactory
= repositoryFactory
;
69 this.sortChildren
= sortChildren
;
72 public void inputChanged(Viewer viewer
, Object oldInput
, Object newInput
) {
73 if (newInput
== null)// dispose
76 if (userSession
!= null) {
77 Node userHome
= UserJcrUtils
.getUserHome(userSession
);
78 if (userHome
!= null) {
79 // TODO : find a way to dynamically get alias for the node
82 homeNode
= new SingleJcrNodeElem(null, userHome
,
83 userSession
.getUserID(), ArgeoJcrConstants
.ALIAS_NODE
);
86 if (repositoryRegister
!= null) {
87 if (repositoriesNode
!= null)
88 repositoriesNode
.dispose();
89 repositoriesNode
= new RepositoriesElem("Repositories",
90 repositoryRegister
, repositoryFactory
, null, userSession
,
96 * Sends back the first level of the Tree. Independent from inputElement
99 public Object
[] getElements(Object inputElement
) {
100 List
<Object
> objs
= new ArrayList
<Object
>();
101 if (homeNode
!= null)
103 if (repositoriesNode
!= null)
104 objs
.add(repositoriesNode
);
105 return objs
.toArray();
108 public Object
[] getChildren(Object parentElement
) {
109 if (parentElement
instanceof TreeParent
) {
111 // TreeParent[] arr = (TreeParent[]) ((TreeParent)
114 Object
[] tmpArr
= ((TreeParent
) parentElement
).getChildren();
115 TreeParent
[] arr
= new TreeParent
[tmpArr
.length
];
116 for (int i
= 0; i
< tmpArr
.length
; i
++)
117 arr
[i
] = (TreeParent
) tmpArr
[i
];
119 Arrays
.sort(arr
, itemComparator
);
122 return ((TreeParent
) parentElement
).getChildren();
125 return new Object
[0];
130 * Sets whether the content provider should order the children nodes or not.
131 * It is user duty to call a full refresh of the tree after changing this
134 public void setSortChildren(boolean sortChildren
) {
135 this.sortChildren
= sortChildren
;
138 public Object
getParent(Object element
) {
139 if (element
instanceof TreeParent
) {
140 return ((TreeParent
) element
).getParent();
145 public boolean hasChildren(Object element
) {
146 if (element
instanceof RepositoriesElem
) {
147 RepositoryRegister rr
= ((RepositoriesElem
) element
)
148 .getRepositoryRegister();
149 return rr
.getRepositories().size() > 0;
150 } else if (element
instanceof TreeParent
) {
151 TreeParent tp
= (TreeParent
) element
;
152 return tp
.hasChildren();
157 public void dispose() {
158 if (homeNode
!= null)
160 if (repositoriesNode
!= null) {
161 // logs out open sessions
162 // see https://bugzilla.argeo.org/show_bug.cgi?id=23
163 repositoriesNode
.dispose();
168 * Specific comparator for this view. See spec in BUG :
169 * https://www.argeo.org/bugzilla/show_bug.cgi?id=139
171 private class TreeBrowserComparator
implements Comparator
<TreeParent
> {
173 public int category(TreeParent element
) {
174 if (element
instanceof SingleJcrNodeElem
) {
175 Node node
= ((SingleJcrNodeElem
) element
).getNode();
177 if (node
.isNodeType(NodeType
.NT_FOLDER
))
179 } catch (RepositoryException e
) {
180 // TODO Auto-generated catch block
187 public int compare(TreeParent o1
, TreeParent o2
) {
188 int cat1
= category(o1
);
189 int cat2
= category(o2
);
194 return o1
.getName().compareTo(o2
.getName());