diff --git a/common/plugins/org.polarsys.capella.common.ef/src/org/polarsys/capella/common/ef/command/AbstractCompoundCommand.java b/common/plugins/org.polarsys.capella.common.ef/src/org/polarsys/capella/common/ef/command/AbstractCompoundCommand.java index 1c667c7355..86b10e56c9 100644 --- a/common/plugins/org.polarsys.capella.common.ef/src/org/polarsys/capella/common/ef/command/AbstractCompoundCommand.java +++ b/common/plugins/org.polarsys.capella.common.ef/src/org/polarsys/capella/common/ef/command/AbstractCompoundCommand.java @@ -1,15 +1,15 @@ -/******************************************************************************* - * Copyright (c) 2006, 2020 THALES GLOBAL SERVICES. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License 2.0 which is available at - * http://www.eclipse.org/legal/epl-2.0 - * - * SPDX-License-Identifier: EPL-2.0 - * - * Contributors: - * Thales - initial API and implementation - *******************************************************************************/ +/******************************************************************************* + * Copyright (c) 2006, 2020 THALES GLOBAL SERVICES. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0 + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * Thales - initial API and implementation + *******************************************************************************/ package org.polarsys.capella.common.ef.command; @@ -17,6 +17,7 @@ import java.util.Collections; import java.util.Iterator; import java.util.List; +import java.util.Optional; import org.eclipse.core.runtime.Assert; @@ -54,9 +55,21 @@ public void append(ICommand command) { _commands.add(command); } + /** + * Append specified optional command to this compound command's list of commands if this optional command is present. + * + * @param command + * the added command. + */ + public void appendIfPresent(Optional optCommand) { + optCommand.ifPresent(command -> _commands.add(command)); + } + /** * Prepend specified command to this compound command's list of commands. - * @param command the added command. + * + * @param command + * the added command. */ public void prepend(ICommand command) { _commands.add(0, command); @@ -91,6 +104,7 @@ public List getContainedCommands() { /** * @see org.polarsys.capella.common.ef.command.ICommand#isReadOnly() */ + @Override public boolean isReadOnly() { boolean isReadOnly = true; // Iterate over contained commands to search one that is not a read only one. @@ -105,6 +119,7 @@ public boolean isReadOnly() { /** * @see java.lang.Runnable#run() */ + @Override public void run() { // Iterate over contained commands to run each contained command. for (ICommand command : _commands) { diff --git a/core/plugins/org.polarsys.capella.core.data.information.properties/src/org/polarsys/capella/core/data/information/properties/fields/NavigableCheckbox.java b/core/plugins/org.polarsys.capella.core.data.information.properties/src/org/polarsys/capella/core/data/information/properties/fields/NavigableCheckbox.java index d33d2a02bc..3a1349271e 100644 --- a/core/plugins/org.polarsys.capella.core.data.information.properties/src/org/polarsys/capella/core/data/information/properties/fields/NavigableCheckbox.java +++ b/core/plugins/org.polarsys.capella.core.data.information.properties/src/org/polarsys/capella/core/data/information/properties/fields/NavigableCheckbox.java @@ -21,6 +21,7 @@ import org.eclipse.swt.widgets.Composite; import org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetWidgetFactory; import org.polarsys.capella.common.data.modellingcore.ModellingcorePackage; +import org.polarsys.capella.common.ef.command.AbstractCompoundCommand; import org.polarsys.capella.common.helpers.EObjectExt; import org.polarsys.capella.core.data.capellacore.CapellacorePackage; import org.polarsys.capella.core.data.capellacore.Classifier; @@ -82,8 +83,12 @@ public void widgetSelected(SelectionEvent event) { if (oppositeMember != null) { EObject oppositeTypeElement = (EObject) oppositeMember.eGet(ModellingcorePackage.Literals.ABSTRACT_TYPED_ELEMENT__ABSTRACT_TYPE); if (oppositeTypeElement != null) { - moveDataValue(semanticElement, oppositeTypeElement, CapellacorePackage.Literals.CLASSIFIER__OWNED_FEATURES); - addDataValue(ownerElement, InformationPackage.Literals.ASSOCIATION__NAVIGABLE_MEMBERS, semanticElement); + AbstractCompoundCommand command = new AbstractCompoundCommand() {}; + command.appendIfPresent(createMoveDataValueCommand(semanticElement, oppositeTypeElement, + CapellacorePackage.Literals.CLASSIFIER__OWNED_FEATURES)); + command.appendIfPresent(createAddDataValueCommand(ownerElement, + InformationPackage.Literals.ASSOCIATION__NAVIGABLE_MEMBERS, semanticElement)); + executeCommand(command); } } } @@ -96,8 +101,12 @@ public void widgetSelected(SelectionEvent event) { } if ((typeElement instanceof Classifier) && (referencerElement instanceof Association)) { - moveDataValue(semanticElement, referencerElement, InformationPackage.Literals.ASSOCIATION__OWNED_MEMBERS); - removeDataValue(referencerElement, InformationPackage.Literals.ASSOCIATION__NAVIGABLE_MEMBERS, semanticElement); + AbstractCompoundCommand command = new AbstractCompoundCommand() {}; + command.appendIfPresent(createMoveDataValueCommand(semanticElement, referencerElement, + InformationPackage.Literals.ASSOCIATION__OWNED_MEMBERS)); + command.appendIfPresent(createRemoveDataValueCommand(referencerElement, + InformationPackage.Literals.ASSOCIATION__NAVIGABLE_MEMBERS, semanticElement)); + executeCommand(command); } } } diff --git a/core/plugins/org.polarsys.capella.core.ui.properties/src/org/polarsys/capella/core/ui/properties/fields/AbstractSemanticField.java b/core/plugins/org.polarsys.capella.core.ui.properties/src/org/polarsys/capella/core/ui/properties/fields/AbstractSemanticField.java index 34b6a5d03f..b8053a36bb 100644 --- a/core/plugins/org.polarsys.capella.core.ui.properties/src/org/polarsys/capella/core/ui/properties/fields/AbstractSemanticField.java +++ b/core/plugins/org.polarsys.capella.core.ui.properties/src/org/polarsys/capella/core/ui/properties/fields/AbstractSemanticField.java @@ -16,6 +16,7 @@ import java.util.Collection; import java.util.Collections; import java.util.List; +import java.util.Optional; import org.eclipse.emf.ecore.EAttribute; import org.eclipse.emf.ecore.EDataType; @@ -427,19 +428,25 @@ public void run() { * Add data value i.e change given object for given feature with specified value. * * @param object + * The EMF EObject to which you want to add the element. * @param feature + * The multivalued feature of the EObject to which you want to add the element. * @param value + * The value to add to the feature of the object. + * @return The command if available, Optional.empty otherwise */ - protected void addDataValue(final EObject object, final EStructuralFeature feature, final Object value) { + protected Optional createAddDataValueCommand(final EObject object, + final EStructuralFeature feature, final Object value) { if (NotificationHelper.isNotificationRequired(object, feature, value)) { - AbstractReadWriteCommand command = new AbstractReadWriteCommand() { + return Optional.of(new AbstractReadWriteCommand() { @Override @SuppressWarnings({ "unchecked", "rawtypes" }) public void run() { ((List) object.eGet(feature)).add(value); } - }; - executeCommand(command); + }); + } else { + return Optional.empty(); } } @@ -447,12 +454,16 @@ public void run() { * Move data value. * * @param object + * The object to move * @param owner + * On which you want to move the object * @param feature + * The owner feature in which you want to move the object. */ @SuppressWarnings("unchecked") - protected void moveDataValue(final EObject object, final EObject owner, final EStructuralFeature feature) { - AbstractReadWriteCommand command = new AbstractReadWriteCommand() { + protected Optional createMoveDataValueCommand(final EObject object, final EObject owner, + final EStructuralFeature feature) { + return Optional.of(new AbstractReadWriteCommand() { @Override public void run() { if (feature.isMany()) { @@ -461,20 +472,24 @@ public void run() { owner.eSet(feature, object); } } - }; - executeCommand(command); + }); } /** * Remove data value i.e change given object for given feature with specified value. * * @param object + * The EMF EObject to which you want to remove the element. * @param feature + * The multivalued feature of the EObject to which you want to remove the element. * @param value + * The value to remove to the feature of the object. + * @return The command if available, Optional.empty otherwise */ - protected void removeDataValue(final EObject object, final EStructuralFeature feature, final Object value) { + protected Optional createRemoveDataValueCommand(final EObject object, + final EStructuralFeature feature, final Object value) { if (NotificationHelper.isNotificationRequired(object, feature, value)) { - AbstractReadWriteCommand command = new AbstractReadWriteCommand() { + return Optional.of(new AbstractReadWriteCommand() { @Override public void run() { if ((feature instanceof EReference) && ((EReference) feature).isContainment()) { @@ -483,8 +498,9 @@ public void run() { ((List) object.eGet(feature)).remove(value); } } - }; - executeCommand(command); + }); + } else { + return Optional.empty(); } }