Skip to content

Commit

Permalink
[cleanup] Factorize methods in CreationUtil
Browse files Browse the repository at this point in the history
  • Loading branch information
peransin-obeo authored and lredor committed Sep 12, 2024
1 parent 82be79e commit 13e0458
Showing 1 changed file with 29 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
*******************************************************************************/
package org.eclipse.sirius.diagram.ui.graphical.edit.policies;

import java.util.function.Function;

import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.emf.ecore.EObject;
Expand Down Expand Up @@ -51,6 +53,7 @@
import org.eclipse.sirius.diagram.ui.tools.internal.commands.emf.SelectionWizardCommand;
import org.eclipse.sirius.tools.api.SiriusPlugin;
import org.eclipse.sirius.viewpoint.DSemanticDecorator;
import org.eclipse.sirius.viewpoint.description.tool.AbstractToolDescription;
import org.eclipse.sirius.viewpoint.description.tool.PaneBasedSelectionWizardDescription;
import org.eclipse.sirius.viewpoint.description.tool.SelectionWizardDescription;
import org.eclipse.sirius.viewpoint.description.tool.ToolDescription;
Expand Down Expand Up @@ -92,11 +95,7 @@ public class CreationUtil {
* @since 0.9.0
*/
public CreationUtil(final CreateRequest request, final IDiagramCommandFactory commandFactory, final Point realLocation, final EditPart editPart) {
this.realLocation = realLocation;
this.realSize = null;
this.request = request;
this.emfCommandFactory = commandFactory;
this.editPart = editPart;
this(request, commandFactory, realLocation, null, editPart);
}

/**
Expand All @@ -122,6 +121,14 @@ public CreationUtil(final CreateRequest request, final IDiagramCommandFactory co
this.editPart = editPart;
}

private CompoundCommand wrapCommandWithLayout(AbstractToolDescription tool, final org.eclipse.emf.common.command.Command emfCommand) {
final CompoundCommand compoundCommand = new CompoundCommand(tool.getName());

compoundCommand.add(createLayoutDataCommand());
compoundCommand.add(new ICommandProxy(new GMFCommandWrapper(getEditingDomain(), emfCommand)));
return compoundCommand;
}

/**
* Returns a command that is able to create a node in the specified container with the specified tool.
*
Expand All @@ -133,12 +140,7 @@ public CreationUtil(final CreateRequest request, final IDiagramCommandFactory co
*/
public Command getNodeCreationCommand(final DDiagramElementContainer container, final NodeCreationDescription tool) {
if (new NodeCreationDescriptionQuery(tool).canCreateIn(container)) {
final org.eclipse.emf.common.command.Command command = emfCommandFactory.buildCreateNodeCommandFromTool(container, tool);
final CompoundCommand compoundCommand = new CompoundCommand(tool.getName());

compoundCommand.add(createLayoutDataCommand());
compoundCommand.add(new ICommandProxy(new GMFCommandWrapper(getEditingDomain(), command)));
return compoundCommand;
return wrapCommandWithLayout(tool, emfCommandFactory.buildCreateNodeCommandFromTool(container, tool));
}
return null;
}
Expand All @@ -154,11 +156,7 @@ public Command getNodeCreationCommand(final DDiagramElementContainer container,
*/
public Command getNodeCreationCommand(final DNode node, final NodeCreationDescription tool) {
if (new NodeCreationDescriptionQuery(tool).canCreateIn(node)) {
final CompoundCommand compoundCommand = new CompoundCommand(tool.getName());
compoundCommand.add(createLayoutDataCommand());
final org.eclipse.emf.common.command.Command command = emfCommandFactory.buildCreateNodeCommandFromTool(node, tool);
compoundCommand.add(new ICommandProxy(new GMFCommandWrapper(getEditingDomain(), command)));
return compoundCommand;
return wrapCommandWithLayout(tool, emfCommandFactory.buildCreateNodeCommandFromTool(node, tool));
}
return null;
}
Expand All @@ -173,13 +171,8 @@ public Command getNodeCreationCommand(final DNode node, final NodeCreationDescri
* @return a command that is able to create a node in the specified viewpoint with the specified tool.
*/
public Command getNodeCreationCommand(final DDiagram diagram, final NodeCreationDescription tool) {

if (new NodeCreationDescriptionQuery(tool).canCreateIn(diagram)) {
final org.eclipse.emf.common.command.Command command = emfCommandFactory.buildCreateNodeCommandFromTool(diagram, tool);
final CompoundCommand compoundCommand = new CompoundCommand(tool.getName());
compoundCommand.add(createLayoutDataCommand());
compoundCommand.add(new ICommandProxy(new GMFCommandWrapper(getEditingDomain(), command)));
return compoundCommand;
return wrapCommandWithLayout(tool, emfCommandFactory.buildCreateNodeCommandFromTool(diagram, tool));
}
return null;
}
Expand All @@ -195,11 +188,7 @@ public Command getNodeCreationCommand(final DDiagram diagram, final NodeCreation
*/
public Command getContainerCreationDescription(final DDiagramElementContainer viewNodeContainer, final ContainerCreationDescription ccdTool) {
if (new ContainerCreationDescriptionQuery(ccdTool).canCreateIn(viewNodeContainer)) {
final org.eclipse.emf.common.command.Command command = emfCommandFactory.buildCreateContainerCommandFromTool(viewNodeContainer, ccdTool);
final CompoundCommand compoundCommand = new CompoundCommand(ccdTool.getName());
compoundCommand.add(createLayoutDataCommand());
compoundCommand.add(new ICommandProxy(new GMFCommandWrapper(getEditingDomain(), command)));
return compoundCommand;
return wrapCommandWithLayout(ccdTool, emfCommandFactory.buildCreateContainerCommandFromTool(viewNodeContainer, ccdTool));
}
return null;
}
Expand All @@ -214,13 +203,8 @@ public Command getContainerCreationDescription(final DDiagramElementContainer vi
* @return a command that is able to create a container in the specified viewpoint with the specified tool.
*/
public Command getContainerCreationDescription(final DDiagram diagram, final ContainerCreationDescription ccdTool) {
// if (container instanceof DiagramDescription) {
if (new ContainerCreationDescriptionQuery(ccdTool).canCreateIn(diagram)) {
final org.eclipse.emf.common.command.Command command = emfCommandFactory.buildCreateContainerCommandFromTool(diagram, ccdTool);
final CompoundCommand compoundCommand = new CompoundCommand(ccdTool.getName());
compoundCommand.add(createLayoutDataCommand());
compoundCommand.add(new ICommandProxy(new GMFCommandWrapper(getEditingDomain(), command)));
return compoundCommand;
return wrapCommandWithLayout(ccdTool, emfCommandFactory.buildCreateContainerCommandFromTool(diagram, ccdTool));
}
return null;
}
Expand All @@ -235,19 +219,8 @@ public Command getContainerCreationDescription(final DDiagram diagram, final Con
* @return a command that is able to launch a wizard tool.
*/
public Command getSelectionWizardCommand(final SelectionWizardDescription selectionTool, final EObject containerView) {
Command cmd = UnexecutableCommand.INSTANCE;
if (containerView != null) {
final CompoundCommand compoundCommand = new CompoundCommand(selectionTool.getName());
final TreeItemWrapper input = new TreeItemWrapper(null, null);
if (AbstractSelectionWizardCommand.canCreateCommand(selectionTool, containerView, input)) {
compoundCommand.add(createLayoutDataCommand());
compoundCommand
.add(new ICommandProxy(new GMFCommandWrapper(getEditingDomain(), new SelectionWizardCommand(emfCommandFactory, selectionTool, input, (DSemanticDecorator) containerView))));
cmd = compoundCommand;
cmd.setLabel(selectionTool.getName());
}
}
return cmd;
return getSelectionWizardCommand(selectionTool, containerView, input ->
new SelectionWizardCommand(emfCommandFactory, selectionTool, input, (DSemanticDecorator) containerView));
}

/**
Expand All @@ -260,21 +233,21 @@ public Command getSelectionWizardCommand(final SelectionWizardDescription select
* @return a command that is able to launch a wizard tool.
*/
public Command getPaneBasedSelectionWizardCommand(final PaneBasedSelectionWizardDescription selectionTool, final EObject containerView) {
Command cmd = UnexecutableCommand.INSTANCE;
return getSelectionWizardCommand(selectionTool, containerView, input ->
new PaneBasedSelectionWizardCommand(emfCommandFactory, selectionTool, input, (DSemanticDecorator) containerView));
}

private Command getSelectionWizardCommand(final AbstractToolDescription selectionTool, final EObject containerView,
final Function<TreeItemWrapper, AbstractSelectionWizardCommand> commandFactory) {
if (containerView != null) {
final CompoundCommand compoundCommand = new CompoundCommand(selectionTool.getName());
final TreeItemWrapper input = new TreeItemWrapper(null, null);
if (AbstractSelectionWizardCommand.canCreateCommand(selectionTool, containerView, input)) {
compoundCommand.add(createLayoutDataCommand());
compoundCommand.add(
new ICommandProxy(new GMFCommandWrapper(getEditingDomain(), new PaneBasedSelectionWizardCommand(emfCommandFactory, selectionTool, input, (DSemanticDecorator) containerView))));
cmd = compoundCommand;
cmd.setLabel(selectionTool.getName());
return wrapCommandWithLayout(selectionTool, commandFactory.apply(input));
}
}
return cmd;
return UnexecutableCommand.INSTANCE;
}

private TransactionalEditingDomain getEditingDomain() {
if (editPart instanceof IGraphicalEditPart) {
return ((IGraphicalEditPart) editPart).getEditingDomain();
Expand Down Expand Up @@ -334,11 +307,7 @@ public Command getRequestToolCommand(final RequestDescription requestDescription
* @return a command which executes the specified {@link ToolDescription} in the context of the view element.
*/
public Command getGenericToolCommand(final EObject containerView, final ToolDescription toolDesc) {
final CompoundCommand compoundCommand = new CompoundCommand(toolDesc.getName());
final org.eclipse.emf.common.command.Command command = emfCommandFactory.buildGenericToolCommandFromTool(containerView, toolDesc);
compoundCommand.add(createLayoutDataCommand());
compoundCommand.add(new ICommandProxy(new GMFCommandWrapper(getEditingDomain(), command)));
return compoundCommand;
return wrapCommandWithLayout(toolDesc, emfCommandFactory.buildGenericToolCommandFromTool(containerView, toolDesc));
}

private Command createLayoutDataCommand() {
Expand Down

0 comments on commit 13e0458

Please sign in to comment.