From eeb1bc03e5772a1aef2be2280d5c64bb8f06382b Mon Sep 17 00:00:00 2001 From: Florian Hoelzl <hoelzl@fortiss.org> Date: Mon, 5 Sep 2011 09:07:19 +0000 Subject: [PATCH] refactored action service by adding implementation classes. refs 133 --- .../kernel/ui/internal/ActionService.java | 113 +++--------------- .../ui/internal/actions/DeleteAction.java | 97 +++++++++++++++ .../ui/internal/actions/RedoAction.java | 63 ++++++++++ .../ui/internal/actions/UndoAction.java | 63 ++++++++++ 4 files changed, 237 insertions(+), 99 deletions(-) create mode 100644 org.fortiss.tooling.kernel.ui/trunk/src/org/fortiss/tooling/kernel/ui/internal/actions/DeleteAction.java create mode 100644 org.fortiss.tooling.kernel.ui/trunk/src/org/fortiss/tooling/kernel/ui/internal/actions/RedoAction.java create mode 100644 org.fortiss.tooling.kernel.ui/trunk/src/org/fortiss/tooling/kernel/ui/internal/actions/UndoAction.java 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 db30d082a..ddc263c04 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 @@ -22,18 +22,14 @@ import java.util.EventObject; import org.eclipse.emf.common.command.CommandStackListener; import org.eclipse.emf.ecore.EObject; import org.eclipse.jface.action.IMenuManager; -import org.eclipse.jface.resource.ImageDescriptor; import org.eclipse.ui.IActionBars; -import org.eclipse.ui.ISharedImages; -import org.eclipse.ui.PlatformUI; import org.eclipse.ui.actions.ActionFactory; import org.fortiss.tooling.kernel.extension.data.ITopLevelElement; -import org.fortiss.tooling.kernel.service.ICommandStackService; -import org.fortiss.tooling.kernel.service.IConnectionCompositorService; -import org.fortiss.tooling.kernel.service.IElementCompositorService; import org.fortiss.tooling.kernel.service.IPersistencyService; import org.fortiss.tooling.kernel.service.listener.IPersistencyServiceListener; -import org.fortiss.tooling.kernel.ui.extension.base.EObjectActionBase; +import org.fortiss.tooling.kernel.ui.internal.actions.DeleteAction; +import org.fortiss.tooling.kernel.ui.internal.actions.RedoAction; +import org.fortiss.tooling.kernel.ui.internal.actions.UndoAction; import org.fortiss.tooling.kernel.ui.service.IActionService; import org.fortiss.tooling.kernel.ui.util.EObjectSelectionUtils; @@ -51,75 +47,20 @@ public class ActionService implements IActionService, /** The menu section ID of the global default action group. */ public static final String GLOBAL_DEFAULT_MENU_SECTION_ID = "globalDefaults"; - /** The global undo action. */ - public final EObjectActionBase globalUndoAction = new EObjectActionBase( - "Undo", PlatformUI.getWorkbench().getSharedImages() - .getImageDescriptor(ISharedImages.IMG_TOOL_UNDO)) { - - @Override - public void run() { - if (getTarget() != null) { - ICommandStackService.INSTANCE.undo(getTarget()); - } - } - }; - - /** The global redo action. */ - public final EObjectActionBase globalRedoAction = new EObjectActionBase( - "Redo", PlatformUI.getWorkbench().getSharedImages() - .getImageDescriptor(ISharedImages.IMG_TOOL_UNDO)) { - - @Override - public void run() { - if (getTarget() != null) { - ICommandStackService.INSTANCE.redo(getTarget()); - } - } - }; - /** The global delete action. */ - public final DeleteAction globalDeleteAction = new DeleteAction("Delete", - PlatformUI.getWorkbench().getSharedImages() - .getImageDescriptor(ISharedImages.IMG_TOOL_DELETE)); - - private static class DeleteAction extends EObjectActionBase { - - private boolean useElementCompositor; - - /** - * @param text - * @param image - */ - public DeleteAction(String text, ImageDescriptor image) { - super(text, image); - } + public final DeleteAction globalDeleteAction; - public void setUseElementCompositor(boolean useElementCompositor) { - this.useElementCompositor = useElementCompositor; - } + /** The global undo action. */ + public final UndoAction globalUndoAction; - @Override - public void run() { - final EObject selectedObject = getTarget(); - if (selectedObject == null) { - return; - } - ICommandStackService.INSTANCE.runAsCommand(selectedObject, - new Runnable() { - @Override - public void run() { - IElementCompositorService.INSTANCE - .decompose(selectedObject); - } - }); - } - } + /** The global redo action. */ + public final RedoAction globalRedoAction; /** Constructor. */ public ActionService() { - globalUndoAction.setId(ActionFactory.UNDO.getId()); - globalRedoAction.setId(ActionFactory.REDO.getId()); - globalDeleteAction.setId(ActionFactory.DELETE.getId()); + globalDeleteAction = new DeleteAction(); + globalUndoAction = new UndoAction(); + globalRedoAction = new RedoAction(); for (ITopLevelElement context : IPersistencyService.INSTANCE .getTopLevelElements()) { @@ -160,35 +101,9 @@ public class ActionService implements IActionService, public void refresh() { EObject target = EObjectSelectionUtils .getCurrentSelectionFirstElement(); - - if (target == null) { - globalUndoAction.setEnabled(false); - globalRedoAction.setEnabled(false); - globalDeleteAction.setEnabled(false); - return; - } - - globalUndoAction.setTarget(target); - globalUndoAction.setEnabled(ICommandStackService.INSTANCE - .canUndo(target)); - - globalRedoAction.setTarget(target); - globalRedoAction.setEnabled(ICommandStackService.INSTANCE - .canRedo(target)); - - globalDeleteAction.setTarget(target); - boolean enabled = false; - if (!IPersistencyService.INSTANCE.isTopLevelElement(target)) { - if (IElementCompositorService.INSTANCE.canDecompose(target)) { - enabled = true; - globalDeleteAction.setUseElementCompositor(true); - } else if (IConnectionCompositorService.INSTANCE - .canDisconnect(target)) { - enabled = true; - globalDeleteAction.setUseElementCompositor(false); - } - } - globalDeleteAction.setEnabled(enabled); + globalDeleteAction.refresh(target); + globalUndoAction.refresh(target); + globalRedoAction.refresh(target); } /** {@inheritDoc} */ diff --git a/org.fortiss.tooling.kernel.ui/trunk/src/org/fortiss/tooling/kernel/ui/internal/actions/DeleteAction.java b/org.fortiss.tooling.kernel.ui/trunk/src/org/fortiss/tooling/kernel/ui/internal/actions/DeleteAction.java new file mode 100644 index 000000000..900ffeb92 --- /dev/null +++ b/org.fortiss.tooling.kernel.ui/trunk/src/org/fortiss/tooling/kernel/ui/internal/actions/DeleteAction.java @@ -0,0 +1,97 @@ +/*--------------------------------------------------------------------------+ +$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.actions; + +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.kernel.service.ICommandStackService; +import org.fortiss.tooling.kernel.service.IConnectionCompositorService; +import org.fortiss.tooling.kernel.service.IElementCompositorService; +import org.fortiss.tooling.kernel.service.IPersistencyService; +import org.fortiss.tooling.kernel.ui.extension.base.EObjectActionBase; + +/** + * The {@link DeleteAction} uses the {@link IElementCompositorService} and the + * {@link IConnectionCompositorService} to determine the enabled state of this + * action. + * + * @author hoelzl + * @author $Author$ + * @version $Rev$ + * @ConQAT.Rating RED Hash: + */ +public class DeleteAction extends EObjectActionBase { + + /** Flag for using element or connection compositor. */ + private boolean useElementCompositor; + + /** Constructor. */ + public DeleteAction() { + super("Delete", PlatformUI.getWorkbench().getSharedImages() + .getImageDescriptor(ISharedImages.IMG_TOOL_DELETE)); + setId(ActionFactory.DELETE.getId()); + } + + /** Refreshes the action's target and enabled state. */ + public void refresh(EObject element) { + setTarget(element); + if (element == null) { + setEnabled(false); + return; + } + + boolean enabled = false; + if (!IPersistencyService.INSTANCE.isTopLevelElement(element)) { + if (IElementCompositorService.INSTANCE.canDecompose(element)) { + enabled = true; + useElementCompositor = true; + return; + } else if (IConnectionCompositorService.INSTANCE + .canDisconnect(element)) { + enabled = true; + useElementCompositor = false; + return; + } + } + setEnabled(enabled); + } + + /** {@inheritDoc} */ + @Override + public void run() { + final EObject selectedObject = getTarget(); + if (selectedObject == null) { + return; + } + ICommandStackService.INSTANCE.runAsCommand(selectedObject, + new Runnable() { + @Override + public void run() { + if (useElementCompositor) { + IElementCompositorService.INSTANCE + .decompose(selectedObject); + } else { + IConnectionCompositorService.INSTANCE + .disconnect(selectedObject); + } + } + }); + } +} \ No newline at end of file diff --git a/org.fortiss.tooling.kernel.ui/trunk/src/org/fortiss/tooling/kernel/ui/internal/actions/RedoAction.java b/org.fortiss.tooling.kernel.ui/trunk/src/org/fortiss/tooling/kernel/ui/internal/actions/RedoAction.java new file mode 100644 index 000000000..9e9cfbb89 --- /dev/null +++ b/org.fortiss.tooling.kernel.ui/trunk/src/org/fortiss/tooling/kernel/ui/internal/actions/RedoAction.java @@ -0,0 +1,63 @@ +/*--------------------------------------------------------------------------+ +$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.actions; + +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.kernel.service.ICommandStackService; +import org.fortiss.tooling.kernel.ui.extension.base.EObjectActionBase; + +/** + * The {@link RedoAction} uses the {@link ICommandStackService} to determine the + * enabled state of this action. + * + * @author hoelzl + * @author $Author$ + * @version $Rev$ + * @ConQAT.Rating RED Hash: + */ +public class RedoAction extends EObjectActionBase { + + /** Constructor. */ + public RedoAction() { + super("Redo", PlatformUI.getWorkbench().getSharedImages() + .getImageDescriptor(ISharedImages.IMG_TOOL_REDO)); + setId(ActionFactory.REDO.getId()); + } + + /** Refreshes the action's target and enabled state. */ + public void refresh(EObject element) { + setTarget(element); + if (element == null) { + setEnabled(false); + return; + } + + setEnabled(ICommandStackService.INSTANCE.canRedo(element)); + } + + /** {@inheritDoc} */ + @Override + public void run() { + if (getTarget() != null) { + ICommandStackService.INSTANCE.redo(getTarget()); + } + } +} diff --git a/org.fortiss.tooling.kernel.ui/trunk/src/org/fortiss/tooling/kernel/ui/internal/actions/UndoAction.java b/org.fortiss.tooling.kernel.ui/trunk/src/org/fortiss/tooling/kernel/ui/internal/actions/UndoAction.java new file mode 100644 index 000000000..e64e63bc9 --- /dev/null +++ b/org.fortiss.tooling.kernel.ui/trunk/src/org/fortiss/tooling/kernel/ui/internal/actions/UndoAction.java @@ -0,0 +1,63 @@ +/*--------------------------------------------------------------------------+ +$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.actions; + +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.kernel.service.ICommandStackService; +import org.fortiss.tooling.kernel.ui.extension.base.EObjectActionBase; + +/** + * The {@link UndoAction} uses the {@link ICommandStackService} to determine the + * enabled state of this action. + * + * @author hoelzl + * @author $Author$ + * @version $Rev$ + * @ConQAT.Rating RED Hash: + */ +public class UndoAction extends EObjectActionBase { + + /** Constructor. */ + public UndoAction() { + super("Undo", PlatformUI.getWorkbench().getSharedImages() + .getImageDescriptor(ISharedImages.IMG_TOOL_UNDO)); + setId(ActionFactory.UNDO.getId()); + } + + /** Refreshes the action's target and enabled state. */ + public void refresh(EObject element) { + setTarget(element); + if (element == null) { + setEnabled(false); + return; + } + + setEnabled(ICommandStackService.INSTANCE.canUndo(element)); + } + + /** {@inheritDoc} */ + @Override + public void run() { + if (getTarget() != null) { + ICommandStackService.INSTANCE.undo(getTarget()); + } + } +} -- GitLab