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
.egit
.ui
.internal
.trace
.GitTraceLocation
;
23 import org
.eclipse
.swt
.graphics
.Image
;
24 import org
.eclipse
.jgit
.lib
.FileTreeEntry
;
25 import org
.eclipse
.jgit
.lib
.ObjectId
;
26 import org
.eclipse
.jgit
.lib
.ObjectLoader
;
27 import org
.eclipse
.jgit
.lib
.Tree
;
28 import org
.eclipse
.jgit
.lib
.TreeEntry
;
31 * A resource node is for letting Eclipse access data in the git repo in a hierarchical
32 * fashion, e.g. for the compare editor.
34 public class GitResourceNode
extends BufferedContent
implements IStructureComparator
, ITypedElement
{
36 GitResourceNode
[] children
;
37 String contentIdentifier
;
40 * Construct a resource not for a {@link TreeEntry}
41 * @param e The {@link TreeEntry}
43 public GitResourceNode(TreeEntry e
) {
47 public Object
[] getChildren() {
50 if (entry
instanceof Tree
) {
53 children
= new GitResourceNode
[t
.memberCount()];
54 for (int i
=0; i
<children
.length
; ++i
) {
55 children
[i
] = new GitResourceNode(t
.members()[i
]);
57 } catch (IOException e
) {
58 // TODO: eclipse error handling
59 if (GitTraceLocation
.UI
.isActive())
60 GitTraceLocation
.getTrace().trace(GitTraceLocation
.UI
.getLocation(), e
.getMessage(), e
);
61 children
= new GitResourceNode
[0];
67 public boolean equals(Object obj
) {
68 return entry
.getId().equals(((GitResourceNode
)obj
).entry
.getId());
71 protected InputStream
createStream() throws CoreException
{
72 if (entry
instanceof FileTreeEntry
) {
74 ObjectId id
= entry
.getId();
75 ObjectLoader reader
= entry
.getRepository().openBlob(id
);
76 byte[] bytes
= reader
.getBytes();
77 return new ByteArrayInputStream(bytes
);
78 } catch (IOException e
) {
79 // TODO: eclipse error handling
80 if (GitTraceLocation
.UI
.isActive())
81 GitTraceLocation
.getTrace().trace(GitTraceLocation
.UI
.getLocation(), e
.getMessage(), e
);
88 public String
getName() {
90 return entry
.getFullName();
92 return "<none>"; //$NON-NLS-1$
95 public Image
getImage() {
96 return CompareUI
.getImage(getType());
99 public String
getType() {
100 if (entry
instanceof Tree
)
101 return ITypedElement
.FOLDER_TYPE
;
104 String name
= entry
.getName();
106 int index
= name
.lastIndexOf('.');
108 return ""; //$NON-NLS-1$
109 if (index
== (name
.length() - 1))
110 return ""; //$NON-NLS-1$
111 return name
.substring(index
+ 1);
113 return ""; //$NON-NLS-1$
115 return ""; //$NON-NLS-1$
120 * @return a user friendly version identification of the resource
122 public String
getContentIdentifier() {
123 return contentIdentifier
;