Bug 433650 - Editor in in dirty state after a Save

* DiagramBehavior should check command stored on last save not the
command stack

Change-Id: I06c6ec3583de71418c914a65c3c0e673ff26b096
diff --git a/plugins/org.eclipse.graphiti.ui/src/org/eclipse/graphiti/ui/editor/DiagramBehavior.java b/plugins/org.eclipse.graphiti.ui/src/org/eclipse/graphiti/ui/editor/DiagramBehavior.java
index 2c68103..1f274c5 100644
--- a/plugins/org.eclipse.graphiti.ui/src/org/eclipse/graphiti/ui/editor/DiagramBehavior.java
+++ b/plugins/org.eclipse.graphiti.ui/src/org/eclipse/graphiti/ui/editor/DiagramBehavior.java
@@ -1,7 +1,7 @@
 /*******************************************************************************
  * <copyright>
  *
- * Copyright (c) 2013 SRC
+ * Copyright (c) 2014 SRC
  * 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
@@ -13,6 +13,7 @@
  *    mwenz - Bug 401859 - Graphiti DiagramEditor#dispose() does not release the editor related objects
  *    mwenz - Bug 407510 - Color background without Grid Layer turned to gray
  *    fvelasco - Bug 403664 - Enable DoubleClickFeature on the diagram background
+ *    mwenz - Bug 433650 - Editor in in dirty state after a Save
  *
  * </copyright>
  *
@@ -33,7 +34,6 @@
 import org.eclipse.draw2d.PositionConstants;
 import org.eclipse.draw2d.geometry.Dimension;
 import org.eclipse.draw2d.geometry.Point;
-import org.eclipse.emf.common.command.BasicCommandStack;
 import org.eclipse.emf.ecore.resource.ResourceSet;
 import org.eclipse.emf.transaction.RecordingCommand;
 import org.eclipse.emf.transaction.TransactionalEditingDomain;
@@ -712,16 +712,11 @@
 	/**
 	 * Returns the dirty state of this behavior object
 	 * 
-	 * @return <code>true</code> in case the command stack reports modification,
-	 *         <code>false</code> otherwise.
+	 * @return <code>true</code> in case the stored saved command is different
+	 *         from the next undo command.
 	 */
 	protected boolean isDirty() {
-		TransactionalEditingDomain editingDomain = getEditingDomain();
-		// Check that the editor is not yet disposed
-		if (editingDomain != null && editingDomain.getCommandStack() != null) {
-			return ((BasicCommandStack) editingDomain.getCommandStack()).isSaveNeeded();
-		}
-		return false;
+		return getPersistencyBehavior().isDirty();
 	}
 
 	// ---------------------- Palette --------------------------------------- //
diff --git a/tests/org.eclipse.graphiti.bot.tests/src/org/eclipse/graphiti/bot/tests/GFOtherTests.java b/tests/org.eclipse.graphiti.bot.tests/src/org/eclipse/graphiti/bot/tests/GFOtherTests.java
index 0a0a031..9a1856d 100644
--- a/tests/org.eclipse.graphiti.bot.tests/src/org/eclipse/graphiti/bot/tests/GFOtherTests.java
+++ b/tests/org.eclipse.graphiti.bot.tests/src/org/eclipse/graphiti/bot/tests/GFOtherTests.java
@@ -1,7 +1,7 @@
 /*******************************************************************************
  * <copyright>
  *
- * Copyright (c) 2005, 2013 SAP AG.
+ * Copyright (c) 2005, 2014 SAP AG.
  * 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
@@ -21,6 +21,7 @@
  *    mwenz - Bug 378342 - Cannot store more than a diagram per file
  *    pjpaulin - Bug 352120 - Now uses IDiagramContainerUI interface
  *    mwenz - Bug 391046 - Deadlock while saving prior to refactoring operation
+ *    mwenz - Bug 433650 - Editor in in dirty state after a Save
  *
  * </copyright>
  *
@@ -1380,6 +1381,50 @@
 		page.shutdownEditor(diagramEditor);
 	}
 
+	/*
+	 * Test for Bug 433650 - Editor in in dirty state after a Save
+	 */
+	@Test
+	public void testIsDirtyIsFalseAfterDoSaveUndoSave() throws Exception {
+		final IDiagramContainerUI diagramEditor = openDiagramEditor(ITestConstants.DIAGRAM_TYPE_ID_ECORE);
+		final IDiagramTypeProvider diagramTypeProvider = diagramEditor.getDiagramTypeProvider();
+		final IFeatureProvider fp = diagramTypeProvider.getFeatureProvider();
+		final Diagram diagram = diagramTypeProvider.getDiagram();
+		syncExec(new VoidResult() {
+			public void run() {
+				executeInRecordingCommand(diagramEditor.getDiagramBehavior(), new Runnable() {
+					public void run() {
+						addClassToDiagram(fp, diagram, 500, 500, "Shape");
+						addClassToDiagram(fp, diagram, 100, 100, "ContainerShape");
+						removeClassShape(fp, diagram, "ContainerShape");
+						moveClassShape(fp, diagram, 0, 0, "Shape");
+
+					}
+				});
+			}
+		});
+
+		diagramEditor.doSave(new NullProgressMonitor());
+
+		syncExec(new VoidResult() {
+			public void run() {
+
+				// get UnDoStack
+				TransactionalEditingDomain editingDomain = diagramEditor.getDiagramBehavior().getEditingDomain();
+				org.eclipse.emf.common.command.CommandStack cmdStack = editingDomain.getCommandStack();
+
+				// Do "undo"
+				cmdStack.undo();
+				assertEquals("Undo/Redo failed: Diagram not empty at end of UndoStack", 0, diagram.getChildren().size());
+			}
+		});
+
+		diagramEditor.doSave(new NullProgressMonitor());
+		assertFalse(diagramEditor.isDirty());
+
+		page.shutdownEditor(diagramEditor);
+	}
+
 	private IFile createPersistentDiagram() throws Exception {
 		return createPersistentDiagram(null);
 	}