Skip to content

Package: DeleteReferenceAction

DeleteReferenceAction

nameinstructionbranchcomplexitylinemethod
DeleteReferenceAction(EditingDomain, EObject, EStructuralFeature, ReferenceService)
M: 0 C: 9
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
DeleteReferenceAction(EditingDomain, EStructuralFeature.Setting, ReferenceService)
M: 0 C: 14
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
askConfirmation(EObject)
M: 84 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 29 C: 0
0%
M: 1 C: 0
0%
delete(EReference)
M: 33 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 8 C: 0
0%
M: 1 C: 0
0%
run()
M: 25 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 8 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2011-2015 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: * Eugen Neufeld - initial API and implementation
13: ******************************************************************************/
14: package org.eclipse.emf.ecp.edit.spi.swt.reference;
15:
16: import org.eclipse.emf.common.command.Command;
17: import org.eclipse.emf.common.notify.AdapterFactory;
18: import org.eclipse.emf.ecore.EObject;
19: import org.eclipse.emf.ecore.EReference;
20: import org.eclipse.emf.ecore.EStructuralFeature;
21: import org.eclipse.emf.ecore.EStructuralFeature.Setting;
22: import org.eclipse.emf.ecore.InternalEObject;
23: import org.eclipse.emf.ecp.edit.internal.swt.Activator;
24: import org.eclipse.emf.ecp.edit.internal.swt.reference.ReferenceMessageKeys;
25: import org.eclipse.emf.ecp.edit.spi.ReferenceService;
26: import org.eclipse.emf.ecp.edit.spi.swt.actions.ECPSWTAction;
27: import org.eclipse.emf.edit.command.RemoveCommand;
28: import org.eclipse.emf.edit.command.SetCommand;
29: import org.eclipse.emf.edit.domain.EditingDomain;
30: import org.eclipse.emf.edit.provider.AdapterFactoryItemDelegator;
31: import org.eclipse.emf.edit.provider.ComposedAdapterFactory;
32: import org.eclipse.emf.edit.provider.ReflectiveItemProviderAdapterFactory;
33: import org.eclipse.emfforms.spi.localization.LocalizationServiceHelper;
34: import org.eclipse.jface.dialogs.MessageDialog;
35: import org.eclipse.jface.window.Window;
36:
37: /**
38: * This action unsets a reference.
39: *
40: * @author Eugen Neufeld
41: * @since 1.5
42: *
43: */
44: public class DeleteReferenceAction extends ECPSWTAction {
45:
46:         /**
47:          * The constructor for a delete reference action.
48:          *
49:          * @param editingDomain the {@link EditingDomain} to use
50:          * @param setting the {@link Setting} to use
51:          * @param referenceService the {@link ReferenceService} to use
52:          * @since 1.6
53:          */
54:         public DeleteReferenceAction(EditingDomain editingDomain, Setting setting, ReferenceService referenceService) {
55:                 super(editingDomain, setting);
56:                 // TODO remove PlatformUI
57:
58:                 setImageDescriptor(Activator.getImageDescriptor("icons/unset_reference.png")); //$NON-NLS-1$
59:                 setToolTipText(LocalizationServiceHelper.getString(DeleteReferenceAction.class,
60:                         ReferenceMessageKeys.DeleteReferenceAction_DeleteReference));
61:         }
62:
63:         /**
64:          * The constructor for a delete reference action.
65:          *
66:          * @param editingDomain The {@link EditingDomain} to use
67:          * @param eObject The {@link EObject} to use
68:          * @param structuralFeature The {@link EStructuralFeature} defining which feature of the {@link EObject} is used
69:          * @param referenceService The {@link ReferenceService} to use
70:          * @since 1.6
71:          */
72:         public DeleteReferenceAction(EditingDomain editingDomain, EObject eObject, EStructuralFeature structuralFeature,
73:                 ReferenceService referenceService) {
74:                 this(editingDomain, ((InternalEObject) eObject).eSetting(structuralFeature), referenceService);
75:         }
76:
77:         @Override
78:         public void run() {
79:                 super.run();
80:
81:                 final EReference reference = (EReference) getSetting().getEStructuralFeature();
82:
83:•                if (reference.isContainment()) // &&
84:                 // context.getMetaModelElementContext().isNonDomainElement(opposite.eClass())||
85:                 // context.getMetaModelElementContext().isAssociationClassElement(opposite)
86:                 {
87:•                        if (askConfirmation((EObject) getSetting().get(true))) {
88:                                 delete(reference);
89:                         }
90:                 } else {
91:                         delete(reference);
92:                 }
93:         }
94:
95:         private void delete(EReference reference) {
96:                 Command removeCommand = null;
97:
98:•                if (reference.isMany()) {
99:                         removeCommand = RemoveCommand.create(getEditingDomain(), getSetting().getEObject(), reference, getSetting()
100:                                 .get(true));
101:                 } else {
102:                         removeCommand = SetCommand.create(getEditingDomain(), getSetting().getEObject(), reference, null);
103:                 }
104:                 getEditingDomain().getCommandStack().execute(removeCommand);
105:         }
106:
107:         private static boolean askConfirmation(EObject toBeDeleted) {
108:                 String question = null;
109:                 ComposedAdapterFactory adapterFactory = null;
110:                 // if (toBeDeleted.size() == 1) {
111:                 adapterFactory = new ComposedAdapterFactory(new AdapterFactory[] { new ReflectiveItemProviderAdapterFactory(),
112:                         new ComposedAdapterFactory(ComposedAdapterFactory.Descriptor.Registry.INSTANCE) });
113:                 final AdapterFactoryItemDelegator adapterFactoryItemDelegator = new AdapterFactoryItemDelegator(adapterFactory);
114:                 // AdapterFactoryLabelProvider adapterFactoryLabelProvider = new AdapterFactoryLabelProvider(adapterFactory);
115:                 final String modelElementName = adapterFactoryItemDelegator.getText(toBeDeleted);
116:                 question = LocalizationServiceHelper.getString(DeleteReferenceAction.class,
117:                         ReferenceMessageKeys.DeleteReferenceAction_DeleteModelQuestion)
118:                         + modelElementName
119:                         + LocalizationServiceHelper.getString(DeleteReferenceAction.class,
120:                                 ReferenceMessageKeys.DeleteReferenceAction_Questionmark);
121:                 // } else {
122:                 // question = "Do you really want to delete these " + toBeDeleted.size() + " model elements?";
123:                 // }
124:                 final MessageDialog dialog = new MessageDialog(
125:                         null,
126:                         LocalizationServiceHelper.getString(DeleteReferenceAction.class,
127:                                 ReferenceMessageKeys.DeleteReferenceAction_Confirmation),
128:                         null,
129:                         question,
130:                         MessageDialog.QUESTION,
131:                         new String[] {
132:                                 LocalizationServiceHelper.getString(DeleteReferenceAction.class,
133:                                         ReferenceMessageKeys.DeleteReferenceAction_Yes),
134:                                 LocalizationServiceHelper.getString(DeleteReferenceAction.class,
135:                                         ReferenceMessageKeys.DeleteReferenceAction_No) },
136:                         0);
137:
138:                 boolean confirm = false;
139:•                if (dialog.open() == Window.OK) {
140:                         confirm = true;
141:                 }
142:
143:                 // if (adapterFactory != null)
144:                 // {
145:                 // adapterFactory.dispose();
146:                 // }
147:                 adapterFactory.dispose();
148:                 return confirm;
149:         }
150: }