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
;
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
{
34 GitResourceNode
[] children
;
35 String contentIdentifier
;
38 * Construct a resource not for a {@link TreeEntry}
39 * @param e The {@link TreeEntry}
41 public GitResourceNode(TreeEntry e
) {
45 public Object
[] getChildren() {
48 if (entry
instanceof Tree
) {
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
58 children
= new GitResourceNode
[0];
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
) {
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
84 public String
getName() {
86 return entry
.getFullName();
91 public Image
getImage() {
92 return CompareUI
.getImage(getType());
95 public String
getType() {
96 if (entry
instanceof Tree
)
97 return ITypedElement
.FOLDER_TYPE
;
100 String name
= entry
.getName();
102 int index
= name
.lastIndexOf('.');
104 return ""; //$NON-NLS-1$
105 if (index
== (name
.length() - 1))
106 return ""; //$NON-NLS-1$
107 return name
.substring(index
+ 1);
116 * @return a user friendly version identification of the resource
118 public String
getContentIdentifier() {
119 return contentIdentifier
;