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.util;
17  
18  import javax.jcr.Node;
19  import javax.jcr.RepositoryException;
20  
21  import org.argeo.eclipse.ui.EclipseUiException;
22  import org.eclipse.jface.viewers.IElementComparer;
23  
24  /** Compare JCR nodes based on their JCR identifiers, for use in JFace viewers. */
25  public class NodeViewerComparer implements IElementComparer {
26  
27  	// force comparison on Node IDs only.
28  	public boolean equals(Object elementA, Object elementB) {
29  		if (!(elementA instanceof Node) || !(elementB instanceof Node)) {
30  			return elementA == null ? elementB == null : elementA
31  					.equals(elementB);
32  		} else {
33  
34  			boolean result = false;
35  			try {
36  				String idA = ((Node) elementA).getIdentifier();
37  				String idB = ((Node) elementB).getIdentifier();
38  				result = idA == null ? idB == null : idA.equals(idB);
39  			} catch (RepositoryException re) {
40  				throw new EclipseUiException("cannot compare nodes", re);
41  			}
42  
43  			return result;
44  		}
45  	}
46  
47  	public int hashCode(Object element) {
48  		// TODO enhanced this method.
49  		return element.getClass().toString().hashCode();
50  	}
51  }