Skip to content

Package: EMFDeleteServiceImpl$1

EMFDeleteServiceImpl$1

nameinstructionbranchcomplexitylinemethod
doExecute()
M: 0 C: 6
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
{...}
M: 0 C: 10
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2011-2020 EclipseSource Muenchen GmbH and others.
3: *
4: * All rights reserved. This program and the accompanying materials
5: * are made available under the terms of the Eclipse Public License 2.0
6: * which accompanies this distribution, and is available at
7: * https://www.eclipse.org/legal/epl-2.0/
8: *
9: * SPDX-License-Identifier: EPL-2.0
10: *
11: * Contributors:
12: * Johannes Faltermeier - initial API and implementation
13: * Christian W. Damus - bugs 552385, 559267
14: ******************************************************************************/
15: package org.eclipse.emf.ecp.edit.spi;
16:
17: import java.util.Collection;
18: import java.util.Collections;
19: import java.util.stream.Collectors;
20: import java.util.stream.StreamSupport;
21:
22: import org.eclipse.emf.common.command.Command;
23: import org.eclipse.emf.ecore.EObject;
24: import org.eclipse.emf.ecore.EReference;
25: import org.eclipse.emf.ecore.util.EcoreUtil;
26: import org.eclipse.emf.ecp.view.spi.context.ViewModelContext;
27: import org.eclipse.emf.edit.command.ChangeCommand;
28: import org.eclipse.emf.edit.command.DeleteCommand;
29: import org.eclipse.emf.edit.command.RemoveCommand;
30: import org.eclipse.emf.edit.domain.AdapterFactoryEditingDomain;
31: import org.eclipse.emf.edit.domain.EditingDomain;
32:
33: /**
34: * Default EMF implementation of the {@link DeleteService}. Uses {@link DeleteCommand}
35: * respectively.
36: *
37: * @author jfaltermeier
38: * @since 1.6
39: *
40: */
41: public class EMFDeleteServiceImpl implements ConditionalDeleteService {
42:
43:         private EditingDomain editingDomain;
44:
45:         @Override
46:         public void instantiate(ViewModelContext context) {
47:                 editingDomain = AdapterFactoryEditingDomain
48:                         .getEditingDomainFor(context.getDomainModel());
49:         }
50:
51:         @Override
52:         public void dispose() {
53:                 // no op
54:         }
55:
56:         @Override
57:         public int getPriority() {
58:                 return 1;
59:         }
60:
61:         @Override
62:         public void deleteElements(final Collection<Object> toDelete) {
63:                 if (toDelete == null || toDelete.isEmpty()) {
64:                         return;
65:                 }
66:
67:                 if (editingDomain == null) {
68:                         deleteWithoutEditingDomain(toDelete);
69:                         return;
70:                 }
71:
72:                 final Command deleteCommand = DeleteCommand.create(editingDomain, toDelete);
73:                 if (deleteCommand.canExecute()) {
74:                         if (editingDomain.getCommandStack() == null) {
75:                                 deleteCommand.execute();
76:                         } else {
77:                                 editingDomain.getCommandStack().execute(deleteCommand);
78:                         }
79:                         return;
80:                 }
81:
82:                 /*
83:                  * the default DeleteCommand cannot be executed for whatever reason.
84:                  * Wrap the default delete in a change command for undo support.
85:                  */
86:                 final Command changeCommand = new ChangeCommand(editingDomain.getResourceSet()) {
87:                         @Override
88:                         protected void doExecute() {
89:                                 deleteWithoutEditingDomain(toDelete);
90:                         }
91:                 };
92:                 if (changeCommand.canExecute()) {
93:                         if (editingDomain.getCommandStack() == null) {
94:                                 changeCommand.execute();
95:                         } else {
96:                                 editingDomain.getCommandStack().execute(changeCommand);
97:                         }
98:                         return;
99:                 }
100:
101:                 throw new IllegalStateException("Delete was not successful."); //$NON-NLS-1$
102:         }
103:
104:         private void deleteWithoutEditingDomain(Collection<Object> toDelete) {
105:                 for (final Object object : toDelete) {
106:                         final Object unwrap = AdapterFactoryEditingDomain.unwrap(object);
107:                         if (!EObject.class.isInstance(unwrap)) {
108:                                 continue;
109:                         }
110:                         EcoreUtil.delete(EObject.class.cast(unwrap), true);
111:                 }
112:         }
113:
114:         @Override
115:         public void deleteElement(Object toDelete) {
116:                 if (toDelete == null) {
117:                         return;
118:                 }
119:                 /* delete command for collections/single object works the same */
120:                 deleteElements(Collections.singleton(toDelete));
121:         }
122:
123:         @Override
124:         public boolean canDelete(Iterable<?> objects) {
125:                 boolean result;
126:
127:                 if (editingDomain != null) {
128:                         final Collection<?> collection = asCollection(objects);
129:                         final Command deleteCommand = DeleteCommand.create(editingDomain, collection);
130:                         result = deleteCommand.canExecute();
131:                 } else {
132:                         // Just see whether any object is a root
133:                         result = true;
134:
135:                         for (final Object next : objects) {
136:                                 if (next instanceof EObject && ((EObject) next).eContainer() == null) {
137:                                         // Cannot delete a root object
138:                                         result = false;
139:                                         break;
140:                                 }
141:                         }
142:                 }
143:
144:                 return result;
145:         }
146:
147:         private static <T> Collection<T> asCollection(Iterable<T> iterable) {
148:                 Collection<T> result;
149:
150:                 if (iterable instanceof Collection<?>) {
151:                         result = (Collection<T>) iterable;
152:                 } else {
153:                         result = StreamSupport.stream(iterable.spliterator(), false).collect(Collectors.toList());
154:                 }
155:
156:                 return result;
157:         }
158:
159:         @Override
160:         public boolean canRemove(Object owner, Object feature, Iterable<?> objects) {
161:                 if (!(feature instanceof EReference)) {
162:                         return canDelete(objects);
163:                 }
164:
165:                 final EReference reference = (EReference) feature;
166:                 if (reference.isContainment()) {
167:                         return canDelete(objects);
168:                 }
169:
170:                 boolean result;
171:
172:                 if (editingDomain != null) {
173:                         final Collection<?> collection = asCollection(objects);
174:                         final Command removeCommand = RemoveCommand.create(editingDomain, owner, reference, collection);
175:                         result = removeCommand.canExecute();
176:                 } else {
177:                         // No commands? Then there's nothing to say 'no'
178:                         result = true;
179:                 }
180:
181:                 return result;
182:         }
183:
184: }