Git Repositories View: Allow to "remove" multiple Repositories
[egit.git] / org.eclipse.egit.ui / src / org / eclipse / egit / ui / internal / repository / RepositoryTreeNode.java
blob3245f9e6af5ebe009bb1c8427c07ade2bfbc272f
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
8 * Contributors:
9 * Mathias Kinzler (SAP AG) - initial implementation
10 *******************************************************************************/
11 package org.eclipse.egit.ui.internal.repository;
13 import java.io.File;
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;
23 /**
24 * A node in the Git Repositories view tree
26 * @param <T>
27 * the type
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;
39 /**
40 * Constructs a node
42 * @param parent
43 * the parent (may be null)
44 * @param type
45 * the type
46 * @param repository
47 * the {@link Repository}
48 * @param treeObject
49 * an object (depending on the type)
51 public RepositoryTreeNode(RepositoryTreeNode parent,
52 RepositoryTreeNodeType type, Repository repository, T treeObject) {
53 myParent = parent;
54 myRepository = repository;
55 myType = type;
56 myObject = treeObject;
59 @SuppressWarnings("unchecked")
60 private RepositoryTreeNode<Repository> getRepositoryNode() {
61 if (myType == RepositoryTreeNodeType.REPO) {
62 return (RepositoryTreeNode<Repository>) this;
63 } else {
64 return getParent().getRepositoryNode();
68 /**
69 * @return the parent, or null
71 public RepositoryTreeNode getParent() {
72 return myParent;
75 /**
76 * @return the type
78 public RepositoryTreeNodeType getType() {
79 return myType;
82 /**
83 * @return the repository
85 public Repository getRepository() {
86 return myRepository;
89 /**
90 * Depending on the node type, the returned type is:
92 * <table border=1>
93 * <th>Type</th>
94 * <th>Object type</th>
95 * <tr>
96 * <td>{@link RepositoryTreeNodeType#BRANCHES}</td>
97 * <td>{@link String}</td>
98 * </tr>
99 * <tr>
100 * <td>{@link RepositoryTreeNodeType#LOCALBRANCHES}</td>
101 * <td>{@link String}</td>
102 * </tr>
103 * <tr>
104 * <td>{@link RepositoryTreeNodeType#REMOTEBRANCHES}</td>
105 * <td>{@link String}</td>
106 * </tr>
107 * <tr>
108 * <td>{@link RepositoryTreeNodeType#TAGS}</td>
109 * <td>{@link String}</td>
110 * </tr>
111 * <tr>
112 * <td>{@link RepositoryTreeNodeType#REMOTE}</td>
113 * <td>{@link String}</td>
114 * </tr>
115 * <tr>
116 * <td>{@link RepositoryTreeNodeType#REMOTES}</td>
117 * <td>{@link String}</td>
118 * </tr>
119 * <tr>
120 * <td>{@link RepositoryTreeNodeType#REPO}</td>
121 * <td>{@link Repository}</td>
122 * </tr>
123 * </table>
125 * @return the type-specific object
127 public T getObject() {
128 return myObject;
131 @Override
132 public int hashCode() {
133 final int prime = 31;
134 int result = 1;
135 switch (myType) {
136 case REPO:
137 // fall through
138 case REMOTES:
139 // fall through
140 case LOCALBRANCHES:
141 // fall through
142 case REMOTEBRANCHES:
143 // fall through
144 case BRANCHES:
145 // fall through
146 case SYMBOLICREFS:
147 // fall through
148 case WORKINGDIR:
149 result = prime
150 * result
151 + ((myObject == null) ? 0 : ((Repository) myObject)
152 .getDirectory().hashCode());
153 break;
154 case REF:
155 // fall through
156 case TAG:
157 // fall through
158 case SYMBOLICREF:
159 result = prime
160 * result
161 + ((myObject == null) ? 0 : ((Ref) myObject).getName()
162 .hashCode());
163 break;
164 case FILE:
165 // fall through
166 case FOLDER:
167 result = prime
168 * result
169 + ((myObject == null) ? 0 : ((File) myObject).getPath()
170 .hashCode());
171 break;
172 case TAGS:
173 // fall through
174 case REMOTE:
175 // fall through
176 case PUSH:
177 // fall through
178 case FETCH:
179 // fall through
180 case ERROR:
181 result = prime * result
182 + ((myObject == null) ? 0 : myObject.hashCode());
186 result = prime * result
187 + ((myParent == null) ? 0 : myParent.hashCode());
188 result = prime
189 * result
190 + ((myRepository == null) ? 0 : myRepository.getDirectory()
191 .hashCode());
192 result = prime * result + ((myType == null) ? 0 : myType.hashCode());
193 return result;
196 @Override
197 public boolean equals(Object obj) {
198 if (this == obj)
199 return true;
200 if (obj == null)
201 return false;
202 if (getClass() != obj.getClass())
203 return false;
205 RepositoryTreeNode other = (RepositoryTreeNode) obj;
207 if (myType == null) {
208 if (other.myType != null)
209 return false;
210 } else if (!myType.equals(other.myType))
211 return false;
212 if (myParent == null) {
213 if (other.myParent != null)
214 return false;
215 } else if (!myParent.equals(other.myParent))
216 return false;
217 if (myRepository == null) {
218 if (other.myRepository != null)
219 return false;
220 } else if (!myRepository.getDirectory().equals(
221 other.myRepository.getDirectory()))
222 return false;
223 if (myObject == null) {
224 if (other.myObject != null)
225 return false;
226 } else if (!checkObjectsEqual(other.myObject))
227 return false;
229 return true;
232 public int compareTo(RepositoryTreeNode otherNode) {
233 int typeDiff = otherNode.getType().ordinal() - this.myType.ordinal();
234 if (typeDiff != 0)
235 return typeDiff;
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
241 switch (myType) {
243 case BRANCHES:
244 // fall through
245 case LOCALBRANCHES:
246 // fall through
247 case REMOTEBRANCHES:
248 // fall through
249 case REMOTES:
250 // fall through
251 case SYMBOLICREFS:
252 // fall through
253 case TAGS:
254 // fall through
255 case WORKINGDIR:
256 return 0;
258 case FETCH:
259 // fall through
260 case PUSH:
261 // fall through
262 case REMOTE:
263 return ((String) myObject)
264 .compareTo((String) otherNode.getObject());
265 case FILE:
266 // fall through
267 case FOLDER:
268 return ((File) myObject).getName().compareTo(
269 ((File) otherNode.getObject()).getName());
270 case TAG:
271 // fall through
272 case SYMBOLICREF:
273 // fall through
274 case REF:
275 return ((Ref) myObject).getName().compareTo(
276 ((Ref) otherNode.getObject()).getName());
277 case REPO:
278 int nameCompare = ((Repository) myObject).getDirectory()
279 .getParentFile().getName().compareTo(
280 (((Repository) otherNode.getObject())
281 .getDirectory().getParentFile().getName()));
282 if (nameCompare != 0)
283 return nameCompare;
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()));
292 return 0;
295 private boolean checkObjectsEqual(Object otherObject) {
296 switch (myType) {
297 case REPO:
298 // fall through
299 case REMOTES:
300 // fall through
301 case BRANCHES:
302 // fall through
303 case LOCALBRANCHES:
304 // fall through
305 case REMOTEBRANCHES:
306 // fall through
307 case SYMBOLICREFS:
308 // fall through
309 case ERROR:
310 // fall through
311 case WORKINGDIR:
312 return ((Repository) myObject).getDirectory().equals(
313 ((Repository) otherObject).getDirectory());
314 case REF:
315 // fall through
316 case TAG:
317 // fall through
318 case SYMBOLICREF:
319 return ((Ref) myObject).getName().equals(
320 ((Ref) otherObject).getName());
321 case FOLDER:
322 // fall through
323 case FILE:
324 return ((File) myObject).getPath().equals(
325 ((File) otherObject).getPath());
326 case REMOTE:
327 // fall through
328 case FETCH:
329 // fall through
330 case PUSH:
331 // fall through
332 case TAGS:
333 return myObject.equals(otherObject);
335 return false;
339 * Specifies the type of a {@link RepositoryTreeNode}
341 public enum RepositoryTreeNodeType {
343 /** */
344 REPO(UIIcons.REPOSITORY.createImage()), //
345 /** */
346 BRANCHES(UIIcons.BRANCHES.createImage()), //
347 /** */
348 REF(UIIcons.BRANCH.createImage()), //
349 /** */
350 LOCALBRANCHES(PlatformUI.getWorkbench().getSharedImages().getImage(
351 ISharedImages.IMG_OBJ_FOLDER)), //
352 /** */
353 REMOTEBRANCHES(PlatformUI.getWorkbench().getSharedImages().getImage(
354 ISharedImages.IMG_OBJ_FOLDER)), //
355 /** */
356 TAGS(UIIcons.TAGS.createImage()), //
357 /** */
358 SYMBOLICREFS(PlatformUI.getWorkbench().getSharedImages().getImage(
359 ISharedImages.IMG_OBJ_FOLDER)), //
360 /** */
361 SYMBOLICREF(PlatformUI.getWorkbench().getSharedImages().getImage(
362 ISharedImages.IMG_OBJ_FILE)), // TODO icon
363 /** */
364 TAG(UIIcons.TAG.createImage()), //
365 /** */
366 FILE(PlatformUI.getWorkbench().getSharedImages().getImage(
367 ISharedImages.IMG_OBJ_FILE)), //
368 /** */
369 FOLDER(PlatformUI.getWorkbench().getSharedImages().getImage(
370 ISharedImages.IMG_OBJ_FOLDER)), //
371 /** */
372 REMOTES(UIIcons.REMOTE_REPOSITORY.createImage()), //
373 /** */
374 REMOTE(PlatformUI.getWorkbench().getSharedImages().getImage(
375 ISharedImages.IMG_OBJ_FOLDER)), //
376 /** */
377 FETCH(UIIcons.IMPORT.createImage()), // TODO icon
378 /** */
379 PUSH(UIIcons.EXPORT.createImage()), // TODO icon
380 /** */
381 WORKINGDIR(PlatformUI.getWorkbench().getSharedImages().getImage(
382 ISharedImages.IMG_OBJ_FOLDER)), //
383 /** */
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(
395 iconName);
396 } else {
397 myImage = null;
402 private RepositoryTreeNodeType(Image icon) {
403 myImage = icon;
408 * @return the icon for this type
410 public Image getIcon() {
411 return myImage;