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$ ...@@ -15,80 +15,93 @@ $Id$
| See the License for the specific language governing permissions and | | See the License for the specific language governing permissions and |
| limitations under the License. | | 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 hummel
* @author hoelzl * @author hoelzl
* @author $Author$ * @author $Author$
* @version $Rev$ * @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. */ /** The edit domain (used e.g. for managing the command stack). */
private final org.eclipse.emf.common.command.Command emfCommand; private final EditDomain editDomain = new DefaultEditDomain(this);
/** Constructor. */ /** The adapter used to find out when the edited object is removed. */
public EMF2GEFCommand(org.eclipse.emf.common.command.Command emfCommand) { private final Adapter removeAdapter = new AdapterImpl() {
this.emfCommand = emfCommand; @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} */ /** {@inheritDoc} */
@Override @Override
public boolean canExecute() { public void init(IEditorSite site, IEditorInput input)
return emfCommand.canExecute(); throws PartInitException {
} super.init(site, input);
/** {@inheritDoc} */ // install another command stack
@Override editDomain.setCommandStack(new GEF2ToolingKernelCommandStack(
public boolean canUndo() { getEditedObject()));
return emfCommand.canUndo();
getEditedObject().eAdapters().add(removeAdapter);
} }
/** {@inheritDoc} */ /** {@inheritDoc} */
@Override @Override
public void dispose() { public void dispose() {
emfCommand.dispose(); if (getEditedObject() != null) {
getEditedObject().eAdapters().remove(removeAdapter);
}
super.dispose(); super.dispose();
} }
/** {@inheritDoc} */ /** {@inheritDoc} */
@SuppressWarnings("rawtypes")
@Override @Override
public void execute() { public Object getAdapter(Class adapter) {
emfCommand.execute(); // The command stack adaption is required for some of the GEF actions
} if (adapter == CommandStack.class) {
return getCommandStack();
/** {@inheritDoc} */ }
@Override
public String getLabel() {
return emfCommand.getLabel();
}
/** {@inheritDoc} */ return super.getAdapter(adapter);
@Override
public void redo() {
emfCommand.redo();
} }
/** /** Returns the command stack. */
* {@inheritDoc} protected final CommandStack getCommandStack() {
* return editDomain.getCommandStack();
* @throws UnsupportedOperationException
* as this is not supported.
*/
@Override
public void setLabel(String label) {
throw new UnsupportedOperationException();
} }
/** {@inheritDoc} */ /** Return the edit domain */
@Override protected final EditDomain getEditDomain() {
public void undo() { return editDomain;
emfCommand.undo();
} }
} }
/*--------------------------------------------------------------------------+
$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; ...@@ -20,81 +20,88 @@ package org.fortiss.tooling.kernel.base.ui.internal.command;
import java.util.EventObject; import java.util.EventObject;
import org.eclipse.emf.common.command.CommandStackListener; import org.eclipse.emf.common.command.CommandStackListener;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.gef.commands.Command; import org.eclipse.gef.commands.Command;
import org.eclipse.gef.commands.CommandStack; 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 * This command stack wraps the tooling kernel command stack. Execute, isDirty,
* forwarded, as well as the listener stuff. Note that the dirty state and save * undo and redo are forwarded, as well as the listener stuff.
* location are <b>not</b> forwarded, i.e. the underlying editing domain should
* be used directly for this.
* *
* @author hummel * @author hummel
* @author hoelzl * @author hoelzl
* @author $Author$ * @author $Author$
* @version $Rev$ * @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 { CommandStackListener {
/** The wrapped command stack. */ /** Stores the underlying {@link ITopLevelElementContext}. */
private final org.eclipse.emf.common.command.CommandStack emfCommandStack; private final ITopLevelElementContext context;
/** Constructor. */ /** Constructor. */
public GEF2EMFCommandStack( public GEF2ToolingKernelCommandStack(EObject modelElement) {
org.eclipse.emf.common.command.CommandStack emfCommandStack) { context = IPersistencyService.INSTANCE
this.emfCommandStack = emfCommandStack; .getTopLevelElementContextFor(modelElement);
emfCommandStack.addCommandStackListener(this); context.addCommandStackListener(this);
} }
/** {@inheritDoc} */ /** {@inheritDoc} */
@Override @Override
public void dispose() { public void dispose() {
emfCommandStack.removeCommandStackListener(this); context.removeCommandStackListener(this);
super.dispose(); super.dispose();
} }
/** {@inheritDoc} */ /** {@inheritDoc} */
@Override @Override
public boolean canRedo() { public boolean canRedo() {
return emfCommandStack.canRedo(); return context.canRedo();
} }
/** {@inheritDoc} */ /** {@inheritDoc} */
@Override @Override
public boolean canUndo() { public boolean canUndo() {
return emfCommandStack.canUndo(); return context.canUndo();
} }
/** {@inheritDoc} */ /** {@inheritDoc} */
@Override @Override
public void execute(Command command) { public void execute(final Command command) {
emfCommandStack.execute(new GEF2EMFCommand(command)); context.runAsCommand(new Runnable() {
@Override
public void run() {
command.execute();
}
});
} }
/** {@inheritDoc} */ /** {@inheritDoc} */
@Override @Override
public Command getRedoCommand() { public Command getRedoCommand() {
return new EMF2GEFCommand(emfCommandStack.getRedoCommand()); return new TopLevelElementContext2GEFCommand();
} }
/** {@inheritDoc} */ /** {@inheritDoc} */
@Override @Override
public Command getUndoCommand() { public Command getUndoCommand() {
return new EMF2GEFCommand(emfCommandStack.getUndoCommand()); return new TopLevelElementContext2GEFCommand();
} }
/** {@inheritDoc} */ /** {@inheritDoc} */
@Override @Override
public void redo() { public void redo() {
emfCommandStack.redo(); context.redo();
} }
/** {@inheritDoc} */ /** {@inheritDoc} */
@Override @Override
public void undo() { public void undo() {
emfCommandStack.undo(); context.undo();
} }
/** {@inheritDoc} */ /** {@inheritDoc} */
...@@ -103,4 +110,70 @@ public class GEF2EMFCommandStack extends CommandStack implements ...@@ -103,4 +110,70 @@ public class GEF2EMFCommandStack extends CommandStack implements
public void commandStackChanged(EventObject event) { public void commandStackChanged(EventObject event) {
notifyListeners(); 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