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
.egit
.ui
.internal
.repository
.tree
.RepositoryTreeNode
;
21 import org
.eclipse
.jface
.resource
.CompositeImageDescriptor
;
22 import org
.eclipse
.jface
.viewers
.ColumnViewer
;
23 import org
.eclipse
.jface
.viewers
.LabelProvider
;
24 import org
.eclipse
.jgit
.lib
.Constants
;
25 import org
.eclipse
.jgit
.lib
.Ref
;
26 import org
.eclipse
.jgit
.lib
.Repository
;
27 import org
.eclipse
.swt
.graphics
.Image
;
28 import org
.eclipse
.swt
.graphics
.Point
;
29 import org
.eclipse
.swt
.graphics
.Rectangle
;
32 * Label Provider for the Git Repositories View
34 public class RepositoriesViewLabelProvider
extends LabelProvider
{
37 * A map of regular images to their decorated counterpart.
39 private Map
<Image
, Image
> decoratedImages
= new HashMap
<Image
, Image
>();
41 // private DefaultInformationControl infoControl;
47 public RepositoriesViewLabelProvider(final ColumnViewer viewer
) {
49 viewer
.setLabelProvider(this);
50 // we could implement some hover here to display additional information
51 // viewer.getTree().addMouseTrackListener(new MouseTrackAdapter() {
54 // public void mouseHover(MouseEvent e) {
56 // Point eventPoint = new Point(e.x, e.y);
58 // TreeItem item = viewer.getTree().getItem(eventPoint);
59 // if (item != null) {
61 // RepositoryTreeNode node = (RepositoryTreeNode) item
63 // String text = node.getRepository().getDirectory()
64 // .getCanonicalPath();
66 // final ViewerCell cell = viewer.getCell(eventPoint);
68 // if (infoControl != null && infoControl.isVisible()) {
69 // infoControl.setVisible(false);
72 // GC testGc = new GC(cell.getControl());
73 // final Point textExtent = testGc.textExtent(text);
76 // if (infoControl == null || !infoControl.isVisible()) {
78 // IInformationPresenter ips = new IInformationPresenter() {
80 // public String updatePresentation(
81 // Display display, String hoverInfo,
82 // TextPresentation presentation,
83 // int maxWidth, int maxHeight) {
89 // infoControl = new DefaultInformationControl(Display
90 // .getCurrent().getActiveShell().getShell(),
94 // public void setInformation(String content) {
95 // super.setInformation(content);
96 // super.setSize(textExtent.x, textExtent.y);
102 // Point dispPoint = viewer.getControl().toDisplay(
105 // infoControl.setLocation(dispPoint);
107 // // the default info provider works better with \r ...
108 // infoControl.setInformation(text);
110 // final MouseMoveListener moveListener = new
111 // MouseMoveListener() {
113 // public void mouseMove(MouseEvent evt) {
114 // infoControl.setVisible(false);
115 // cell.getControl().removeMouseMoveListener(this);
120 // cell.getControl().addMouseMoveListener(moveListener);
122 // infoControl.setVisible(true);
133 public Image
getImage(Object element
) {
134 return decorateImage(
135 ((RepositoryTreeNode
) element
).getType().getIcon(), element
);
139 public String
getText(Object element
) {
141 RepositoryTreeNode node
= (RepositoryTreeNode
) element
;
142 switch (node
.getType()) {
144 File directory
= ((Repository
) node
.getObject()).getDirectory();
145 StringBuilder sb
= new StringBuilder();
146 sb
.append(directory
.getParentFile().getName());
147 sb
.append(" - "); //$NON-NLS-1$
148 sb
.append(directory
.getAbsolutePath());
149 return sb
.toString();
153 return ((File
) node
.getObject()).getName();
155 return UIText
.RepositoriesView_Branches_Nodetext
;
157 return UIText
.RepositoriesViewLabelProvider_LocalBranchesNodetext
;
159 return UIText
.RepositoriesViewLabelProvider_RemoteBrancheNodetext
;
161 return UIText
.RepositoriesViewLabelProvider_TagsNodeText
;
163 return UIText
.RepositoriesViewLabelProvider_SymbolicRefNodeText
;
165 return UIText
.RepositoriesView_RemotesNodeText
;
169 return (String
) node
.getObject();
175 Ref ref
= (Ref
) node
.getObject();
177 String refName
= node
.getRepository().shortenRefName(ref
.getName());
178 if (ref
.isSymbolic()) {
179 refName
= refName
+ " - " //$NON-NLS-1$
180 + ref
.getLeaf().getName();
184 if (node
.getRepository().getConfig().getBoolean(
185 "core", "bare", false)) //$NON-NLS-1$ //$NON-NLS-2$
186 return UIText
.RepositoriesView_WorkingDir_treenode
187 + " - " //$NON-NLS-1$
188 + UIText
.RepositoriesViewLabelProvider_BareRepositoryMessage
;
190 return UIText
.RepositoriesView_WorkingDir_treenode
+ " - " //$NON-NLS-1$
191 + node
.getRepository().getWorkDir().getAbsolutePath();
195 return (String
) node
.getObject();
203 public void dispose() {
204 // dispose of our decorated images
205 for (Image image
: decoratedImages
.values()) {
208 decoratedImages
.clear();
212 private Image
decorateImage(final Image image
, Object element
) {
214 RepositoryTreeNode node
= (RepositoryTreeNode
) element
;
215 switch (node
.getType()) {
220 // if the branch or tag is checked out,
221 // we want to decorate the corresponding
222 // node with a little check indicator
223 String refName
= ((Ref
) node
.getObject()).getName();
226 String compareString
;
229 branchName
= node
.getRepository().getFullBranch();
230 if (branchName
== null)
232 if (refName
.startsWith(Constants
.R_HEADS
)) {
233 // local branch: HEAD would be on the branch
234 compareString
= refName
;
235 } else if (refName
.startsWith(Constants
.R_TAGS
)) {
236 // tag: HEAD would be on the commit id to which the tag is
238 compareString
= node
.getRepository().mapTag(refName
)
239 .getObjId().getName();
240 } else if (refName
.startsWith(Constants
.R_REMOTES
)) {
241 // remote branch: HEAD would be on the commit id to which
242 // the branch is pointing
243 compareString
= node
.getRepository().mapCommit(refName
)
244 .getCommitId().getName();
246 // some other symbolic reference
249 } catch (IOException e1
) {
253 if (compareString
.equals(branchName
)) {
254 return getDecoratedImage(image
);
264 private Image
getDecoratedImage(final Image image
) {
265 // check if we have a decorated image yet or not
266 Image decoratedImage
= decoratedImages
.get(image
);
267 if (decoratedImage
== null) {
269 CompositeImageDescriptor cd
= new CompositeImageDescriptor() {
272 protected Point
getSize() {
273 Rectangle bounds
= image
.getBounds();
274 return new Point(bounds
.width
, bounds
.height
);
278 protected void drawCompositeImage(int width
, int height
) {
279 drawImage(image
.getImageData(), 0, 0);
280 drawImage(UIIcons
.OVR_CHECKEDOUT
.getImageData(), 0, 0);
284 decoratedImage
= cd
.createImage();
286 decoratedImages
.put(image
, decoratedImage
);
288 return decoratedImage
;