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
;
15 import org
.eclipse
.egit
.ui
.Activator
;
16 import org
.eclipse
.egit
.ui
.UIIcons
;
17 import org
.eclipse
.jgit
.lib
.Ref
;
18 import org
.eclipse
.jgit
.lib
.Repository
;
19 import org
.eclipse
.swt
.graphics
.Image
;
20 import org
.eclipse
.ui
.ISharedImages
;
21 import org
.eclipse
.ui
.PlatformUI
;
24 * A node in the Git Repositories view tree
29 public class RepositoryTreeNode
<T
> implements Comparable
<RepositoryTreeNode
> {
31 private final Repository myRepository
;
33 private final T myObject
;
35 private final RepositoryTreeNodeType myType
;
37 private final RepositoryTreeNode myParent
;
43 * the parent (may be null)
47 * the {@link Repository}
49 * an object (depending on the type)
51 public RepositoryTreeNode(RepositoryTreeNode parent
,
52 RepositoryTreeNodeType type
, Repository repository
, T treeObject
) {
54 myRepository
= repository
;
56 myObject
= treeObject
;
59 @SuppressWarnings("unchecked")
60 private RepositoryTreeNode
<Repository
> getRepositoryNode() {
61 if (myType
== RepositoryTreeNodeType
.REPO
) {
62 return (RepositoryTreeNode
<Repository
>) this;
64 return getParent().getRepositoryNode();
69 * @return the parent, or null
71 public RepositoryTreeNode
getParent() {
78 public RepositoryTreeNodeType
getType() {
83 * @return the repository
85 public Repository
getRepository() {
90 * Depending on the node type, the returned type is:
94 * <th>Object type</th>
96 * <td>{@link RepositoryTreeNodeType#BRANCHES}</td>
97 * <td>{@link String}</td>
100 * <td>{@link RepositoryTreeNodeType#LOCALBRANCHES}</td>
101 * <td>{@link String}</td>
104 * <td>{@link RepositoryTreeNodeType#REMOTEBRANCHES}</td>
105 * <td>{@link String}</td>
108 * <td>{@link RepositoryTreeNodeType#TAGS}</td>
109 * <td>{@link String}</td>
112 * <td>{@link RepositoryTreeNodeType#REMOTE}</td>
113 * <td>{@link String}</td>
116 * <td>{@link RepositoryTreeNodeType#REMOTES}</td>
117 * <td>{@link String}</td>
120 * <td>{@link RepositoryTreeNodeType#REPO}</td>
121 * <td>{@link Repository}</td>
125 * @return the type-specific object
127 public T
getObject() {
132 public int hashCode() {
133 final int prime
= 31;
151 + ((myObject
== null) ?
0 : ((Repository
) myObject
)
152 .getDirectory().hashCode());
161 + ((myObject
== null) ?
0 : ((Ref
) myObject
).getName()
169 + ((myObject
== null) ?
0 : ((File
) myObject
).getPath()
181 result
= prime
* result
182 + ((myObject
== null) ?
0 : myObject
.hashCode());
186 result
= prime
* result
187 + ((myParent
== null) ?
0 : myParent
.hashCode());
190 + ((myRepository
== null) ?
0 : myRepository
.getDirectory()
192 result
= prime
* result
+ ((myType
== null) ?
0 : myType
.hashCode());
197 public boolean equals(Object obj
) {
202 if (getClass() != obj
.getClass())
205 RepositoryTreeNode other
= (RepositoryTreeNode
) obj
;
207 if (myType
== null) {
208 if (other
.myType
!= null)
210 } else if (!myType
.equals(other
.myType
))
212 if (myParent
== null) {
213 if (other
.myParent
!= null)
215 } else if (!myParent
.equals(other
.myParent
))
217 if (myRepository
== null) {
218 if (other
.myRepository
!= null)
220 } else if (!myRepository
.getDirectory().equals(
221 other
.myRepository
.getDirectory()))
223 if (myObject
== null) {
224 if (other
.myObject
!= null)
226 } else if (!checkObjectsEqual(other
.myObject
))
232 public int compareTo(RepositoryTreeNode otherNode
) {
233 int typeDiff
= otherNode
.getType().ordinal() - this.myType
.ordinal();
237 // we only implement this for sorting, so we only have to
238 // implement this for nodes that can be on the same level
239 // i.e. siblings to each other
263 return ((String
) myObject
)
264 .compareTo((String
) otherNode
.getObject());
268 return ((File
) myObject
).getName().compareTo(
269 ((File
) otherNode
.getObject()).getName());
275 return ((Ref
) myObject
).getName().compareTo(
276 ((Ref
) otherNode
.getObject()).getName());
278 int nameCompare
= ((Repository
) myObject
).getDirectory()
279 .getParentFile().getName().compareTo(
280 (((Repository
) otherNode
.getObject())
281 .getDirectory().getParentFile().getName()));
282 if (nameCompare
!= 0)
284 // if the name is not unique, let's look at the whole path
285 return ((Repository
) myObject
).getDirectory().getParentFile()
286 .getParentFile().getPath().compareTo(
287 (((Repository
) otherNode
.getObject())
288 .getDirectory().getParentFile()
289 .getParentFile().getPath()));
295 private boolean checkObjectsEqual(Object otherObject
) {
312 return ((Repository
) myObject
).getDirectory().equals(
313 ((Repository
) otherObject
).getDirectory());
319 return ((Ref
) myObject
).getName().equals(
320 ((Ref
) otherObject
).getName());
324 return ((File
) myObject
).getPath().equals(
325 ((File
) otherObject
).getPath());
333 return myObject
.equals(otherObject
);
339 * Specifies the type of a {@link RepositoryTreeNode}
341 public enum RepositoryTreeNodeType
{
344 REPO(UIIcons
.REPOSITORY
.createImage()), //
346 BRANCHES(UIIcons
.BRANCHES
.createImage()), //
348 REF(UIIcons
.BRANCH
.createImage()), //
350 LOCALBRANCHES(PlatformUI
.getWorkbench().getSharedImages().getImage(
351 ISharedImages
.IMG_OBJ_FOLDER
)), //
353 REMOTEBRANCHES(PlatformUI
.getWorkbench().getSharedImages().getImage(
354 ISharedImages
.IMG_OBJ_FOLDER
)), //
356 TAGS(UIIcons
.TAGS
.createImage()), //
358 SYMBOLICREFS(PlatformUI
.getWorkbench().getSharedImages().getImage(
359 ISharedImages
.IMG_OBJ_FOLDER
)), //
361 SYMBOLICREF(PlatformUI
.getWorkbench().getSharedImages().getImage(
362 ISharedImages
.IMG_OBJ_FILE
)), // TODO icon
364 TAG(UIIcons
.TAG
.createImage()), //
366 FILE(PlatformUI
.getWorkbench().getSharedImages().getImage(
367 ISharedImages
.IMG_OBJ_FILE
)), //
369 FOLDER(PlatformUI
.getWorkbench().getSharedImages().getImage(
370 ISharedImages
.IMG_OBJ_FOLDER
)), //
372 REMOTES(UIIcons
.REMOTE_REPOSITORY
.createImage()), //
374 REMOTE(PlatformUI
.getWorkbench().getSharedImages().getImage(
375 ISharedImages
.IMG_OBJ_FOLDER
)), //
377 FETCH(UIIcons
.IMPORT
.createImage()), // TODO icon
379 PUSH(UIIcons
.EXPORT
.createImage()), // TODO icon
381 WORKINGDIR(PlatformUI
.getWorkbench().getSharedImages().getImage(
382 ISharedImages
.IMG_OBJ_FOLDER
)), //
384 ERROR(PlatformUI
.getWorkbench().getSharedImages().getImage(
385 ISharedImages
.IMG_ELCL_STOP
)) // TODO icon?
389 private final Image myImage
;
391 private RepositoryTreeNodeType(String iconName
) {
393 if (iconName
!= null) {
394 myImage
= Activator
.getDefault().getImageRegistry().get(
402 private RepositoryTreeNodeType(Image icon
) {
408 * @return the icon for this type
410 public Image
getIcon() {