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
;
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
{
35 GitResourceNode
[] children
;
36 String contentIdentifier
;
39 * Construct a resource not for a {@link TreeEntry}
40 * @param e The {@link TreeEntry}
42 public GitResourceNode(TreeEntry e
) {
46 public Object
[] getChildren() {
49 if (entry
instanceof Tree
) {
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
59 children
= new GitResourceNode
[0];
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
) {
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
85 public String
getName() {
87 return entry
.getFullName();
92 public Image
getImage() {
93 return CompareUI
.getImage(getType());
96 public String
getType() {
97 if (entry
instanceof Tree
)
98 return ITypedElement
.FOLDER_TYPE
;
101 String name
= entry
.getName();
103 int index
= name
.lastIndexOf('.');
105 return ""; //$NON-NLS-1$
106 if (index
== (name
.length() - 1))
107 return ""; //$NON-NLS-1$
108 return name
.substring(index
+ 1);
117 * @return a user friendly version identification of the resource
119 public String
getContentIdentifier() {
120 return contentIdentifier
;