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.slc.client.ui.dist.controllers;
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.jcr.utils.JcrItemsComparator;
27  import org.argeo.slc.SlcException;
28  import org.argeo.slc.SlcTypes;
29  import org.eclipse.jface.viewers.ITreeContentProvider;
30  import org.eclipse.jface.viewers.Viewer;
31  
32  /** Enable specific browsing of an artifact tree */
33  public class ArtifactsTreeContentProvider implements ITreeContentProvider,
34  		SlcTypes {
35  	private static final long serialVersionUID = -8097817288192073987L;
36  
37  	// Utils
38  	private boolean sortChildren = true;
39  	private JcrItemsComparator itemComparator = new JcrItemsComparator();
40  
41  	public Object[] getElements(Object parent) {
42  		return getChildren(parent);
43  	}
44  
45  	public Object getParent(Object child) {
46  		return null;
47  	}
48  
49  	public Object[] getChildren(Object parent) {
50  		Object[] elements = null;
51  		try {
52  			if (parent instanceof Node) {
53  				Node node = (Node) parent;
54  				NodeIterator ni = node.getNodes();
55  				List<Node> nodesList = new ArrayList<Node>();
56  				while (ni.hasNext()) {
57  					nodesList.add(ni.nextNode());
58  				}
59  				if (sortChildren) {
60  					Node[] arr = (Node[]) nodesList.toArray(new Node[nodesList
61  							.size()]);
62  					Arrays.sort(arr, itemComparator);
63  					return arr;
64  				} else
65  					return nodesList.toArray();
66  
67  			}
68  		} catch (RepositoryException e) {
69  			throw new SlcException(
70  					"Unexpected exception while listing node properties", e);
71  		}
72  		return elements;
73  	}
74  
75  	public boolean hasChildren(Object parent) {
76  		try {
77  			if (parent instanceof Node) {
78  				Node curNode = (Node) parent;
79  				// We manually stop digging at this level
80  				if (curNode.isNodeType(SLC_ARTIFACT_VERSION_BASE))
81  					return false;
82  				else if (curNode.hasNodes())
83  					return true;
84  			}
85  		} catch (RepositoryException e) {
86  			throw new SlcException(
87  					"Unexpected exception while checking if property is multiple",
88  					e);
89  		}
90  		return false;
91  	}
92  
93  	public void setSortChildren(boolean sortChildren) {
94  		this.sortChildren = sortChildren;
95  	}
96  
97  	public boolean getSortChildren() {
98  		return sortChildren;
99  	}
100 
101 	public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
102 	}
103 
104 	public void dispose() {
105 	}
106 }