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
.cms
.ui
.workbench
.internal
.jcr
;
18 import java
.util
.ArrayList
;
19 import java
.util
.Arrays
;
20 import java
.util
.List
;
22 import javax
.jcr
.Node
;
23 import javax
.jcr
.NodeIterator
;
24 import javax
.jcr
.RepositoryException
;
26 import org
.argeo
.eclipse
.ui
.EclipseUiException
;
27 import org
.argeo
.eclipse
.ui
.jcr
.utils
.JcrItemsComparator
;
28 import org
.eclipse
.jface
.viewers
.ITreeContentProvider
;
29 import org
.eclipse
.jface
.viewers
.Viewer
;
32 * Implementation of the {@code ITreeContentProvider} in order to display a
33 * single JCR node and its children in a tree like structure
35 public class JcrTreeContentProvider
implements ITreeContentProvider
{
36 private static final long serialVersionUID
= -2128326504754297297L;
37 // private Node rootNode;
38 private JcrItemsComparator itemComparator
= new JcrItemsComparator();
41 * Sends back the first level of the Tree. input element must be a single
44 public Object
[] getElements(Object inputElement
) {
46 Node rootNode
= (Node
) inputElement
;
47 List
<Node
> result
= new ArrayList
<Node
>();
48 NodeIterator ni
= rootNode
.getNodes();
50 result
.add(ni
.nextNode());
51 return result
.toArray();
52 } catch (RepositoryException re
) {
53 throw new EclipseUiException("Unable to retrieve elements for " + inputElement
, re
);
57 public Object
[] getChildren(Object parentElement
) {
58 return childrenNodes((Node
) parentElement
);
61 public Object
getParent(Object element
) {
63 Node node
= (Node
) element
;
64 if (!node
.getPath().equals("/"))
65 return node
.getParent();
68 } catch (RepositoryException e
) {
73 public boolean hasChildren(Object element
) {
75 return ((Node
) element
).hasNodes();
76 } catch (RepositoryException e
) {
77 throw new EclipseUiException("Cannot check children existence on " + element
, e
);
81 protected Object
[] childrenNodes(Node parentNode
) {
83 List
<Node
> children
= new ArrayList
<Node
>();
84 NodeIterator nit
= parentNode
.getNodes();
86 children
.add(nit
.nextNode());
87 Node
[] arr
= children
.toArray(new Node
[0]);
88 Arrays
.sort(arr
, itemComparator
);
90 } catch (RepositoryException e
) {
91 throw new EclipseUiException("Cannot list children of " + parentNode
, e
);
95 public void dispose() {
98 public void inputChanged(Viewer viewer
, Object oldInput
, Object newInput
) {