Skip to content

Package: NewReferenceAction$1

NewReferenceAction$1

nameinstructionbranchcomplexitylinemethod
handleResult(int)
M: 12 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
{...}
M: 10 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2011-2013 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: package org.eclipse.emf.ecp.edit.spi.swt.reference;
16:
17: import java.net.MalformedURLException;
18: import java.net.URL;
19:
20: import org.eclipse.core.databinding.observable.value.IObservableValue;
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;
25: import org.eclipse.emf.ecore.EStructuralFeature.Setting;
26: import org.eclipse.emf.ecore.InternalEObject;
27: import org.eclipse.emf.ecp.edit.internal.swt.Activator;
28: import org.eclipse.emf.ecp.edit.internal.swt.reference.ReferenceMessageKeys;
29: import org.eclipse.emf.ecp.edit.internal.swt.util.OverlayImageDescriptor;
30: import org.eclipse.emf.ecp.edit.spi.ReferenceService;
31: import org.eclipse.emf.ecp.edit.spi.swt.actions.ECPSWTAction;
32: import org.eclipse.emf.ecp.edit.spi.swt.util.ECPDialogExecutor;
33: import org.eclipse.emf.ecp.view.spi.model.VDomainModelReference;
34: import org.eclipse.emf.edit.domain.EditingDomain;
35: import org.eclipse.emf.edit.provider.ComposedImage;
36: import org.eclipse.emfforms.spi.common.report.AbstractReport;
37: import org.eclipse.emfforms.spi.common.report.ReportService;
38: import org.eclipse.emfforms.spi.core.services.editsupport.EMFFormsEditSupport;
39: import org.eclipse.emfforms.spi.core.services.label.EMFFormsLabelProvider;
40: import org.eclipse.emfforms.spi.core.services.label.NoLabelFoundException;
41: import org.eclipse.emfforms.spi.localization.LocalizationServiceHelper;
42: import org.eclipse.jface.dialogs.MessageDialog;
43: import org.eclipse.jface.resource.ImageDescriptor;
44: import org.eclipse.jface.window.Window;
45: import org.eclipse.swt.graphics.ImageData;
46:
47: /**
48: * An Action for adding reference links to a model element. It is mainly used in the
49: * {@link org.eclipse.emf.ecp.edit.internal.swt.reference.ReferenceMultiControl ReferenceMultiControl}
50: *
51: * @author shterev
52: * @author Eugen Neufeld
53: * @since 1.5
54: */
55: public class NewReferenceAction extends ECPSWTAction {
56:
57:         private final ReferenceService referenceService;
58:
59:         /**
60:          * The constructor for a new reference action.
61:          *
62:          * @param editingDomain the {@link EditingDomain} to use
63:          * @param setting the {@link Setting} to use
64:          * @param editSupport the {@link EMFFormsEditSupport} to use
65:          * @param labelProvider the {@link EMFFormsLabelProvider} to use
66:          * @param referenceService the {@link ReferenceService} to use
67:          * @param reportService the {@link ReportService} to use
68:          * @param domainModelReference the {@link VDomainModelReference} to use
69:          * @param domainModel the domain model of the given {@link VDomainModelReference}
70:          * @since 1.6
71:          *
72:          */
73:         public NewReferenceAction(EditingDomain editingDomain, Setting setting, EMFFormsEditSupport editSupport,
74:                 EMFFormsLabelProvider labelProvider, ReferenceService referenceService, ReportService reportService,
75:                 VDomainModelReference domainModelReference, EObject domainModel) {
76:                 super(editingDomain, setting);
77:                 this.referenceService = referenceService;
78:                 Object obj = null;
79:                 final EReference eReference = (EReference) getSetting().getEStructuralFeature();
80:                 // Only create a temporary object in order to get the correct icon from the label provider
81:                 // the actual ME is created later on.
82:                 if (!eReference.getEReferenceType().isAbstract()) {
83:                         obj = eReference.getEReferenceType().getEPackage().getEFactoryInstance()
84:                                 .create(eReference.getEReferenceType());
85:                 }
86:                 Object labelProviderImageResult = editSupport.getImage(domainModelReference, domainModel, obj);
87:
88:                 ImageData imageData = null;
89:
90:                 if (ComposedImage.class.isInstance(labelProviderImageResult)) {
91:                         labelProviderImageResult = ((ComposedImage) labelProviderImageResult).getImages().get(0);
92:                 }
93:                 if (URI.class.isInstance(labelProviderImageResult)) {
94:                         try {
95:                                 labelProviderImageResult = new URL(((URI) labelProviderImageResult).toString());
96:                         } catch (final MalformedURLException ex) {
97:                                 Activator.logException(ex);
98:                         }
99:                 }
100:                 if (URL.class.isInstance(labelProviderImageResult)) {
101:                         imageData = Activator.getImageData((URL) labelProviderImageResult);
102:                 } else {
103:                         imageData = Activator.getImageData((URL) null);
104:                 }
105:
106:                 final ImageDescriptor addOverlay = Activator.getImageDescriptor("icons/add_overlay.png");//$NON-NLS-1$
107:                 final OverlayImageDescriptor imageDescriptor = new OverlayImageDescriptor(imageData, addOverlay,
108:                         OverlayImageDescriptor.LOWER_RIGHT);
109:                 setImageDescriptor(imageDescriptor);
110:
111:                 String attribute;
112:                 try {
113:                         final IObservableValue displayName = labelProvider.getDisplayName(domainModelReference, domainModel);
114:                         attribute = (String) displayName.getValue();
115:                         displayName.dispose();
116:                 } catch (final NoLabelFoundException ex) {
117:                         reportService.report(new AbstractReport(ex));
118:                         setToolTipText(ex.getMessage());
119:                         return;
120:                 }
121:                 // TODO language, same text as in addreference
122:                 // make singular attribute labels
123:                 if (attribute.endsWith("ies")) {//$NON-NLS-1$
124:                         attribute = attribute.substring(0, attribute.length() - 3) + "y";//$NON-NLS-1$
125:                 } else if (attribute.endsWith("s")) {//$NON-NLS-1$
126:                         attribute = attribute.substring(0, attribute.length() - 1);
127:                 }
128:                 setToolTipText(LocalizationServiceHelper.getString(NewReferenceAction.class,
129:                         ReferenceMessageKeys.NewReferenceAction_CreateAndLinkNew)
130:                         + attribute);
131:         }
132:
133:         /**
134:          * The constructor for a new reference action.
135:          *
136:          * @param editingDomain The {@link EditingDomain} to use
137:          * @param eObject The {@link EObject} to use
138:          * @param structuralFeature The {@link EStructuralFeature} defining which feature of the {@link EObject} is used
139:          * @param editSupport The {@link EMFFormsEditSupport} to use
140:          * @param labelProvider the {@link EMFFormsLabelProvider} to use
141:          * @param referenceService The {@link ReferenceService} to use
142:          * @param reportService The {@link ReportService} to use
143:          * @param domainModelReference the {@link VDomainModelReference} to use
144:          * @param domainModel the domain model of the given {@link VDomainModelReference}
145:          * @since 1.6
146:          */
147:         public NewReferenceAction(EditingDomain editingDomain, EObject eObject, EStructuralFeature structuralFeature,
148:                 EMFFormsEditSupport editSupport, EMFFormsLabelProvider labelProvider, ReferenceService referenceService,
149:                 ReportService reportService, VDomainModelReference domainModelReference, EObject domainModel) {
150:                 this(editingDomain, ((InternalEObject) eObject).eSetting(structuralFeature), editSupport, labelProvider,
151:                         referenceService, reportService, domainModelReference, domainModel);
152:         }
153:
154:         /**
155:          * Default constructor.
156:          *
157:          * @param modelElement
158:          * the source model element
159:          * @param eReference
160:          * the target reference
161:          * @param descriptor
162:          * the descriptor used to generate display content
163:          * @param modelElementContext
164:          * the model element context
165:          */
166:         // public NewReferenceAction(EObject modelElement, EReference eReference, IItemPropertyDescriptor descriptor,
167:         // EditModelElementContext modelElementContext, Shell shell, AdapterFactoryLabelProvider labelProvider) {
168:         // this.modelElement = modelElement;
169:         // this.eReference = eReference;
170:         // this.modelElementContext = modelElementContext;
171:         // this.shell = shell;
172:         //
173:         // Object obj = null;
174:         // // Only create a temporary object in order to get the correct icon from the label provider
175:         // // the actual ME is created later on.
176:         // if (!eReference.getEReferenceType().isAbstract()) {
177:         // obj = eReference.getEReferenceType().getEPackage().getEFactoryInstance()
178:         // .create(eReference.getEReferenceType());
179:         // }
180:         // Image image = labelProvider.getImage(obj);
181:         //
182:         // ImageDescriptor addOverlay = Activator.getImageDescriptor("icons/add_overlay.png");//$NON-NLS-1$
183:         // OverlayImageDescriptor imageDescriptor = new OverlayImageDescriptor(image, addOverlay,
184:         // OverlayImageDescriptor.LOWER_RIGHT);
185:         // setImageDescriptor(imageDescriptor);
186:         //
187:         // String attribute = descriptor.getDisplayName(eReference);
188:         //
189:         // // make singular attribute labels
190:         // if (attribute.endsWith("ies")) {//$NON-NLS-1$
191:         // attribute = attribute.substring(0, attribute.length() - 3) + "y";//$NON-NLS-1$
192:         // } else if (attribute.endsWith("s")) {//$NON-NLS-1$
193:         // attribute = attribute.substring(0, attribute.length() - 1);
194:         // }
195:         // setToolTipText(ActionMessages.NewReferenceAction_CreateAndLinkNew + attribute);
196:         //
197:         // }
198:
199:         /**
200:          * {@inheritDoc}
201:          */
202:         @Override
203:         public void run() {
204:                 // checks if we try to create a container for ourself, this is not allowed
205:                 final EReference eReference = (EReference) getSetting().getEStructuralFeature();
206:                 if (eReference.isContainment() && getSetting().getEObject().eIsSet(eReference)) {
207:                         final MessageDialog dialog = getContainmentWarningDialog();
208:                         new ECPDialogExecutor(dialog) {
209:                                 @Override
210:                                 public void handleResult(int codeResult) {
211:•                                        if (codeResult == Window.OK) {
212:                                                 addNewElementsToReferenceService(getSetting().getEObject(), eReference);
213:                                         }
214:                                 }
215:                         }.execute();
216:                 }
217:                 addNewElementsToReferenceService(getSetting().getEObject(), eReference);
218:         }
219:
220:         private void addNewElementsToReferenceService(EObject eObject, EReference eReference) {
221:                 if (referenceService == null) {
222:                         return;
223:                 }
224:                 referenceService.addNewModelElements(eObject, eReference, true);
225:         }
226:
227:         private MessageDialog getContainmentWarningDialog() {
228:                 return new MessageDialog(null,
229:                         LocalizationServiceHelper.getString(NewReferenceAction.class,
230:                                 ReferenceMessageKeys.NewReferenceAction_Confirmation),
231:                         null,
232:                         LocalizationServiceHelper.getString(NewReferenceAction.class,
233:                                 ReferenceMessageKeys.NewReferenceAction_Warning),
234:                         MessageDialog.WARNING,
235:                         new String[] {
236:                                 LocalizationServiceHelper.getString(NewReferenceAction.class,
237:                                         ReferenceMessageKeys.NewReferenceAction_Yes),
238:                                 LocalizationServiceHelper.getString(NewReferenceAction.class,
239:                                         ReferenceMessageKeys.NewReferenceAction_No) },
240:                         0);
241:         }
242: }