Skip to content

Commit

Permalink
Add icon decoration for tracked and untracked resources
Browse files Browse the repository at this point in the history
Can be enabled/disabled in the preferences

Signed-off-by: Tor Arne Vestbø <torarnv@gmail.com>
Signed-off-by: Robin Rosenberg <robin.rosenberg@dewire.com>
  • Loading branch information
torarnv authored and robinrosenberg committed Feb 18, 2009
1 parent 58a6ba8 commit 3c0fab8
Show file tree
Hide file tree
Showing 10 changed files with 259 additions and 29 deletions.
Binary file removed org.spearce.egit.ui/icons/ovr/shared.gif
Binary file not shown.
Binary file added org.spearce.egit.ui/icons/ovr/untracked.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,15 @@ public void initializeDefaultPreferences() {
prefs.setDefault(UIPreferences.RESOURCEHISTORY_SHOW_REV_COMMENT, true);
prefs.setDefault(UIPreferences.RESOURCEHISTORY_SHOW_TOOLTIPS, false);

prefs.setDefault(UIPreferences.DECORATOR_CALCULATE_DIRTY, true);
prefs.setDefault(UIPreferences.DECORATOR_FILETEXT_DECORATION,
UIText.DecoratorPreferencesPage_fileFormatDefault);
prefs.setDefault(UIPreferences.DECORATOR_FOLDERTEXT_DECORATION,
UIText.DecoratorPreferencesPage_folderFormatDefault);
prefs.setDefault(UIPreferences.DECORATOR_PROJECTTEXT_DECORATION,
UIText.DecoratorPreferencesPage_projectFormatDefault);
prefs.setDefault(UIPreferences.DECORATOR_CALCULATE_DIRTY, true);
prefs.setDefault(UIPreferences.DECORATOR_SHOW_TRACKED_ICON, true);
prefs.setDefault(UIPreferences.DECORATOR_SHOW_UNTRACKED_ICON, true);

w = new int[] { 500, 500 };
UIPreferences.setDefault(prefs,
Expand Down
6 changes: 3 additions & 3 deletions org.spearce.egit.ui/src/org/spearce/egit/ui/UIIcons.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ public class UIIcons {
/** Decoration for resource removed from the index but not commit. */
public static final ImageDescriptor OVR_PENDING_REMOVE;

/** Decoration for resource tracked and committed in git. */
public static final ImageDescriptor OVR_SHARED;
/** Decoration for resource not being tracked by Git */
public static final ImageDescriptor OVR_UNTRACKED;

/** Decoration for tracked resource with a merge conflict. */
public static final ImageDescriptor OVR_CONFLICT;
Expand Down Expand Up @@ -86,7 +86,7 @@ public class UIIcons {
base = init();
OVR_PENDING_ADD = map("ovr/pending_add.gif");
OVR_PENDING_REMOVE = map("ovr/pending_remove.gif");
OVR_SHARED = map("ovr/shared.gif");
OVR_UNTRACKED = map("ovr/untracked.gif");
OVR_CONFLICT = map("ovr/conflict.gif");
OVR_ASSUMEVALID = map("ovr/assumevalid.gif");
ELCL16_FIND = map("elcl16/find.gif");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ public class UIPreferences {
public final static String DECORATOR_FOLDERTEXT_DECORATION = "decorator_foldertext_decoration";
/** */
public final static String DECORATOR_PROJECTTEXT_DECORATION = "decorator_projecttext_decoration";
/** */
public final static String DECORATOR_SHOW_TRACKED_ICON = "decorator_show_tracked_icon";
/** */
public final static String DECORATOR_SHOW_UNTRACKED_ICON = "decorator_show_untracked_icon";

/**
* Get the preference values associated with a fixed integer array.
Expand Down
6 changes: 6 additions & 0 deletions org.spearce.egit.ui/src/org/spearce/egit/ui/UIText.java
Original file line number Diff line number Diff line change
Expand Up @@ -988,6 +988,12 @@ public class UIText extends NLS {
/** */
public static String DecoratorPreferencesPage_labelDecorationsLink;

/** */
public static String DecoratorPreferencesPage_iconsShowTracked;

/** */
public static String DecoratorPreferencesPage_iconsShowUntracked;

static {
initializeMessages(UIText.class.getPackage().getName() + ".uitext",
UIText.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
Expand All @@ -34,14 +35,19 @@
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.util.IPropertyChangeListener;
import org.eclipse.jface.util.PropertyChangeEvent;
import org.eclipse.jface.viewers.IDecoration;
import org.eclipse.jface.viewers.ILightweightLabelDecorator;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.LabelProviderChangedEvent;
import org.eclipse.osgi.util.TextProcessor;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.widgets.Display;
import org.eclipse.team.core.Team;
import org.eclipse.team.ui.ISharedImages;
import org.eclipse.team.ui.TeamImages;
import org.eclipse.team.ui.TeamUI;
import org.eclipse.ui.IContributorResourceAdapter;
import org.eclipse.ui.PlatformUI;
Expand All @@ -51,13 +57,22 @@
import org.spearce.egit.core.project.RepositoryChangeListener;
import org.spearce.egit.core.project.RepositoryMapping;
import org.spearce.egit.ui.Activator;
import org.spearce.egit.ui.UIIcons;
import org.spearce.egit.ui.UIPreferences;
import org.spearce.egit.ui.UIText;
import org.spearce.jgit.dircache.DirCache;
import org.spearce.jgit.dircache.DirCacheIterator;
import org.spearce.jgit.lib.Constants;
import org.spearce.jgit.lib.IndexChangedEvent;
import org.spearce.jgit.lib.ObjectId;
import org.spearce.jgit.lib.RefsChangedEvent;
import org.spearce.jgit.lib.Repository;
import org.spearce.jgit.lib.RepositoryChangedEvent;
import org.spearce.jgit.lib.RepositoryListener;
import org.spearce.jgit.revwalk.RevWalk;
import org.spearce.jgit.treewalk.EmptyTreeIterator;
import org.spearce.jgit.treewalk.TreeWalk;
import org.spearce.jgit.treewalk.filter.PathFilterGroup;

/**
* Supplies annotations for displayed resources
Expand Down Expand Up @@ -144,7 +159,7 @@ public void decorate(Object element, IDecoration decoration) {
if (!resource.exists() && !resource.isPhantom())
return;

// Make sure we're dealing with a Git project
// Make sure we're dealing with a project under Git revision control
final RepositoryMapping mapping = RepositoryMapping
.getMapping(resource);
if (mapping == null)
Expand All @@ -171,14 +186,80 @@ public void decorate(Object element, IDecoration decoration) {

private class DecoratableResourceAdapter implements IDecoratableResource {

private IResource resource;
private String branch;
private final IResource resource;

public DecoratableResourceAdapter(IResource resourceToWrap) throws IOException {
private final RepositoryMapping mapping;

private final Repository repository;

private final ObjectId headId;

private String branch = "";

private boolean tracked = false;

private boolean ignored = false;

public DecoratableResourceAdapter(IResource resourceToWrap)
throws IOException {
resource = resourceToWrap;
RepositoryMapping mapping = RepositoryMapping.getMapping(resource);
Repository repository = mapping.getRepository();
mapping = RepositoryMapping.getMapping(resource);
repository = mapping.getRepository();
headId = repository.resolve(Constants.HEAD);

initializeValues();
}

/**
* Initialize the various values that are used for making decoration
* decisions later on.
*
* We might as well pre-load these now, instead of using lazy
* initialization, because they are all read by the decorator when
* building variable bindings and computing the preferred overlay.
*
* @throws IOException
*/
private void initializeValues() throws IOException {

// Resolve current branch
branch = repository.getBranch();

// Resolve tracked state
if (getType() == IResource.PROJECT) {
tracked = true;
} else {
final TreeWalk treeWalk = new TreeWalk(repository);

Set<String> repositoryPaths = Collections.singleton(mapping
.getRepoRelativePath(resource));
if (!(repositoryPaths.isEmpty() || repositoryPaths.contains(""))) {
treeWalk.setFilter(PathFilterGroup
.createFromStrings(repositoryPaths));
treeWalk.setRecursive(treeWalk.getFilter()
.shouldBeRecursive());
treeWalk.reset();

if (headId != null)
treeWalk.addTree(new RevWalk(repository)
.parseTree(headId));
else
treeWalk.addTree(new EmptyTreeIterator());

treeWalk.addTree(new DirCacheIterator(DirCache
.read(repository)));
if (treeWalk.next()) {
tracked = true;
}
}
}

// Resolve ignored state (currently only reads the global Eclipse
// ignores)
// TODO: Also read ignores from .git/info/excludes et al.
if (Team.isIgnoredHint(resource)) {
ignored = true;
}
}

public String getName() {
Expand All @@ -192,6 +273,14 @@ public int getType() {
public String getBranch() {
return branch;
}

public boolean isTracked() {
return tracked;
}

public boolean isIgnored() {
return ignored;
}
}

/**
Expand All @@ -203,13 +292,45 @@ public String getBranch() {
*/
public static class DecorationHelper {

private IPreferenceStore store;

/** */
public static final String BINDING_RESOURCE_NAME = "name"; //$NON-NLS-1$

/** */
public static final String BINDING_BRANCH_NAME = "branch"; //$NON-NLS-1$

private IPreferenceStore store;

/**
* Define a cached image descriptor which only creates the image data
* once
*/
private static class CachedImageDescriptor extends ImageDescriptor {
ImageDescriptor descriptor;

ImageData data;

public CachedImageDescriptor(ImageDescriptor descriptor) {
this.descriptor = descriptor;
}

public ImageData getImageData() {
if (data == null) {
data = descriptor.getImageData();
}
return data;
}
}

private static ImageDescriptor trackedImage;

private static ImageDescriptor untrackedImage;

static {
trackedImage = new CachedImageDescriptor(TeamImages
.getImageDescriptor(ISharedImages.IMG_CHECKEDIN_OVR));
untrackedImage = new CachedImageDescriptor(UIIcons.OVR_UNTRACKED);
}

/**
* Constructs a decorator using the rules from the given
* <code>preferencesStore</code>
Expand All @@ -233,6 +354,12 @@ public DecorationHelper(IPreferenceStore preferencesStore) {
*/
public void decorate(IDecoration decoration,
IDecoratableResource resource) {
decorateText(decoration, resource);
decorateIcons(decoration, resource);
}

private void decorateText(IDecoration decoration,
IDecoratableResource resource) {
String format = "";
switch (resource.getType()) {
case IResource.FILE:
Expand All @@ -256,9 +383,24 @@ public void decorate(IDecoration decoration,
decorate(decoration, format, bindings);
}

private void decorateIcons(IDecoration decoration,
IDecoratableResource resource) {
if (resource.isIgnored())
return;

if (resource.isTracked()) {
if (store.getBoolean(UIPreferences.DECORATOR_SHOW_TRACKED_ICON))
decoration.addOverlay(trackedImage);
} else if (store
.getBoolean(UIPreferences.DECORATOR_SHOW_UNTRACKED_ICON)) {
decoration.addOverlay(untrackedImage);
}
}

/**
* Decorates the given <code>decoration</code>, using the given
* <code>format</code>, and mapped using <code>bindings</code>
* Decorates the given <code>decoration</code>, using the specified text
* <code>format</code>, and mapped using the variable bindings from
* <code>bindings</code>
*
* @param decoration
* the decoration to decorate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,19 @@ public interface IDecoratableResource {
* applicable
*/
String getBranch();

/**
* Returns whether or not the resource is tracked by Git
*
* @return whether or not the resource is tracked by Git
*/
boolean isTracked();

/**
* Returns whether or not the resource is ignored, either by a global team
* ignore in Eclipse, or by .git/info/exclude et al.
*
* @return whether or not the resource is ignored
*/
boolean isIgnored();
}
Loading

0 comments on commit 3c0fab8

Please sign in to comment.