Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: ebee104fb0848286d73f6c5847530f302e9fcf70 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*******************************************************************************
 * Copyright (c) 2010, 2018 SAP AG and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *    Mathias Kinzler (SAP AG) - initial implementation
 *    Chris Aniszczyk <caniszczyk@gmail.com> - added styled label support
 *    Thomas Wolf <thomas.wolf@paranor.ch> - bug 536814: completely refactored
 *******************************************************************************/
package org.eclipse.egit.ui.internal.repository;

import java.util.WeakHashMap;

import org.eclipse.egit.ui.internal.GitLabels;
import org.eclipse.egit.ui.internal.repository.tree.AdditionalRefNode;
import org.eclipse.egit.ui.internal.repository.tree.RepositoryNode;
import org.eclipse.egit.ui.internal.repository.tree.WorkingDirNode;
import org.eclipse.jface.viewers.DecoratingStyledCellLabelProvider;
import org.eclipse.jface.viewers.DelegatingStyledCellLabelProvider.IStyledLabelProvider;
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.jface.viewers.StyledString;
import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.model.WorkbenchLabelProvider;

/**
 * A decorating label provider for repository tree nodes.
 */
public class RepositoryTreeNodeLabelProvider
		extends DecoratingStyledCellLabelProvider
		implements ILabelProvider, IStyledLabelProvider {

	private final WorkbenchLabelProvider labelProvider;

	private final WeakHashMap<Object, StyledString> previousDecoratedLabels = new WeakHashMap<>();

	/**
	 * Creates a new {@link RepositoryTreeNodeLabelProvider}.
	 */
	public RepositoryTreeNodeLabelProvider() {
		this(new WorkbenchLabelProvider());
	}

	private RepositoryTreeNodeLabelProvider(
			WorkbenchLabelProvider labelProvider) {
		super(labelProvider, PlatformUI.getWorkbench()
				.getDecoratorManager().getLabelDecorator(), null);
		this.labelProvider = labelProvider;
	}

	@Override
	public void dispose() {
		super.dispose();
		previousDecoratedLabels.clear();
	}

	@Override
	public StyledString getStyledText(Object element) {
		StyledString decoratedLabel = super.getStyledText(element);
		if (decoratedLabel.getString().equals(labelProvider.getText(element))) {
			// Decoration not available yet... but may be shortly. Try to
			// prevent flickering by returning the previous decorated label, if
			// any.
			StyledString previousLabel = previousDecoratedLabels.get(element);
			if (previousLabel != null) {
				return previousLabel;
			}
		}
		if (element instanceof RepositoryNode) {
			Repository repository = ((RepositoryNode) element).getRepository();
			if (repository != null) {
				decoratedLabel.append(" - ", StyledString.QUALIFIER_STYLER) //$NON-NLS-1$
						.append(repository.getDirectory().getAbsolutePath(),
								StyledString.QUALIFIER_STYLER);
			}
		} else if (element instanceof WorkingDirNode) {
			Repository repository = ((WorkingDirNode) element).getRepository();
			if (repository != null) {
				decoratedLabel.append(" - ", StyledString.QUALIFIER_STYLER) //$NON-NLS-1$
						.append(repository.getWorkTree().getAbsolutePath(),
								StyledString.QUALIFIER_STYLER);
			}
		}
		previousDecoratedLabels.put(element, decoratedLabel);
		return decoratedLabel;
	}

	@Override
	public String getText(Object element) {
		return labelProvider.getText(element);
	}

	@Override
	public String getToolTipText(Object element) {
		if (element instanceof AdditionalRefNode) {
			AdditionalRefNode additionalRefNode = (AdditionalRefNode) element;
			Ref ref = additionalRefNode.getObject();
			return GitLabels.getRefDescription(ref);
		}
		return null;
	}
}

Back to the top