Skip to content

Package: CreateDomainModelReferenceWizard$CustomizeDomainModelReferencePage

CreateDomainModelReferenceWizard$CustomizeDomainModelReferencePage

nameinstructionbranchcomplexitylinemethod
CreateDomainModelReferenceWizard.CustomizeDomainModelReferencePage(CreateDomainModelReferenceWizard, String, String, String, VControl)
M: 0 C: 22
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 7
100%
M: 0 C: 1
100%
clear()
M: 0 C: 28
100%
M: 2 C: 4
67%
M: 2 C: 2
50%
M: 0 C: 4
100%
M: 0 C: 1
100%
createControl(Composite)
M: 0 C: 31
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 7
100%
M: 0 C: 1
100%
dispose()
M: 0 C: 10
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
getvControl()
M: 0 C: 3
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
isValueSet()
M: 12 C: 39
76%
M: 2 C: 6
75%
M: 2 C: 3
60%
M: 3 C: 13
81%
M: 0 C: 1
100%
render()
M: 3 C: 47
94%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 1 C: 11
92%
M: 0 C: 1
100%
setEClass(EClass)
M: 0 C: 9
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 4
100%
M: 0 C: 1
100%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2011-2014 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: * Alexandra Buzila - initial API and implementation
13: ******************************************************************************/
14: package org.eclipse.emf.ecp.view.internal.editor.handler;
15:
16: import org.eclipse.emf.common.command.BasicCommandStack;
17: import org.eclipse.emf.common.command.Command;
18: import org.eclipse.emf.common.notify.Adapter;
19: import org.eclipse.emf.common.notify.Notification;
20: import org.eclipse.emf.common.notify.impl.AdapterImpl;
21: import org.eclipse.emf.common.util.URI;
22: import org.eclipse.emf.ecore.EClass;
23: import org.eclipse.emf.ecore.EObject;
24: import org.eclipse.emf.ecore.EStructuralFeature;
25: import org.eclipse.emf.ecore.EStructuralFeature.Setting;
26: import org.eclipse.emf.ecore.resource.Resource;
27: import org.eclipse.emf.ecore.resource.ResourceSet;
28: import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
29: import org.eclipse.emf.ecore.util.EcoreUtil;
30: import org.eclipse.emf.ecp.spi.common.ui.SelectModelElementWizard;
31: import org.eclipse.emf.ecp.ui.view.ECPRendererException;
32: import org.eclipse.emf.ecp.ui.view.swt.DefaultReferenceService;
33: import org.eclipse.emf.ecp.ui.view.swt.ECPSWTViewRenderer;
34: import org.eclipse.emf.ecp.view.internal.editor.controls.Activator;
35: import org.eclipse.emf.ecp.view.spi.context.ViewModelContext;
36: import org.eclipse.emf.ecp.view.spi.context.ViewModelContextFactory;
37: import org.eclipse.emf.ecp.view.spi.custom.model.VCustomDomainModelReference;
38: import org.eclipse.emf.ecp.view.spi.model.VControl;
39: import org.eclipse.emf.ecp.view.spi.model.VDomainModelReference;
40: import org.eclipse.emf.ecp.view.spi.model.VView;
41: import org.eclipse.emf.ecp.view.spi.model.VViewFactory;
42: import org.eclipse.emf.ecp.view.spi.provider.ViewProviderHelper;
43: import org.eclipse.emf.ecp.view.spi.table.model.VTableDomainModelReference;
44: import org.eclipse.emf.edit.command.AddCommand;
45: import org.eclipse.emf.edit.command.SetCommand;
46: import org.eclipse.emf.edit.domain.AdapterFactoryEditingDomain;
47: import org.eclipse.emf.edit.domain.EditingDomain;
48: import org.eclipse.emf.edit.provider.ComposedAdapterFactory;
49: import org.eclipse.emfforms.spi.core.services.databinding.DatabindingFailedException;
50: import org.eclipse.emfforms.spi.core.services.databinding.DatabindingFailedReport;
51: import org.eclipse.jface.layout.GridDataFactory;
52: import org.eclipse.jface.layout.GridLayoutFactory;
53: import org.eclipse.jface.wizard.IWizardPage;
54: import org.eclipse.jface.wizard.WizardPage;
55: import org.eclipse.swt.SWT;
56: import org.eclipse.swt.widgets.Composite;
57: import org.eclipse.swt.widgets.Control;
58:
59: /**
60: * @author Alexandra Buzila
61: *
62: */
63: public class CreateDomainModelReferenceWizard extends SelectModelElementWizard {
64:
65:         private CustomizeDomainModelReferencePage customizeDMRPage;
66:         private WizardPageExtension firstPage;
67:         private final EClass eclass;
68:         private final EditingDomain editingDomain;
69:         private final EObject eObject;
70:         private final EStructuralFeature structuralFeature;
71:         private final VDomainModelReference domainModelReference;
72:
73:         /**
74:          * A wizard used for creating a new DomainModelReference.
75:          *
76:          * @param setting - the setting to use
77:          * @param editingDomain - the setting's editing domain
78:          * @param eclass - the root EClass of the VView the setting belongs to
79:          * @param windowTitle - title for the wizard window
80:          * @param pageName - the name of the page
81:          * @param pageTitle - the title of the page
82:          * @param description - the description
83:          * @param domainModelReference - the domain model reference
84:          */
85:         public CreateDomainModelReferenceWizard(final Setting setting, final EditingDomain editingDomain,
86:                 final EClass eclass, final String windowTitle,
87:                 final String pageName, String pageTitle, String description, VDomainModelReference domainModelReference) {
88:                 this(setting.getEObject(), setting.getEStructuralFeature(), editingDomain, eclass, windowTitle, pageName,
89:                         pageTitle, description, domainModelReference);
90:         }
91:
92:         /**
93:          * A wizard used for creating a new DomainModelReference.
94:          *
95:          * @param eObject The {@link EObject} to use
96:          * @param structuralFeature The corresponding {@link EStructuralFeature}
97:          * @param editingDomain - the setting's editing domain
98:          * @param eclass - the root EClass of the VView the setting belongs to
99:          * @param windowTitle - title for the wizard window
100:          * @param pageName - the name of the page
101:          * @param pageTitle - the title of the page
102:          * @param description - the description
103:          * @param domainModelReference - the domain model reference
104:          */
105:         public CreateDomainModelReferenceWizard(final EObject eObject, final EStructuralFeature structuralFeature,
106:                 final EditingDomain editingDomain, final EClass eclass, final String windowTitle, final String pageName,
107:                 String pageTitle, String description, VDomainModelReference domainModelReference) {
108:                 super(windowTitle, pageName, pageTitle, description);
109:                 this.eObject = eObject;
110:                 this.structuralFeature = structuralFeature;
111:                 this.editingDomain = editingDomain;
112:                 this.eclass = eclass;
113:                 this.domainModelReference = domainModelReference;
114:         }
115:
116:         /**
117:          * {@inheritDoc}
118:          */
119:
120:         @Override
121:         public void addPages() {
122:
123:                 customizeDMRPage = new CustomizeDomainModelReferencePage(
124:                         "New Domain Model Reference", "Select an EStructuralFeature", //$NON-NLS-1$ //$NON-NLS-2$
125:                         "Select a domain model EStructuralFeature for the domain model reference.", getDummyControl()); //$NON-NLS-1$
126:
127:                 if (domainModelReference == null) {
128:                         firstPage = new WizardPageExtension(getPageName());
129:                         firstPage.setTitle(getPageTitle());
130:                         firstPage.setDescription(getDescription());
131:                         addPage(firstPage);
132:                 } else {
133:                         customizeDMRPage.setEClass(domainModelReference.eClass());
134:                         if (VCustomDomainModelReference.class.isInstance(domainModelReference)) {
135:                                 customizeDMRPage.setTitle("Select a Custom Domain Model Reference"); //$NON-NLS-1$
136:                                 customizeDMRPage
137:                                         .setDescription("Please specify the bundle and class name for the Custom Domain Model Reference."); //$NON-NLS-1$
138:                         }
139:                 }
140:
141:                 addPage(customizeDMRPage);
142:         }
143:
144:         /**
145:          * @return
146:          */
147:         private VControl getDummyControl() {
148:                 final VView domainModelView = VViewFactory.eINSTANCE.createView();
149:                 domainModelView.setRootEClass(eclass);
150:                 final VControl control = VViewFactory.eINSTANCE.createControl();
151:                 domainModelView.getChildren().add(control);
152:                 addDomainToResource(domainModelView);
153:
154:                 return control;
155:         }
156:
157:         /**
158:          * @param domainModelView
159:          */
160:         private void addDomainToResource(VView domainModelView) {
161:                 final ResourceSet rs = new ResourceSetImpl();
162:                 final AdapterFactoryEditingDomain domain = new AdapterFactoryEditingDomain(
163:                         new ComposedAdapterFactory(ComposedAdapterFactory.Descriptor.Registry.INSTANCE),
164:                         new BasicCommandStack(), rs);
165:                 rs.eAdapters().add(new AdapterFactoryEditingDomain.EditingDomainProvider(domain));
166:                 final Resource resource = rs.createResource(URI.createURI("VIRTUAL_URI")); //$NON-NLS-1$
167:                 resource.getContents().add(domainModelView);
168:         }
169:
170:         /**
171:          * {@inheritDoc}
172:          *
173:          * @see org.eclipse.jface.wizard.Wizard#getNextPage(org.eclipse.jface.wizard.IWizardPage)
174:          */
175:         @Override
176:         public IWizardPage getNextPage(IWizardPage page) {
177:                 if (page == firstPage) {
178:                         final Object[] selection = getCompositeProvider().getSelection();
179:                         if (selection == null || selection.length == 0) {
180:                                 return null;
181:                         }
182:                         final EClass eClass = (EClass) selection[0];
183:                         customizeDMRPage.setEClass(eClass);
184:                         return customizeDMRPage;
185:                 }
186:                 return null;
187:         }
188:
189:         /**
190:          * {@inheritDoc}
191:          *
192:          * @see org.eclipse.emf.ecp.spi.common.ui.SelectModelElementWizard#performFinish()
193:          */
194:         @Override
195:         public boolean performFinish() {
196:                 if (!canFinish()) {
197:                         return false;
198:                 }
199:                 Command command = null;
200:                 if (structuralFeature.isMany()) {
201:                         command = AddCommand.create(editingDomain, eObject,
202:                                 structuralFeature, customizeDMRPage.getvControl().getDomainModelReference());
203:                 } else {
204:                         command = SetCommand.create(editingDomain, eObject,
205:                                 structuralFeature, customizeDMRPage.getvControl().getDomainModelReference());
206:                 }
207:                 editingDomain.getCommandStack().execute(command);
208:                 return super.performFinish();
209:         }
210:
211:         /**
212:          * {@inheritDoc}
213:          *
214:          * @see org.eclipse.jface.wizard.Wizard#canFinish()
215:          */
216:         @Override
217:         public boolean canFinish() {
218:                 if (customizeDMRPage == null) {
219:                         return false;
220:                 }
221:                 if (customizeDMRPage.getvControl() == null) {
222:                         return false;
223:                 }
224:                 if (customizeDMRPage.getvControl().getDomainModelReference() == null) {
225:                         return false;
226:                 }
227:
228:                 if (VCustomDomainModelReference.class.isInstance(customizeDMRPage.getvControl().getDomainModelReference())) {
229:                         final VCustomDomainModelReference customDMR = (VCustomDomainModelReference) customizeDMRPage.getvControl()
230:                                 .getDomainModelReference();
231:                         if (customDMR.getBundleName() != null && customDMR.getClassName() != null) {
232:                                 return true;
233:                         }
234:                         return false;
235:                 }
236:                 VDomainModelReference dmrToCheck = customizeDMRPage.getvControl().getDomainModelReference();
237:                 if (VTableDomainModelReference.class.isInstance(dmrToCheck)) {
238:                         final VTableDomainModelReference tableDomainModelReference = VTableDomainModelReference.class
239:                                 .cast(dmrToCheck);
240:                         if (tableDomainModelReference.getDomainModelReference() != null) {
241:                                 dmrToCheck = tableDomainModelReference.getDomainModelReference();
242:                         }
243:                 }
244:                 try {
245:                         Activator.getDefault().getEMFFormsDatabinding()
246:                                 .getValueProperty(dmrToCheck, eclass);
247:                 } catch (final DatabindingFailedException ex) {
248:                         // Activator.getDefault().getReportService().report(new DatabindingFailedReport(ex));
249:                         return false;
250:                 }
251:
252:                 return super.canFinish();
253:         }
254:
255:         /** Wizard page containing the control for setting a DomainModelReference. */
256:         private class CustomizeDomainModelReferencePage extends WizardPage {
257:
258:                 private EClass dmrEClass;
259:                 private Composite composite;
260:                 private final VControl vControl;
261:                 private final Adapter adapter;
262:
263:                 /**
264:                  * @param pageName
265:                  * @param domainModelEClass
266:                  */
267:                 protected CustomizeDomainModelReferencePage(String pageName, String pageTitle, String pageDescription,
268:                         VControl vControl) {
269:                         super(pageName);
270:                         setTitle(pageTitle);
271:                         setDescription(pageDescription);
272:                         this.vControl = vControl;
273:                         adapter = new AdapterImpl() {
274:
275:                                 /**
276:                                  * {@inheritDoc}
277:                                  *
278:                                  * @see org.eclipse.emf.common.notify.impl.AdapterImpl#notifyChanged(org.eclipse.emf.common.notify.Notification)
279:                                  */
280:                                 @Override
281:                                 public void notifyChanged(Notification msg) {
282:                                         super.notifyChanged(msg);
283:                                         setPageComplete(isValueSet());
284:                                 }
285:
286:                         };
287:                 }
288:
289:                 /**
290:                  * {@inheritDoc}
291:                  *
292:                  * @see org.eclipse.jface.dialogs.DialogPage#dispose()
293:                  */
294:                 @Override
295:                 public void dispose() {
296:                         vControl.eAdapters().remove(adapter);
297:                         super.dispose();
298:                 }
299:
300:                 public void setEClass(EClass dmrEClass) {
301:                         this.dmrEClass = dmrEClass;
302:•                        if (isControlCreated()) {
303:                                 render();
304:                         }
305:                 }
306:
307:                 /**
308:                  *
309:                  */
310:                 private void render() {
311:                         clear();
312:
313:                         final VDomainModelReference dmr = (VDomainModelReference) EcoreUtil.create(dmrEClass);
314:                         getvControl().setDomainModelReference(dmr);
315:                         getvControl().getDomainModelReference().eAdapters().add(adapter);
316:                         final VView view = ViewProviderHelper.getView(dmr, null);
317:                         final ViewModelContext viewContext = ViewModelContextFactory.INSTANCE
318:                                 .createViewModelContext(view, dmr, new DefaultReferenceService());
319:                         try {
320:                                 ECPSWTViewRenderer.INSTANCE.render(composite, viewContext);
321:                                 composite.layout();
322:                         } catch (final ECPRendererException ex) {
323:                                 ex.printStackTrace();
324:                         }
325:                 }
326:
327:                 /**
328:                  *
329:                  */
330:                 private void clear() {
331:•                        if (composite != null && !composite.isDisposed()) {
332:•                                for (final Control c : composite.getChildren()) {
333:                                         c.dispose();
334:                                 }
335:                         }
336:
337:                 }
338:
339:                 /**
340:                  * {@inheritDoc}
341:                  *
342:                  * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
343:                  */
344:                 @Override
345:                 public void createControl(Composite parent) {
346:                         composite = new Composite(parent, SWT.FILL);
347:                         GridLayoutFactory.fillDefaults().applyTo(composite);
348:                         GridDataFactory.fillDefaults().align(SWT.FILL, SWT.FILL).grab(true, true).applyTo(composite);
349:                         setControl(composite);
350:
351:•                        if (dmrEClass != null) {
352:                                 render();
353:                         }
354:
355:                 }
356:
357:                 /**
358:                  * @return the vControl
359:                  */
360:                 public VControl getvControl() {
361:                         return vControl;
362:                 }
363:
364:                 private boolean isValueSet() {
365:•                        if (getvControl() == null) {
366:                                 return false;
367:                         }
368:•                        if (getvControl().getDomainModelReference() == null) {
369:                                 return false;
370:                         }
371:                         VDomainModelReference dmrToCheck = getvControl().getDomainModelReference();
372:•                        if (VTableDomainModelReference.class.isInstance(dmrToCheck)) {
373:                                 final VTableDomainModelReference tableDomainModelReference = VTableDomainModelReference.class
374:                                         .cast(dmrToCheck);
375:•                                if (tableDomainModelReference.getDomainModelReference() != null) {
376:                                         dmrToCheck = tableDomainModelReference.getDomainModelReference();
377:                                 }
378:                         }
379:                         try {
380:                                 Activator.getDefault().getEMFFormsDatabinding()
381:                                         .getValueProperty(dmrToCheck, eclass);
382:                         } catch (final DatabindingFailedException ex) {
383:                                 Activator.getDefault().getReportService().report(new DatabindingFailedReport(ex));
384:                                 return false;
385:                         }
386:
387:                         return true;
388:                 }
389:
390:         }
391:
392: }