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 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 *******************************************************************************/
9 package org
.eclipse
.egit
.ui
.internal
.components
;
11 import java
.io
.IOException
;
14 import org
.eclipse
.egit
.ui
.Activator
;
15 import org
.eclipse
.egit
.ui
.UIText
;
16 import org
.eclipse
.jface
.fieldassist
.IContentProposal
;
17 import org
.eclipse
.jgit
.lib
.Commit
;
18 import org
.eclipse
.jgit
.lib
.Constants
;
19 import org
.eclipse
.jgit
.lib
.ObjectId
;
20 import org
.eclipse
.jgit
.lib
.PersonIdent
;
21 import org
.eclipse
.jgit
.lib
.Ref
;
22 import org
.eclipse
.jgit
.lib
.Repository
;
23 import org
.eclipse
.jgit
.lib
.Tag
;
24 import org
.eclipse
.jgit
.lib
.Tree
;
25 import org
.eclipse
.osgi
.util
.NLS
;
28 * Content proposal class for refs names, specifically Ref objects - name with
29 * optionally associated object id. This class can be used for Eclipse field
30 * assist as content proposal.
32 * Content of this proposal is simply a ref name, but description and labels
33 * tries to be smarter - showing easier to read label for user (stripping
34 * prefixes) and information about pointed object if it exists locally.
36 public class RefContentProposal
implements IContentProposal
{
37 private static final String PREFIXES
[] = new String
[] { Constants
.R_HEADS
,
38 Constants
.R_REMOTES
, Constants
.R_TAGS
};
40 private final static String branchPF
= " [" //$NON-NLS-1$
41 + UIText
.RefContentProposal_branch
44 private final static String trackingBranchPF
= " [" //$NON-NLS-1$
45 + UIText
.RefContentProposal_trackingBranch
48 private final static String tagPF
= " [" //$NON-NLS-1$
49 + UIText
.RefContentProposal_tag
52 private static final String PREFIXES_DESCRIPTIONS
[] = new String
[] {
53 branchPF
, trackingBranchPF
, tagPF
};
55 private static void appendObjectSummary(final StringBuilder sb
,
56 final String type
, final PersonIdent author
, final String message
) {
58 sb
.append(" "); //$NON-NLS-1$
59 sb
.append(UIText
.RefContentProposal_by
);
60 sb
.append(" "); //$NON-NLS-1$
61 sb
.append(author
.getName());
62 sb
.append("\n"); //$NON-NLS-1$
63 sb
.append(author
.getWhen());
64 sb
.append("\n\n"); //$NON-NLS-1$
65 final int newLine
= message
.indexOf('\n');
66 final int last
= (newLine
!= -1 ? newLine
: message
.length());
67 sb
.append(message
.substring(0, last
));
70 private final Repository db
;
72 private final String refName
;
74 private final ObjectId objectId
;
77 * Create content proposal for specified ref.
80 * repository for accessing information about objects. Could be a
81 * local repository even for remote objects.
83 * ref being a content proposal. May have null or locally
84 * non-existent object id.
86 public RefContentProposal(final Repository repo
, final Ref ref
) {
87 this(repo
, ref
.getName(), ref
.getObjectId());
91 * Create content proposal for specified ref name and object id.
94 * repository for accessing information about objects. Could be a
95 * local repository even for remote objects.
97 * ref name being a content proposal.
99 * object being pointed by this ref name. May be null or locally
100 * non-existent object.
102 public RefContentProposal(final Repository repo
, final String refName
,
103 final ObjectId objectId
) {
105 this.refName
= refName
;
106 this.objectId
= objectId
;
109 public String
getContent() {
113 public int getCursorPosition() {
114 return refName
.length();
117 public String
getDescription() {
118 if (objectId
== null)
122 obj
= db
.mapObject(objectId
, refName
);
123 } catch (IOException e
) {
124 Activator
.logError(NLS
.bind(
125 UIText
.RefContentProposal_errorReadingObject
, objectId
), e
);
129 final StringBuilder sb
= new StringBuilder();
132 sb
.append(objectId
.abbreviate(db
).name());
133 sb
.append(" - "); //$NON-NLS-1$
134 if (obj
instanceof Commit
) {
135 final Commit c
= ((Commit
) obj
);
136 appendObjectSummary(sb
, UIText
.RefContentProposal_commit
, c
137 .getAuthor(), c
.getMessage());
138 } else if (obj
instanceof Tag
) {
139 final Tag t
= ((Tag
) obj
);
140 appendObjectSummary(sb
, UIText
.RefContentProposal_tag
, t
141 .getAuthor(), t
.getMessage());
142 } else if (obj
instanceof Tree
) {
143 sb
.append(UIText
.RefContentProposal_tree
);
144 } else if (obj
instanceof Blob
) {
145 sb
.append(UIText
.RefContentProposal_blob
);
147 sb
.append(UIText
.RefContentProposal_unknownObject
);
148 return sb
.toString();
151 public String
getLabel() {
152 for (int i
= 0; i
< PREFIXES
.length
; i
++)
153 if (refName
.startsWith(PREFIXES
[i
]))
154 return refName
.substring(PREFIXES
[i
].length())
155 + PREFIXES_DESCRIPTIONS
[i
];