Skip to content

Package: SelectFeaturePathWizardPage

SelectFeaturePathWizardPage

nameinstructionbranchcomplexitylinemethod
SelectFeaturePathWizardPage(String, String, String, EClass, ISelection, SegmentGenerator, EStructuralFeatureSelectionValidator, boolean)
M: 0 C: 29
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 10
100%
M: 0 C: 1
100%
configureSegments(List)
M: 0 C: 21
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
createControl(Composite)
M: 0 C: 96
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 18
100%
M: 0 C: 1
100%
createSelectionChangedListener()
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%
createTreeViewer(Composite)
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
dispose()
M: 0 C: 9
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
getDomainModelReference()
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%
lambda$0(SelectionChangedEvent)
M: 1 C: 70
99%
M: 2 C: 8
80%
M: 2 C: 4
67%
M: 1 C: 19
95%
M: 0 C: 1
100%
setRootEClass(EClass)
M: 15 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2011-2019 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: * lucas - initial API and implementation
13: ******************************************************************************/
14: package org.eclipse.emf.ecp.view.internal.editor.handler;
15:
16: import java.util.LinkedList;
17: import java.util.List;
18:
19: import org.eclipse.emf.common.notify.AdapterFactory;
20: import org.eclipse.emf.ecore.EClass;
21: import org.eclipse.emf.ecore.EStructuralFeature;
22: import org.eclipse.emf.ecp.view.spi.editor.controls.EStructuralFeatureSelectionValidator;
23: import org.eclipse.emf.ecp.view.spi.model.VDomainModelReference;
24: import org.eclipse.emf.ecp.view.spi.model.VDomainModelReferenceSegment;
25: import org.eclipse.emf.ecp.view.spi.model.VViewFactory;
26: import org.eclipse.emf.edit.provider.ComposedAdapterFactory;
27: import org.eclipse.emf.edit.provider.ReflectiveItemProviderAdapterFactory;
28: import org.eclipse.emf.edit.ui.provider.AdapterFactoryLabelProvider;
29: import org.eclipse.jface.layout.GridDataFactory;
30: import org.eclipse.jface.layout.GridLayoutFactory;
31: import org.eclipse.jface.viewers.ISelection;
32: import org.eclipse.jface.viewers.ISelectionChangedListener;
33: import org.eclipse.jface.viewers.ITreeSelection;
34: import org.eclipse.jface.viewers.TreePath;
35: import org.eclipse.jface.viewers.TreeViewer;
36: import org.eclipse.jface.wizard.WizardPage;
37: import org.eclipse.swt.SWT;
38: import org.eclipse.swt.widgets.Composite;
39:
40: /**
41: * A wizard page that allows to create a segment based domain model reference by selecting the DMR's target feature in a
42: * tree view. The page automatically generates the segment path to the target feature.
43: *
44: * @author Lucas Koehler
45: */
46: public class SelectFeaturePathWizardPage extends WizardPage {
47:         private final VDomainModelReference domainModelReference;
48:         private EClass rootEClass;
49:         private final ISelection firstSelection;
50:         private ComposedAdapterFactory composedAdapterFactory;
51:         private AdapterFactoryLabelProvider labelProvider;
52:         private final boolean allowMultiReferencesInPath;
53:         private final SegmentGenerator segmentGenerator;
54:         private final EStructuralFeatureSelectionValidator selectionValidator;
55:         private TreeViewer treeViewer;
56:
57:         /**
58:          *
59:          * @param pageName
60:          * @param pageTitle
61:          * @param pageDescription
62:          * @param rootEClass
63:          * @param firstSelection
64:          * @param segmentGenerator
65:          * @param selectionValidator
66:          * @param allowMultiReferencesInPath <code>true</code>: Multi references are allowed in the middle of a
67:          * reference path; <code>false</code>: they are only allowed as the last path segment
68:          */
69:         // BEGIN COMPLEX CODE
70:         public SelectFeaturePathWizardPage(String pageName, String pageTitle, String pageDescription,
71:                 EClass rootEClass, ISelection firstSelection, SegmentGenerator segmentGenerator,
72:                 EStructuralFeatureSelectionValidator selectionValidator, boolean allowMultiReferencesInPath) {
73:                 // END COMPLEX CODE
74:                 super(pageName);
75:                 setTitle(pageTitle);
76:                 setDescription(pageDescription);
77:                 this.rootEClass = rootEClass;
78:                 this.firstSelection = firstSelection;
79:                 domainModelReference = VViewFactory.eINSTANCE.createDomainModelReference();
80:                 this.allowMultiReferencesInPath = allowMultiReferencesInPath;
81:                 this.segmentGenerator = segmentGenerator;
82:                 this.selectionValidator = selectionValidator;
83:         }
84:
85:         /**
86:          * @return The {@link VDomainModelReference} which is configured in this page
87:          */
88:         public VDomainModelReference getDomainModelReference() {
89:                 return domainModelReference;
90:         }
91:
92:         @Override
93:         public void createControl(Composite parent) {
94:                 final Composite composite = new Composite(parent, SWT.FILL);
95:                 GridLayoutFactory.fillDefaults().applyTo(composite);
96:                 GridDataFactory.fillDefaults().align(SWT.FILL, SWT.FILL).grab(true, true).applyTo(composite);
97:                 treeViewer = createTreeViewer(composite);
98:                 GridDataFactory.fillDefaults().align(SWT.FILL, SWT.FILL).grab(true, true).applyTo(treeViewer.getControl());
99:
100:                 composedAdapterFactory = new ComposedAdapterFactory(new AdapterFactory[] {
101:                         new ReflectiveItemProviderAdapterFactory(),
102:                         new ComposedAdapterFactory(ComposedAdapterFactory.Descriptor.Registry.INSTANCE) });
103:                 labelProvider = new AdapterFactoryLabelProvider(composedAdapterFactory);
104:                 final EStructuralFeatureContentProvider contentProvider = new EStructuralFeatureContentProvider(
105:                         allowMultiReferencesInPath);
106:
107:                 treeViewer.setContentProvider(contentProvider);
108:                 treeViewer.setLabelProvider(labelProvider);
109:                 treeViewer.setInput(rootEClass);
110:                 treeViewer.addSelectionChangedListener(createSelectionChangedListener());
111:                 treeViewer.setSelection(firstSelection, true);
112:
113:                 setControl(composite);
114:         }
115:
116:         /**
117:          * Creates the tree viewer of this wizard page. Overwrite this if you want to use custom style flags for the
118:          * {@link TreeViewer}.
119:          * <p>
120:          * <strong>Note:</strong> This method should only create the viewer but not configure anymore stuff like the label
121:          * provider.
122:          *
123:          * @param composite The {@link Composite} which will contain the tree viewer
124:          * @return The created {@link TreeViewer}
125:          */
126:         protected TreeViewer createTreeViewer(Composite composite) {
127:                 return new TreeViewer(composite);
128:         }
129:
130:         /**
131:          * (Re-)sets the root {@link EClass} of this wizard page. This clears the current selection.
132:          *
133:          * @param rootEClass The new root {@link EClass}
134:          */
135:         public void setRootEClass(EClass rootEClass) {
136:                 this.rootEClass = rootEClass;
137:                 domainModelReference.getSegments().clear();
138:                 setPageComplete(false);
139:                 treeViewer.setInput(rootEClass);
140:         }
141:
142:         /**
143:          * @return The {@link ISelectionChangedListener} for this page's {@link TreeViewer}.
144:          */
145:         protected ISelectionChangedListener createSelectionChangedListener() {
146:                 return event -> {
147:                         // Page is not complete until the opposite is proven
148:                         setPageComplete(false);
149:
150:                         // Get the selected element and create an EReference path from its tree path
151:                         final ITreeSelection treeSelection = (ITreeSelection) event.getSelection();
152:•                        if (treeSelection.getPaths().length < 1) {
153:                                 return;
154:                         }
155:
156:                         // Validate that a valid structural feature was selected
157:                         final EStructuralFeature structuralFeature = (EStructuralFeature) treeSelection.getFirstElement();
158:                         final String errorMessage = selectionValidator.isValid(structuralFeature);
159:                         setErrorMessage(errorMessage);
160:•                        if (errorMessage != null) {
161:                                 return;
162:                         }
163:
164:                         final TreePath treePath = treeSelection.getPaths()[0];
165:•                        if (treePath.getSegmentCount() < 1) {
166:                                 return;
167:                         }
168:                         final List<EStructuralFeature> bottomUpPath = new LinkedList<EStructuralFeature>();
169:
170:•                        for (int i = 0; i < treePath.getSegmentCount(); i++) {
171:                                 final Object o = treePath.getSegment(i);
172:                                 bottomUpPath.add((EStructuralFeature) o);
173:                         }
174:                         configureSegments(bottomUpPath);
175:
176:•                        if (!domainModelReference.getSegments().isEmpty()) {
177:                                 setPageComplete(true);
178:                         }
179:                 };
180:         }
181:
182:         /**
183:          * Generates segments from the given path and set them in this page's domain model reference.
184:          *
185:          * @param bottomUpPath Path to the selected feature (including it). The selected feature is the last element in
186:          * the list.
187:          */
188:         protected void configureSegments(List<EStructuralFeature> bottomUpPath) {
189:•                if (!domainModelReference.getSegments().isEmpty()) {
190:                         domainModelReference.getSegments().clear();
191:                 }
192:                 final List<VDomainModelReferenceSegment> segments = segmentGenerator.generateSegments(bottomUpPath);
193:                 domainModelReference.getSegments().addAll(segments);
194:         }
195:
196:         @Override
197:         public void dispose() {
198:                 composedAdapterFactory.dispose();
199:                 labelProvider.dispose();
200:                 super.dispose();
201:         }
202: }