1 /*******************************************************************************
2 * Copyright (c) 2010 SAP AG.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
9 * Mathias Kinzler (SAP AG) - initial implementation
10 *******************************************************************************/
11 package org
.eclipse
.egit
.ui
.internal
.repository
;
14 import java
.io
.IOException
;
15 import java
.util
.HashMap
;
18 import org
.eclipse
.egit
.ui
.UIIcons
;
19 import org
.eclipse
.egit
.ui
.UIText
;
20 import org
.eclipse
.jface
.resource
.CompositeImageDescriptor
;
21 import org
.eclipse
.jface
.viewers
.ColumnViewer
;
22 import org
.eclipse
.jface
.viewers
.LabelProvider
;
23 import org
.eclipse
.jgit
.lib
.Constants
;
24 import org
.eclipse
.jgit
.lib
.Ref
;
25 import org
.eclipse
.jgit
.lib
.Repository
;
26 import org
.eclipse
.swt
.graphics
.Image
;
27 import org
.eclipse
.swt
.graphics
.Point
;
28 import org
.eclipse
.swt
.graphics
.Rectangle
;
31 * Label Provider for the Git Repositories View
33 public class RepositoriesViewLabelProvider
extends LabelProvider
{
36 * A map of regular images to their decorated counterpart.
38 private Map
<Image
, Image
> decoratedImages
= new HashMap
<Image
, Image
>();
40 // private DefaultInformationControl infoControl;
46 public RepositoriesViewLabelProvider(final ColumnViewer viewer
) {
48 viewer
.setLabelProvider(this);
49 // we could implement some hover here to display additional information
50 // viewer.getTree().addMouseTrackListener(new MouseTrackAdapter() {
53 // public void mouseHover(MouseEvent e) {
55 // Point eventPoint = new Point(e.x, e.y);
57 // TreeItem item = viewer.getTree().getItem(eventPoint);
58 // if (item != null) {
60 // RepositoryTreeNode node = (RepositoryTreeNode) item
62 // String text = node.getRepository().getDirectory()
63 // .getCanonicalPath();
65 // final ViewerCell cell = viewer.getCell(eventPoint);
67 // if (infoControl != null && infoControl.isVisible()) {
68 // infoControl.setVisible(false);
71 // GC testGc = new GC(cell.getControl());
72 // final Point textExtent = testGc.textExtent(text);
75 // if (infoControl == null || !infoControl.isVisible()) {
77 // IInformationPresenter ips = new IInformationPresenter() {
79 // public String updatePresentation(
80 // Display display, String hoverInfo,
81 // TextPresentation presentation,
82 // int maxWidth, int maxHeight) {
88 // infoControl = new DefaultInformationControl(Display
89 // .getCurrent().getActiveShell().getShell(),
93 // public void setInformation(String content) {
94 // super.setInformation(content);
95 // super.setSize(textExtent.x, textExtent.y);
101 // Point dispPoint = viewer.getControl().toDisplay(
104 // infoControl.setLocation(dispPoint);
106 // // the default info provider works better with \r ...
107 // infoControl.setInformation(text);
109 // final MouseMoveListener moveListener = new
110 // MouseMoveListener() {
112 // public void mouseMove(MouseEvent evt) {
113 // infoControl.setVisible(false);
114 // cell.getControl().removeMouseMoveListener(this);
119 // cell.getControl().addMouseMoveListener(moveListener);
121 // infoControl.setVisible(true);
132 public Image
getImage(Object element
) {
133 return decorateImage(
134 ((RepositoryTreeNode
) element
).getType().getIcon(), element
);
138 public String
getText(Object element
) {
140 RepositoryTreeNode node
= (RepositoryTreeNode
) element
;
141 switch (node
.getType()) {
143 File directory
= ((Repository
) node
.getObject()).getDirectory();
144 StringBuilder sb
= new StringBuilder();
145 sb
.append(directory
.getParentFile().getName());
146 sb
.append(" - "); //$NON-NLS-1$
147 sb
.append(directory
.getAbsolutePath());
148 return sb
.toString();
152 return ((File
) node
.getObject()).getName();
154 return UIText
.RepositoriesView_Branches_Nodetext
;
156 return UIText
.RepositoriesViewLabelProvider_LocalBranchesNodetext
;
158 return UIText
.RepositoriesViewLabelProvider_RemoteBrancheNodetext
;
160 return UIText
.RepositoriesViewLabelProvider_TagsNodeText
;
162 return UIText
.RepositoriesViewLabelProvider_SymbolicRefNodeText
;
164 return UIText
.RepositoriesView_RemotesNodeText
;
168 return (String
) node
.getObject();
174 Ref ref
= (Ref
) node
.getObject();
176 String refName
= node
.getRepository().shortenRefName(ref
.getName());
177 if (ref
.isSymbolic()) {
178 refName
= refName
+ " - " //$NON-NLS-1$
179 + ref
.getLeaf().getName();
183 if (node
.getRepository().getConfig().getBoolean(
184 "core", "bare", false)) //$NON-NLS-1$ //$NON-NLS-2$
185 return UIText
.RepositoriesView_WorkingDir_treenode
186 + " - " //$NON-NLS-1$
187 + UIText
.RepositoriesViewLabelProvider_BareRepositoryMessage
;
189 return UIText
.RepositoriesView_WorkingDir_treenode
+ " - " //$NON-NLS-1$
190 + node
.getRepository().getWorkDir().getAbsolutePath();
194 return (String
) node
.getObject();
202 public void dispose() {
203 // dispose of our decorated images
204 for (Image image
: decoratedImages
.values()) {
207 decoratedImages
.clear();
211 private Image
decorateImage(final Image image
, Object element
) {
213 RepositoryTreeNode node
= (RepositoryTreeNode
) element
;
214 switch (node
.getType()) {
219 // if the branch or tag is checked out,
220 // we want to decorate the corresponding
221 // node with a little check indicator
222 String refName
= ((Ref
) node
.getObject()).getName();
225 String compareString
;
228 branchName
= node
.getRepository().getFullBranch();
229 if (branchName
== null)
231 if (refName
.startsWith(Constants
.R_HEADS
)) {
232 // local branch: HEAD would be on the branch
233 compareString
= refName
;
234 } else if (refName
.startsWith(Constants
.R_TAGS
)) {
235 // tag: HEAD would be on the commit id to which the tag is
237 compareString
= node
.getRepository().mapTag(refName
)
238 .getObjId().getName();
239 } else if (refName
.startsWith(Constants
.R_REMOTES
)) {
240 // remote branch: HEAD would be on the commit id to which
241 // the branch is pointing
242 compareString
= node
.getRepository().mapCommit(refName
)
243 .getCommitId().getName();
245 // some other symbolic reference
248 } catch (IOException e1
) {
252 if (compareString
.equals(branchName
)) {
253 return getDecoratedImage(image
);
263 private Image
getDecoratedImage(final Image image
) {
264 // check if we have a decorated image yet or not
265 Image decoratedImage
= decoratedImages
.get(image
);
266 if (decoratedImage
== null) {
268 CompositeImageDescriptor cd
= new CompositeImageDescriptor() {
271 protected Point
getSize() {
272 Rectangle bounds
= image
.getBounds();
273 return new Point(bounds
.width
, bounds
.height
);
277 protected void drawCompositeImage(int width
, int height
) {
278 drawImage(image
.getImageData(), 0, 0);
279 drawImage(UIIcons
.OVR_CHECKEDOUT
.getImageData(), 0, 0);
283 decoratedImage
= cd
.createImage();
285 decoratedImages
.put(image
, decoratedImage
);
287 return decoratedImage
;