Git Import Wizard
[egit.git] / org.eclipse.egit.ui / src / org / eclipse / egit / ui / internal / repository / RepositoryTreeNode.java
blob2e585e79bc6ab174fc8a849e4bcbf95e9d5c3d95
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;
22 import org.eclipse.ui.ide.IDE.SharedImages;
24 /**
25 * A node in the Git Repositories view tree
27 * @param <T>
28 * the type
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;
40 /**
41 * Constructs a node
43 * @param parent
44 * the parent (may be null)
45 * @param type
46 * the type
47 * @param repository
48 * the {@link Repository}
49 * @param treeObject
50 * an object (depending on the type)
52 public RepositoryTreeNode(RepositoryTreeNode parent,
53 RepositoryTreeNodeType type, Repository repository, T treeObject) {
54 myParent = parent;
55 myRepository = repository;
56 myType = type;
57 myObject = treeObject;
60 @SuppressWarnings("unchecked")
61 private RepositoryTreeNode<Repository> getRepositoryNode() {
62 if (myType == RepositoryTreeNodeType.REPO) {
63 return (RepositoryTreeNode<Repository>) this;
64 } else {
65 return getParent().getRepositoryNode();
69 /**
70 * @return the parent, or null
72 public RepositoryTreeNode getParent() {
73 return myParent;
76 /**
77 * @return the type
79 public RepositoryTreeNodeType getType() {
80 return myType;
83 /**
84 * @return the repository
86 public Repository getRepository() {
87 return myRepository;
90 /**
91 * Depending on the node type, the returned type is:
93 * <table border=1>
94 * <th>Type</th>
95 * <th>Object type</th>
96 * <tr>
97 * <td>{@link RepositoryTreeNodeType#BRANCHES}</td>
98 * <td>{@link String}</td>
99 * </tr>
100 * <tr>
101 * <td>{@link RepositoryTreeNodeType#PROJ}</td>
102 * <td>{@link File}</td>
103 * </tr>
104 * <tr>
105 * <td>{@link RepositoryTreeNodeType#PROJECTS}</td>
106 * <td>{@link String}</td>
107 * </tr>
108 * <tr>
109 * <td>{@link RepositoryTreeNodeType#LOCALBRANCHES}</td>
110 * <td>{@link String}</td>
111 * </tr>
112 * <tr>
113 * <td>{@link RepositoryTreeNodeType#REMOTEBRANCHES}</td>
114 * <td>{@link String}</td>
115 * </tr>
116 * <tr>
117 * <td>{@link RepositoryTreeNodeType#TAGS}</td>
118 * <td>{@link String}</td>
119 * </tr>
120 * <tr>
121 * <td>{@link RepositoryTreeNodeType#REMOTE}</td>
122 * <td>{@link String}</td>
123 * </tr>
124 * <tr>
125 * <td>{@link RepositoryTreeNodeType#REMOTES}</td>
126 * <td>{@link String}</td>
127 * </tr>
128 * <tr>
129 * <td>{@link RepositoryTreeNodeType#REPO}</td>
130 * <td>{@link Repository}</td>
131 * </tr>
132 * </table>
134 * @return the type-specific object
136 public T getObject() {
137 return myObject;
140 @Override
141 public int hashCode() {
142 final int prime = 31;
143 int result = 1;
144 switch (myType) {
145 case REPO:
146 // fall through
147 case PROJECTS:
148 // fall through
149 case REMOTES:
150 // fall through
151 case LOCALBRANCHES:
152 // fall through
153 case REMOTEBRANCHES:
154 // fall through
155 case BRANCHES:
156 // fall through
157 case SYMBOLICREFS:
158 // fall through
159 case WORKINGDIR:
160 result = prime
161 * result
162 + ((myObject == null) ? 0 : ((Repository) myObject)
163 .getDirectory().hashCode());
164 break;
165 case HEAD:
166 // fall through
167 case REF:
168 // fall through
169 case TAG:
170 // fall through
171 case SYMBOLICREF:
172 result = prime
173 * result
174 + ((myObject == null) ? 0 : ((Ref) myObject).getName()
175 .hashCode());
176 break;
177 case PROJ:
178 case FILE:
179 case FOLDER:
180 result = prime
181 * result
182 + ((myObject == null) ? 0 : ((File) myObject).getPath()
183 .hashCode());
184 break;
185 case TAGS:
186 // fall through
187 case REMOTE:
188 // fall through
189 case PUSH:
190 // fall through
191 case FETCH:
192 // fall through
193 case ERROR:
194 result = prime * result
195 + ((myObject == null) ? 0 : myObject.hashCode());
199 result = prime * result
200 + ((myParent == null) ? 0 : myParent.hashCode());
201 result = prime
202 * result
203 + ((myRepository == null) ? 0 : myRepository.getDirectory()
204 .hashCode());
205 result = prime * result + ((myType == null) ? 0 : myType.hashCode());
206 return result;
209 @Override
210 public boolean equals(Object obj) {
211 if (this == obj)
212 return true;
213 if (obj == null)
214 return false;
215 if (getClass() != obj.getClass())
216 return false;
218 RepositoryTreeNode other = (RepositoryTreeNode) obj;
220 if (myType == null) {
221 if (other.myType != null)
222 return false;
223 } else if (!myType.equals(other.myType))
224 return false;
225 if (myParent == null) {
226 if (other.myParent != null)
227 return false;
228 } else if (!myParent.equals(other.myParent))
229 return false;
230 if (myRepository == null) {
231 if (other.myRepository != null)
232 return false;
233 } else if (!myRepository.getDirectory().equals(
234 other.myRepository.getDirectory()))
235 return false;
236 if (myObject == null) {
237 if (other.myObject != null)
238 return false;
239 } else if (!checkObjectsEqual(other.myObject))
240 return false;
242 return true;
245 public int compareTo(RepositoryTreeNode otherNode) {
246 int typeDiff = otherNode.getType().ordinal() - this.myType.ordinal();
247 if (typeDiff != 0)
248 return typeDiff;
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
254 switch (myType) {
256 case BRANCHES:
257 // fall through
258 case PROJECTS:
259 // fall through
260 case REMOTES:
261 // fall through
262 case WORKINGDIR:
263 return 0;
265 case FETCH:
266 // fall through
267 case PROJ:
268 // fall through
269 case PUSH:
270 // fall through
271 case REMOTE:
272 return ((String) myObject)
273 .compareTo((String) otherNode.getObject());
274 case FILE:
275 // fall through
276 case FOLDER:
277 return ((File) myObject).getName().compareTo(
278 ((File) otherNode.getObject()).getName());
279 case REF:
280 return ((Ref) myObject).getName().compareTo(
281 ((Ref) otherNode.getObject()).getName());
282 case REPO:
283 int nameCompare = ((Repository) myObject).getDirectory()
284 .getParentFile().getName().compareTo(
285 (((Repository) otherNode.getObject())
286 .getDirectory().getParentFile().getName()));
287 if (nameCompare != 0)
288 return nameCompare;
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()));
297 return 0;
300 private boolean checkObjectsEqual(Object otherObject) {
301 switch (myType) {
302 case REPO:
303 // fall through
304 case PROJECTS:
305 // fall through
306 case REMOTES:
307 // fall through
308 case BRANCHES:
309 // fall through
310 case LOCALBRANCHES:
311 // fall through
312 case REMOTEBRANCHES:
313 // fall through
314 case SYMBOLICREFS:
315 // fall through
316 case ERROR:
317 // fall through
318 case WORKINGDIR:
319 return ((Repository) myObject).getDirectory().equals(
320 ((Repository) otherObject).getDirectory());
321 case REF:
322 // fall through
323 case HEAD:
324 // fall through
325 case TAG:
326 // fall through
327 case SYMBOLICREF:
328 return ((Ref) myObject).getName().equals(
329 ((Ref) otherObject).getName());
330 case PROJ:
331 // fall through
332 case FOLDER:
333 // fall through
334 case FILE:
335 return ((File) myObject).getPath().equals(
336 ((File) otherObject).getPath());
337 case REMOTE:
338 // fall through
339 case FETCH:
340 // fall through
341 case PUSH:
342 // fall through
343 case TAGS:
344 return myObject.equals(otherObject);
346 return false;
350 * Specifies the type of a {@link RepositoryTreeNode}
352 public enum RepositoryTreeNodeType {
354 /** */
355 REPO(UIIcons.REPOSITORY.createImage()), //
356 /** */
357 PROJ(PlatformUI.getWorkbench().getSharedImages().getImage(
358 SharedImages.IMG_OBJ_PROJECT_CLOSED)), //
359 /** */
360 BRANCHES(UIIcons.BRANCHES.createImage()), //
361 /** */
362 REF(UIIcons.BRANCH.createImage()), //
363 /** */
364 HEAD(PlatformUI.getWorkbench().getSharedImages().getImage(
365 ISharedImages.IMG_OBJ_FILE)), // TODO icon
366 /** */
367 LOCALBRANCHES(PlatformUI.getWorkbench().getSharedImages().getImage(
368 ISharedImages.IMG_OBJ_FOLDER)), //
369 /** */
370 REMOTEBRANCHES(PlatformUI.getWorkbench().getSharedImages().getImage(
371 ISharedImages.IMG_OBJ_FOLDER)), //
372 /** */
373 TAGS(UIIcons.TAGS.createImage()), //
374 /** */
375 SYMBOLICREFS(PlatformUI.getWorkbench().getSharedImages().getImage(
376 ISharedImages.IMG_OBJ_FOLDER)), //
377 /** */
378 SYMBOLICREF(PlatformUI.getWorkbench().getSharedImages().getImage(
379 ISharedImages.IMG_OBJ_FILE)), // TODO icon
380 /** */
381 TAG(UIIcons.TAG.createImage()), //
382 /** */
383 FILE(PlatformUI.getWorkbench().getSharedImages().getImage(
384 ISharedImages.IMG_OBJ_FILE)), //
385 /** */
386 FOLDER(PlatformUI.getWorkbench().getSharedImages().getImage(
387 ISharedImages.IMG_OBJ_FOLDER)), //
388 /** */
389 PROJECTS(PlatformUI.getWorkbench().getSharedImages().getImage(
390 SharedImages.IMG_OBJ_PROJECT_CLOSED)), //
391 /** */
392 REMOTES(UIIcons.REMOTE_REPOSITORY.createImage()), //
393 /** */
394 REMOTE(PlatformUI.getWorkbench().getSharedImages().getImage(
395 ISharedImages.IMG_OBJ_FOLDER)), //
396 /** */
397 FETCH(UIIcons.IMPORT.createImage()), // TODO icon
398 /** */
399 PUSH(UIIcons.EXPORT.createImage()), // TODO icon
400 /** */
401 WORKINGDIR(PlatformUI.getWorkbench().getSharedImages().getImage(
402 ISharedImages.IMG_OBJ_FOLDER)), //
403 /** */
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(
415 iconName);
416 } else {
417 myImage = null;
422 private RepositoryTreeNodeType(Image icon) {
423 myImage = icon;
428 * @return the icon for this type
430 public Image getIcon() {
431 return myImage;