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
;
22 import org
.eclipse
.ui
.ide
.IDE
.SharedImages
;
25 * A node in the Git Repositories view tree
30 public class RepositoryTreeNode
<T
> implements Comparable
<RepositoryTreeNode
> {
32 private final Repository myRepository
;
34 private final T myObject
;
36 private final RepositoryTreeNodeType myType
;
38 private final RepositoryTreeNode myParent
;
44 * the parent (may be null)
48 * the {@link Repository}
50 * an object (depending on the type)
52 public RepositoryTreeNode(RepositoryTreeNode parent
,
53 RepositoryTreeNodeType type
, Repository repository
, T treeObject
) {
55 myRepository
= repository
;
57 myObject
= treeObject
;
60 @SuppressWarnings("unchecked")
61 private RepositoryTreeNode
<Repository
> getRepositoryNode() {
62 if (myType
== RepositoryTreeNodeType
.REPO
) {
63 return (RepositoryTreeNode
<Repository
>) this;
65 return getParent().getRepositoryNode();
70 * @return the parent, or null
72 public RepositoryTreeNode
getParent() {
79 public RepositoryTreeNodeType
getType() {
84 * @return the repository
86 public Repository
getRepository() {
91 * Depending on the node type, the returned type is:
95 * <th>Object type</th>
97 * <td>{@link RepositoryTreeNodeType#BRANCHES}</td>
98 * <td>{@link String}</td>
101 * <td>{@link RepositoryTreeNodeType#PROJ}</td>
102 * <td>{@link File}</td>
105 * <td>{@link RepositoryTreeNodeType#PROJECTS}</td>
106 * <td>{@link String}</td>
109 * <td>{@link RepositoryTreeNodeType#LOCALBRANCHES}</td>
110 * <td>{@link String}</td>
113 * <td>{@link RepositoryTreeNodeType#REMOTEBRANCHES}</td>
114 * <td>{@link String}</td>
117 * <td>{@link RepositoryTreeNodeType#TAGS}</td>
118 * <td>{@link String}</td>
121 * <td>{@link RepositoryTreeNodeType#REMOTE}</td>
122 * <td>{@link String}</td>
125 * <td>{@link RepositoryTreeNodeType#REMOTES}</td>
126 * <td>{@link String}</td>
129 * <td>{@link RepositoryTreeNodeType#REPO}</td>
130 * <td>{@link Repository}</td>
134 * @return the type-specific object
136 public T
getObject() {
141 public int hashCode() {
142 final int prime
= 31;
162 + ((myObject
== null) ?
0 : ((Repository
) myObject
)
163 .getDirectory().hashCode());
174 + ((myObject
== null) ?
0 : ((Ref
) myObject
).getName()
182 + ((myObject
== null) ?
0 : ((File
) myObject
).getPath()
194 result
= prime
* result
195 + ((myObject
== null) ?
0 : myObject
.hashCode());
199 result
= prime
* result
200 + ((myParent
== null) ?
0 : myParent
.hashCode());
203 + ((myRepository
== null) ?
0 : myRepository
.getDirectory()
205 result
= prime
* result
+ ((myType
== null) ?
0 : myType
.hashCode());
210 public boolean equals(Object obj
) {
215 if (getClass() != obj
.getClass())
218 RepositoryTreeNode other
= (RepositoryTreeNode
) obj
;
220 if (myType
== null) {
221 if (other
.myType
!= null)
223 } else if (!myType
.equals(other
.myType
))
225 if (myParent
== null) {
226 if (other
.myParent
!= null)
228 } else if (!myParent
.equals(other
.myParent
))
230 if (myRepository
== null) {
231 if (other
.myRepository
!= null)
233 } else if (!myRepository
.getDirectory().equals(
234 other
.myRepository
.getDirectory()))
236 if (myObject
== null) {
237 if (other
.myObject
!= null)
239 } else if (!checkObjectsEqual(other
.myObject
))
245 public int compareTo(RepositoryTreeNode otherNode
) {
246 int typeDiff
= otherNode
.getType().ordinal() - this.myType
.ordinal();
250 // we only implement this for sorting, so we only have to
251 // implement this for nodes that can be on the same level
252 // i.e. siblings to each other
272 return ((String
) myObject
)
273 .compareTo((String
) otherNode
.getObject());
277 return ((File
) myObject
).getName().compareTo(
278 ((File
) otherNode
.getObject()).getName());
280 return ((Ref
) myObject
).getName().compareTo(
281 ((Ref
) otherNode
.getObject()).getName());
283 int nameCompare
= ((Repository
) myObject
).getDirectory()
284 .getParentFile().getName().compareTo(
285 (((Repository
) otherNode
.getObject())
286 .getDirectory().getParentFile().getName()));
287 if (nameCompare
!= 0)
289 // if the name is not unique, let's look at the whole path
290 return ((Repository
) myObject
).getDirectory().getParentFile()
291 .getParentFile().getPath().compareTo(
292 (((Repository
) otherNode
.getObject())
293 .getDirectory().getParentFile()
294 .getParentFile().getPath()));
300 private boolean checkObjectsEqual(Object otherObject
) {
319 return ((Repository
) myObject
).getDirectory().equals(
320 ((Repository
) otherObject
).getDirectory());
328 return ((Ref
) myObject
).getName().equals(
329 ((Ref
) otherObject
).getName());
335 return ((File
) myObject
).getPath().equals(
336 ((File
) otherObject
).getPath());
344 return myObject
.equals(otherObject
);
350 * Specifies the type of a {@link RepositoryTreeNode}
352 public enum RepositoryTreeNodeType
{
355 REPO(UIIcons
.REPOSITORY
.createImage()), //
357 PROJ(PlatformUI
.getWorkbench().getSharedImages().getImage(
358 SharedImages
.IMG_OBJ_PROJECT_CLOSED
)), //
360 BRANCHES(UIIcons
.BRANCHES
.createImage()), //
362 REF(UIIcons
.BRANCH
.createImage()), //
364 HEAD(PlatformUI
.getWorkbench().getSharedImages().getImage(
365 ISharedImages
.IMG_OBJ_FILE
)), // TODO icon
367 LOCALBRANCHES(PlatformUI
.getWorkbench().getSharedImages().getImage(
368 ISharedImages
.IMG_OBJ_FOLDER
)), //
370 REMOTEBRANCHES(PlatformUI
.getWorkbench().getSharedImages().getImage(
371 ISharedImages
.IMG_OBJ_FOLDER
)), //
373 TAGS(UIIcons
.TAGS
.createImage()), //
375 SYMBOLICREFS(PlatformUI
.getWorkbench().getSharedImages().getImage(
376 ISharedImages
.IMG_OBJ_FOLDER
)), //
378 SYMBOLICREF(PlatformUI
.getWorkbench().getSharedImages().getImage(
379 ISharedImages
.IMG_OBJ_FILE
)), // TODO icon
381 TAG(UIIcons
.TAG
.createImage()), //
383 FILE(PlatformUI
.getWorkbench().getSharedImages().getImage(
384 ISharedImages
.IMG_OBJ_FILE
)), //
386 FOLDER(PlatformUI
.getWorkbench().getSharedImages().getImage(
387 ISharedImages
.IMG_OBJ_FOLDER
)), //
389 PROJECTS(PlatformUI
.getWorkbench().getSharedImages().getImage(
390 SharedImages
.IMG_OBJ_PROJECT_CLOSED
)), //
392 REMOTES(UIIcons
.REMOTE_REPOSITORY
.createImage()), //
394 REMOTE(PlatformUI
.getWorkbench().getSharedImages().getImage(
395 ISharedImages
.IMG_OBJ_FOLDER
)), //
397 FETCH(UIIcons
.IMPORT
.createImage()), // TODO icon
399 PUSH(UIIcons
.EXPORT
.createImage()), // TODO icon
401 WORKINGDIR(PlatformUI
.getWorkbench().getSharedImages().getImage(
402 ISharedImages
.IMG_OBJ_FOLDER
)), //
404 ERROR(PlatformUI
.getWorkbench().getSharedImages().getImage(
405 ISharedImages
.IMG_ELCL_STOP
)) // TODO icon?
409 private final Image myImage
;
411 private RepositoryTreeNodeType(String iconName
) {
413 if (iconName
!= null) {
414 myImage
= Activator
.getDefault().getImageRegistry().get(
422 private RepositoryTreeNodeType(Image icon
) {
428 * @return the icon for this type
430 public Image
getIcon() {