From cdd098c9834f150dc540a5bdd8629570138208c7 Mon Sep 17 00:00:00 2001
From: Luca Cristoforetti <cristofo@fbk.eu>
Date: Tue, 31 Mar 2020 15:07:31 +0200
Subject: [PATCH] Improve marker generation and handling

---
 .../checkers/core/checkerManager/Checker.java | 38 ++++++++++++-------
 .../core/checkerManager/CheckerManager.java   | 27 ++++++++++---
 .../checkers/core/impl/NameDistance.java      |  5 ++-
 .../chess/checkers/core/impl/StateStatus.java |  9 +++--
 .../service/gui/utils/ReportProblemsUtil.java | 24 +++++++-----
 5 files changed, 69 insertions(+), 34 deletions(-)

diff --git a/plugins/org.polarsys.chess.checkers/src/org/polarsys/chess/checkers/core/checkerManager/Checker.java b/plugins/org.polarsys.chess.checkers/src/org/polarsys/chess/checkers/core/checkerManager/Checker.java
index 30094df4f..6b0973d9d 100644
--- a/plugins/org.polarsys.chess.checkers/src/org/polarsys/chess/checkers/core/checkerManager/Checker.java
+++ b/plugins/org.polarsys.chess.checkers/src/org/polarsys/chess/checkers/core/checkerManager/Checker.java
@@ -9,19 +9,15 @@ import org.eclipse.core.resources.IFile;
 import org.eclipse.core.runtime.IProgressMonitor;
 import org.eclipse.core.runtime.IStatus;
 import org.eclipse.core.runtime.Status;
-import org.eclipse.core.runtime.jobs.IJobChangeEvent;
-import org.eclipse.core.runtime.jobs.IJobChangeListener;
 import org.eclipse.core.runtime.jobs.Job;
 import org.eclipse.emf.workspace.util.WorkspaceSynchronizer;
-import org.eclipse.swt.widgets.Display;
-import org.eclipse.swt.widgets.Shell;
 import org.polarsys.chess.contracts.profile.chesscontract.util.EntityUtil;
 
 public abstract class Checker {
 
 	private static final Logger logger = Logger.getLogger(Checker.class);
 
-	private static String pluginID = "org.polarsys.chess.checkers";
+//	private static String pluginID = "org.polarsys.chess.checkers";
 
 	public final String unifiedName;
 	protected final Set<String> checkerTags;
@@ -49,10 +45,6 @@ public abstract class Checker {
 
 	public void asyncCheck(boolean isFirstChecker) throws Exception {
 		
-		Display defaultDisplay = Display.getDefault();
-		Shell activeShell = defaultDisplay.getActiveShell();
-		
-		
 		CheckerManager checkerManager = org.polarsys.chess.checkers.Activator.getCheckerManager();
 		
 		org.eclipse.uml2.uml.Package pack = EntityUtil.getInstance().getCurrentSystemView();
@@ -73,8 +65,8 @@ public abstract class Checker {
 					errors = new ArrayList<Exception>();
 					List<CheckerMessage> messages = check(monitor);
 					if (messages != null) {
-
-						checkerManager.deleteMarkers(iFile, unifiedName);
+//						checkerManager.deleteMarkers(iFile, unifiedName);							
+						checkerManager.deleteProjectMarkers(iFile.getProject(), unifiedName);
 						checkerManager.addMessages(unifiedName, messages);
 					}
 				} catch (Exception e) {
@@ -93,9 +85,29 @@ public abstract class Checker {
 		
 		job.setUser(isFirstChecker);
 		job.schedule();
-
-		
 	}
+	
+//	/**
+//	 * Recursively returns all the files in the given container
+//	 * @param container the IContainer
+//	 * @return
+//	 */
+//	private List<IFile> getProjectFiles(IContainer container) {
+//		final List<IFile> files = new ArrayList<IFile>();
+//		try {
+//			for (IResource member : container.members()) {
+//				if (member instanceof IContainer) {
+//					files.addAll(getProjectFiles((IContainer) member));
+//				} else if (member instanceof IFile) {
+//					files.add((IFile) member);
+//				}
+//			}
+//		} catch (Exception e) {
+//			e.printStackTrace();
+//		}
+//		return files;
+//	}
+
 	/*
 	 * private void syncWithUI(Shell shell) { Display.getDefault().asyncExec(new
 	 * Runnable() { public void run() { MessageDialog.openInformation(shell,
diff --git a/plugins/org.polarsys.chess.checkers/src/org/polarsys/chess/checkers/core/checkerManager/CheckerManager.java b/plugins/org.polarsys.chess.checkers/src/org/polarsys/chess/checkers/core/checkerManager/CheckerManager.java
index 5e6663884..c7184e301 100644
--- a/plugins/org.polarsys.chess.checkers/src/org/polarsys/chess/checkers/core/checkerManager/CheckerManager.java
+++ b/plugins/org.polarsys.chess.checkers/src/org/polarsys/chess/checkers/core/checkerManager/CheckerManager.java
@@ -9,6 +9,7 @@ import java.util.Set;
 import org.apache.log4j.Logger;
 import org.eclipse.core.resources.IFile;
 import org.eclipse.core.resources.IMarker;
+import org.eclipse.core.resources.IProject;
 import org.eclipse.core.resources.IResource;
 import org.eclipse.core.runtime.CoreException;
 import org.eclipse.emf.ecore.EObject;
@@ -118,14 +119,14 @@ public class CheckerManager {
 			} else if (msn.object != null && msn.object instanceof INode) {
 				try {
 					final INode node = (INode) msn.object;
-					if (msn.severity == 0) {
-						ReportProblemsUtil.reportInfo(msn.file, msn.message, node.getStartLine(), 
+					if (msn.severity == IMarker.SEVERITY_INFO) {
+						ReportProblemsUtil.reportInfo(msn.file, CHECKER_NAME, msn.checkerName, msn.message, node.getStartLine(), 
 								node.getOffset(), node.getEndOffset());
-					} else if (msn.severity == 1) {
-						ReportProblemsUtil.reportWarning(msn.file, msn.message, node.getStartLine(), 
+					} else if (msn.severity == IMarker.SEVERITY_WARNING) {
+						ReportProblemsUtil.reportWarning(msn.file, CHECKER_NAME, msn.checkerName, msn.message, node.getStartLine(), 
 								node.getOffset(), node.getEndOffset());
-					} else if (msn.severity == 2) {
-						ReportProblemsUtil.reportError(msn.file, msn.message, node.getStartLine(), 
+					} else if (msn.severity == IMarker.SEVERITY_ERROR) {
+						ReportProblemsUtil.reportError(msn.file, CHECKER_NAME, msn.checkerName, msn.message, node.getStartLine(), 
 								node.getOffset(), node.getEndOffset());
 					}
 				} catch (NullPointerException | CoreException e) {
@@ -155,7 +156,21 @@ public class CheckerManager {
 				marker.delete();
 			}
 		}
+	}
 
+	/**
+	 * Deletes all the matching markers inside the given project.
+	 * @param project the active project
+	 * @param checkerName the name of the creating checker
+	 * @throws CoreException
+	 */
+	void deleteProjectMarkers(IProject project, String checkerName) throws CoreException {
+		IMarker[] markers = project.findMarkers(null, false, IResource.DEPTH_INFINITE);
+		for (IMarker marker : markers) {
+			if (marker != null && marker.exists() && marker.getAttribute(CHECKER_NAME, "").equals(checkerName)) {
+				marker.delete();
+			}
+		}
 	}
 
 	private ModelExplorerPageBookView getModelExplorerPageBookView() {
diff --git a/plugins/org.polarsys.chess.checkers/src/org/polarsys/chess/checkers/core/impl/NameDistance.java b/plugins/org.polarsys.chess.checkers/src/org/polarsys/chess/checkers/core/impl/NameDistance.java
index 94da2aba4..46727bed9 100644
--- a/plugins/org.polarsys.chess.checkers/src/org/polarsys/chess/checkers/core/impl/NameDistance.java
+++ b/plugins/org.polarsys.chess.checkers/src/org/polarsys/chess/checkers/core/impl/NameDistance.java
@@ -6,6 +6,7 @@ import java.util.HashSet;
 import java.util.List;
 import java.util.Set;
 import org.apache.log4j.Logger;
+import org.eclipse.core.resources.IMarker;
 import org.eclipse.core.runtime.IProgressMonitor;
 import org.eclipse.emf.common.util.BasicEList;
 import org.eclipse.emf.common.util.EList;
@@ -154,7 +155,7 @@ public class NameDistance extends Checker {
 
 				if (distance == 0) {
 					final String msg = equalsMsg(first, second);
-					messages.add(createMessage(msg, 2, first, unifiedName));
+					messages.add(createMessage(msg, IMarker.SEVERITY_ERROR, first, unifiedName));
 				}
 				if (distance <= threshold) {
 					similarNames.add(second.getName());
@@ -164,7 +165,7 @@ public class NameDistance extends Checker {
 			// If some similarities are found, store the entry in the warnings
 			if (similarNames.size() > 0) {
 				final String msg = similarMsg(first, similarNames);
-				messages.add(createMessage(msg, 1, first, unifiedName));
+				messages.add(createMessage(msg, IMarker.SEVERITY_WARNING, first, unifiedName));
 			}
 		}
 		return messages;
diff --git a/plugins/org.polarsys.chess.checkers/src/org/polarsys/chess/checkers/core/impl/StateStatus.java b/plugins/org.polarsys.chess.checkers/src/org/polarsys/chess/checkers/core/impl/StateStatus.java
index 5cfb952a5..ae751ed9f 100644
--- a/plugins/org.polarsys.chess.checkers/src/org/polarsys/chess/checkers/core/impl/StateStatus.java
+++ b/plugins/org.polarsys.chess.checkers/src/org/polarsys/chess/checkers/core/impl/StateStatus.java
@@ -7,6 +7,7 @@ import java.util.List;
 import java.util.Set;
 
 import org.apache.log4j.Logger;
+import org.eclipse.core.resources.IMarker;
 import org.eclipse.core.runtime.IProgressMonitor;
 import org.eclipse.emf.common.util.EList;
 import org.eclipse.emf.ecore.EObject;
@@ -128,7 +129,7 @@ public class StateStatus extends Checker {
 				}
 				if (dead) {
 					final String msg = deadMsg(state, block);
-					errors.add(createMessage(msg, 2, state, unifiedName));
+					errors.add(createMessage(msg, IMarker.SEVERITY_ERROR, state, unifiedName));
 
 					deadStates.add(state.getName());
 				}
@@ -179,7 +180,7 @@ public class StateStatus extends Checker {
 				}
 				if (unreachable) {
 					final String msg = unreachableMsg(state, block);
-					errors.add(createMessage(msg, 2, state, unifiedName));
+					errors.add(createMessage(msg, IMarker.SEVERITY_ERROR, state, unifiedName));
 					unreachableStates.add(state.getName());					
 				}
 			}
@@ -254,13 +255,13 @@ public class StateStatus extends Checker {
 
 				if ((dead && stateCounter > 2)) {
 					final String msg = deadMsg(state, block);
-					errors.add(createMessage(msg, 2, state, unifiedName));
+					errors.add(createMessage(msg, IMarker.SEVERITY_ERROR, state, unifiedName));
 					deadStates.add(state.getName());
 				}
 
 				if ((unreachable)) {
 					final String msg = unreachableMsg(state, block);
-					errors.add(createMessage(msg, 2, state, unifiedName));
+					errors.add(createMessage(msg, IMarker.SEVERITY_ERROR, state, unifiedName));
 					unreachableStates.add(state.getName());
 				}
 			}
diff --git a/plugins/org.polarsys.chess.service/src/org/polarsys/chess/service/gui/utils/ReportProblemsUtil.java b/plugins/org.polarsys.chess.service/src/org/polarsys/chess/service/gui/utils/ReportProblemsUtil.java
index c28272cdb..8f1db202e 100644
--- a/plugins/org.polarsys.chess.service/src/org/polarsys/chess/service/gui/utils/ReportProblemsUtil.java
+++ b/plugins/org.polarsys.chess.service/src/org/polarsys/chess/service/gui/utils/ReportProblemsUtil.java
@@ -36,6 +36,8 @@ public class ReportProblemsUtil {
 	/**
 	 * Reports a warning in the Problems tab.
 	 * @param resource the resource
+	 * @param ownerField the name of the field to store the owner
+	 * @param ownerName the name of the creator of this report
 	 * @param msg the message to display
 	 * @param lineNumber the line to select (or 0)
 	 * @param charStart the start of selection (or 0)
@@ -44,15 +46,17 @@ public class ReportProblemsUtil {
 	 * @throws CoreException
 	 * @throws NullPointerException
 	 */
-	public static IMarker reportWarning(IResource resource, String msg, int lineNumber, 
+	public static IMarker reportWarning(IResource resource, String ownerField, String ownerName, String msg, int lineNumber, 
 			int charStart, int charEnd) throws CoreException, NullPointerException {
-		return reportMessage(resource, msg, lineNumber, charStart, charEnd, 
+		return reportMessage(resource, ownerField, ownerName, msg, lineNumber, charStart, charEnd, 
 				IMarker.PROBLEM, IMarker.SEVERITY_WARNING);
 	}
 
 	/**
 	 * Reports an error in the Problems tab.
 	 * @param resource the resource
+	 * @param ownerField the name of the field to store the owner
+	 * @param ownerName the name of the creator of this report
 	 * @param msg the message to display
 	 * @param lineNumber the line to select (or 0)
 	 * @param charStart the start of selection (or 0)
@@ -61,15 +65,17 @@ public class ReportProblemsUtil {
 	 * @throws CoreException
 	 * @throws NullPointerException
 	 */
-	public static IMarker reportError(IResource resource, String msg, int lineNumber, 
+	public static IMarker reportError(IResource resource, String ownerField, String ownerName, String msg, int lineNumber, 
 			int charStart, int charEnd) throws CoreException, NullPointerException {
-		return reportMessage(resource, msg, lineNumber, charStart, charEnd, 
+		return reportMessage(resource, ownerField, ownerName, msg, lineNumber, charStart, charEnd, 
 				IMarker.PROBLEM, IMarker.SEVERITY_ERROR);
 	}
 
 	/**
 	 * Reports an info in the Problems tab.
 	 * @param resource the resource
+	 * @param ownerField the name of the field to store the owner
+	 * @param ownerName the name of the creator of this report
 	 * @param msg the message to display
 	 * @param lineNumber the line to select (or 0)
 	 * @param charStart the start of selection (or 0)
@@ -78,14 +84,14 @@ public class ReportProblemsUtil {
 	 * @throws CoreException
 	 * @throws NullPointerException
 	 */
-	public static IMarker reportInfo(IResource resource, String msg, int lineNumber, 
+	public static IMarker reportInfo(IResource resource, String ownerField, String ownerName, String msg, int lineNumber, 
 			int charStart, int charEnd) throws CoreException, NullPointerException {
-		return reportMessage(resource, msg, lineNumber, charStart, charEnd, 
+		return reportMessage(resource, ownerField, ownerName, msg, lineNumber, charStart, charEnd, 
 				IMarker.PROBLEM, IMarker.SEVERITY_INFO);
 	}
 
-	private static IMarker reportMessage(IResource resource, String msg, int lineNumber, int charStart, 
-			int charEnd, String type, int severity) throws CoreException, NullPointerException {
+	private static IMarker reportMessage(IResource resource, String ownerField, String ownerName, String msg, int lineNumber,
+			int charStart, int charEnd, String type, int severity) throws CoreException, NullPointerException {
 		if (resource == null) {
 			throw new NullPointerException("Resource is null");
 		}
@@ -100,7 +106,7 @@ public class ReportProblemsUtil {
 		if (charStart > 0) attributes.put(IMarker.CHAR_START, new Integer(charStart)); // Note: this value is from the beginning of the document
 		if (charEnd > 0) attributes.put(IMarker.CHAR_END, new Integer(charEnd)); // Note: this value is from the beginning of the document
 //		m.setAttribute(IMarker.LOCATION, "whole file");	// This is a generic info about location
-
+		attributes.put(ownerField, ownerName);
 		m.setAttributes(attributes);
 		return m;
 	}
-- 
GitLab