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
;
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
;
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.
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());
45 sb
.append(author
.getWhen());
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
;
59 * Create content proposal for specified ref.
62 * repository for accessing information about objects. Could be a
63 * local repository even for remote objects.
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());
73 * Create content proposal for specified ref name and object id.
76 * repository for accessing information about objects. Could be a
77 * local repository even for remote objects.
79 * ref name being a content proposal.
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
) {
87 this.refName
= refName
;
88 this.objectId
= objectId
;
91 public String
getContent() {
95 public int getCursorPosition() {
96 return refName
.length();
99 public String
getDescription() {
100 if (objectId
== null)
104 obj
= db
.mapObject(objectId
, refName
);
105 } catch (IOException e
) {
106 Activator
.logError("Unable to read object " + objectId
107 + " for content proposal assistance", e
);
111 final StringBuilder sb
= new StringBuilder();
114 sb
.append(objectId
.abbreviate(db
).name());
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
) {
124 } else if (obj
instanceof Blob
) {
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
];