Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authoraradermache2011-04-07 08:27:41 +0000
committeraradermache2011-04-07 08:27:41 +0000
commit962b8ac694c73fec5d6f322d5d1b34165bf400e6 (patch)
treed16d014be9f079e324a7c3d080cf42087bd9952b
parentd7648639cb5a26c43723538c68759089936001a2 (diff)
downloadorg.eclipse.papyrus-962b8ac694c73fec5d6f322d5d1b34165bf400e6.tar.gz
org.eclipse.papyrus-962b8ac694c73fec5d6f322d5d1b34165bf400e6.tar.xz
org.eclipse.papyrus-962b8ac694c73fec5d6f322d5d1b34165bf400e6.zip
Fix for fix of bug 341964 (resource update question only on editor activation):
- handle case of changes between different views of active editor (diagram, modelexplorer, properties) - Restructuration (split of update service class)
-rw-r--r--plugins/uml/org.eclipse.papyrus.diagram.common/src/org/eclipse/papyrus/diagram/common/resourceupdate/PartActivationListener.java147
-rw-r--r--plugins/uml/org.eclipse.papyrus.diagram.common/src/org/eclipse/papyrus/diagram/common/resourceupdate/ResourceUpdateService.java114
-rw-r--r--plugins/uml/org.eclipse.papyrus.diagram.common/src/org/eclipse/papyrus/diagram/common/resourceupdate/SaveListener.java78
3 files changed, 242 insertions, 97 deletions
diff --git a/plugins/uml/org.eclipse.papyrus.diagram.common/src/org/eclipse/papyrus/diagram/common/resourceupdate/PartActivationListener.java b/plugins/uml/org.eclipse.papyrus.diagram.common/src/org/eclipse/papyrus/diagram/common/resourceupdate/PartActivationListener.java
new file mode 100644
index 00000000000..bdc2e419db6
--- /dev/null
+++ b/plugins/uml/org.eclipse.papyrus.diagram.common/src/org/eclipse/papyrus/diagram/common/resourceupdate/PartActivationListener.java
@@ -0,0 +1,147 @@
+/*****************************************************************************
+ * Copyright (c) 2010 CEA LIST.
+ *
+ *
+ * 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:
+ * Ansgar Radermacher (CEA LIST) ansgar.radermacher@cea.fr - Initial API and implementation
+ *
+ *****************************************************************************/
+
+package org.eclipse.papyrus.diagram.common.resourceupdate;
+
+import static org.eclipse.papyrus.core.Activator.log;
+
+import org.eclipse.core.resources.IResourceDelta;
+import org.eclipse.jface.dialogs.MessageDialog;
+import org.eclipse.papyrus.core.editor.IMultiDiagramEditor;
+import org.eclipse.papyrus.core.utils.EditorUtils;
+import org.eclipse.swt.widgets.Display;
+import org.eclipse.swt.widgets.Shell;
+import org.eclipse.ui.IEditorDescriptor;
+import org.eclipse.ui.IEditorInput;
+import org.eclipse.ui.IPartListener;
+import org.eclipse.ui.IWorkbench;
+import org.eclipse.ui.IWorkbenchPage;
+import org.eclipse.ui.IWorkbenchPart;
+import org.eclipse.ui.PartInitException;
+import org.eclipse.ui.PlatformUI;
+
+/**
+ * A listener for part activation. Will ask the user whether to reload when he enters an editor
+ * whose underlying resources have changed, used to trigger an update of
+ *
+ * @author Ansgar Radermacher (CEA LIST)
+ */
+public class PartActivationListener implements IPartListener {
+
+ public PartActivationListener(IMultiDiagramEditor editor) {
+ this.editor = editor;
+ modifiedMainResource = false;
+ }
+
+ /**
+ * @return true, if a resource for an underlying editor have been updated?
+ */
+ public boolean isModied() {
+ return modifiedMainResource;
+ }
+
+ /**
+ * indicate that the resource for an editor have been modified
+ *
+ * @param changedResourcePath
+ * The path to the resource that has been changed
+ * @param delta
+ * additional information about the change
+ */
+ public void setModificationData(String changedResourcePath, IResourceDelta delta) {
+ this.changedResourcePath = changedResourcePath;
+ this.delta = delta;
+ modifiedMainResource = true;
+ }
+
+ public void partActivated(IWorkbenchPart part) {
+ // don't use (part == editor.getSite().getPart()), since different views (e.g. model explorer or property)
+ // of an active editor may actually be selected
+ IMultiDiagramEditor activeEditor = EditorUtils.getMultiDiagramEditor();
+ if(editor == activeEditor) {
+ System.out.println("activate: " + editor.getTitle());
+ }
+ if((editor == activeEditor) && modifiedMainResource) {
+
+ switch(delta.getKind()) {
+ case IResourceDelta.ADDED:
+ break;
+ case IResourceDelta.REMOVED:
+ // asynchronous notification to avoid that the removal of multiple resource files
+ // belonging to the editor (e.g. .uml and .notation) at the same time leads to multiple
+ // user feedback.
+
+ MessageDialog.openInformation(new Shell(), "Resource removal", "The resource " + changedResourcePath + " that is in use by a Papyrus editor has been removed. Use save/save as, if you want to keep the model");
+ break;
+
+ case IResourceDelta.CHANGED:
+ // reopen the editor asynchronously to avoid that changes of multiple resource files
+ // belonging to the editor (e.g. .uml and .notation) lead to multiple reloads.
+ // de-activate until user responds to message dialog
+
+ String message = "The resource " + changedResourcePath + " that is in use by a Papyrus editor has changed. Do you want to reopen the editor in order to update its contents?";
+ if(editor.isDirty()) {
+ message += " CAVEAT: the editor contains unsaved modifications that would be lost.";
+ }
+
+ if(MessageDialog.openQuestion(new Shell(), "Resource change", message)) {
+ // unloading and reloading all resources of the main causes the following problems
+ // - since resources are removed during the modelSets unload operation, the call eResource().getContents ()
+ // used by the model explorer leads to a null pointer exception
+ // - diagrams in model explorer are not shown
+ // - would need to reset dirty flags
+ // => clean & simple option is to close and reopen the editor.
+
+ Display.getCurrent().asyncExec(new Runnable() {
+
+ public void run() {
+ IWorkbench wb = PlatformUI.getWorkbench();
+ IWorkbenchPage page = wb.getActiveWorkbenchWindow().getActivePage();
+ IEditorInput input = editor.getEditorInput();
+ page.closeEditor(editor, false);
+ try {
+ IEditorDescriptor desc = wb.getEditorRegistry().getDefaultEditor(input.getName());
+ page.openEditor(input, desc.getId(), false);
+ } catch (PartInitException e) {
+ log.error(e);
+ }
+ }
+ });
+ }
+ break;
+ }
+ modifiedMainResource = false;
+ }
+ }
+
+ public void partDeactivated(IWorkbenchPart part) {
+ }
+
+ public void partBroughtToTop(IWorkbenchPart part) {
+ }
+
+ public void partClosed(IWorkbenchPart part) {
+ }
+
+ public void partOpened(IWorkbenchPart part) {
+ }
+
+ private IMultiDiagramEditor editor;
+
+ private String changedResourcePath;
+
+ private IResourceDelta delta;
+
+ private boolean modifiedMainResource;
+}
diff --git a/plugins/uml/org.eclipse.papyrus.diagram.common/src/org/eclipse/papyrus/diagram/common/resourceupdate/ResourceUpdateService.java b/plugins/uml/org.eclipse.papyrus.diagram.common/src/org/eclipse/papyrus/diagram/common/resourceupdate/ResourceUpdateService.java
index bc1a98f8e18..83ed7c7f448 100644
--- a/plugins/uml/org.eclipse.papyrus.diagram.common/src/org/eclipse/papyrus/diagram/common/resourceupdate/ResourceUpdateService.java
+++ b/plugins/uml/org.eclipse.papyrus.diagram.common/src/org/eclipse/papyrus/diagram/common/resourceupdate/ResourceUpdateService.java
@@ -32,23 +32,14 @@ import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.URIConverter;
-import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.papyrus.core.editor.IMultiDiagramEditor;
+import org.eclipse.papyrus.core.lifecycleevents.ILifeCycleEventsProvider;
import org.eclipse.papyrus.core.services.IService;
import org.eclipse.papyrus.core.services.ServiceException;
import org.eclipse.papyrus.core.services.ServicesRegistry;
import org.eclipse.papyrus.diagram.common.Activator;
import org.eclipse.papyrus.resource.ModelSet;
-import org.eclipse.swt.widgets.Display;
-import org.eclipse.swt.widgets.Shell;
-import org.eclipse.ui.IEditorDescriptor;
-import org.eclipse.ui.IEditorInput;
-import org.eclipse.ui.IPartListener;
-import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPage;
-import org.eclipse.ui.IWorkbenchPart;
-import org.eclipse.ui.PartInitException;
-import org.eclipse.ui.PlatformUI;
import org.eclipse.uml2.uml.Profile;
/**
@@ -57,15 +48,20 @@ import org.eclipse.uml2.uml.Profile;
*
* @author Ansgar Radermacher (CEA LIST)
*/
-public class ResourceUpdateService implements IService, IResourceChangeListener, IResourceDeltaVisitor, IPartListener {
+public class ResourceUpdateService implements IService, IResourceChangeListener, IResourceDeltaVisitor {
public static final String RESOURCE_UPDATE_ID = Activator.ID + ".resourceUpdate";
// public init (CoreMultiDiagramEditor editor, ISaveAndDirtyService saveAndDirty, ModelSet modelSet) {
- public void init(ServicesRegistry servicesRegistry) throws ServiceException {
- modifiedMainResource = false;
- modelSet = servicesRegistry.getService(ModelSet.class);
- editor = servicesRegistry.getService(IMultiDiagramEditor.class);
+ public void init(ServicesRegistry serviceRegistry) throws ServiceException {
+ modelSet = serviceRegistry.getService(ModelSet.class);
+ editor = serviceRegistry.getService(IMultiDiagramEditor.class);
+ ILifeCycleEventsProvider lcEventsProvider = serviceRegistry.getService(ILifeCycleEventsProvider.class);
+ saveListener = new SaveListener();
+ lcEventsProvider.addDoSaveListener(saveListener.preSaveListener);
+ lcEventsProvider.addPostDoSaveListener(saveListener.postSaveListener);
+
+ partActivationListener = new PartActivationListener(editor);
}
/**
@@ -124,10 +120,8 @@ public class ResourceUpdateService implements IService, IResourceChangeListener,
if(changedResourcePathWOExt.equals(modelSet.getFilenameWithoutExtension())) {
// model itself has changed.
// mark main resource as changed. User will asked later, when he activates the editor.
- if(!modifiedMainResource) {
- modifiedMainResource = true;
- this.changedResourcePath = changedResourcePath;
- this.delta = delta;
+ if(!saveListener.isSaveActive() && !partActivationListener.isModied()) {
+ partActivationListener.setModificationData(changedResourcePath, delta);
}
}
// changed resource does not belong to the model, it might however belong to a referenced
@@ -147,12 +141,6 @@ public class ResourceUpdateService implements IService, IResourceChangeListener,
return true;
}
- private String changedResourcePath;
-
- private IResourceDelta delta;
-
- private boolean modifiedMainResource;
-
private IMultiDiagramEditor editor;
private ModelSet modelSet;
@@ -166,7 +154,7 @@ public class ResourceUpdateService implements IService, IResourceChangeListener,
// ... add service to the workspace
ResourcesPlugin.getWorkspace().addResourceChangeListener(this, IResourceChangeEvent.POST_CHANGE);
IWorkbenchPage page = editor.getSite().getPage();
- page.addPartListener(this);
+ page.addPartListener(partActivationListener);
}
/**
@@ -176,7 +164,7 @@ public class ResourceUpdateService implements IService, IResourceChangeListener,
// remove it from workspace
ResourcesPlugin.getWorkspace().removeResourceChangeListener(this);
IWorkbenchPage page = editor.getSite().getPage();
- page.removePartListener(this);
+ page.removePartListener(partActivationListener);
}
public void startService() throws ServiceException {
@@ -188,75 +176,7 @@ public class ResourceUpdateService implements IService, IResourceChangeListener,
// lifeCycleEvents.removeDoSaveListener(listener);
}
- public void partActivated(IWorkbenchPart part) {
- // TODO Auto-generated method stub
- if((part == editor.getSite().getPart()) && modifiedMainResource) {
-
- switch(delta.getKind()) {
- case IResourceDelta.ADDED:
- break;
- case IResourceDelta.REMOVED:
- // asynchronous notification to avoid that the removal of multiple resource files
- // belonging to the editor (e.g. .uml and .notation) at the same time leads to multiple
- // user feedback.
-
- MessageDialog.openInformation(new Shell(), "Resource removal", "The resource " + changedResourcePath + " that is in use by a Papyrus editor has been removed. Use save/save as, if you want to keep the model");
- break;
-
- case IResourceDelta.CHANGED:
- // reopen the editor asynchronously to avoid that changes of multiple resource files
- // belonging to the editor (e.g. .uml and .notation) lead to multiple reloads.
- // de-activate until user responds to message dialog
+ private PartActivationListener partActivationListener;
- String message = "The resource " + changedResourcePath + " that is in use by a Papyrus editor has changed. Do you want to reopen the editor in order to update its contents?";
- if(editor.isDirty()) {
- message += " CAVEAT: the editor contains unsaved modifications that would be lost.";
- }
-
- if(MessageDialog.openQuestion(new Shell(), "Resource change", message)) {
- // unloading and reloading all resources of the main causes the following problems
- // - since resources are removed during the modelSets unload operation, the call eResource().getContents ()
- // used by the model explorer leads to a null pointer exception
- // - diagrams in model explorer are not shown
- // - would need to reset dirty flags
- // => clean & simple option is to close and reopen the editor.
-
- Display.getCurrent().asyncExec(new Runnable() {
-
- public void run() {
- IWorkbench wb = PlatformUI.getWorkbench();
- IWorkbenchPage page = wb.getActiveWorkbenchWindow().getActivePage();
- IEditorInput input = editor.getEditorInput();
- page.closeEditor(editor, false);
- try {
- IEditorDescriptor desc = wb.getEditorRegistry().getDefaultEditor(input.getName());
- page.openEditor(input, desc.getId(), false);
- } catch (PartInitException e) {
- log.error(e);
- }
- }
- });
- }
- break;
- }
- modifiedMainResource = false;
- }
- }
-
- public void partDeactivated(IWorkbenchPart part) {
- if(part == editor.getSite().getPart()) {
- // reset modified state when leaving - assume that actions while active (in particular save)
- // do not require a re-load
- modifiedMainResource = false;
- }
- }
-
- public void partBroughtToTop(IWorkbenchPart part) {
- }
-
- public void partClosed(IWorkbenchPart part) {
- }
-
- public void partOpened(IWorkbenchPart part) {
- }
+ private SaveListener saveListener;
}
diff --git a/plugins/uml/org.eclipse.papyrus.diagram.common/src/org/eclipse/papyrus/diagram/common/resourceupdate/SaveListener.java b/plugins/uml/org.eclipse.papyrus.diagram.common/src/org/eclipse/papyrus/diagram/common/resourceupdate/SaveListener.java
new file mode 100644
index 00000000000..82b6456b65d
--- /dev/null
+++ b/plugins/uml/org.eclipse.papyrus.diagram.common/src/org/eclipse/papyrus/diagram/common/resourceupdate/SaveListener.java
@@ -0,0 +1,78 @@
+/*****************************************************************************
+ * Copyright (c) 2010 CEA LIST.
+ *
+ *
+ * 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:
+ * Ansgar Radermacher (CEA LIST) ansgar.radermacher@cea.fr - Initial API and implementation
+ *
+ *****************************************************************************/
+
+package org.eclipse.papyrus.diagram.common.resourceupdate;
+
+import org.eclipse.papyrus.core.lifecycleevents.DoSaveEvent;
+import org.eclipse.papyrus.core.lifecycleevents.ISaveEventListener;
+import org.eclipse.swt.widgets.Display;
+
+/**
+ * A listener for save actions. The goal is to track te editors save operations in order to
+ * distinguish external resource modifications and those triggered by the save operation
+ *
+ * @author Ansgar Radermacher (CEA LIST)
+ */
+public class SaveListener {
+
+ public SaveListener() {
+ preSaveListener = new PreSaveListener();
+ postSaveListener = new PreSaveListener();
+ postSaveRunnable = new PostSaveRunnable();
+ }
+
+ class PreSaveListener implements ISaveEventListener {
+
+ public void doSaveAs(DoSaveEvent event) {
+ saveActive = true;
+ }
+
+ public void doSave(DoSaveEvent event) {
+ saveActive = true;
+ }
+
+ };
+
+ class PostSaveListener implements ISaveEventListener {
+
+ public void doSaveAs(DoSaveEvent event) {
+ // do not reset saveActive directly to avoid eventual race conditions (avoid that the
+ // resource change listener executes after the flag has been reset)
+ Display.getDefault().asyncExec(postSaveRunnable);
+ }
+
+ public void doSave(DoSaveEvent event) {
+ Display.getDefault().asyncExec(postSaveRunnable);
+ }
+ };
+
+ class PostSaveRunnable implements Runnable {
+
+ public void run() {
+ saveActive = false;
+ };
+ }
+
+ public boolean isSaveActive() {
+ return saveActive;
+ }
+
+ private boolean saveActive;
+
+ ISaveEventListener preSaveListener;
+
+ ISaveEventListener postSaveListener;
+
+ Runnable postSaveRunnable;
+};

Back to the top