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.workbench.internal.jcr.parts;
17  
18  import javax.jcr.Node;
19  import javax.jcr.RepositoryException;
20  
21  import org.argeo.eclipse.ui.EclipseUiException;
22  import org.eclipse.jface.resource.ImageDescriptor;
23  import org.eclipse.ui.IEditorInput;
24  import org.eclipse.ui.IPersistableElement;
25  
26  /** Editor input for {@link Node} editors */
27  public class GenericNodeEditorInput implements IEditorInput {
28  	private final Node currentNode;
29  
30  	// Caches key properties at creation time to avoid Exception at recovering
31  	// time when the session has been closed
32  	private String path;
33  	private String uid;
34  	private String name;
35  
36  	public GenericNodeEditorInput(Node currentNode) {
37  		this.currentNode = currentNode;
38  		try {
39  			name = currentNode.getName();
40  			uid = currentNode.getIdentifier();
41  			path = currentNode.getPath();
42  		} catch (RepositoryException re) {
43  			throw new EclipseUiException("Cannot cache the key properties for " + currentNode, re);
44  		}
45  	}
46  
47  	public Node getCurrentNode() {
48  		return currentNode;
49  	}
50  
51  	@SuppressWarnings("unchecked")
52  	public Object getAdapter(@SuppressWarnings("rawtypes") Class adapter) {
53  		return null;
54  	}
55  
56  	public boolean exists() {
57  		return true;
58  	}
59  
60  	public ImageDescriptor getImageDescriptor() {
61  		return null;
62  	}
63  
64  	public String getName() {
65  		return name;
66  	}
67  
68  	public String getUid() {
69  		return uid;
70  	}
71  
72  	public String getToolTipText() {
73  		return path;
74  	}
75  
76  	public String getPath() {
77  		return path;
78  	}
79  
80  	public IPersistableElement getPersistable() {
81  		return null;
82  	}
83  
84  	/**
85  	 * Equals method based on UID that is unique within a workspace and path of
86  	 * the node, thus 2 shared node that have same UID as defined in the spec
87  	 * but 2 different paths will open two distinct editors.
88  	 * 
89  	 * TODO enhance this method to support multi repository and multi workspace
90  	 * environments
91  	 */
92  	public boolean equals(Object obj) {
93  		if (this == obj)
94  			return true;
95  		if (obj == null)
96  			return false;
97  		if (getClass() != obj.getClass())
98  			return false;
99  
100 		GenericNodeEditorInput other = (GenericNodeEditorInput) obj;
101 		if (!getUid().equals(other.getUid()))
102 			return false;
103 		if (!getPath().equals(other.getPath()))
104 			return false;
105 		return true;
106 	}
107 }