1 /*******************************************************************************
2 * Copyright (c) 2010, 2018 SAP AG and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License 2.0
5 * which accompanies this distribution, and is available at
6 * https://www.eclipse.org/legal/epl-2.0/
8 * SPDX-License-Identifier: EPL-2.0
11 * Mathias Kinzler (SAP AG) - initial implementation
12 * Chris Aniszczyk <caniszczyk@gmail.com> - added styled label support
13 * Thomas Wolf <thomas.wolf@paranor.ch> - bug 536814: completely refactored
14 *******************************************************************************/
15 package org
.eclipse
.egit
.ui
.internal
.repository
;
17 import java
.util
.WeakHashMap
;
19 import org
.eclipse
.egit
.ui
.internal
.GitLabels
;
20 import org
.eclipse
.egit
.ui
.internal
.repository
.tree
.AdditionalRefNode
;
21 import org
.eclipse
.egit
.ui
.internal
.repository
.tree
.RepositoryNode
;
22 import org
.eclipse
.egit
.ui
.internal
.repository
.tree
.WorkingDirNode
;
23 import org
.eclipse
.jface
.viewers
.DecoratingStyledCellLabelProvider
;
24 import org
.eclipse
.jface
.viewers
.DelegatingStyledCellLabelProvider
.IStyledLabelProvider
;
25 import org
.eclipse
.jface
.viewers
.ILabelProvider
;
26 import org
.eclipse
.jface
.viewers
.StyledString
;
27 import org
.eclipse
.jgit
.lib
.Ref
;
28 import org
.eclipse
.jgit
.lib
.Repository
;
29 import org
.eclipse
.ui
.PlatformUI
;
30 import org
.eclipse
.ui
.model
.WorkbenchLabelProvider
;
33 * A decorating label provider for repository tree nodes.
35 public class RepositoryTreeNodeLabelProvider
36 extends DecoratingStyledCellLabelProvider
37 implements ILabelProvider
, IStyledLabelProvider
{
39 private final WorkbenchLabelProvider labelProvider
;
42 * Keeps the last label. If the label we originally get is undecorated, we
43 * return this last decorated label instead to prevent flickering. When the
44 * asynchronous lightweight decorator then has computed the decoration, the
45 * label will be updated. Note that this works only because our
46 * RepositoryTreeNodeDecorator always decorates! (If there's no decoration,
47 * it appends a single blank to ensure the decorated label is different from
48 * the undecorated one.)
50 * For images, there is no such work-around, and thus we need to do the
51 * image decorations in the label provider (in the
52 * RepositoryTreeNodeWorkbenchAdapter in our case) in the UI thread.
54 private final WeakHashMap
<Object
, StyledString
> previousDecoratedLabels
= new WeakHashMap
<>();
57 * Creates a new {@link RepositoryTreeNodeLabelProvider}.
59 public RepositoryTreeNodeLabelProvider() {
60 this(new WorkbenchLabelProvider());
63 private RepositoryTreeNodeLabelProvider(
64 WorkbenchLabelProvider labelProvider
) {
65 super(labelProvider
, PlatformUI
.getWorkbench()
66 .getDecoratorManager().getLabelDecorator(), null);
67 this.labelProvider
= labelProvider
;
71 public void dispose() {
73 previousDecoratedLabels
.clear();
77 public StyledString
getStyledText(Object element
) {
78 StyledString decoratedLabel
= super.getStyledText(element
);
79 String decoratedValue
= decoratedLabel
.getString();
80 String simpleValue
= labelProvider
.getText(element
);
81 if (decoratedValue
.equals(simpleValue
)) {
82 // Decoration not available yet... but may be shortly. Try to
83 // prevent flickering by returning the previous decorated label, if
85 StyledString previousLabel
= previousDecoratedLabels
.get(element
);
86 if (previousLabel
!= null) {
89 } else if (decoratedValue
.trim().equals(simpleValue
)) {
91 decoratedLabel
= labelProvider
.getStyledText(element
);
93 if (element
instanceof RepositoryNode
) {
94 Repository repository
= ((RepositoryNode
) element
).getRepository();
95 if (repository
!= null) {
96 decoratedLabel
.append(" - ", StyledString
.QUALIFIER_STYLER
) //$NON-NLS-1$
97 .append(repository
.getDirectory().getAbsolutePath(),
98 StyledString
.QUALIFIER_STYLER
);
100 } else if (element
instanceof WorkingDirNode
) {
101 Repository repository
= ((WorkingDirNode
) element
).getRepository();
102 if (repository
!= null) {
103 decoratedLabel
.append(" - ", StyledString
.QUALIFIER_STYLER
) //$NON-NLS-1$
104 .append(repository
.getWorkTree().getAbsolutePath(),
105 StyledString
.QUALIFIER_STYLER
);
108 previousDecoratedLabels
.put(element
, decoratedLabel
);
109 return decoratedLabel
;
113 public String
getText(Object element
) {
114 return labelProvider
.getText(element
);
118 public String
getToolTipText(Object element
) {
119 if (element
instanceof AdditionalRefNode
) {
120 AdditionalRefNode additionalRefNode
= (AdditionalRefNode
) element
;
121 Ref ref
= additionalRefNode
.getObject();
122 return GitLabels
.getRefDescription(ref
);