[579937] Bugfix: Check editor disposal when handling events

Change-Id: If95e759a4ec1ef44126aab3f86b0c6097c526e5c
Signed-off-by: Balázs Grill <balazs.grill@incquerylabs.com>
Signed-off-by: Dietrich Travkin <travkin@gmx.de>
diff --git a/plugins/org.eclipse.sphinx.emf.editors.forms/src/org/eclipse/sphinx/emf/editors/forms/BasicTransactionalFormEditor.java b/plugins/org.eclipse.sphinx.emf.editors.forms/src/org/eclipse/sphinx/emf/editors/forms/BasicTransactionalFormEditor.java
index 87f9b71..77d53d8 100644
--- a/plugins/org.eclipse.sphinx.emf.editors.forms/src/org/eclipse/sphinx/emf/editors/forms/BasicTransactionalFormEditor.java
+++ b/plugins/org.eclipse.sphinx.emf.editors.forms/src/org/eclipse/sphinx/emf/editors/forms/BasicTransactionalFormEditor.java
@@ -1082,7 +1082,8 @@
 

 	public void setStatusLineManager(ISelection selection) {

 		IStatusLineManager statusLineManager = currentSelectionProvider != null && currentSelectionProvider == contentOutlineViewer

-				? contentOutlineStatusLineManager : getActionBars() != null ? getActionBars().getStatusLineManager() : null;

+				? contentOutlineStatusLineManager

+				: getActionBars() != null ? getActionBars().getStatusLineManager() : null;

 		if (statusLineManager != null) {

 			if (selection instanceof IStructuredSelection) {

 				Collection<?> collection = ((IStructuredSelection) selection).toList();

@@ -1190,14 +1191,18 @@
 	 * Tests if this editor has already been disposed.

 	 * <p>

 	 * This implementation determines the disposed state of the editor by checking if the {@link FormEditor#pages} field

-	 * is <code>null</code> or not. When the editor has been disposed, it is an error to invoke any other method using

-	 * the editor.

+	 * is <code>null</code> or not and if its site and workbench window are available. When the editor has been

+	 * disposed, it is an error to invoke any other method using the editor.

 	 * </p>

 	 *

 	 * @return <code>true</code> when the editor is disposed and <code>false</code> otherwise.

 	 */

 	protected boolean isDisposed() {

-		return pages == null;

+		if (pages == null || getSite() == null || getSite().getWorkbenchWindow() == null) {

+			return true;

+		}

+

+		return false;

 	}

 

 	/**

@@ -1219,10 +1224,14 @@
 	}

 

 	protected boolean isActivePart() {

-		return this == getSite().getWorkbenchWindow().getPartService().getActivePart();

+		return !isDisposed() && this == getSite().getWorkbenchWindow().getPartService().getActivePart();

 	}

 

 	protected boolean isActivePropertySheetPage() {

+		if (isDisposed()) {

+			return false;

+		}

+

 		IWorkbenchPart activePart = getSite().getWorkbenchWindow().getPartService().getActivePart();

 		if (activePart instanceof PropertySheet) {

 			return propertySheetPages.contains(((PropertySheet) activePart).getCurrentPage());

@@ -1604,21 +1613,25 @@
 		@Override

 		public void handleEditorInputObjectAdded(IEditorInput editorInput, final Set<EObject> addedObjects) {

 			IWorkbenchPartSite site = getSite();

-			if (site != null && site.getShell() != null && !site.getShell().isDisposed()) {

+			if (!isDisposed() && site.getShell() != null && !site.getShell().isDisposed()) {

 				site.getShell().getDisplay().asyncExec(new Runnable() {

+

 					@Override

 					public void run() {

-						// Reset the editor state

-						reset();

+						if (!isDisposed()) {

+							// Reset the editor state

+							reset();

 

-						// Finish page creation if editor is currently visible; otherwise mark that as to be

-						// done when editor gets activated the next time

-						if (getSite().getPage().isPartVisible(BasicTransactionalFormEditor.this)) {

-							finishCreatePages();

-						} else {

-							finishCreatePagesOnActivation = true;

+							// Finish page creation if editor is currently visible; otherwise mark that as to be

+							// done when editor gets activated the next time

+							if (getSite().getPage().isPartVisible(BasicTransactionalFormEditor.this)) {

+								finishCreatePages();

+							} else {

+								finishCreatePagesOnActivation = true;

+							}

 						}

 					}

+

 				});

 			}

 		}

@@ -1629,7 +1642,9 @@
 		@Override

 		public void handleEditorInputObjectRemoved(IEditorInput editorInput, Set<EObject> removedObjects) {

 			// Close editor

-			close(false);

+			if (!isDisposed()) {

+				close(false);

+			}

 		}

 

 		/**

@@ -1647,27 +1662,31 @@
 		public void handleEditorInputObjectChanged(IEditorInput editorInput, final Set<EObject> changedObjects) {

 			// Handle affected objects

 			IWorkbenchPartSite site = getSite();

-			if (site != null && site.getShell() != null && !site.getShell().isDisposed()) {

+			if (!isDisposed() && site.getShell() != null && !site.getShell().isDisposed()) {

 				site.getShell().getDisplay().asyncExec(new Runnable() {

+

 					@Override

 					public void run() {

-						if (isActivePart() || isActivePropertySheetPage()) {

-							// Try to select the affected objects

-							setSelectionToViewer(changedObjects);

-						}

+						if (!isDisposed()) {

+							if (isActivePart() || isActivePropertySheetPage()) {

+								// Try to select the affected objects

+								setSelectionToViewer(changedObjects);

+							}

 

-						// Update editor input if necessary (e.g., when editor input object is a model object and

-						// the latter has been renamed)

-						Object editorInputObject = getEditorInputObject();

-						if (editorInputObject instanceof EObject) {

-							URI newEditorInputObjectURI = EcoreUtil.getURI((EObject) editorInputObject);

-							updateEditorInput(newEditorInputObjectURI);

-						}

+							// Update editor input if necessary (e.g., when editor input object is a model object and

+							// the latter has been renamed)

+							Object editorInputObject = getEditorInputObject();

+							if (editorInputObject instanceof EObject) {

+								URI newEditorInputObjectURI = EcoreUtil.getURI((EObject) editorInputObject);

+								updateEditorInput(newEditorInputObjectURI);

+							}

 

-						// Update editor part name

-						setPartName(getEditorInputName());

-						setTitleImage(getEditorInputImage());

+							// Update editor part name

+							setPartName(getEditorInputName());

+							setTitleImage(getEditorInputImage());

+						}

 					}

+

 				});

 			}

 		}

@@ -1678,24 +1697,7 @@
 		@Override

 		public void handleEditorInputResourceLoaded(IEditorInput editorInput) {

 			// Invoked when editor input object has been added.

-			final IWorkbenchPartSite site = getSite();

-			if (site != null && site.getShell() != null && !site.getShell().isDisposed()) {

-				site.getShell().getDisplay().asyncExec(new Runnable() {

-					@Override

-					public void run() {

-						// Reset the editor state

-						reset();

-

-						// Finish page creation if editor is currently visible; otherwise mark that as to be done when

-						// editor gets activated the next time

-						if (site.getPage().isPartVisible(BasicTransactionalFormEditor.this)) {

-							finishCreatePages();

-						} else {

-							finishCreatePagesOnActivation = true;

-						}

-					}

-				});

-			}

+			handleEditorInputObjectAdded(editorInput, Collections.emptySet());

 		}

 

 		/**

@@ -1704,17 +1706,21 @@
 		@Override

 		public void handleEditorInputResourceUnloaded(IEditorInput editorInput) {

 			IWorkbenchPartSite site = getSite();

-			if (site != null && site.getShell() != null && !site.getShell().isDisposed()) {

+			if (!isDisposed() && site.getShell() != null && !site.getShell().isDisposed()) {

 				site.getShell().getDisplay().asyncExec(new Runnable() {

+

 					@Override

 					public void run() {

-						// Reset the editor state

-						reset();

+						if (!isDisposed()) {

+							// Reset the editor state

+							reset();

 

-						// Add message page indicating that no editor input is present (even if the editor is not

-						// visible)

-						setMessagePage(createNoEditorInputPage());

+							// Add message page indicating that no editor input is present (even if the editor is not

+							// visible)

+							setMessagePage(createNoEditorInputPage());

+						}

 					}

+

 				});

 			}

 		}

@@ -1725,22 +1731,26 @@
 		@Override

 		public void handleEditorInputResourceMoved(IEditorInput editorInput, URI oldURI, final URI newURI) {

 			IWorkbenchPartSite site = getSite();

-			if (site != null && site.getShell() != null && !site.getShell().isDisposed()) {

+			if (!isDisposed() && site.getShell() != null && !site.getShell().isDisposed()) {

 				site.getShell().getDisplay().asyncExec(new Runnable() {

+

 					@Override

 					public void run() {

-						// Discard undo context

-						IOperationHistory operationHistory = getOperationHistory();

-						if (operationHistory != null) {

-							operationHistory.dispose(getModelEditorUndoContextManager().getUndoContext(), true, true, true);

+						if (!isDisposed()) {

+							// Discard undo context

+							IOperationHistory operationHistory = getOperationHistory();

+							if (operationHistory != null) {

+								operationHistory.dispose(getModelEditorUndoContextManager().getUndoContext(), true, true, true);

+							}

+

+							// Update editor input

+							updateEditorInput(newURI);

+

+							// Update this editor's dirty state

+							firePropertyChange(IEditorPart.PROP_DIRTY);

 						}

-

-						// Update editor input

-						updateEditorInput(newURI);

-

-						// Update this editor's dirty state

-						firePropertyChange(IEditorPart.PROP_DIRTY);

 					}

+

 				});

 			}

 		}

@@ -1750,8 +1760,7 @@
 		 */

 		@Override

 		public void handleEditorInputResourceRemoved(IEditorInput editorInput) {

-			// Close editor

-			close(false);

+			handleEditorInputObjectRemoved(editorInput, Collections.emptySet());

 		}

 	}

 }
\ No newline at end of file