From c97a40c186ca6f4d17d893f096e0dce949643ca5 Mon Sep 17 00:00:00 2001
From: Florian Hoelzl <hoelzl@fortiss.org>
Date: Mon, 27 Jun 2011 12:09:33 +0000
Subject: [PATCH] bugfix: automatic reload of disk changes works properly now.

---
 .../kernel/ui/internal/NavigatorService.java  | 19 +++--
 .../ui/internal/views/NavigatorViewPart.java  |  6 +-
 .../kernel/ui/services/INavigatorService.java | 12 ++--
 .../EclipseResourceStorageProvider.java       | 69 +++++++++++--------
 4 files changed, 57 insertions(+), 49 deletions(-)

diff --git a/org.fortiss.tooling.kernel.ui/trunk/src/org/fortiss/tooling/kernel/ui/internal/NavigatorService.java b/org.fortiss.tooling.kernel.ui/trunk/src/org/fortiss/tooling/kernel/ui/internal/NavigatorService.java
index 06ed176cb..e2e6ab6b5 100644
--- a/org.fortiss.tooling.kernel.ui/trunk/src/org/fortiss/tooling/kernel/ui/internal/NavigatorService.java
+++ b/org.fortiss.tooling.kernel.ui/trunk/src/org/fortiss/tooling/kernel/ui/internal/NavigatorService.java
@@ -32,7 +32,6 @@ import org.eclipse.emf.ecore.EObject;
 import org.eclipse.jface.resource.ImageDescriptor;
 import org.eclipse.jface.viewers.ISelection;
 import org.eclipse.ui.Saveable;
-import org.eclipse.ui.part.ViewPart;
 import org.fortiss.tooling.kernel.ToolingKernelActivator;
 import org.fortiss.tooling.kernel.interfaces.ITopLevelElementChangeListener;
 import org.fortiss.tooling.kernel.interfaces.ITopLevelElementContext;
@@ -47,7 +46,7 @@ import org.fortiss.tooling.kernel.ui.util.EObjectSelectionUtils;
  * @author hoelzl
  * @author $Author$
  * @version $Rev$
- * @ConQAT.Rating YELLOW Hash: B7F899DC2CFB74320D2F7A19A2D5FEBB
+ * @ConQAT.Rating YELLOW Hash: FB5662C65C21DF94B8E8D3C1F25DC679
  */
 public class NavigatorService implements INavigatorService,
 		ITopLevelElementChangeListener, CommandStackListener {
@@ -73,12 +72,6 @@ public class NavigatorService implements INavigatorService,
 		this.navigatorViewPart = part;
 	}
 
-	/** {@inheritDoc} */
-	@Override
-	public ViewPart getNavigatorView() {
-		return navigatorViewPart;
-	}
-
 	/** {@inheritDoc} */
 	@Override
 	public ISelection getCurrentSelection() {
@@ -91,7 +84,7 @@ public class NavigatorService implements INavigatorService,
 	public void topLevelElementAdded(ITopLevelElementContext context) {
 		context.addCommandStackListener(this);
 		saveables.put(context, new TopLevelElementSaveable(context));
-		navigatorViewPart.refresh();
+		refresh();
 	}
 
 	/** {@inheritDoc} */
@@ -99,12 +92,18 @@ public class NavigatorService implements INavigatorService,
 	public void topLevelElementRemoved(ITopLevelElementContext context) {
 		context.removeCommandStackListener(this);
 		saveables.remove(context);
-		navigatorViewPart.refresh();
+		refresh();
 	}
 
 	/** {@inheritDoc} */
 	@Override
 	public void commandStackChanged(EventObject event) {
+		refresh();
+	}
+
+	/** {@inheritDoc} */
+	@Override
+	public void refresh() {
 		navigatorViewPart.refresh();
 	}
 
diff --git a/org.fortiss.tooling.kernel.ui/trunk/src/org/fortiss/tooling/kernel/ui/internal/views/NavigatorViewPart.java b/org.fortiss.tooling.kernel.ui/trunk/src/org/fortiss/tooling/kernel/ui/internal/views/NavigatorViewPart.java
index 1b69d1f09..acdd22a25 100644
--- a/org.fortiss.tooling.kernel.ui/trunk/src/org/fortiss/tooling/kernel/ui/internal/views/NavigatorViewPart.java
+++ b/org.fortiss.tooling.kernel.ui/trunk/src/org/fortiss/tooling/kernel/ui/internal/views/NavigatorViewPart.java
@@ -109,6 +109,8 @@ public final class NavigatorViewPart extends ViewPart implements
 			}
 			IActionService.INSTANCE.refresh();
 			viewer.refresh();
+
+			firePropertyChange(IWorkbenchPartConstants.PROP_DIRTY);
 			return Status.OK_STATUS;
 		}
 	};
@@ -298,11 +300,7 @@ public final class NavigatorViewPart extends ViewPart implements
 
 	/** Refreshes the navigator view. */
 	public void refresh() {
-		if (INavigatorService.INSTANCE.getNavigatorView() != this) {
-			return;
-		}
 		updateUI.schedule();
-		firePropertyChange(IWorkbenchPartConstants.PROP_DIRTY);
 	}
 
 	/** {@inheritDoc} */
diff --git a/org.fortiss.tooling.kernel.ui/trunk/src/org/fortiss/tooling/kernel/ui/services/INavigatorService.java b/org.fortiss.tooling.kernel.ui/trunk/src/org/fortiss/tooling/kernel/ui/services/INavigatorService.java
index 2fb7b4b73..5b9cc1418 100644
--- a/org.fortiss.tooling.kernel.ui/trunk/src/org/fortiss/tooling/kernel/ui/services/INavigatorService.java
+++ b/org.fortiss.tooling.kernel.ui/trunk/src/org/fortiss/tooling/kernel/ui/services/INavigatorService.java
@@ -20,7 +20,6 @@ package org.fortiss.tooling.kernel.ui.services;
 import org.eclipse.emf.ecore.EObject;
 import org.eclipse.jface.viewers.ISelection;
 import org.eclipse.ui.Saveable;
-import org.eclipse.ui.part.ViewPart;
 import org.fortiss.tooling.kernel.ui.internal.NavigatorService;
 
 /**
@@ -29,7 +28,7 @@ import org.fortiss.tooling.kernel.ui.internal.NavigatorService;
  * @author hoelzl
  * @author $Author$
  * @version $Rev$
- * @ConQAT.Rating YELLOW Hash: 3C201DC07138A2C1A15C710F8A81A242
+ * @ConQAT.Rating YELLOW Hash: E84D4BEC9A2F32EAF84A8FDCF21EE3F4
  */
 public interface INavigatorService {
 	/** Returns the singleton instance of the service. */
@@ -38,12 +37,6 @@ public interface INavigatorService {
 	/** Returns view id of the navigator view. */
 	public static final String NAVIGATOR_VIEW = "org.fortiss.tooling.kernel.model.navigator";
 
-	/**
-	 * Returns the navigator view part. May return <code>null</code> if
-	 * navigator has not yet been created or has already been disposed.
-	 */
-	ViewPart getNavigatorView();
-
 	/** Returns the current naviaator selection. */
 	ISelection getCurrentSelection();
 
@@ -55,4 +48,7 @@ public interface INavigatorService {
 
 	/** Reveals the given model element. */
 	void revealModelElement(EObject modelElement);
+
+	/** Refreshes the navigator view part. */
+	void refresh();
 }
diff --git a/org.fortiss.tooling.kernel/trunk/src/org/fortiss/tooling/kernel/internal/storage/eclipse/EclipseResourceStorageProvider.java b/org.fortiss.tooling.kernel/trunk/src/org/fortiss/tooling/kernel/internal/storage/eclipse/EclipseResourceStorageProvider.java
index 714adc85d..95d631192 100644
--- a/org.fortiss.tooling.kernel/trunk/src/org/fortiss/tooling/kernel/internal/storage/eclipse/EclipseResourceStorageProvider.java
+++ b/org.fortiss.tooling.kernel/trunk/src/org/fortiss/tooling/kernel/internal/storage/eclipse/EclipseResourceStorageProvider.java
@@ -32,8 +32,12 @@ import org.eclipse.core.resources.IResourceChangeListener;
 import org.eclipse.core.resources.IResourceDelta;
 import org.eclipse.core.resources.IResourceDeltaVisitor;
 import org.eclipse.core.resources.ResourcesPlugin;
+import org.eclipse.core.resources.WorkspaceJob;
 import org.eclipse.core.runtime.CoreException;
 import org.eclipse.core.runtime.IConfigurationElement;
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.core.runtime.IStatus;
+import org.eclipse.core.runtime.Status;
 import org.eclipse.emf.ecore.EObject;
 import org.fortiss.tooling.kernel.ToolingKernelActivator;
 import org.fortiss.tooling.kernel.interfaces.IEclipseResourceStorageLocationProvider;
@@ -51,7 +55,7 @@ import org.osgi.framework.Bundle;
  * @author hoelzlf
  * @author $Author$
  * @version $Rev$
- * @ConQAT.Rating YELLOW Hash: B8363ADD63E4E6D34D97577B26670CEB
+ * @ConQAT.Rating YELLOW Hash: E6C694CF323750F7A6415B755FAC5522
  */
 public class EclipseResourceStorageProvider implements IResourceChangeListener,
 		IResourceDeltaVisitor, IStorageProvider {
@@ -144,39 +148,50 @@ public class EclipseResourceStorageProvider implements IResourceChangeListener,
 		if (delta.getResource() instanceof IFile
 				&& delta.getResource().getParent() instanceof IProject
 				&& delta.getResource().getFileExtension().equals("af3_20")) {
-			IFile file = (IFile) delta.getResource();
-			if (delta.getKind() == IResourceDelta.ADDED) {
-				try {
-					loadContext(file);
-				} catch (IOException ioex) {
-					LoggingUtils.error(ToolingKernelActivator.getDefault(),
-							"Had an error during reloading the file!", ioex);
-				}
-			} else if (delta.getKind() == IResourceDelta.REMOVED
-					&& isLoaded(file)) {
-				unloadContext((IFile) delta.getResource());
-			} else if (delta.getKind() == IResourceDelta.CHANGED) {
-				handleChange((IFile) delta.getResource());
-			}
-
-			// Refresh the top-level elements
-			IPersistencyService.INSTANCE
-					.refreshTopLevelElements(EclipseResourceStorageProvider.this);
+			runWorkspaceChangeJob((IFile) delta.getResource(), delta.getKind());
 		}
 		return true;
 	}
 
+	/** Runs resource change job as workspace job. */
+	private void runWorkspaceChangeJob(final IFile file, final int changeKind) {
+		new WorkspaceJob("ModelFileChangedJob") {
+
+			@Override
+			public IStatus runInWorkspace(IProgressMonitor monitor) {
+				synchronized (EclipseResourceStorageProvider.this) {
+					if (changeKind == IResourceDelta.ADDED) {
+						try {
+							loadContext(file);
+						} catch (IOException ioex) {
+							LoggingUtils.error(
+									ToolingKernelActivator.getDefault(),
+									"Had an error during reloading the file!",
+									ioex);
+						}
+					} else if (changeKind == IResourceDelta.REMOVED
+							&& isLoaded(file)) {
+						unloadContext(file);
+					} else if (changeKind == IResourceDelta.CHANGED) {
+						handleChange(file);
+					}
+
+					// Refresh the top-level elements
+					IPersistencyService.INSTANCE
+							.refreshTopLevelElements(EclipseResourceStorageProvider.this);
+				}
+				return Status.OK_STATUS;
+			}
+		}.schedule();
+	}
+
 	/** Handles a change of the given file (which is actually managed). */
-	private void handleChange(final IFile file) {
-		final ModelContext context = loadedContexts.get(file);
+	private void handleChange(IFile file) {
+		ModelContext context = loadedContexts.get(file);
 		if (!context.getLastChangeWasIntended()) {
 			unloadContext(file);
 			try {
-				loadContext(file).setLastChangeWasIntended();
-
-				// perform touch to make sure the viewer does a
-				// refresh
-				file.touch(null);
+				loadContext(file);
 			} catch (final Exception e) {
 				LoggingUtils.error(ToolingKernelActivator.getDefault(),
 						"Had an error during reloading the file!", e);
@@ -199,7 +214,7 @@ public class EclipseResourceStorageProvider implements IResourceChangeListener,
 
 	/** Loads a file and creates a new context from its contents. */
 	private void unloadContext(IFile file) {
-		final ModelContext context = loadedContexts.remove(file);
+		ModelContext context = loadedContexts.remove(file);
 		if (context != null) {
 			rootElementContexts.remove(context.getTopLevelElement());
 			context.destroy();
-- 
GitLab