Skip to content

Package: AddReferenceAction

AddReferenceAction

nameinstructionbranchcomplexitylinemethod
AddReferenceAction(EditingDomain, EStructuralFeature.Setting, IItemPropertyDescriptor, ReferenceService)
M: 143 C: 0
0%
M: 14 C: 0
0%
M: 8 C: 0
0%
M: 35 C: 0
0%
M: 1 C: 0
0%
addExistingElementsToReferenceService(EObject, EReference)
M: 10 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
getContainmentWarningDialog()
M: 28 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 13 C: 0
0%
M: 1 C: 0
0%
run()
M: 27 C: 0
0%
M: 2 C: 0
0%
M: 2 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: *******************************************************************************/
15:
16: package org.eclipse.emf.ecp.edit.spi.swt.reference;
17:
18: import java.net.MalformedURLException;
19: import java.net.URL;
20:
21: import org.eclipse.emf.common.util.URI;
22: import org.eclipse.emf.ecore.EObject;
23: import org.eclipse.emf.ecore.EReference;
24: import org.eclipse.emf.ecore.EStructuralFeature.Setting;
25: import org.eclipse.emf.ecp.edit.internal.swt.Activator;
26: import org.eclipse.emf.ecp.edit.internal.swt.reference.ReferenceMessageKeys;
27: import org.eclipse.emf.ecp.edit.internal.swt.util.OverlayImageDescriptor;
28: import org.eclipse.emf.ecp.edit.spi.ReferenceService;
29: import org.eclipse.emf.ecp.edit.spi.swt.actions.ECPSWTAction;
30: import org.eclipse.emf.ecp.edit.spi.swt.util.ECPDialogExecutor;
31: import org.eclipse.emf.edit.domain.EditingDomain;
32: import org.eclipse.emf.edit.provider.ComposedImage;
33: import org.eclipse.emf.edit.provider.IItemLabelProvider;
34: import org.eclipse.emf.edit.provider.IItemPropertyDescriptor;
35: import org.eclipse.emfforms.spi.localization.LocalizationServiceHelper;
36: import org.eclipse.jface.dialogs.MessageDialog;
37: import org.eclipse.jface.resource.ImageDescriptor;
38: import org.eclipse.jface.window.Window;
39: import org.eclipse.swt.graphics.ImageData;
40:
41: /**
42: * An Action for adding reference links to a model element. It is mainly used in the
43: * {@link org.eclipse.emf.ecp.edit.internal.swt.controls.MultiControl MultiControl}
44: *
45: * @author shterev
46: * @author Eugen Neufeld
47: * @since 1.5
48: */
49: public class AddReferenceAction extends ECPSWTAction {
50:
51:         private final ReferenceService referenceService;
52:
53:         /**
54:          * The constructor for the add reference action.
55:          *
56:          * @param editingDomain the {@link EditingDomain} to use
57:          * @param setting the {@link Setting} to use
58:          * @param itemPropertyDescriptor the property descriptor
59:          * @param referenceService the {@link ReferenceService}
60:          */
61:         public AddReferenceAction(EditingDomain editingDomain, Setting setting,
62:                 IItemPropertyDescriptor itemPropertyDescriptor, ReferenceService referenceService) {
63:                 super(editingDomain, setting);
64:                 this.referenceService = referenceService;
65:                 final EReference eReference = (EReference) setting.getEStructuralFeature();
66:                 Object obj = null;
67:•                if (!eReference.getEReferenceType().isAbstract()) {
68:                         obj = eReference.getEReferenceType().getEPackage().getEFactoryInstance()
69:                                 .create(eReference.getEReferenceType());
70:                 }
71:                 final IItemLabelProvider labelProvider = itemPropertyDescriptor.getLabelProvider(setting.getEObject());
72:                 Object labelProviderImageResult = labelProvider.getImage(obj);
73:                 ImageData imageData = null;
74:
75:•                if (ComposedImage.class.isInstance(labelProviderImageResult)) {
76:                         labelProviderImageResult = ((ComposedImage) labelProviderImageResult).getImages().get(0);
77:                 }
78:•                if (URI.class.isInstance(labelProviderImageResult)) {
79:                         try {
80:                                 labelProviderImageResult = new URL(((URI) labelProviderImageResult).toString());
81:                         } catch (final MalformedURLException ex) {
82:                                 Activator.logException(ex);
83:                         }
84:                 }
85:•                if (URL.class.isInstance(labelProviderImageResult)) {
86:                         imageData = Activator.getImageData((URL) labelProviderImageResult);
87:                 } else {
88:                         imageData = Activator.getImageData((URL) null);
89:                 }
90:                 String overlayString = "icons/link_overlay.png";//$NON-NLS-1$
91:•                if (eReference.isContainment()) {
92:                         overlayString = "icons/containment_overlay.png";//$NON-NLS-1$
93:                 }
94:                 final ImageDescriptor addOverlay = Activator.getImageDescriptor(overlayString);
95:                 final OverlayImageDescriptor imageDescriptor = new OverlayImageDescriptor(imageData, addOverlay,
96:                         OverlayImageDescriptor.LOWER_RIGHT);
97:                 setImageDescriptor(imageDescriptor);
98:
99:                 String attribute = itemPropertyDescriptor.getDisplayName(null);
100:                 // make singular attribute labels
101:                 // TODO language dependent
102:•                if (attribute.endsWith("ies")) {//$NON-NLS-1$
103:                         attribute = attribute.substring(0, attribute.length() - 3) + "y"; //$NON-NLS-1$
104:•                } else if (attribute.endsWith("s")) {//$NON-NLS-1$
105:                         attribute = attribute.substring(0, attribute.length() - 1);
106:                 }
107:                 setToolTipText(
108:                         LocalizationServiceHelper.getString(getClass(), ReferenceMessageKeys.AddReferenceAction_Link) + attribute);
109:         }
110:
111:         /**
112:          * {@inheritDoc}
113:          */
114:         @Override
115:         public void run() {
116:                 final EReference eReference = (EReference) getSetting().getEStructuralFeature();
117:•                if (eReference.isContainment()) {
118:                         final MessageDialog dialog = getContainmentWarningDialog();
119:                         new ECPDialogExecutor(dialog) {
120:                                 @Override
121:                                 public void handleResult(int codeResult) {
122:                                         if (codeResult == Window.OK) {
123:                                                 addExistingElementsToReferenceService(getSetting().getEObject(), (EReference) getSetting()
124:                                                         .getEStructuralFeature());
125:                                         }
126:                                 }
127:                         }.execute();
128:                 }
129:                 addExistingElementsToReferenceService(getSetting().getEObject(), (EReference) getSetting()
130:                         .getEStructuralFeature());
131:         }
132:
133:         private void addExistingElementsToReferenceService(EObject eObject, EReference eReference) {
134:•                if (referenceService == null) {
135:                         return;
136:                 }
137:                 referenceService.addExistingModelElements(eObject, eReference);
138:         }
139:
140:         private MessageDialog getContainmentWarningDialog() {
141:                 return new MessageDialog(null,
142:                         LocalizationServiceHelper.getString(AddReferenceAction.class,
143:                                 ReferenceMessageKeys.AddReferenceAction_Confirmation),
144:                         null,
145:                         LocalizationServiceHelper.getString(AddReferenceAction.class,
146:                                 ReferenceMessageKeys.AddReferenceAction_Warning),
147:                         MessageDialog.WARNING,
148:                         new String[] {
149:                                 LocalizationServiceHelper.getString(AddReferenceAction.class,
150:                                         ReferenceMessageKeys.AddReferenceAction_Yes),
151:                                 LocalizationServiceHelper.getString(AddReferenceAction.class,
152:                                         ReferenceMessageKeys.AddReferenceAction_No) },
153:                         0);
154:         }
155: }