Remove System.out.println from RevWalkFilterTest
[jgit.git] / org.spearce.egit.ui / src / org / spearce / egit / ui / internal / components / RefContentProposal.java
blobb63c42858f15a4ae75cb9f0c80b9f98d04c3a6f0
1 /*******************************************************************************
2 * Copyright (C) 2008, Marek Zawirski <marek.zawirski@gmail.com>
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * See LICENSE for the full license text, also available.
7 *******************************************************************************/
8 package org.spearce.egit.ui.internal.components;
10 import java.io.IOException;
11 import java.sql.Blob;
13 import org.eclipse.jface.fieldassist.IContentProposal;
14 import org.spearce.egit.ui.Activator;
15 import org.spearce.jgit.lib.Commit;
16 import org.spearce.jgit.lib.Constants;
17 import org.spearce.jgit.lib.ObjectId;
18 import org.spearce.jgit.lib.PersonIdent;
19 import org.spearce.jgit.lib.Ref;
20 import org.spearce.jgit.lib.Repository;
21 import org.spearce.jgit.lib.Tag;
22 import org.spearce.jgit.lib.Tree;
24 /**
25 * Content proposal class for refs names, specifically Ref objects - name with
26 * optionally associated object id. This class can be used for Eclipse field
27 * assist as content proposal.
28 * <p>
29 * Content of this proposal is simply a ref name, but description and labels
30 * tries to be smarter - showing easier to read label for user (stripping
31 * prefixes) and information about pointed object if it exists locally.
33 public class RefContentProposal implements IContentProposal {
34 private static final String PREFIXES[] = new String[] { Constants.R_HEADS,
35 Constants.R_REMOTES, Constants.R_TAGS };
37 private static final String PREFIXES_DESCRIPTIONS[] = new String[] {
38 " [branch]", " [tracking branch]", " [tag]" };
40 private static void appendObjectSummary(final StringBuilder sb,
41 final String type, final PersonIdent author, final String message) {
42 sb.append(type + " by ");
43 sb.append(author.getName());
44 sb.append("\n");
45 sb.append(author.getWhen());
46 sb.append("\n\n");
47 final int newLine = message.indexOf('\n');
48 final int last = (newLine != -1 ? newLine : message.length());
49 sb.append(message.substring(0, last));
52 private final Repository db;
54 private final String refName;
56 private final ObjectId objectId;
58 /**
59 * Create content proposal for specified ref.
61 * @param repo
62 * repository for accessing information about objects. Could be a
63 * local repository even for remote objects.
64 * @param ref
65 * ref being a content proposal. May have null or locally
66 * non-existent object id.
68 public RefContentProposal(final Repository repo, final Ref ref) {
69 this(repo, ref.getName(), ref.getObjectId());
72 /**
73 * Create content proposal for specified ref name and object id.
75 * @param repo
76 * repository for accessing information about objects. Could be a
77 * local repository even for remote objects.
78 * @param refName
79 * ref name being a content proposal.
80 * @param objectId
81 * object being pointed by this ref name. May be null or locally
82 * non-existent object.
84 public RefContentProposal(final Repository repo, final String refName,
85 final ObjectId objectId) {
86 this.db = repo;
87 this.refName = refName;
88 this.objectId = objectId;
91 public String getContent() {
92 return refName;
95 public int getCursorPosition() {
96 return refName.length();
99 public String getDescription() {
100 if (objectId == null)
101 return null;
102 final Object obj;
103 try {
104 obj = db.mapObject(objectId, refName);
105 } catch (IOException e) {
106 Activator.logError("Unable to read object " + objectId
107 + " for content proposal assistance", e);
108 return null;
111 final StringBuilder sb = new StringBuilder();
112 sb.append(refName);
113 sb.append('\n');
114 sb.append(objectId.abbreviate(db).name());
115 sb.append(" - ");
116 if (obj instanceof Commit) {
117 final Commit c = ((Commit) obj);
118 appendObjectSummary(sb, "commit", c.getAuthor(), c.getMessage());
119 } else if (obj instanceof Tag) {
120 final Tag t = ((Tag) obj);
121 appendObjectSummary(sb, "tag", t.getAuthor(), t.getMessage());
122 } else if (obj instanceof Tree) {
123 sb.append("tree");
124 } else if (obj instanceof Blob) {
125 sb.append("blob");
126 } else
127 sb.append("locally unknown object");
128 return sb.toString();
131 public String getLabel() {
132 for (int i = 0; i < PREFIXES.length; i++)
133 if (refName.startsWith(PREFIXES[i]))
134 return refName.substring(PREFIXES[i].length())
135 + PREFIXES_DESCRIPTIONS[i];
136 return refName;