Skip to content
Snippets Groups Projects
Commit 69025ced authored by Florian Hölzl's avatar Florian Hölzl
Browse files

added GEF editorBase

changed GEF command stack implementation
parent 8ffee5b0
No related branches found
No related tags found
No related merge requests found
......@@ -15,80 +15,93 @@ $Id$
| See the License for the specific language governing permissions and |
| limitations under the License. |
+--------------------------------------------------------------------------*/
package org.fortiss.tooling.kernel.base.ui.internal.command;
package org.fortiss.tooling.kernel.base.ui.editor.gef;
import org.eclipse.gef.commands.Command;
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;
/**
* This class wraps an EMF command in a GEF command.
* GEF editor base implementation.
*
* It provides {@link EditDomain} and thus a {@link CommandStack}.
*
* @author hummel
* @author hoelzl
* @author $Author$
* @version $Rev$
* @ConQAT.Rating RED Hash: D6B33EFB704B0CA151E1C945B5BC48C6
* @ConQAT.Rating YELLOW Hash: 63FF468A2D042586A22495828A0798EF
*/
public class EMF2GEFCommand extends Command {
public abstract class EditorBase<T extends EObject> extends
org.fortiss.tooling.kernel.ui.base.EditorBase<T> {
/** The wrapped command. */
private final org.eclipse.emf.common.command.Command emfCommand;
/** The edit domain (used e.g. for managing the command stack). */
private final EditDomain editDomain = new DefaultEditDomain(this);
/** Constructor. */
public EMF2GEFCommand(org.eclipse.emf.common.command.Command emfCommand) {
this.emfCommand = emfCommand;
}
/** 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 boolean canExecute() {
return emfCommand.canExecute();
}
public void init(IEditorSite site, IEditorInput input)
throws PartInitException {
super.init(site, input);
/** {@inheritDoc} */
@Override
public boolean canUndo() {
return emfCommand.canUndo();
// install another command stack
editDomain.setCommandStack(new GEF2ToolingKernelCommandStack(
getEditedObject()));
getEditedObject().eAdapters().add(removeAdapter);
}
/** {@inheritDoc} */
@Override
public void dispose() {
emfCommand.dispose();
if (getEditedObject() != null) {
getEditedObject().eAdapters().remove(removeAdapter);
}
super.dispose();
}
/** {@inheritDoc} */
@SuppressWarnings("rawtypes")
@Override
public void execute() {
emfCommand.execute();
}
/** {@inheritDoc} */
@Override
public String getLabel() {
return emfCommand.getLabel();
}
public Object getAdapter(Class adapter) {
// The command stack adaption is required for some of the GEF actions
if (adapter == CommandStack.class) {
return getCommandStack();
}
/** {@inheritDoc} */
@Override
public void redo() {
emfCommand.redo();
return super.getAdapter(adapter);
}
/**
* {@inheritDoc}
*
* @throws UnsupportedOperationException
* as this is not supported.
*/
@Override
public void setLabel(String label) {
throw new UnsupportedOperationException();
/** Returns the command stack. */
protected final CommandStack getCommandStack() {
return editDomain.getCommandStack();
}
/** {@inheritDoc} */
@Override
public void undo() {
emfCommand.undo();
/** Return the edit domain */
protected final EditDomain getEditDomain() {
return editDomain;
}
}
/*--------------------------------------------------------------------------+
$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();
}
}
......@@ -20,81 +20,88 @@ 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 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.
* 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 RED Hash: D6B33EFB704B0CA151E1C945B5BC48C6
* @ConQAT.Rating YELLOW Hash: 3E1BC1C0506F211714AAE4DB9FD1ADC8
*/
public class GEF2EMFCommandStack extends CommandStack implements
public class GEF2ToolingKernelCommandStack extends CommandStack implements
CommandStackListener {
/** The wrapped command stack. */
private final org.eclipse.emf.common.command.CommandStack emfCommandStack;
/** Stores the underlying {@link ITopLevelElementContext}. */
private final ITopLevelElementContext context;
/** Constructor. */
public GEF2EMFCommandStack(
org.eclipse.emf.common.command.CommandStack emfCommandStack) {
this.emfCommandStack = emfCommandStack;
emfCommandStack.addCommandStackListener(this);
public GEF2ToolingKernelCommandStack(EObject modelElement) {
context = IPersistencyService.INSTANCE
.getTopLevelElementContextFor(modelElement);
context.addCommandStackListener(this);
}
/** {@inheritDoc} */
@Override
public void dispose() {
emfCommandStack.removeCommandStackListener(this);
context.removeCommandStackListener(this);
super.dispose();
}
/** {@inheritDoc} */
@Override
public boolean canRedo() {
return emfCommandStack.canRedo();
return context.canRedo();
}
/** {@inheritDoc} */
@Override
public boolean canUndo() {
return emfCommandStack.canUndo();
return context.canUndo();
}
/** {@inheritDoc} */
@Override
public void execute(Command command) {
emfCommandStack.execute(new GEF2EMFCommand(command));
public void execute(final Command command) {
context.runAsCommand(new Runnable() {
@Override
public void run() {
command.execute();
}
});
}
/** {@inheritDoc} */
@Override
public Command getRedoCommand() {
return new EMF2GEFCommand(emfCommandStack.getRedoCommand());
return new TopLevelElementContext2GEFCommand();
}
/** {@inheritDoc} */
@Override
public Command getUndoCommand() {
return new EMF2GEFCommand(emfCommandStack.getUndoCommand());
return new TopLevelElementContext2GEFCommand();
}
/** {@inheritDoc} */
@Override
public void redo() {
emfCommandStack.redo();
context.redo();
}
/** {@inheritDoc} */
@Override
public void undo() {
emfCommandStack.undo();
context.undo();
}
/** {@inheritDoc} */
......@@ -103,4 +110,70 @@ public class GEF2EMFCommandStack extends CommandStack implements
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();
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment