Skip to content

Commit

Permalink
eclipse-capella#2913 Merge two commands to avoid refresh between them
Browse files Browse the repository at this point in the history
This commit merges two commands for navigability state of association in
CDB to avoid diagram refresh (and unwanted behaviour) between them.

Signed-off-by: Séraphin Costa <[email protected]>
  • Loading branch information
scosta-obeo committed Dec 3, 2024
1 parent 461d255 commit 83b20fc
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 29 deletions.
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
/*******************************************************************************
* 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;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Optional;

import org.eclipse.core.runtime.Assert;

Expand Down Expand Up @@ -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<? extends ICommand> 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);
Expand Down Expand Up @@ -91,6 +104,7 @@ public List<ICommand> 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.
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
}
Expand All @@ -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);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -427,32 +428,42 @@ 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, <code>Optional.empty</code> otherwise
*/
protected void addDataValue(final EObject object, final EStructuralFeature feature, final Object value) {
protected Optional<AbstractReadWriteCommand> 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();
}
}

/**
* 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<AbstractReadWriteCommand> createMoveDataValueCommand(final EObject object, final EObject owner,
final EStructuralFeature feature) {
return Optional.of(new AbstractReadWriteCommand() {
@Override
public void run() {
if (feature.isMany()) {
Expand All @@ -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, <code>Optional.empty</code> otherwise
*/
protected void removeDataValue(final EObject object, final EStructuralFeature feature, final Object value) {
protected Optional<AbstractReadWriteCommand> 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()) {
Expand All @@ -483,8 +498,9 @@ public void run() {
((List<?>) object.eGet(feature)).remove(value);
}
}
};
executeCommand(command);
});
} else {
return Optional.empty();
}
}

Expand Down

0 comments on commit 83b20fc

Please sign in to comment.