rename org.spearce.egit -> org.eclipse.egit and bump version to 0.5.0
[egit/imyousuf.git] / org.eclipse.egit.ui / src / org / eclipse / egit / ui / internal / GitResourceNode.java
blob97d83ed3bf531a88364748b53e272f2f5953e8fa
1 /*******************************************************************************
2 * Copyright (C) 2007, Robin Rosenberg <robin.rosenberg@dewire.com>
3 * Copyright (C) 2008, Roger C. Soares <rogersoares@intelinet.com.br>
4 * Copyright (C) 2007, Shawn O. Pearce <spearce@spearce.org>
6 * All rights reserved. This program and the accompanying materials
7 * are made available under the terms of the Eclipse Public License v1.0
8 * See LICENSE for the full license text, also available.
9 *******************************************************************************/
10 package org.eclipse.egit.ui.internal;
12 import java.io.ByteArrayInputStream;
13 import java.io.IOException;
14 import java.io.InputStream;
16 import org.eclipse.compare.BufferedContent;
17 import org.eclipse.compare.CompareUI;
18 import org.eclipse.compare.ITypedElement;
19 import org.eclipse.compare.structuremergeviewer.IStructureComparator;
20 import org.eclipse.core.runtime.CoreException;
21 import org.eclipse.swt.graphics.Image;
22 import org.spearce.jgit.lib.FileTreeEntry;
23 import org.spearce.jgit.lib.ObjectId;
24 import org.spearce.jgit.lib.ObjectLoader;
25 import org.spearce.jgit.lib.Tree;
26 import org.spearce.jgit.lib.TreeEntry;
28 /**
29 * A resource node is for letting Eclipse access data in the git repo in a hierarchical
30 * fashion, e.g. for the compare editor.
32 public class GitResourceNode extends BufferedContent implements IStructureComparator, ITypedElement {
33 TreeEntry entry;
34 GitResourceNode[] children;
35 String contentIdentifier;
37 /**
38 * Construct a resource not for a {@link TreeEntry}
39 * @param e The {@link TreeEntry}
41 public GitResourceNode(TreeEntry e) {
42 entry = e;
45 public Object[] getChildren() {
46 if (children != null)
47 return children;
48 if (entry instanceof Tree) {
49 try {
50 Tree t = (Tree)entry;
51 children = new GitResourceNode[t.memberCount()];
52 for (int i=0; i<children.length; ++i) {
53 children[i] = new GitResourceNode(t.members()[i]);
55 } catch (IOException e) {
56 // TODO: eclipse error handling
57 e.printStackTrace();
58 children = new GitResourceNode[0];
61 return children;
64 public boolean equals(Object obj) {
65 return entry.getId().equals(((GitResourceNode)obj).entry.getId());
68 protected InputStream createStream() throws CoreException {
69 if (entry instanceof FileTreeEntry) {
70 try {
71 ObjectId id = entry.getId();
72 ObjectLoader reader = entry.getRepository().openBlob(id);
73 byte[] bytes = reader.getBytes();
74 return new ByteArrayInputStream(bytes);
75 } catch (IOException e) {
76 // TODO: eclipse error handling
77 e.printStackTrace();
78 return null;
81 return null;
84 public String getName() {
85 if (entry != null)
86 return entry.getFullName();
87 else
88 return "<none>";
91 public Image getImage() {
92 return CompareUI.getImage(getType());
95 public String getType() {
96 if (entry instanceof Tree)
97 return ITypedElement.FOLDER_TYPE;
98 else {
99 if (entry != null) {
100 String name = entry.getName();
101 if (name != null) {
102 int index = name.lastIndexOf('.');
103 if (index == -1)
104 return ""; //$NON-NLS-1$
105 if (index == (name.length() - 1))
106 return ""; //$NON-NLS-1$
107 return name.substring(index + 1);
109 return "";
110 } else
111 return "";
116 * @return a user friendly version identification of the resource
118 public String getContentIdentifier() {
119 return contentIdentifier;