1 /*******************************************************************************
2 * Copyright (c) 2010 SAP AG.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
9 * Mathias Kinzler (SAP AG) - initial implementation
10 *******************************************************************************/
11 package org
.eclipse
.egit
.ui
.internal
.repository
.tree
;
15 import org
.eclipse
.jgit
.lib
.Ref
;
16 import org
.eclipse
.jgit
.lib
.Repository
;
19 * A node in the Git Repositories view tree
24 public abstract class RepositoryTreeNode
<T
> implements Comparable
<RepositoryTreeNode
> {
26 private final Repository myRepository
;
28 private final T myObject
;
30 private final RepositoryTreeNodeType myType
;
32 private final RepositoryTreeNode myParent
;
38 * the parent (may be null)
42 * the {@link Repository}
44 * an object (depending on the type)
46 public RepositoryTreeNode(RepositoryTreeNode parent
,
47 RepositoryTreeNodeType type
, Repository repository
, T treeObject
) {
49 myRepository
= repository
;
51 myObject
= treeObject
;
54 @SuppressWarnings("unchecked")
55 private RepositoryTreeNode
<Repository
> getRepositoryNode() {
56 if (myType
== RepositoryTreeNodeType
.REPO
) {
57 return (RepositoryTreeNode
<Repository
>) this;
59 return getParent().getRepositoryNode();
64 * @return the parent, or null
66 public RepositoryTreeNode
getParent() {
73 public RepositoryTreeNodeType
getType() {
78 * @return the repository
80 public Repository
getRepository() {
85 * Depending on the node type, the returned type is:
89 * <th>Object type</th>
91 * <td>{@link RepositoryTreeNodeType#BRANCHES}</td>
92 * <td>{@link String}</td>
95 * <td>{@link RepositoryTreeNodeType#LOCALBRANCHES}</td>
96 * <td>{@link String}</td>
99 * <td>{@link RepositoryTreeNodeType#REMOTEBRANCHES}</td>
100 * <td>{@link String}</td>
103 * <td>{@link RepositoryTreeNodeType#TAGS}</td>
104 * <td>{@link String}</td>
107 * <td>{@link RepositoryTreeNodeType#REMOTE}</td>
108 * <td>{@link String}</td>
111 * <td>{@link RepositoryTreeNodeType#REMOTES}</td>
112 * <td>{@link String}</td>
115 * <td>{@link RepositoryTreeNodeType#REPO}</td>
116 * <td>{@link Repository}</td>
120 * @return the type-specific object
122 public T
getObject() {
127 public int hashCode() {
128 final int prime
= 31;
146 + ((myObject
== null) ?
0 : ((Repository
) myObject
)
147 .getDirectory().hashCode());
156 + ((myObject
== null) ?
0 : ((Ref
) myObject
).getName()
164 + ((myObject
== null) ?
0 : ((File
) myObject
).getPath()
176 result
= prime
* result
177 + ((myObject
== null) ?
0 : myObject
.hashCode());
181 result
= prime
* result
182 + ((myParent
== null) ?
0 : myParent
.hashCode());
185 + ((myRepository
== null) ?
0 : myRepository
.getDirectory()
187 result
= prime
* result
+ ((myType
== null) ?
0 : myType
.hashCode());
192 public boolean equals(Object obj
) {
197 if (getClass() != obj
.getClass())
200 RepositoryTreeNode other
= (RepositoryTreeNode
) obj
;
202 if (myType
== null) {
203 if (other
.myType
!= null)
205 } else if (!myType
.equals(other
.myType
))
207 if (myParent
== null) {
208 if (other
.myParent
!= null)
210 } else if (!myParent
.equals(other
.myParent
))
212 if (myRepository
== null) {
213 if (other
.myRepository
!= null)
215 } else if (!myRepository
.getDirectory().equals(
216 other
.myRepository
.getDirectory()))
218 if (myObject
== null) {
219 if (other
.myObject
!= null)
221 } else if (!checkObjectsEqual(other
.myObject
))
227 public int compareTo(RepositoryTreeNode otherNode
) {
228 int typeDiff
= this.myType
.ordinal() - otherNode
.getType().ordinal();
232 // we only implement this for sorting, so we only have to
233 // implement this for nodes that can be on the same level
234 // i.e. siblings to each other
260 return ((String
) myObject
)
261 .compareTo((String
) otherNode
.getObject());
265 return ((File
) myObject
).getName().compareTo(
266 ((File
) otherNode
.getObject()).getName());
272 return ((Ref
) myObject
).getName().compareTo(
273 ((Ref
) otherNode
.getObject()).getName());
275 int nameCompare
= ((Repository
) myObject
).getDirectory()
276 .getParentFile().getName().compareTo(
277 (((Repository
) otherNode
.getObject())
278 .getDirectory().getParentFile().getName()));
279 if (nameCompare
!= 0)
281 // if the name is not unique, let's look at the whole path
282 return ((Repository
) myObject
).getDirectory().getParentFile()
283 .getParentFile().getPath().compareTo(
284 (((Repository
) otherNode
.getObject())
285 .getDirectory().getParentFile()
286 .getParentFile().getPath()));
292 private boolean checkObjectsEqual(Object otherObject
) {
309 return ((Repository
) myObject
).getDirectory().equals(
310 ((Repository
) otherObject
).getDirectory());
316 return ((Ref
) myObject
).getName().equals(
317 ((Ref
) otherObject
).getName());
321 return ((File
) myObject
).getPath().equals(
322 ((File
) otherObject
).getPath());
330 return myObject
.equals(otherObject
);