diff options
author | Gabriel Pascual | 2014-08-11 15:58:50 +0000 |
---|---|---|
committer | Gabriel Pascual | 2014-08-11 16:10:26 +0000 |
commit | 42be5042ce6b1baba655c5668ec778b5a5dcf7cf (patch) | |
tree | 6edbf6c230a52fea8305177f93af493dfc62537d | |
parent | 63d60e51b963bd707d3de6559caa72c22becd4e4 (diff) | |
download | org.eclipse.papyrus-42be5042ce6b1baba655c5668ec778b5a5dcf7cf.tar.gz org.eclipse.papyrus-42be5042ce6b1baba655c5668ec778b5a5dcf7cf.tar.xz org.eclipse.papyrus-42be5042ce6b1baba655c5668ec778b5a5dcf7cf.zip |
441511: [Properties View] Deletion of several applied comments
https://bugs.eclipse.org/bugs/show_bug.cgi?id=441511
- Fixes add and remove behaviour
Change-Id: I2db04d86ada363d611c9ea4a56278a5e8d675edd
Signed-off-by: Gabriel Pascual <gabriel.pascual@all4tec.net>
-rw-r--r-- | plugins/uml/properties/org.eclipse.papyrus.uml.properties/src/org/eclipse/papyrus/uml/properties/databinding/AppliedCommentsObservableList.java | 460 |
1 files changed, 229 insertions, 231 deletions
diff --git a/plugins/uml/properties/org.eclipse.papyrus.uml.properties/src/org/eclipse/papyrus/uml/properties/databinding/AppliedCommentsObservableList.java b/plugins/uml/properties/org.eclipse.papyrus.uml.properties/src/org/eclipse/papyrus/uml/properties/databinding/AppliedCommentsObservableList.java index 5518e837f85..a18def01a14 100644 --- a/plugins/uml/properties/org.eclipse.papyrus.uml.properties/src/org/eclipse/papyrus/uml/properties/databinding/AppliedCommentsObservableList.java +++ b/plugins/uml/properties/org.eclipse.papyrus.uml.properties/src/org/eclipse/papyrus/uml/properties/databinding/AppliedCommentsObservableList.java @@ -1,231 +1,229 @@ -/*****************************************************************************
- * Copyright (c) 2012 CEA LIST.
- *
- * All rights reserved. This program and the accompanying materials
- * are made available under the terms of the Eclipse Public License v1.0
- * which accompanies this distribution, and is available at
- * http://www.eclipse.org/legal/epl-v10.html
- *
- * Contributors:
- * Sebastien Poissonnet (CEA LIST) sebastien.poissonnet@cea.fr
- * Mickaël ADAM (ALL4TEC) mickael.adam@all4tec.net - bug 435174
- *****************************************************************************/
-package org.eclipse.papyrus.uml.properties.databinding;
-
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.Iterator;
-import java.util.LinkedList;
-import java.util.List;
-
-import org.eclipse.core.runtime.Assert;
-import org.eclipse.emf.common.command.Command;
-import org.eclipse.emf.common.command.CompoundCommand;
-import org.eclipse.emf.ecore.EObject;
-import org.eclipse.emf.ecore.EReference;
-import org.eclipse.emf.ecore.EStructuralFeature.Setting;
-import org.eclipse.emf.edit.command.AddCommand;
-import org.eclipse.emf.edit.domain.EditingDomain;
-import org.eclipse.emf.transaction.TransactionalEditingDomain;
-import org.eclipse.gmf.runtime.emf.type.core.requests.DestroyElementRequest;
-import org.eclipse.gmf.runtime.emf.type.core.requests.IEditCommandRequest;
-import org.eclipse.gmf.runtime.emf.type.core.requests.SetRequest;
-import org.eclipse.papyrus.infra.services.edit.service.IElementEditService;
-import org.eclipse.papyrus.uml.tools.databinding.PapyrusObservableList;
-import org.eclipse.uml2.common.util.UML2Util;
-import org.eclipse.uml2.uml.Comment;
-import org.eclipse.uml2.uml.Element;
-import org.eclipse.uml2.uml.UMLPackage;
-
-public class AppliedCommentsObservableList extends PapyrusObservableList {
-
- public AppliedCommentsObservableList(EditingDomain domain, Element source) {
- super(getAppliedCommentsList(source), domain, source, UMLPackage.eINSTANCE.getElement_OwnedComment());
- }
-
- private static List<Comment> getAppliedCommentsList(Element source) {
- List<Comment> result = new LinkedList<Comment>();
- Iterator<Setting> it = UML2Util.getNonNavigableInverseReferences(source).iterator();
- while(it.hasNext()) {
- Setting setting = it.next();
- if(setting.getEStructuralFeature() == UMLPackage.Literals.COMMENT__ANNOTATED_ELEMENT) {
- if(setting.getEObject() instanceof Comment) {
- Comment comment = (Comment)setting.getEObject();
- // small bugfix...
- // UML2Util.getNonNavigableInverseReferences returns more element than
- // needed, especially elements that are not real ones
- // so we must check if they are contained by the current resource or
- // not...
- boolean isProxy = false;
- for(Element annotatedElement : comment.getAnnotatedElements()) {
- if(annotatedElement.eResource() == null) {
- isProxy = true;
- }
- }
- // this is the real element, not a ghost one. display it in the list
- if(!isProxy) {
- if(comment.getAnnotatedElements().contains(source)) {
- result.add(comment);
- }
- }
- }
- }
- }
- return result;
- }
-
- @Override
- protected void refreshCacheList() {
- if(isDisposed()) {
- return;
- }
- wrappedList.clear();
- wrappedList.addAll(getAppliedCommentsList((Element)source));
- fireListChange(null);
- }
-
- @Override
- public Command getAddAllCommand(Collection<?> values) {
- throw new UnsupportedOperationException();
- // return super.getAddAllCommand(values);
- }
-
- @Override
- public Command getAddAllCommand(int index, Collection<?> values) {
- throw new UnsupportedOperationException();
- // return super.getAddAllCommand(index, values);
- }
-
- @Override
- public Command getAddCommand(int index, Object value) {
- throw new UnsupportedOperationException();
- // return super.getAddCommand(index, value);
- }
-
- @Override
- public Command getAddCommand(Object value) {
- Assert.isTrue(value instanceof Comment);
- Comment comment = (Comment)value;
- //Add the comment to source#ownedComment
- CompoundCommand addAppliedCommentCommand = new CompoundCommand("Add applied comment");
- addAppliedCommentCommand.append(super.getAddCommand(value));
- //Add the source element to comment#annotatedElement
- // List<Element> values = new LinkedList<Element>(comment.getAnnotatedElements());
- // values.add((Element)source);
- //
- // SetRequest setRequest = new SetRequest(comment, UMLPackage.eINSTANCE.getComment_AnnotatedElement(), values);
- // IElementEditService provider = getProvider();
- //
- // addAppliedCommentCommand.append(getCommandFromRequests(provider, Collections.singletonList(setRequest)));
- AddCommand addCommand = new AddCommand(editingDomain, comment, UMLPackage.eINSTANCE.getComment_AnnotatedElement(), source);
- addAppliedCommentCommand.append(addCommand);
- return addAppliedCommentCommand;
- }
-
- @Override
- public Command getRemoveCommand(Object value) {
- Assert.isTrue(value instanceof Comment);
- CompoundCommand removeAppliedCommentCommand = new CompoundCommand("Remove applied comment");
- Comment comment = (Comment)value;
- if(comment.getAnnotatedElements().size() <= 1) {
- return super.getRemoveCommand(value);
- }
- //Remove the source element to comment#annotatedElement
- List<Element> values = new LinkedList<Element>(comment.getAnnotatedElements());
- values.remove(source);
- SetRequest setRequest = new SetRequest(comment, UMLPackage.eINSTANCE.getComment_AnnotatedElement(), values);
- IElementEditService provider = getProvider();
- removeAppliedCommentCommand.append(getCommandFromRequests(provider, Collections.singletonList(setRequest)));
- // if(editingDomain != null) {
- // RemoveCommand command = new RemoveCommand(editingDomain, comment, UMLPackage.eINSTANCE.getComment_AnnotatedElement(), source);
- // return command;
- // }
- return removeAppliedCommentCommand;
- }
-
- @Override
- public Command getRemoveAllCommand(Collection<?> values) {
- Iterator<?> itr = values.iterator();
- Comment comment;
- Element value;
- List<Element> values_;
- CompoundCommand removeAppliedCommentCommand = new CompoundCommand("Remove applied comment");
- while(itr.hasNext()) {
- value = (Element)itr.next();
- Assert.isTrue(value instanceof Comment);
- comment = (Comment)value;
- if(comment.getAnnotatedElements().size() <= 1) {
- removeAppliedCommentCommand.append(super.getRemoveCommand(value));
- } else {
- values_ = new LinkedList<Element>(comment.getAnnotatedElements());
- values_.remove(source);
- SetRequest setRequest = new SetRequest(comment, UMLPackage.eINSTANCE.getComment_AnnotatedElement(), values_);
- IElementEditService provider = getProvider();
- removeAppliedCommentCommand.append(getCommandFromRequests(provider, Collections.singletonList(setRequest)));
- }
- }
- return removeAppliedCommentCommand;
- }
-
- //override to patch bug 435174
- @Override
- protected Collection<? extends IEditCommandRequest> getRequests(List<Object> newValues, Collection<?> removedValues) {
- LinkedList<IEditCommandRequest> requests = new LinkedList<IEditCommandRequest>();
- //DestroyElement request
- if(feature instanceof EReference && ((EReference)feature).isContainment() && removedValues != null) {
- for(Object o : removedValues) {
- if(o instanceof EObject) {
- requests.add(new DestroyElementRequest((TransactionalEditingDomain)editingDomain, (EObject)o, false));
- }
- }
- }
- //List to take into account the no annotated owned comment
- List<Object> ownedComments = new ArrayList<Object>();
- //Look for all owned comments of the source
- for(EObject ownedComment : ((Element)source).getOwnedComments()) {
- //Add comment the list
- if(removedValues == null || !removedValues.contains(ownedComment)) {
- ownedComments.add(ownedComment);
- }
- }
- //Add new values
- for(Object value : newValues) {
- if(value instanceof Comment) {
- //Only if the comment isn't owned by an other object
- if(((Comment)value).eContainer() == null) {
- ownedComments.add(value);
- }
- }
- }
- requests.add(new SetRequest((TransactionalEditingDomain)editingDomain, source, feature, ownedComments));
- return requests;
- }
-
- //
- // Unsupported operations
- //
- @Override
- public Command getClearCommand() {
- throw new UnsupportedOperationException();
- // return super.getClearCommand();
- }
-
- @Override
- public List<Command> getMoveCommands(int oldIndex, int newIndex) {
- throw new UnsupportedOperationException();
- // return super.getMoveCommands(oldIndex, newIndex);
- }
-
- @Override
- public Command getRemoveCommand(int index) {
- throw new UnsupportedOperationException();
- // return super.getRemoveCommand(index);
- }
-
- @Override
- public Command getSetCommand(int index, Object value) {
- throw new UnsupportedOperationException();
- // return super.getSetCommand(index, value);
- }
-}
+/***************************************************************************** + * Copyright (c) 2012 CEA LIST. + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Sebastien Poissonnet (CEA LIST) sebastien.poissonnet@cea.fr + * Mickaël ADAM (ALL4TEC) mickael.adam@all4tec.net - bug 435174 + * Gabriel Pascual (ALL4TEC) - Bug 441511 + *****************************************************************************/ +package org.eclipse.papyrus.uml.properties.databinding; + +import java.util.Collection; +import java.util.Collections; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; + +import org.eclipse.core.runtime.Assert; +import org.eclipse.emf.common.command.Command; +import org.eclipse.emf.common.command.CompoundCommand; +import org.eclipse.emf.ecore.EObject; +import org.eclipse.emf.ecore.EStructuralFeature.Setting; +import org.eclipse.emf.edit.command.AddCommand; +import org.eclipse.emf.edit.domain.EditingDomain; +import org.eclipse.emf.transaction.TransactionalEditingDomain; +import org.eclipse.gmf.runtime.emf.type.core.requests.DestroyElementRequest; +import org.eclipse.gmf.runtime.emf.type.core.requests.SetRequest; +import org.eclipse.papyrus.uml.tools.databinding.PapyrusObservableList; +import org.eclipse.uml2.common.util.UML2Util; +import org.eclipse.uml2.uml.Comment; +import org.eclipse.uml2.uml.Element; +import org.eclipse.uml2.uml.UMLPackage; + +/** + * Observable list for applied comments. + */ +public class AppliedCommentsObservableList extends PapyrusObservableList { + + public AppliedCommentsObservableList(EditingDomain domain, Element source) { + super(getAppliedCommentsList(source), domain, source, UMLPackage.eINSTANCE.getElement_OwnedComment()); + } + + /** + * Gets the applied comments list. + * + * @param source + * the source + * @return the applied comments list + */ + private static List<Comment> getAppliedCommentsList(Element source) { + List<Comment> result = new LinkedList<Comment>(); + Iterator<Setting> it = UML2Util.getNonNavigableInverseReferences(source).iterator(); + while(it.hasNext()) { + Setting setting = it.next(); + if(setting.getEStructuralFeature() == UMLPackage.Literals.COMMENT__ANNOTATED_ELEMENT) { + if(setting.getEObject() instanceof Comment) { + Comment comment = (Comment)setting.getEObject(); + // small bugfix... + // UML2Util.getNonNavigableInverseReferences returns more element than + // needed, especially elements that are not real ones + // so we must check if they are contained by the current resource or + // not... + boolean isProxy = false; + for(Element annotatedElement : comment.getAnnotatedElements()) { + if(annotatedElement.eResource() == null) { + isProxy = true; + } + } + // this is the real element, not a ghost one. display it in the list + if(!isProxy) { + if(comment.getAnnotatedElements().contains(source)) { + result.add(comment); + } + } + } + } + } + return result; + } + + /** + * <p> + * Redefine refresh cache because applied comments list is a subset of {@link Element#getOwnedComments <em>Owned Comments</em>}. + * </p> + * + * @see org.eclipse.papyrus.infra.emf.databinding.EMFObservableList#refreshCacheList() + * + */ + @Override + protected void refreshCacheList() { + if(isDisposed()) { + return; + } + wrappedList.clear(); + wrappedList.addAll(getAppliedCommentsList((Element)source)); + fireListChange(null); + } + + + + /** + * @see org.eclipse.papyrus.uml.tools.databinding.PapyrusObservableList#getAddCommand(java.lang.Object) + * + * @param value + * @return + */ + @Override + public Command getAddCommand(Object value) { + CompoundCommand addAppliedCommentCommand = null; + + if(value instanceof Comment) { + + addAppliedCommentCommand = new CompoundCommand("Add applied comment"); + + //Add the comment to source#ownedComment + SetRequest setRequest = new SetRequest(source, feature, value); + addAppliedCommentCommand.append(getCommandFromRequests(getProvider(), Collections.singletonList(setRequest))); + + //Check if source was already had to comment + if(!((Comment)value).getAnnotatedElements().contains(source)) { + // Add comment to element + AddCommand addCommand = new AddCommand(editingDomain, (EObject)value, UMLPackage.eINSTANCE.getComment_AnnotatedElement(), source); + addAppliedCommentCommand.append(addCommand); + } + } + + return addAppliedCommentCommand; + } + + /** + * @see org.eclipse.papyrus.uml.tools.databinding.PapyrusObservableList#getRemoveCommand(java.lang.Object) + * + * @param value + * @return + */ + @Override + public Command getRemoveCommand(Object value) { + + Command removeAppliedCommentCommand = null; + if(value instanceof Comment) { + + Comment comment = (Comment)value; + + if(comment.getAnnotatedElements().size() > 1) { + //Remove on link between source and comment + List<Element> values = new LinkedList<Element>(comment.getAnnotatedElements()); + values.remove(source); + SetRequest setRequest = new SetRequest(comment, UMLPackage.eINSTANCE.getComment_AnnotatedElement(), values); + removeAppliedCommentCommand = getCommandFromRequests(getProvider(), Collections.singletonList(setRequest)); + + } else { + //Remove comment in element + DestroyElementRequest detroyRequest = new DestroyElementRequest((TransactionalEditingDomain)editingDomain, comment, false); + removeAppliedCommentCommand = getCommandFromRequests(getProvider(), Collections.singleton(detroyRequest)); + } + + } + + return removeAppliedCommentCommand; + } + + /** + * @see org.eclipse.papyrus.uml.tools.databinding.PapyrusObservableList#getRemoveAllCommand(java.util.Collection) + * + * @param values + * @return + */ + @Override + public Command getRemoveAllCommand(Collection<?> values) { + Iterator<?> itr = values.iterator(); + Element value; + CompoundCommand removeAppliedCommentCommand = new CompoundCommand("Remove applied comment"); + while(itr.hasNext()) { + value = (Element)itr.next(); + Assert.isTrue(value instanceof Comment); + removeAppliedCommentCommand.append(getRemoveCommand(value)); + } + return removeAppliedCommentCommand; + } + + // + // Unsupported operations + // + @Override + public Command getClearCommand() { + throw new UnsupportedOperationException(); + // return super.getClearCommand(); + } + + @Override + public List<Command> getMoveCommands(int oldIndex, int newIndex) { + throw new UnsupportedOperationException(); + // return super.getMoveCommands(oldIndex, newIndex); + } + + @Override + public Command getRemoveCommand(int index) { + throw new UnsupportedOperationException(); + // return super.getRemoveCommand(index); + } + + @Override + public Command getSetCommand(int index, Object value) { + throw new UnsupportedOperationException(); + // return super.getSetCommand(index, value); + } + + @Override + public Command getAddAllCommand(Collection<?> values) { + throw new UnsupportedOperationException(); + // return super.getAddAllCommand(values); + } + + @Override + public Command getAddAllCommand(int index, Collection<?> values) { + throw new UnsupportedOperationException(); + // return super.getAddAllCommand(index, values); + } + + @Override + public Command getAddCommand(int index, Object value) { + throw new UnsupportedOperationException(); + // return super.getAddCommand(index, value); + } +} |