From 7d9984b0b564574095d7dd0f9b5e3bf1b65f8918 Mon Sep 17 00:00:00 2001 From: Florian Hoelzl <hoelzl@fortiss.org> Date: Wed, 22 Jun 2011 16:13:44 +0000 Subject: [PATCH] added global actions trial for undo/redo fix --- .../ui/action/CopyModelElementsAction.java | 80 +++++++++++++ .../base/ui/action/CopyPasteUtils.java | 111 ++++++++++++++++++ .../ui/action/CutModelElementsAction.java | 91 ++++++++++++++ .../ui/action/ModelElementsActionBase.java | 11 +- .../ui/action/PasteModelElementAction.java | 72 ++++++++++++ .../editor/gef/GraphicalViewerEditorBase.java | 67 ++++++++++- .../trunk/plugin.xml | 1 + .../tooling/kernel/ui/base/EditorBase.java | 18 ++- .../interfaces/IActionContributingEditor.java | 46 ++++++++ .../kernel/ui/internal/ActionService.java | 16 +-- .../internal/editor/BindingContributor.java | 81 +++++++++++++ .../ui/internal/views/NavigatorViewPart.java | 3 +- .../kernel/ui/services/IActionService.java | 10 +- .../kernel/ui/util/DragAndDropUtils.java | 3 + 14 files changed, 575 insertions(+), 35 deletions(-) create mode 100644 org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/action/CopyModelElementsAction.java create mode 100644 org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/action/CopyPasteUtils.java create mode 100644 org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/action/CutModelElementsAction.java create mode 100644 org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/action/PasteModelElementAction.java create mode 100644 org.fortiss.tooling.kernel.ui/trunk/src/org/fortiss/tooling/kernel/ui/interfaces/IActionContributingEditor.java create mode 100644 org.fortiss.tooling.kernel.ui/trunk/src/org/fortiss/tooling/kernel/ui/internal/editor/BindingContributor.java diff --git a/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/action/CopyModelElementsAction.java b/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/action/CopyModelElementsAction.java new file mode 100644 index 000000000..c3b1a7b78 --- /dev/null +++ b/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/action/CopyModelElementsAction.java @@ -0,0 +1,80 @@ +/*--------------------------------------------------------------------------+ +$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.base.ui.action; + +import java.io.IOException; +import java.util.Collection; + +import org.eclipse.emf.common.command.AbstractCommand; +import org.eclipse.emf.common.command.Command; +import org.eclipse.emf.ecore.EObject; +import org.eclipse.ui.ISharedImages; +import org.eclipse.ui.PlatformUI; +import org.eclipse.ui.actions.ActionFactory; +import org.fortiss.tooling.base.ui.ToolingBaseUIActivator; +import org.fortiss.tooling.kernel.util.LoggingUtils; + +/** + * Action for copying the current selection. + * + * @author hummel + * @author hoelzl + * @author $Author$ + * @version $Rev$ + * @ConQAT.Rating RED Hash: + */ +public class CopyModelElementsAction extends ModelElementsActionBase { + + /** Constructor. */ + public CopyModelElementsAction() { + super("Copy", ActionFactory.COPY.getId(), PlatformUI.getWorkbench() + .getSharedImages() + .getImageDescriptor(ISharedImages.IMG_TOOL_COPY), PlatformUI + .getWorkbench().getSharedImages() + .getImageDescriptor(ISharedImages.IMG_TOOL_COPY_DISABLED), + false); + } + + /** {@inheritDoc} */ + @Override + protected Command createCommand(final Collection<EObject> selection) { + return new AbstractCommand("Copy") { + + @Override + public void execute() { + try { + CopyPasteUtils.copyToClipboard(selection); + } catch (IOException e) { + LoggingUtils.error(ToolingBaseUIActivator.getDefault(), + "Had problems during copy", e); + } + } + + @Override + public void redo() { + // not needed as our transactional command stack + // handles this + } + + @Override + public boolean canExecute() { + return true; + } + }; + } +} diff --git a/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/action/CopyPasteUtils.java b/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/action/CopyPasteUtils.java new file mode 100644 index 000000000..bed9dd92b --- /dev/null +++ b/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/action/CopyPasteUtils.java @@ -0,0 +1,111 @@ +/*--------------------------------------------------------------------------+ +$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.base.ui.action; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + +import org.eclipse.emf.common.util.URI; +import org.eclipse.emf.ecore.EObject; +import org.eclipse.emf.ecore.resource.Resource; +import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl; +import org.eclipse.emf.ecore.util.EcoreUtil; +import org.eclipse.jface.dialogs.MessageDialog; +import org.eclipse.swt.dnd.Clipboard; +import org.eclipse.swt.dnd.DND; +import org.eclipse.swt.dnd.TextTransfer; +import org.eclipse.swt.dnd.Transfer; +import org.eclipse.swt.widgets.Display; +import org.fortiss.tooling.kernel.services.ICompositorService; +import org.fortiss.tooling.kernel.util.EMFResourceUtils; + +/** + * Methods for simplifying copy/paste. + * + * @author hummel + * @author hoelzl + * + * @author $Author$ + * @version $Rev$ + * @ConQAT.Rating RED Hash: + */ +final class CopyPasteUtils { + /** Copies the given selected objects to the clipboard. */ + public static void copyToClipboard(Collection<EObject> selection) + throws IOException { + ByteArrayOutputStream bytes = new ByteArrayOutputStream(); + Resource resource = new ResourceSetImpl().createResource(URI + .createPlatformResourceURI("file://does/not/exist", true)); + for (EObject sel : selection) { + resource.getContents().add(EcoreUtil.copy(sel)); + } + resource.save(bytes, EMFResourceUtils.buildOptionsMap()); + String text = bytes.toString(); + + Clipboard clipboard = new Clipboard(Display.getDefault()); + clipboard.setContents(new Object[] { text }, + new Transfer[] { TextTransfer.getInstance() }, DND.CLIPBOARD + | DND.SELECTION_CLIPBOARD); + clipboard.dispose(); + } + + /** + * Attempts to paste the contents of the clipboard into the given target + * object. + */ + public static void pasteFromClipboard(EObject target) { + Clipboard clipboard = new Clipboard(Display.getDefault()); + String text = (String) clipboard + .getContents(TextTransfer.getInstance()); + clipboard.dispose(); + + if (text == null) { + return; + } + + List<EObject> contents = new ArrayList<EObject>(); + try { + ByteArrayInputStream in = new ByteArrayInputStream(text.getBytes()); + Resource resource = new ResourceSetImpl().createResource(URI + .createPlatformResourceURI("file://does/not/exist", true)); + resource.load(in, EMFResourceUtils.buildOptionsMap()); + contents.addAll(resource.getContents()); + } catch (Exception e) { + MessageDialog.openError(Display.getDefault().getActiveShell(), + "Paste", + "Clipboard contents not readable: " + e.getMessage()); + return; + } + + for (EObject insertObj : contents) { + if (!ICompositorService.INSTANCE + .canCompose(target, insertObj, null)) { + return; + } + } + + for (EObject insertObj : contents) { + ICompositorService.INSTANCE.compose(target, insertObj, null); + // LibrarySupport.activateAllElements(insertObj); + } + } +} diff --git a/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/action/CutModelElementsAction.java b/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/action/CutModelElementsAction.java new file mode 100644 index 000000000..7ff197e0d --- /dev/null +++ b/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/action/CutModelElementsAction.java @@ -0,0 +1,91 @@ +/*--------------------------------------------------------------------------+ +$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.base.ui.action; + +import java.io.IOException; +import java.util.Collection; + +import org.eclipse.emf.common.command.AbstractCommand; +import org.eclipse.emf.common.command.Command; +import org.eclipse.emf.ecore.EObject; +import org.eclipse.ui.ISharedImages; +import org.eclipse.ui.PlatformUI; +import org.eclipse.ui.actions.ActionFactory; +import org.fortiss.tooling.base.ui.ToolingBaseUIActivator; +import org.fortiss.tooling.kernel.model.IRemovable; +import org.fortiss.tooling.kernel.util.LoggingUtils; + +/** + * Action for cutting the current selection. + * + * @author hummel + * @author hoelzl + * @author $Author$ + * @version $Rev$ + * @ConQAT.Rating RED Hash: + */ +public class CutModelElementsAction extends ModelElementsActionBase { + + /** Constructor. */ + public CutModelElementsAction() { + super("Cut", ActionFactory.CUT.getId(), PlatformUI.getWorkbench() + .getSharedImages() + .getImageDescriptor(ISharedImages.IMG_TOOL_CUT), PlatformUI + .getWorkbench().getSharedImages() + .getImageDescriptor(ISharedImages.IMG_TOOL_CUT_DISABLED), true); + } + + /** {@inheritDoc} */ + @Override + protected Command createCommand(final Collection<EObject> selection) { + return new AbstractCommand("Cut") { + + @Override + public void execute() { + try { + CopyPasteUtils.copyToClipboard(selection); + for (EObject o : selection) { + ((IRemovable) o).remove(); + } + } catch (IOException e) { + LoggingUtils.error(ToolingBaseUIActivator.getDefault(), + "Had problems during copy", e); + } + } + + @Override + public void redo() { + // not needed as our transactional command stack + // handles this + } + + @Override + public boolean canExecute() { + for (EObject o : selection) { + if (!(o instanceof IRemovable)) { + return false; + } + if (!((IRemovable) o).canRemove()) { + return false; + } + } + return true; + } + }; + } +} diff --git a/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/action/ModelElementsActionBase.java b/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/action/ModelElementsActionBase.java index 8cb16cc5e..f974d26c2 100644 --- a/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/action/ModelElementsActionBase.java +++ b/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/action/ModelElementsActionBase.java @@ -27,6 +27,7 @@ import org.eclipse.jface.action.Action; import org.eclipse.jface.resource.ImageDescriptor; import org.fortiss.tooling.kernel.interfaces.ITopLevelElementContext; import org.fortiss.tooling.kernel.services.IPersistencyService; +import org.fortiss.tooling.kernel.ui.util.EObjectSelectionUtils; /** * GEF editor base implementation. @@ -54,8 +55,13 @@ public abstract class ModelElementsActionBase extends Action { this.modifiesModel = modifiesModel; } + /** Returns the Command to be started in the run method. */ + protected abstract Command createCommand(Collection<EObject> selection); + /** Returns the selection to be used for this action. */ - protected abstract Collection<EObject> getSelection(); + protected Collection<EObject> getSelection() { + return EObjectSelectionUtils.getCurrentSelectionEObjects(); + } /** * This method checks if the current selection is non-empty and all elements @@ -103,9 +109,6 @@ public abstract class ModelElementsActionBase extends Action { } } - /** Returns the Command to be started in the run method. */ - protected abstract Command createCommand(Collection<EObject> selection); - /** Updates the enablement of this action. */ public void update() { if (!isConsistentSelection()) { diff --git a/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/action/PasteModelElementAction.java b/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/action/PasteModelElementAction.java new file mode 100644 index 000000000..7a1a01bcb --- /dev/null +++ b/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/action/PasteModelElementAction.java @@ -0,0 +1,72 @@ +/*--------------------------------------------------------------------------+ +$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.base.ui.action; + +import java.util.Collection; + +import org.eclipse.emf.common.command.AbstractCommand; +import org.eclipse.emf.common.command.Command; +import org.eclipse.emf.ecore.EObject; +import org.eclipse.ui.ISharedImages; +import org.eclipse.ui.PlatformUI; +import org.eclipse.ui.actions.ActionFactory; + +/** + * Action for pasting the current selection. + * + * @author hummel + * @author hoelzlf + * @author $Author$ + * @version $Rev$ + * @ConQAT.Rating RED Hash: + */ +public class PasteModelElementAction extends ModelElementsActionBase { + + /** Constructor. */ + public PasteModelElementAction() { + super("Paste", ActionFactory.PASTE.getId(), PlatformUI.getWorkbench() + .getSharedImages() + .getImageDescriptor(ISharedImages.IMG_TOOL_PASTE), PlatformUI + .getWorkbench().getSharedImages() + .getImageDescriptor(ISharedImages.IMG_TOOL_PASTE_DISABLED), + true); + } + + /** {@inheritDoc} */ + @Override + protected Command createCommand(final Collection<EObject> selection) { + return new AbstractCommand("Copy") { + + @Override + public void execute() { + CopyPasteUtils.pasteFromClipboard(selection.iterator().next()); + } + + @Override + public void redo() { + // not needed as our transactional command stack + // handles this + } + + @Override + public boolean canExecute() { + return selection.size() == 1; + } + }; + } +} diff --git a/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/editor/gef/GraphicalViewerEditorBase.java b/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/editor/gef/GraphicalViewerEditorBase.java index 8ab4ff662..6a0dbd045 100644 --- a/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/editor/gef/GraphicalViewerEditorBase.java +++ b/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/editor/gef/GraphicalViewerEditorBase.java @@ -35,11 +35,15 @@ import org.eclipse.gef.editparts.ScalableFreeformRootEditPart; import org.eclipse.gef.editparts.ScalableRootEditPart; import org.eclipse.gef.editparts.ZoomManager; import org.eclipse.gef.ui.actions.ActionRegistry; +import org.eclipse.gef.ui.actions.DeleteAction; +import org.eclipse.gef.ui.actions.PrintAction; +import org.eclipse.gef.ui.actions.SelectAllAction; import org.eclipse.gef.ui.actions.UpdateAction; import org.eclipse.gef.ui.actions.ZoomComboContributionItem; import org.eclipse.gef.ui.actions.ZoomInAction; import org.eclipse.gef.ui.actions.ZoomOutAction; import org.eclipse.gef.ui.parts.ScrollingGraphicalViewer; +import org.eclipse.jface.action.Action; import org.eclipse.jface.action.IAction; import org.eclipse.jface.util.IPropertyChangeListener; import org.eclipse.jface.util.PropertyChangeEvent; @@ -55,7 +59,10 @@ import org.eclipse.ui.ISelectionListener; import org.eclipse.ui.IWorkbenchPart; import org.eclipse.ui.PartInitException; import org.fortiss.tooling.base.ui.ToolingBaseUIActivator; +import org.fortiss.tooling.base.ui.action.CopyModelElementsAction; +import org.fortiss.tooling.base.ui.action.CutModelElementsAction; import org.fortiss.tooling.base.ui.action.ModelElementsActionBase; +import org.fortiss.tooling.base.ui.action.PasteModelElementAction; import org.fortiss.tooling.base.ui.dnd.DropTargetListener; import org.fortiss.tooling.base.ui.editpart.figure.EVisualStyle; import org.fortiss.tooling.kernel.ui.services.IEditPartFactoryService; @@ -252,18 +259,59 @@ public class GraphicalViewerEditorBase<T extends EObject> extends EditorBase<T> bars.getToolBarManager().remove(zoomContribution); } + /** + * Registers the select all action. Sub-classes may override to prevent the + * action registering. + */ + protected void registerSelectAllAction() { + registerAction(new SelectAllAction(this)); + } + + /** + * Registers the print action. Sub-classes may override to prevent the + * action registering. + */ + protected void registerPrintAction() { + registerAction(new PrintAction(this)); + } + + /** + * Register any other action defined by sub-classes. Sub-classes should + * override to implement. + */ + protected void registerOtherActions() { + // do nothing by default + } + + /** + * Registers the copy, cut, and past action. Sub-classes may override to + * prevent the action registering. + */ + protected void registerEditActions() { + registerSelectionAction(new DeleteAction((IWorkbenchPart) this)); + registerSelectionAction(new CopyModelElementsAction()); + registerSelectionAction(new CutModelElementsAction()); + registerSelectionAction(new PasteModelElementAction()); + } + + /** Registers and returns the id of the given selection action */ + protected final String registerSelectionAction(Action action) { + selectionActions.add(registerAction(action)); + return action.getId(); + } + /** Creates actions for this editor. */ private void createActions() { - // registerSelectAllAction(); - // registerPrintAction(); - // registerEditActions(); - // registerOtherActions(); + registerSelectAllAction(); + registerPrintAction(); + registerEditActions(); + registerOtherActions(); } /** Updates a list of actions. */ private void updateActions(List<String> actionIds) { - final ActionRegistry registry = actionRegistry; - for (final String id : actionIds) { + ActionRegistry registry = actionRegistry; + for (String id : actionIds) { final IAction action = registry.getAction(id); if (action instanceof UpdateAction) { ((UpdateAction) action).update(); @@ -279,6 +327,13 @@ public class GraphicalViewerEditorBase<T extends EObject> extends EditorBase<T> viewer.getControl().setFocus(); } + /** {@inheritDoc} */ + @Override + protected void firePropertyChange(int property) { + super.firePropertyChange(property); + updateActions(propertyActions); + } + // The following methods are necessary in order to avoid Eclipse Helios SR2 // bug #339360 diff --git a/org.fortiss.tooling.kernel.ui/trunk/plugin.xml b/org.fortiss.tooling.kernel.ui/trunk/plugin.xml index c6ce02925..23c30782b 100644 --- a/org.fortiss.tooling.kernel.ui/trunk/plugin.xml +++ b/org.fortiss.tooling.kernel.ui/trunk/plugin.xml @@ -8,6 +8,7 @@ point="org.eclipse.ui.editors"> <editor class="org.fortiss.tooling.kernel.ui.internal.editor.BindingEditor" + contributorClass="org.fortiss.tooling.kernel.ui.internal.editor.BindingContributor" default="false" id="org.fortiss.tooling.kernel.ui.internal.editor.BindingEditor" name="Binding Editor"> diff --git a/org.fortiss.tooling.kernel.ui/trunk/src/org/fortiss/tooling/kernel/ui/base/EditorBase.java b/org.fortiss.tooling.kernel.ui/trunk/src/org/fortiss/tooling/kernel/ui/base/EditorBase.java index c5484805a..c92d15a66 100644 --- a/org.fortiss.tooling.kernel.ui/trunk/src/org/fortiss/tooling/kernel/ui/base/EditorBase.java +++ b/org.fortiss.tooling.kernel.ui/trunk/src/org/fortiss/tooling/kernel/ui/base/EditorBase.java @@ -20,11 +20,13 @@ package org.fortiss.tooling.kernel.ui.base; import org.eclipse.core.databinding.DataBindingContext; import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.emf.ecore.EObject; +import org.eclipse.ui.IActionBars; import org.eclipse.ui.IEditorInput; import org.eclipse.ui.IEditorSite; import org.eclipse.ui.PartInitException; import org.eclipse.ui.part.EditorPart; import org.fortiss.tooling.kernel.services.ICommandStackService; +import org.fortiss.tooling.kernel.ui.interfaces.IActionContributingEditor; import org.fortiss.tooling.kernel.ui.interfaces.IEditorBinding; import org.fortiss.tooling.kernel.ui.interfaces.IModelElementHandler; import org.fortiss.tooling.kernel.ui.internal.editor.ModelElementEditorInput; @@ -38,8 +40,8 @@ import org.fortiss.tooling.kernel.ui.internal.editor.ModelElementEditorInput; * @version $Rev$ * @ConQAT.Rating YELLOW Hash: FAA96049E4259A80E6F68B3CB804C2DB */ -// TODO (FH): move to UI -public abstract class EditorBase<T extends EObject> extends EditorPart { +public abstract class EditorBase<T extends EObject> extends EditorPart + implements IActionContributingEditor { /** * The object shown in this editor. This is valid as soon as @@ -125,4 +127,16 @@ public abstract class EditorBase<T extends EObject> extends EditorPart { public final void doSaveAs() { // Saving is handled automatically by emfStore } + + /** {@inheritDoc} */ + @Override + public void registerGlobalActions(IActionBars bars) { + // nothing to do + } + + /** {@inheritDoc} */ + @Override + public void unregisterGlobalActions(IActionBars bars) { + // nothing to do + } } diff --git a/org.fortiss.tooling.kernel.ui/trunk/src/org/fortiss/tooling/kernel/ui/interfaces/IActionContributingEditor.java b/org.fortiss.tooling.kernel.ui/trunk/src/org/fortiss/tooling/kernel/ui/interfaces/IActionContributingEditor.java new file mode 100644 index 000000000..7bca16aae --- /dev/null +++ b/org.fortiss.tooling.kernel.ui/trunk/src/org/fortiss/tooling/kernel/ui/interfaces/IActionContributingEditor.java @@ -0,0 +1,46 @@ +/*--------------------------------------------------------------------------+ +$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.ui.interfaces; + +import org.eclipse.ui.IActionBars; +import org.fortiss.tooling.kernel.ui.internal.editor.BindingContributor; + +/** + * Interface for editors which contribute own actions to the + * {@link BindingContributor}. + * + * @author hoelzl + * @author $Author$ + * @version $Rev$ + * @ConQAT.Rating RED Hash: + */ +public interface IActionContributingEditor { + /** + * This method is a hook to register additional actions in an editor. This + * base implementation is empty. The implementation of this should match + * with the implementation of {@link #unregisterGlobalActions(IActionBars)}. + */ + void registerGlobalActions(IActionBars bars); + + /** + * This method is a hook to unregister actions registered in + * {@link #registerGlobalActions(IActionBars)}. This base implementation is + * empty. + */ + void unregisterGlobalActions(IActionBars bars); +} diff --git a/org.fortiss.tooling.kernel.ui/trunk/src/org/fortiss/tooling/kernel/ui/internal/ActionService.java b/org.fortiss.tooling.kernel.ui/trunk/src/org/fortiss/tooling/kernel/ui/internal/ActionService.java index 858121afe..f929e7ba3 100644 --- a/org.fortiss.tooling.kernel.ui/trunk/src/org/fortiss/tooling/kernel/ui/internal/ActionService.java +++ b/org.fortiss.tooling.kernel.ui/trunk/src/org/fortiss/tooling/kernel/ui/internal/ActionService.java @@ -21,9 +21,7 @@ import org.eclipse.emf.ecore.EObject; import org.eclipse.emf.ecore.util.EcoreUtil; import org.eclipse.jface.action.IMenuManager; import org.eclipse.ui.IActionBars; -import org.eclipse.ui.IEditorSite; import org.eclipse.ui.ISharedImages; -import org.eclipse.ui.IViewSite; import org.eclipse.ui.PlatformUI; import org.eclipse.ui.actions.ActionFactory; import org.fortiss.tooling.kernel.model.IRemovable; @@ -41,7 +39,6 @@ import org.fortiss.tooling.kernel.ui.util.EObjectSelectionUtils; * @version $Rev$ * @ConQAT.Rating YELLOW Hash: 5DDB14A5557BDA2375928D87F4F4B2BB */ -// TODO (FH): move to UI public class ActionService implements IActionService { /** The group id of the global default action group. */ @@ -107,18 +104,7 @@ public class ActionService implements IActionService { /** {@inheritDoc} */ @Override - public void registerGlobalActions(IViewSite site) { - fillActionBars(site.getActionBars()); - } - - /** {@inheritDoc} */ - @Override - public void registerGlobalActions(IEditorSite site) { - fillActionBars(site.getActionBars()); - } - - /** Fills the given action bar with the global actions. */ - private void fillActionBars(IActionBars actionBars) { + public void registerGlobalActions(IActionBars actionBars) { globalUndoAction.setId(ActionFactory.UNDO.getId()); globalUndoAction.setActionDefinitionId(ActionFactory.UNDO .getCommandId()); diff --git a/org.fortiss.tooling.kernel.ui/trunk/src/org/fortiss/tooling/kernel/ui/internal/editor/BindingContributor.java b/org.fortiss.tooling.kernel.ui/trunk/src/org/fortiss/tooling/kernel/ui/internal/editor/BindingContributor.java new file mode 100644 index 000000000..935be9db1 --- /dev/null +++ b/org.fortiss.tooling.kernel.ui/trunk/src/org/fortiss/tooling/kernel/ui/internal/editor/BindingContributor.java @@ -0,0 +1,81 @@ +/*--------------------------------------------------------------------------+ +$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.ui.internal.editor; + +import org.conqat.lib.commons.assertion.CCSMPre; +import org.eclipse.ui.IActionBars; +import org.eclipse.ui.IEditorPart; +import org.eclipse.ui.actions.ActionFactory; +import org.eclipse.ui.part.MultiPageEditorActionBarContributor; +import org.fortiss.tooling.kernel.ui.interfaces.IActionContributingEditor; +import org.fortiss.tooling.kernel.ui.internal.ActionService; +import org.fortiss.tooling.kernel.ui.services.IActionService; + +/** + * The action bar contributor for the {@link BindingEditor}. + * + * @author hummel + * @author hoelzl + * @author $Author$ + * @version $Rev$ + * @ConQAT.Rating RED Hash: + */ +public final class BindingContributor extends + MultiPageEditorActionBarContributor { + + /** The last editor seen. */ + private IActionContributingEditor lastEditor; + + /** + * {@inheritDoc} + * <p> + * We set those actions shared between all pages of the editor here. + */ + @Override + public void setActiveEditor(IEditorPart part) { + CCSMPre.isTrue(part instanceof BindingEditor, + "This contributor may only be used for CCTSBindingEditors!"); + IActionBars bars = getActionBars(); + bars.setGlobalActionHandler(ActionFactory.UNDO.getId(), + ((ActionService) IActionService.INSTANCE).globalUndoAction); + bars.setGlobalActionHandler(ActionFactory.REDO.getId(), + ((ActionService) IActionService.INSTANCE).globalRedoAction); + bars.updateActionBars(); + super.setActiveEditor(part); + } + + /** {@inheritDoc} */ + @Override + public void setActivePage(IEditorPart activeEditor) { + IActionBars bars = getActionBars(); + + // unregister old actions + if (lastEditor != null) { + lastEditor.unregisterGlobalActions(bars); + lastEditor = null; + } + + if (activeEditor instanceof IActionContributingEditor) { + lastEditor = (IActionContributingEditor) activeEditor; + lastEditor.registerGlobalActions(bars); + } + + bars.updateActionBars(); + bars.getToolBarManager().update(true); + } +} diff --git a/org.fortiss.tooling.kernel.ui/trunk/src/org/fortiss/tooling/kernel/ui/internal/views/NavigatorViewPart.java b/org.fortiss.tooling.kernel.ui/trunk/src/org/fortiss/tooling/kernel/ui/internal/views/NavigatorViewPart.java index e421d78cf..1b69d1f09 100644 --- a/org.fortiss.tooling.kernel.ui/trunk/src/org/fortiss/tooling/kernel/ui/internal/views/NavigatorViewPart.java +++ b/org.fortiss.tooling.kernel.ui/trunk/src/org/fortiss/tooling/kernel/ui/internal/views/NavigatorViewPart.java @@ -141,7 +141,8 @@ public final class NavigatorViewPart extends ViewPart implements viewer.addDoubleClickListener(this); - IActionService.INSTANCE.registerGlobalActions(getViewSite()); + IActionService.INSTANCE.registerGlobalActions(getViewSite() + .getActionBars()); createLinkWithEditorAction(); PlatformUI.getWorkbench().getActiveWorkbenchWindow() diff --git a/org.fortiss.tooling.kernel.ui/trunk/src/org/fortiss/tooling/kernel/ui/services/IActionService.java b/org.fortiss.tooling.kernel.ui/trunk/src/org/fortiss/tooling/kernel/ui/services/IActionService.java index 349e6f662..1593a250e 100644 --- a/org.fortiss.tooling.kernel.ui/trunk/src/org/fortiss/tooling/kernel/ui/services/IActionService.java +++ b/org.fortiss.tooling.kernel.ui/trunk/src/org/fortiss/tooling/kernel/ui/services/IActionService.java @@ -18,8 +18,7 @@ $Id$ package org.fortiss.tooling.kernel.ui.services; import org.eclipse.jface.action.IMenuManager; -import org.eclipse.ui.IEditorSite; -import org.eclipse.ui.IViewSite; +import org.eclipse.ui.IActionBars; import org.fortiss.tooling.kernel.ui.internal.ActionService; /** @@ -36,11 +35,8 @@ public interface IActionService { /** Returns the singleton instance of the service. */ public static final IActionService INSTANCE = new ActionService(); - /** Registers global actions with a view site. */ - void registerGlobalActions(IViewSite site); - - /** Registers global actions with an editor site. */ - void registerGlobalActions(IEditorSite site); + /** Registers global actions with the given {@link IActionBars}. */ + void registerGlobalActions(IActionBars actionBars); /** Adds the global default actions like undo to the given context menu. */ void addGlobalDefaultActionSectionToMenu(IMenuManager menuManager); diff --git a/org.fortiss.tooling.kernel.ui/trunk/src/org/fortiss/tooling/kernel/ui/util/DragAndDropUtils.java b/org.fortiss.tooling.kernel.ui/trunk/src/org/fortiss/tooling/kernel/ui/util/DragAndDropUtils.java index bb752da4f..465ff228e 100644 --- a/org.fortiss.tooling.kernel.ui/trunk/src/org/fortiss/tooling/kernel/ui/util/DragAndDropUtils.java +++ b/org.fortiss.tooling.kernel.ui/trunk/src/org/fortiss/tooling/kernel/ui/util/DragAndDropUtils.java @@ -33,13 +33,16 @@ public final class DragAndDropUtils { /** Extracts the EObject contained in the given drop object. */ public static EObject extractDroppedEObject(Object data) { + // if the dropped object is a selection, extract content if (data instanceof IStructuredSelection && !((IStructuredSelection) data).isEmpty()) { data = ((IStructuredSelection) data).getFirstElement(); } + if (data instanceof Prototype) { return ((Prototype) data).getPrototypeCopy(); } + if (data instanceof EObject) { return (EObject) data; } -- GitLab