Bug 264404 - a Decorator for Problem markers

Change-Id: Ic67d5ccd75aa77c680fd97772b58a831c163be76
Signed-off-by: Mickael Istria <mistria@redhat.com>
diff --git a/bundles/org.eclipse.ui.navigator.resources/META-INF/MANIFEST.MF b/bundles/org.eclipse.ui.navigator.resources/META-INF/MANIFEST.MF
index 4311e46..09a5bd6 100644
--- a/bundles/org.eclipse.ui.navigator.resources/META-INF/MANIFEST.MF
+++ b/bundles/org.eclipse.ui.navigator.resources/META-INF/MANIFEST.MF
@@ -8,9 +8,10 @@
 Bundle-Localization: plugin
 Export-Package: org.eclipse.ui.internal.navigator.resources;x-internal:=true,
  org.eclipse.ui.internal.navigator.resources.actions;x-internal:=true,
- org.eclipse.ui.internal.navigator.resources.plugin;x-internal:=true,
- org.eclipse.ui.internal.navigator.resources.workbench;x-internal:=true,
  org.eclipse.ui.internal.navigator.resources.nested;x-internal:=true,
+ org.eclipse.ui.internal.navigator.resources.plugin;x-internal:=true,
+ org.eclipse.ui.internal.navigator.resources.problems;x-internal:=true,
+ org.eclipse.ui.internal.navigator.resources.workbench;x-internal:=true,
  org.eclipse.ui.internal.navigator.workingsets;x-internal:=true,
  org.eclipse.ui.navigator.resources
 Require-Bundle: org.eclipse.ui.ide;bundle-version="[3.6.0,4.0.0)",
diff --git a/bundles/org.eclipse.ui.navigator.resources/icons/full/ovr16/error_co.png b/bundles/org.eclipse.ui.navigator.resources/icons/full/ovr16/error_co.png
deleted file mode 100644
index 94dc52d..0000000
--- a/bundles/org.eclipse.ui.navigator.resources/icons/full/ovr16/error_co.png
+++ /dev/null
Binary files differ
diff --git a/bundles/org.eclipse.ui.navigator.resources/icons/full/ovr16/error_co@2x.png b/bundles/org.eclipse.ui.navigator.resources/icons/full/ovr16/error_co@2x.png
deleted file mode 100644
index 2fb71f1..0000000
--- a/bundles/org.eclipse.ui.navigator.resources/icons/full/ovr16/error_co@2x.png
+++ /dev/null
Binary files differ
diff --git a/bundles/org.eclipse.ui.navigator.resources/icons/full/ovr16/warning_co.png b/bundles/org.eclipse.ui.navigator.resources/icons/full/ovr16/warning_co.png
deleted file mode 100644
index 7e4c116..0000000
--- a/bundles/org.eclipse.ui.navigator.resources/icons/full/ovr16/warning_co.png
+++ /dev/null
Binary files differ
diff --git a/bundles/org.eclipse.ui.navigator.resources/icons/full/ovr16/warning_co@2x.png b/bundles/org.eclipse.ui.navigator.resources/icons/full/ovr16/warning_co@2x.png
deleted file mode 100644
index e9755bf..0000000
--- a/bundles/org.eclipse.ui.navigator.resources/icons/full/ovr16/warning_co@2x.png
+++ /dev/null
Binary files differ
diff --git a/bundles/org.eclipse.ui.navigator.resources/plugin.properties b/bundles/org.eclipse.ui.navigator.resources/plugin.properties
index dabb505..e6474d4 100644
--- a/bundles/org.eclipse.ui.navigator.resources/plugin.properties
+++ b/bundles/org.eclipse.ui.navigator.resources/plugin.properties
@@ -38,3 +38,6 @@
 nestedProjects.filters.HideTopLevelProjectIfNested.description=When a project is shown as nested under its container, hide the view of the project as immediate child of workspace or working set
 nestedProjects.radioStateName=Nested Project view - Radio State
 
+problems.decorator.label=Problem markers
+problems.decorator.description=Shows a warning or error icon on resources.
+
diff --git a/bundles/org.eclipse.ui.navigator.resources/plugin.xml b/bundles/org.eclipse.ui.navigator.resources/plugin.xml
index 124c5eb..310eea0 100644
--- a/bundles/org.eclipse.ui.navigator.resources/plugin.xml
+++ b/bundles/org.eclipse.ui.navigator.resources/plugin.xml
@@ -347,7 +347,6 @@
                class="org.eclipse.ui.internal.navigator.workingsets.WorkingSetSorter"
                id="org.eclipse.ui.navigator.resources.workingSets.sorter"/>
       </navigatorContent>
-
    </extension>
     <extension
           point="org.eclipse.core.runtime.adapters">
@@ -525,4 +524,22 @@
          </commandParameter>
       </command>
    </extension>
+   <extension
+         point="org.eclipse.ui.decorators">
+      <decorator
+            class="org.eclipse.ui.internal.navigator.resources.problems.ProblemMarkersLabelDecorator"
+            id="org.eclipse.ui.navigator.resources.decorators.problems"
+            label="%problems.decorator.label"
+            state="true"
+            lightweight="false">
+         <enablement>
+         	<objectClass
+                name="java.lang.Object">
+         	</objectClass>
+         </enablement>
+         <description>
+            %problems.decorator.description
+         </description>
+      </decorator>
+   </extension>
 </plugin>
diff --git a/bundles/org.eclipse.ui.navigator.resources/src/org/eclipse/ui/internal/navigator/resources/problems/ProblemMarkersLabelDecorator.java b/bundles/org.eclipse.ui.navigator.resources/src/org/eclipse/ui/internal/navigator/resources/problems/ProblemMarkersLabelDecorator.java
new file mode 100644
index 0000000..4e6c60f
--- /dev/null
+++ b/bundles/org.eclipse.ui.navigator.resources/src/org/eclipse/ui/internal/navigator/resources/problems/ProblemMarkersLabelDecorator.java
@@ -0,0 +1,119 @@
+/*******************************************************************************
+ * Copyright (C) 2012-2016, Robin Stocker <robin@nibor.org> and others,
+ *
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Robin Stocker
+ * - initial implementation
+ * Mickael Istria (Red Hat Inc.)
+ * - [264404] Problems markers as decorators (so in Project Explorer too)
+ *******************************************************************************/
+package org.eclipse.ui.internal.navigator.resources.problems;
+
+import org.eclipse.core.resources.IMarker;
+import org.eclipse.core.resources.IResource;
+import org.eclipse.core.runtime.Adapters;
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IStatus;
+import org.eclipse.core.runtime.Status;
+import org.eclipse.jface.resource.ImageDescriptor;
+import org.eclipse.jface.resource.JFaceResources;
+import org.eclipse.jface.resource.LocalResourceManager;
+import org.eclipse.jface.resource.ResourceManager;
+import org.eclipse.jface.viewers.DecorationOverlayIcon;
+import org.eclipse.jface.viewers.IDecoration;
+import org.eclipse.jface.viewers.ILabelDecorator;
+import org.eclipse.jface.viewers.ILabelProviderListener;
+import org.eclipse.jface.viewers.ILightweightLabelDecorator;
+import org.eclipse.swt.graphics.Image;
+import org.eclipse.ui.ISharedImages;
+import org.eclipse.ui.PlatformUI;
+import org.eclipse.ui.internal.navigator.resources.plugin.WorkbenchNavigatorPlugin;
+
+public class ProblemMarkersLabelDecorator implements ILightweightLabelDecorator, ILabelDecorator {
+
+	private final ResourceManager resourceManager = new LocalResourceManager(
+			JFaceResources.getResources());
+
+	public ProblemMarkersLabelDecorator() {
+	}
+
+	@Override
+	public void dispose() {
+		resourceManager.dispose();
+	}
+
+
+	@Override
+	public void decorate(Object element, IDecoration decoration) {
+		IResource resource = Adapters.adapt(element, IResource.class);
+		if (resource != null) {
+			int problemSeverity = -1;
+			try {
+				for (IMarker marker : resource.findMarkers(IMarker.PROBLEM, true, IResource.DEPTH_INFINITE)) {
+					problemSeverity = Math.max(problemSeverity, marker.getAttribute(IMarker.SEVERITY, -1));
+				}
+			} catch (CoreException e) {
+				WorkbenchNavigatorPlugin.log(e.getMessage(),
+						new Status(IStatus.ERROR, WorkbenchNavigatorPlugin.PLUGIN_ID, e.getMessage(), e));
+			}
+			if (problemSeverity == IMarker.SEVERITY_ERROR) {
+				decoration.addOverlay(PlatformUI.getWorkbench().getSharedImages()
+						.getImageDescriptor(ISharedImages.IMG_DEC_FIELD_ERROR), IDecoration.BOTTOM_LEFT);
+			} else if (problemSeverity == IMarker.SEVERITY_WARNING) {
+				decoration.addOverlay(PlatformUI.getWorkbench().getSharedImages()
+						.getImageDescriptor(ISharedImages.IMG_DEC_FIELD_WARNING), IDecoration.BOTTOM_LEFT);
+			}
+		}
+	}
+
+	@Override
+	public Image decorateImage(Image image, Object element) {
+		IResource resource = Adapters.adapt(element, IResource.class);
+		if (resource != null) {
+			int problemSeverity = -1;
+			try {
+				for (IMarker marker : resource.findMarkers(IMarker.PROBLEM, true, IResource.DEPTH_INFINITE)) {
+					problemSeverity = Math.max(problemSeverity, marker.getAttribute(IMarker.SEVERITY, -1));
+				}
+			} catch (CoreException e) {
+				WorkbenchNavigatorPlugin.log(e.getMessage(),
+						new Status(IStatus.ERROR, WorkbenchNavigatorPlugin.PLUGIN_ID, e.getMessage(), e));
+			}
+			if (problemSeverity == IMarker.SEVERITY_ERROR)
+				return getDecoratedImage(image, ISharedImages.IMG_DEC_FIELD_ERROR);
+			else if (problemSeverity == IMarker.SEVERITY_WARNING)
+				return getDecoratedImage(image, ISharedImages.IMG_DEC_FIELD_WARNING);
+		}
+		return null;
+	}
+
+	private Image getDecoratedImage(Image base, String teamImageId) {
+		ImageDescriptor overlay = PlatformUI.getWorkbench().getSharedImages().getImageDescriptor(teamImageId);
+		DecorationOverlayIcon decorated = new DecorationOverlayIcon(base, overlay, IDecoration.BOTTOM_LEFT);
+		return (Image) this.resourceManager.get(decorated);
+	}
+
+	@Override
+	public void addListener(ILabelProviderListener listener) {
+	}
+
+	@Override
+	public boolean isLabelProperty(Object element, String property) {
+		return true;
+	}
+
+	@Override
+	public void removeListener(ILabelProviderListener listener) {
+	}
+
+	@Override
+	public String decorateText(String text, Object element) {
+		return null;
+	}
+
+}
\ No newline at end of file
diff --git a/bundles/org.eclipse.ui.navigator.resources/src/org/eclipse/ui/internal/navigator/resources/workbench/ResourceExtensionContentProvider.java b/bundles/org.eclipse.ui.navigator.resources/src/org/eclipse/ui/internal/navigator/resources/workbench/ResourceExtensionContentProvider.java
index 5fe3d63..8e2cb3f 100644
--- a/bundles/org.eclipse.ui.navigator.resources/src/org/eclipse/ui/internal/navigator/resources/workbench/ResourceExtensionContentProvider.java
+++ b/bundles/org.eclipse.ui.navigator.resources/src/org/eclipse/ui/internal/navigator/resources/workbench/ResourceExtensionContentProvider.java
@@ -171,6 +171,9 @@
 			runnables.add(getRefreshRunnable(resource));
 			return;
 		}
+		if ((changeFlags & IResourceDelta.MARKERS) != 0) {
+			runnables.add(getRefreshRunnable(resource.getProject()));
+		}
 
 
 		// Handle changed children .