Fix build
[egit.git] / org.eclipse.egit.ui / src / org / eclipse / egit / ui / internal / GitResourceNode.java
blob90a4ed4a3d7a5bdfe692e678e855489fb6dddbad
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;
30 /**
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 {
35 TreeEntry entry;
36 GitResourceNode[] children;
37 String contentIdentifier;
39 /**
40 * Construct a resource not for a {@link TreeEntry}
41 * @param e The {@link TreeEntry}
43 public GitResourceNode(TreeEntry e) {
44 entry = e;
47 public Object[] getChildren() {
48 if (children != null)
49 return children;
50 if (entry instanceof Tree) {
51 try {
52 Tree t = (Tree)entry;
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];
64 return children;
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) {
73 try {
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);
82 return null;
85 return null;
88 public String getName() {
89 if (entry != null)
90 return entry.getFullName();
91 else
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;
102 else {
103 if (entry != null) {
104 String name = entry.getName();
105 if (name != null) {
106 int index = name.lastIndexOf('.');
107 if (index == -1)
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$
114 } else
115 return ""; //$NON-NLS-1$
120 * @return a user friendly version identification of the resource
122 public String getContentIdentifier() {
123 return contentIdentifier;