]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.cms.ui.workbench/src/org/argeo/cms/ui/workbench/internal/jcr/JcrTreeContentProvider.java
Introduce basic NIO Java 7 UI
[lgpl/argeo-commons.git] / org.argeo.cms.ui.workbench / src / org / argeo / cms / ui / workbench / internal / jcr / JcrTreeContentProvider.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.cms.ui.workbench.internal.jcr;
17
18 import java.util.ArrayList;
19 import java.util.Arrays;
20 import java.util.List;
21
22 import javax.jcr.Node;
23 import javax.jcr.NodeIterator;
24 import javax.jcr.RepositoryException;
25
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;
30
31 /**
32 * Implementation of the {@code ITreeContentProvider} in order to display a
33 * single JCR node and its children in a tree like structure
34 */
35 public class JcrTreeContentProvider implements ITreeContentProvider {
36 private static final long serialVersionUID = -2128326504754297297L;
37 // private Node rootNode;
38 private JcrItemsComparator itemComparator = new JcrItemsComparator();
39
40 /**
41 * Sends back the first level of the Tree. input element must be a single
42 * node object
43 */
44 public Object[] getElements(Object inputElement) {
45 try {
46 Node rootNode = (Node) inputElement;
47 List<Node> result = new ArrayList<Node>();
48 NodeIterator ni = rootNode.getNodes();
49 while (ni.hasNext())
50 result.add(ni.nextNode());
51 return result.toArray();
52 } catch (RepositoryException re) {
53 throw new EclipseUiException("Unable to retrieve elements for " + inputElement, re);
54 }
55 }
56
57 public Object[] getChildren(Object parentElement) {
58 return childrenNodes((Node) parentElement);
59 }
60
61 public Object getParent(Object element) {
62 try {
63 Node node = (Node) element;
64 if (!node.getPath().equals("/"))
65 return node.getParent();
66 else
67 return null;
68 } catch (RepositoryException e) {
69 return null;
70 }
71 }
72
73 public boolean hasChildren(Object element) {
74 try {
75 return ((Node) element).hasNodes();
76 } catch (RepositoryException e) {
77 throw new EclipseUiException("Cannot check children existence on " + element, e);
78 }
79 }
80
81 protected Object[] childrenNodes(Node parentNode) {
82 try {
83 List<Node> children = new ArrayList<Node>();
84 NodeIterator nit = parentNode.getNodes();
85 while (nit.hasNext())
86 children.add(nit.nextNode());
87 Node[] arr = children.toArray(new Node[0]);
88 Arrays.sort(arr, itemComparator);
89 return arr;
90 } catch (RepositoryException e) {
91 throw new EclipseUiException("Cannot list children of " + parentNode, e);
92 }
93 }
94
95 public void dispose() {
96 }
97
98 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
99 }
100 }