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.eclipse.ui.jcr;
17  
18  import java.util.ArrayList;
19  import java.util.List;
20  
21  import javax.jcr.Node;
22  import javax.jcr.NodeIterator;
23  import javax.jcr.RepositoryException;
24  
25  import org.argeo.eclipse.ui.EclipseUiException;
26  
27  /**
28   * Element of tree which is based on a node, but whose children are not
29   * necessarily this node children.
30   */
31  public class NodesWrapper {
32  	private final Node node;
33  
34  	public NodesWrapper(Node node) {
35  		super();
36  		this.node = node;
37  	}
38  
39  	protected NodeIterator getNodeIterator() throws RepositoryException {
40  		return node.getNodes();
41  	}
42  
43  	protected List<WrappedNode> getWrappedNodes() throws RepositoryException {
44  		List<WrappedNode> nodes = new ArrayList<WrappedNode>();
45  		for (NodeIterator nit = getNodeIterator(); nit.hasNext();)
46  			nodes.add(new WrappedNode(this, nit.nextNode()));
47  		return nodes;
48  	}
49  
50  	public Object[] getChildren() {
51  		try {
52  			return getWrappedNodes().toArray();
53  		} catch (RepositoryException e) {
54  			throw new EclipseUiException("Cannot get wrapped children", e);
55  		}
56  	}
57  
58  	/**
59  	 * @return true by default because we don't want to compute the wrapped
60  	 *         nodes twice
61  	 */
62  	public Boolean hasChildren() {
63  		return true;
64  	}
65  
66  	public Node getNode() {
67  		return node;
68  	}
69  
70  	@Override
71  	public int hashCode() {
72  		return node.hashCode();
73  	}
74  
75  	@Override
76  	public boolean equals(Object obj) {
77  		if (obj instanceof NodesWrapper)
78  			return node.equals(((NodesWrapper) obj).getNode());
79  		else
80  			return false;
81  	}
82  
83  	public String toString() {
84  		return "nodes wrapper based on " + node;
85  	}
86  }