From 69025ced8b3979c833ab7fe84e67dc74bfb7fb96 Mon Sep 17 00:00:00 2001
From: Florian Hoelzl <hoelzl@fortiss.org>
Date: Tue, 14 Jun 2011 15:17:44 +0000
Subject: [PATCH] added GEF editorBase changed GEF command stack implementation

---
 .../kernel/base/ui/editor/gef/EditorBase.java | 107 +++++++++++
 .../ui/internal/command/EMF2GEFCommand.java   |  94 ---------
 .../ui/internal/command/GEF2EMFCommand.java   | 117 ------------
 .../internal/command/GEF2EMFCommandStack.java | 106 -----------
 .../GEF2ToolingKernelCommandStack.java        | 179 ++++++++++++++++++
 5 files changed, 286 insertions(+), 317 deletions(-)
 create mode 100644 org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/kernel/base/ui/editor/gef/EditorBase.java
 delete mode 100644 org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/kernel/base/ui/internal/command/EMF2GEFCommand.java
 delete mode 100644 org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/kernel/base/ui/internal/command/GEF2EMFCommand.java
 delete mode 100644 org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/kernel/base/ui/internal/command/GEF2EMFCommandStack.java
 create mode 100644 org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/kernel/base/ui/internal/command/GEF2ToolingKernelCommandStack.java

diff --git a/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/kernel/base/ui/editor/gef/EditorBase.java b/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/kernel/base/ui/editor/gef/EditorBase.java
new file mode 100644
index 000000000..96381af1e
--- /dev/null
+++ b/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/kernel/base/ui/editor/gef/EditorBase.java
@@ -0,0 +1,107 @@
+/*--------------------------------------------------------------------------+
+$Id$
+|                                                                          |
+| Copyright 2011 ForTISS GmbH                                              |
+|                                                                          |
+| Licensed under the Apache License, Version 2.0 (the "License");          |
+| you may not use this file except in compliance with the License.         |
+| You may obtain a copy of the License at                                  |
+|                                                                          |
+|    http://www.apache.org/licenses/LICENSE-2.0                            |
+|                                                                          |
+| Unless required by applicable law or agreed to in writing, software      |
+| distributed under the License is distributed on an "AS IS" BASIS,        |
+| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
+| See the License for the specific language governing permissions and      |
+| limitations under the License.                                           |
++--------------------------------------------------------------------------*/
+package org.fortiss.tooling.kernel.base.ui.editor.gef;
+
+import org.eclipse.emf.common.notify.Adapter;
+import org.eclipse.emf.common.notify.Notification;
+import org.eclipse.emf.common.notify.impl.AdapterImpl;
+import org.eclipse.emf.ecore.EObject;
+import org.eclipse.gef.DefaultEditDomain;
+import org.eclipse.gef.EditDomain;
+import org.eclipse.gef.commands.CommandStack;
+import org.eclipse.ui.IEditorInput;
+import org.eclipse.ui.IEditorSite;
+import org.eclipse.ui.PartInitException;
+import org.fortiss.tooling.kernel.base.ui.internal.command.GEF2ToolingKernelCommandStack;
+import org.fortiss.tooling.kernel.services.IPersistencyService;
+
+/**
+ * GEF editor base implementation.
+ * 
+ * It provides {@link EditDomain} and thus a {@link CommandStack}.
+ * 
+ * @author hummel
+ * @author hoelzl
+ * @author $Author$
+ * @version $Rev$
+ * @ConQAT.Rating YELLOW Hash: 63FF468A2D042586A22495828A0798EF
+ */
+public abstract class EditorBase<T extends EObject> extends
+		org.fortiss.tooling.kernel.ui.base.EditorBase<T> {
+
+	/** The edit domain (used e.g. for managing the command stack). */
+	private final EditDomain editDomain = new DefaultEditDomain(this);
+
+	/** The adapter used to find out when the edited object is removed. */
+	private final Adapter removeAdapter = new AdapterImpl() {
+		@Override
+		public void notifyChanged(Notification notification) {
+			// object is deleted if it does not have a parent and is not a
+			// top-level element
+			if (getEditedObject().eContainer() == null
+					&& IPersistencyService.INSTANCE
+							.getTopLevelElementContextFor(getEditedObject()) != null) {
+				getSite().getPage().closeEditor(EditorBase.this, false);
+			}
+		}
+	};
+
+	/** {@inheritDoc} */
+	@Override
+	public void init(IEditorSite site, IEditorInput input)
+			throws PartInitException {
+		super.init(site, input);
+
+		// install another command stack
+		editDomain.setCommandStack(new GEF2ToolingKernelCommandStack(
+				getEditedObject()));
+
+		getEditedObject().eAdapters().add(removeAdapter);
+	}
+
+	/** {@inheritDoc} */
+	@Override
+	public void dispose() {
+		if (getEditedObject() != null) {
+			getEditedObject().eAdapters().remove(removeAdapter);
+		}
+		super.dispose();
+	}
+
+	/** {@inheritDoc} */
+	@SuppressWarnings("rawtypes")
+	@Override
+	public Object getAdapter(Class adapter) {
+		// The command stack adaption is required for some of the GEF actions
+		if (adapter == CommandStack.class) {
+			return getCommandStack();
+		}
+
+		return super.getAdapter(adapter);
+	}
+
+	/** Returns the command stack. */
+	protected final CommandStack getCommandStack() {
+		return editDomain.getCommandStack();
+	}
+
+	/** Return the edit domain */
+	protected final EditDomain getEditDomain() {
+		return editDomain;
+	}
+}
diff --git a/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/kernel/base/ui/internal/command/EMF2GEFCommand.java b/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/kernel/base/ui/internal/command/EMF2GEFCommand.java
deleted file mode 100644
index 2cc2bc8aa..000000000
--- a/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/kernel/base/ui/internal/command/EMF2GEFCommand.java
+++ /dev/null
@@ -1,94 +0,0 @@
-/*--------------------------------------------------------------------------+
-$Id$
-|                                                                          |
-| Copyright 2011 ForTISS GmbH                                              |
-|                                                                          |
-| Licensed under the Apache License, Version 2.0 (the "License");          |
-| you may not use this file except in compliance with the License.         |
-| You may obtain a copy of the License at                                  |
-|                                                                          |
-|    http://www.apache.org/licenses/LICENSE-2.0                            |
-|                                                                          |
-| Unless required by applicable law or agreed to in writing, software      |
-| distributed under the License is distributed on an "AS IS" BASIS,        |
-| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
-| See the License for the specific language governing permissions and      |
-| limitations under the License.                                           |
-+--------------------------------------------------------------------------*/
-package org.fortiss.tooling.kernel.base.ui.internal.command;
-
-import org.eclipse.gef.commands.Command;
-
-/**
- * This class wraps an EMF command in a GEF command.
- * 
- * @author hummel
- * @author hoelzl
- * @author $Author$
- * @version $Rev$
- * @ConQAT.Rating RED Hash: D6B33EFB704B0CA151E1C945B5BC48C6
- */
-public class EMF2GEFCommand extends Command {
-
-	/** The wrapped command. */
-	private final org.eclipse.emf.common.command.Command emfCommand;
-
-	/** Constructor. */
-	public EMF2GEFCommand(org.eclipse.emf.common.command.Command emfCommand) {
-		this.emfCommand = emfCommand;
-	}
-
-	/** {@inheritDoc} */
-	@Override
-	public boolean canExecute() {
-		return emfCommand.canExecute();
-	}
-
-	/** {@inheritDoc} */
-	@Override
-	public boolean canUndo() {
-		return emfCommand.canUndo();
-	}
-
-	/** {@inheritDoc} */
-	@Override
-	public void dispose() {
-		emfCommand.dispose();
-		super.dispose();
-	}
-
-	/** {@inheritDoc} */
-	@Override
-	public void execute() {
-		emfCommand.execute();
-	}
-
-	/** {@inheritDoc} */
-	@Override
-	public String getLabel() {
-		return emfCommand.getLabel();
-	}
-
-	/** {@inheritDoc} */
-	@Override
-	public void redo() {
-		emfCommand.redo();
-	}
-
-	/**
-	 * {@inheritDoc}
-	 * 
-	 * @throws UnsupportedOperationException
-	 *             as this is not supported.
-	 */
-	@Override
-	public void setLabel(String label) {
-		throw new UnsupportedOperationException();
-	}
-
-	/** {@inheritDoc} */
-	@Override
-	public void undo() {
-		emfCommand.undo();
-	}
-}
diff --git a/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/kernel/base/ui/internal/command/GEF2EMFCommand.java b/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/kernel/base/ui/internal/command/GEF2EMFCommand.java
deleted file mode 100644
index c367a47d2..000000000
--- a/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/kernel/base/ui/internal/command/GEF2EMFCommand.java
+++ /dev/null
@@ -1,117 +0,0 @@
-/*--------------------------------------------------------------------------+
-$Id$
-|                                                                          |
-| Copyright 2011 ForTISS GmbH                                              |
-|                                                                          |
-| Licensed under the Apache License, Version 2.0 (the "License");          |
-| you may not use this file except in compliance with the License.         |
-| You may obtain a copy of the License at                                  |
-|                                                                          |
-|    http://www.apache.org/licenses/LICENSE-2.0                            |
-|                                                                          |
-| Unless required by applicable law or agreed to in writing, software      |
-| distributed under the License is distributed on an "AS IS" BASIS,        |
-| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
-| See the License for the specific language governing permissions and      |
-| limitations under the License.                                           |
-+--------------------------------------------------------------------------*/
-package org.fortiss.tooling.kernel.base.ui.internal.command;
-
-import java.util.Collection;
-import java.util.Collections;
-
-import org.eclipse.emf.common.command.Command;
-import org.eclipse.emf.common.command.CompoundCommand;
-
-/**
- * This class wraps a GEF command in an EMF command.
- * 
- * @author hummel
- * @author hoelzl
- * @author $Author$
- * @version $Rev$
- * @ConQAT.Rating RED Hash: D6B33EFB704B0CA151E1C945B5BC48C6
- */
-public class GEF2EMFCommand implements Command {
-
-	/** The wrapped command. */
-	private final org.eclipse.gef.commands.Command gefCommand;
-
-	/** Constructor. */
-	public GEF2EMFCommand(org.eclipse.gef.commands.Command gefCommand) {
-		this.gefCommand = gefCommand;
-	}
-
-	/** {@inheritDoc} */
-	@Override
-	public boolean canExecute() {
-		return gefCommand.canExecute();
-	}
-
-	/** {@inheritDoc} */
-	@Override
-	public boolean canUndo() {
-		return gefCommand.canUndo();
-	}
-
-	/** {@inheritDoc} */
-	@Override
-	public Command chain(Command command) {
-		if (command == null) {
-			return this;
-		}
-
-		CompoundCommand result = new CompoundCommand();
-		result.append(this);
-		result.append(command);
-		return result;
-	}
-
-	/** {@inheritDoc} */
-	@Override
-	public void dispose() {
-		gefCommand.dispose();
-	}
-
-	/** {@inheritDoc} */
-	@Override
-	public void execute() {
-		gefCommand.execute();
-	}
-
-	/** {@inheritDoc} */
-	@Override
-	public Collection<?> getAffectedObjects() {
-		return Collections.EMPTY_LIST;
-	}
-
-	/** {@inheritDoc} */
-	@Override
-	public String getDescription() {
-		return gefCommand.getLabel();
-	}
-
-	/** {@inheritDoc} */
-	@Override
-	public String getLabel() {
-		return gefCommand.getLabel();
-	}
-
-	/** {@inheritDoc} */
-	@Override
-	public Collection<?> getResult() {
-		return Collections.EMPTY_LIST;
-	}
-
-	/** {@inheritDoc} */
-	@Override
-	public void redo() {
-		gefCommand.redo();
-	}
-
-	/** {@inheritDoc} */
-	@Override
-	public void undo() {
-		gefCommand.undo();
-	}
-}
diff --git a/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/kernel/base/ui/internal/command/GEF2EMFCommandStack.java b/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/kernel/base/ui/internal/command/GEF2EMFCommandStack.java
deleted file mode 100644
index 8b457d4e0..000000000
--- a/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/kernel/base/ui/internal/command/GEF2EMFCommandStack.java
+++ /dev/null
@@ -1,106 +0,0 @@
-/*--------------------------------------------------------------------------+
-$Id$
-|                                                                          |
-| Copyright 2011 ForTISS GmbH                                              |
-|                                                                          |
-| Licensed under the Apache License, Version 2.0 (the "License");          |
-| you may not use this file except in compliance with the License.         |
-| You may obtain a copy of the License at                                  |
-|                                                                          |
-|    http://www.apache.org/licenses/LICENSE-2.0                            |
-|                                                                          |
-| Unless required by applicable law or agreed to in writing, software      |
-| distributed under the License is distributed on an "AS IS" BASIS,        |
-| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
-| See the License for the specific language governing permissions and      |
-| limitations under the License.                                           |
-+--------------------------------------------------------------------------*/
-package org.fortiss.tooling.kernel.base.ui.internal.command;
-
-import java.util.EventObject;
-
-import org.eclipse.emf.common.command.CommandStackListener;
-import org.eclipse.gef.commands.Command;
-import org.eclipse.gef.commands.CommandStack;
-
-/**
- * This command stack wraps an EMF command stack. Execute, undo and redo are
- * forwarded, as well as the listener stuff. Note that the dirty state and save
- * location are <b>not</b> forwarded, i.e. the underlying editing domain should
- * be used directly for this.
- * 
- * @author hummel
- * @author hoelzl
- * @author $Author$
- * @version $Rev$
- * @ConQAT.Rating RED Hash: D6B33EFB704B0CA151E1C945B5BC48C6
- */
-public class GEF2EMFCommandStack extends CommandStack implements
-		CommandStackListener {
-
-	/** The wrapped command stack. */
-	private final org.eclipse.emf.common.command.CommandStack emfCommandStack;
-
-	/** Constructor. */
-	public GEF2EMFCommandStack(
-			org.eclipse.emf.common.command.CommandStack emfCommandStack) {
-		this.emfCommandStack = emfCommandStack;
-		emfCommandStack.addCommandStackListener(this);
-	}
-
-	/** {@inheritDoc} */
-	@Override
-	public void dispose() {
-		emfCommandStack.removeCommandStackListener(this);
-		super.dispose();
-	}
-
-	/** {@inheritDoc} */
-	@Override
-	public boolean canRedo() {
-		return emfCommandStack.canRedo();
-	}
-
-	/** {@inheritDoc} */
-	@Override
-	public boolean canUndo() {
-		return emfCommandStack.canUndo();
-	}
-
-	/** {@inheritDoc} */
-	@Override
-	public void execute(Command command) {
-		emfCommandStack.execute(new GEF2EMFCommand(command));
-	}
-
-	/** {@inheritDoc} */
-	@Override
-	public Command getRedoCommand() {
-		return new EMF2GEFCommand(emfCommandStack.getRedoCommand());
-	}
-
-	/** {@inheritDoc} */
-	@Override
-	public Command getUndoCommand() {
-		return new EMF2GEFCommand(emfCommandStack.getUndoCommand());
-	}
-
-	/** {@inheritDoc} */
-	@Override
-	public void redo() {
-		emfCommandStack.redo();
-	}
-
-	/** {@inheritDoc} */
-	@Override
-	public void undo() {
-		emfCommandStack.undo();
-	}
-
-	/** {@inheritDoc} */
-	@Override
-	@SuppressWarnings("deprecation")
-	public void commandStackChanged(EventObject event) {
-		notifyListeners();
-	}
-}
diff --git a/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/kernel/base/ui/internal/command/GEF2ToolingKernelCommandStack.java b/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/kernel/base/ui/internal/command/GEF2ToolingKernelCommandStack.java
new file mode 100644
index 000000000..834e81c8e
--- /dev/null
+++ b/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/kernel/base/ui/internal/command/GEF2ToolingKernelCommandStack.java
@@ -0,0 +1,179 @@
+/*--------------------------------------------------------------------------+
+$Id$
+|                                                                          |
+| Copyright 2011 ForTISS GmbH                                              |
+|                                                                          |
+| Licensed under the Apache License, Version 2.0 (the "License");          |
+| you may not use this file except in compliance with the License.         |
+| You may obtain a copy of the License at                                  |
+|                                                                          |
+|    http://www.apache.org/licenses/LICENSE-2.0                            |
+|                                                                          |
+| Unless required by applicable law or agreed to in writing, software      |
+| distributed under the License is distributed on an "AS IS" BASIS,        |
+| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
+| See the License for the specific language governing permissions and      |
+| limitations under the License.                                           |
++--------------------------------------------------------------------------*/
+package org.fortiss.tooling.kernel.base.ui.internal.command;
+
+import java.util.EventObject;
+
+import org.eclipse.emf.common.command.CommandStackListener;
+import org.eclipse.emf.ecore.EObject;
+import org.eclipse.gef.commands.Command;
+import org.eclipse.gef.commands.CommandStack;
+import org.fortiss.tooling.kernel.interfaces.ITopLevelElementContext;
+import org.fortiss.tooling.kernel.services.IPersistencyService;
+
+/**
+ * This command stack wraps the tooling kernel command stack. Execute, isDirty,
+ * undo and redo are forwarded, as well as the listener stuff.
+ * 
+ * @author hummel
+ * @author hoelzl
+ * @author $Author$
+ * @version $Rev$
+ * @ConQAT.Rating YELLOW Hash: 3E1BC1C0506F211714AAE4DB9FD1ADC8
+ */
+public class GEF2ToolingKernelCommandStack extends CommandStack implements
+		CommandStackListener {
+
+	/** Stores the underlying {@link ITopLevelElementContext}. */
+	private final ITopLevelElementContext context;
+
+	/** Constructor. */
+	public GEF2ToolingKernelCommandStack(EObject modelElement) {
+		context = IPersistencyService.INSTANCE
+				.getTopLevelElementContextFor(modelElement);
+		context.addCommandStackListener(this);
+	}
+
+	/** {@inheritDoc} */
+	@Override
+	public void dispose() {
+		context.removeCommandStackListener(this);
+		super.dispose();
+	}
+
+	/** {@inheritDoc} */
+	@Override
+	public boolean canRedo() {
+		return context.canRedo();
+	}
+
+	/** {@inheritDoc} */
+	@Override
+	public boolean canUndo() {
+		return context.canUndo();
+	}
+
+	/** {@inheritDoc} */
+	@Override
+	public void execute(final Command command) {
+		context.runAsCommand(new Runnable() {
+
+			@Override
+			public void run() {
+				command.execute();
+			}
+		});
+	}
+
+	/** {@inheritDoc} */
+	@Override
+	public Command getRedoCommand() {
+		return new TopLevelElementContext2GEFCommand();
+	}
+
+	/** {@inheritDoc} */
+	@Override
+	public Command getUndoCommand() {
+		return new TopLevelElementContext2GEFCommand();
+	}
+
+	/** {@inheritDoc} */
+	@Override
+	public void redo() {
+		context.redo();
+	}
+
+	/** {@inheritDoc} */
+	@Override
+	public void undo() {
+		context.undo();
+	}
+
+	/** {@inheritDoc} */
+	@Override
+	@SuppressWarnings("deprecation")
+	public void commandStackChanged(EventObject event) {
+		notifyListeners();
+	}
+
+	/** {@inheritDoc} */
+	@Override
+	public boolean isDirty() {
+		return context.isDirty();
+	}
+
+	/**
+	 * This class wraps an EMF command in a GEF command.
+	 * 
+	 * @author hummel
+	 * @author hoelzl
+	 * @author $Author$
+	 * @version $Rev$
+	 * @ConQAT.Rating RED Hash: D6B33EFB704B0CA151E1C945B5BC48C6
+	 */
+	private class TopLevelElementContext2GEFCommand extends Command {
+
+		/** {@inheritDoc} */
+		@Override
+		public boolean canExecute() {
+			// wrapper can never be executed; undo/redo only
+			return false;
+		}
+
+		/** {@inheritDoc} */
+		@Override
+		public boolean canUndo() {
+			return context.canUndo();
+		}
+
+		/** {@inheritDoc} */
+		@Override
+		public void execute() {
+			// never called
+		}
+
+		/** {@inheritDoc} */
+		@Override
+		public String getLabel() {
+			return "";
+		}
+
+		/** {@inheritDoc} */
+		@Override
+		public void redo() {
+			context.redo();
+		}
+
+		/**
+		 * {@inheritDoc}
+		 * 
+		 * @throws UnsupportedOperationException
+		 *             as this is not supported.
+		 */
+		@Override
+		public void setLabel(String label) {
+			throw new UnsupportedOperationException();
+		}
+
+		/** {@inheritDoc} */
+		@Override
+		public void undo() {
+			context.undo();
+		}
+	}
+}
-- 
GitLab