Initial EGit contribution to eclipse.org
[egit.git] / org.eclipse.egit.ui / src / org / eclipse / egit / ui / internal / GitResourceNode.java
blobd6830009dd7d32ef34f8aeb46f4f2d60e01805c9
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 * which accompanies this distribution, and is available at
9 * http://www.eclipse.org/legal/epl-v10.html
10 *******************************************************************************/
11 package org.eclipse.egit.ui.internal;
13 import java.io.ByteArrayInputStream;
14 import java.io.IOException;
15 import java.io.InputStream;
17 import org.eclipse.compare.BufferedContent;
18 import org.eclipse.compare.CompareUI;
19 import org.eclipse.compare.ITypedElement;
20 import org.eclipse.compare.structuremergeviewer.IStructureComparator;
21 import org.eclipse.core.runtime.CoreException;
22 import org.eclipse.swt.graphics.Image;
23 import org.eclipse.jgit.lib.FileTreeEntry;
24 import org.eclipse.jgit.lib.ObjectId;
25 import org.eclipse.jgit.lib.ObjectLoader;
26 import org.eclipse.jgit.lib.Tree;
27 import org.eclipse.jgit.lib.TreeEntry;
29 /**
30 * A resource node is for letting Eclipse access data in the git repo in a hierarchical
31 * fashion, e.g. for the compare editor.
33 public class GitResourceNode extends BufferedContent implements IStructureComparator, ITypedElement {
34 TreeEntry entry;
35 GitResourceNode[] children;
36 String contentIdentifier;
38 /**
39 * Construct a resource not for a {@link TreeEntry}
40 * @param e The {@link TreeEntry}
42 public GitResourceNode(TreeEntry e) {
43 entry = e;
46 public Object[] getChildren() {
47 if (children != null)
48 return children;
49 if (entry instanceof Tree) {
50 try {
51 Tree t = (Tree)entry;
52 children = new GitResourceNode[t.memberCount()];
53 for (int i=0; i<children.length; ++i) {
54 children[i] = new GitResourceNode(t.members()[i]);
56 } catch (IOException e) {
57 // TODO: eclipse error handling
58 e.printStackTrace();
59 children = new GitResourceNode[0];
62 return children;
65 public boolean equals(Object obj) {
66 return entry.getId().equals(((GitResourceNode)obj).entry.getId());
69 protected InputStream createStream() throws CoreException {
70 if (entry instanceof FileTreeEntry) {
71 try {
72 ObjectId id = entry.getId();
73 ObjectLoader reader = entry.getRepository().openBlob(id);
74 byte[] bytes = reader.getBytes();
75 return new ByteArrayInputStream(bytes);
76 } catch (IOException e) {
77 // TODO: eclipse error handling
78 e.printStackTrace();
79 return null;
82 return null;
85 public String getName() {
86 if (entry != null)
87 return entry.getFullName();
88 else
89 return "<none>";
92 public Image getImage() {
93 return CompareUI.getImage(getType());
96 public String getType() {
97 if (entry instanceof Tree)
98 return ITypedElement.FOLDER_TYPE;
99 else {
100 if (entry != null) {
101 String name = entry.getName();
102 if (name != null) {
103 int index = name.lastIndexOf('.');
104 if (index == -1)
105 return ""; //$NON-NLS-1$
106 if (index == (name.length() - 1))
107 return ""; //$NON-NLS-1$
108 return name.substring(index + 1);
110 return "";
111 } else
112 return "";
117 * @return a user friendly version identification of the resource
119 public String getContentIdentifier() {
120 return contentIdentifier;