View Javadoc
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.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.util.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 node
42  	 * object
43  	 */
44  	public Object[] getElements(Object inputElement) {
45  		Node rootNode = (Node) inputElement;
46  		return childrenNodes(rootNode);
47  	}
48  
49  	public Object[] getChildren(Object parentElement) {
50  		return childrenNodes((Node) parentElement);
51  	}
52  
53  	public Object getParent(Object element) {
54  		try {
55  			Node node = (Node) element;
56  			if (!node.getPath().equals("/"))
57  				return node.getParent();
58  			else
59  				return null;
60  		} catch (RepositoryException e) {
61  			return null;
62  		}
63  	}
64  
65  	public boolean hasChildren(Object element) {
66  		try {
67  			return ((Node) element).hasNodes();
68  		} catch (RepositoryException e) {
69  			throw new EclipseUiException("Cannot check children existence on " + element, e);
70  		}
71  	}
72  
73  	protected Object[] childrenNodes(Node parentNode) {
74  		try {
75  			List<Node> children = new ArrayList<Node>();
76  			NodeIterator nit = parentNode.getNodes();
77  			while (nit.hasNext()) {
78  				Node node = nit.nextNode();
79  //				if (node.getName().startsWith("rep:") || node.getName().startsWith("jcr:")
80  //						|| node.getName().startsWith("nt:"))
81  //					continue nodes;
82  				children.add(node);
83  			}
84  			Node[] arr = children.toArray(new Node[0]);
85  			Arrays.sort(arr, itemComparator);
86  			return arr;
87  		} catch (RepositoryException e) {
88  			throw new EclipseUiException("Cannot list children of " + parentNode, e);
89  		}
90  	}
91  
92  	public void dispose() {
93  	}
94  
95  	public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
96  	}
97  }