Skip to content

Package: SelectAttributesWizardPage$1

SelectAttributesWizardPage$1

nameinstructionbranchcomplexitylinemethod
checkStateChanged(CheckStateChangedEvent)
M: 8 C: 24
75%
M: 2 C: 2
50%
M: 2 C: 1
33%
M: 1 C: 6
86%
M: 0 C: 1
100%
{...}
M: 0 C: 6
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
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 java.util.ArrayList;
17: import java.util.LinkedHashSet;
18: import java.util.List;
19: import java.util.Set;
20:
21: import org.eclipse.core.databinding.property.value.IValueProperty;
22: import org.eclipse.emf.common.notify.AdapterFactory;
23: import org.eclipse.emf.common.util.TreeIterator;
24: import org.eclipse.emf.ecore.EClass;
25: import org.eclipse.emf.ecore.EObject;
26: import org.eclipse.emf.ecore.EStructuralFeature;
27: import org.eclipse.emf.ecp.view.internal.editor.controls.Activator;
28: import org.eclipse.emf.ecp.view.spi.model.VDomainModelReference;
29: import org.eclipse.emf.ecp.view.spi.model.VView;
30: import org.eclipse.emf.edit.provider.ComposedAdapterFactory;
31: import org.eclipse.emf.edit.provider.ReflectiveItemProviderAdapterFactory;
32: import org.eclipse.emf.edit.ui.provider.AdapterFactoryLabelProvider;
33: import org.eclipse.emfforms.spi.core.services.databinding.DatabindingFailedException;
34: import org.eclipse.emfforms.spi.core.services.databinding.DatabindingFailedReport;
35: import org.eclipse.jface.layout.GridDataFactory;
36: import org.eclipse.jface.layout.GridLayoutFactory;
37: import org.eclipse.jface.viewers.ArrayContentProvider;
38: import org.eclipse.jface.viewers.CheckStateChangedEvent;
39: import org.eclipse.jface.viewers.CheckboxTableViewer;
40: import org.eclipse.jface.viewers.ICheckStateListener;
41: import org.eclipse.jface.wizard.IWizardPage;
42: import org.eclipse.jface.wizard.WizardPage;
43: import org.eclipse.swt.SWT;
44: import org.eclipse.swt.custom.ScrolledComposite;
45: import org.eclipse.swt.events.SelectionAdapter;
46: import org.eclipse.swt.events.SelectionEvent;
47: import org.eclipse.swt.graphics.Point;
48: import org.eclipse.swt.layout.GridLayout;
49: import org.eclipse.swt.widgets.Button;
50: import org.eclipse.swt.widgets.Composite;
51: import org.eclipse.swt.widgets.Control;
52: import org.eclipse.swt.widgets.Label;
53:
54: /**
55: * @author Alexandra Buzila
56: *
57: */
58: public class SelectAttributesWizardPage extends WizardPage {
59:
60:         private EClass rootClass;
61:         private AdapterFactoryLabelProvider labelProvider;
62:         private ComposedAdapterFactory composedAdapterFactory;
63:         private final Set<EStructuralFeature> selectedFeatures = new LinkedHashSet<EStructuralFeature>();
64:         private VView view;
65:         private Composite parent;
66:         private Composite composite;
67:         private CheckboxTableViewer tvAttributes;
68:         private Button bUnreferenced;
69:
70:         /**
71:          * Default constructor.
72:          */
73:         protected SelectAttributesWizardPage() {
74:                 super("Select Attributes");
75:         }
76:
77:         /**
78:          * {@inheritDoc}
79:          *
80:          * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
81:          */
82:         @Override
83:         public void createControl(Composite parent) {
84:                 this.parent = parent;
85:                 composite = new Composite(parent, SWT.NONE);
86:                 final GridLayout layout = new GridLayout();
87:                 composite.setLayout(layout);
88:                 setControl(composite);
89:                 composedAdapterFactory = new ComposedAdapterFactory(new AdapterFactory[] {
90:                         new ReflectiveItemProviderAdapterFactory(),
91:                         new ComposedAdapterFactory(ComposedAdapterFactory.Descriptor.Registry.INSTANCE) });
92:
93:                 labelProvider = new AdapterFactoryLabelProvider(composedAdapterFactory);
94:
95:                 bUnreferenced = new Button(composite, SWT.CHECK);
96:                 bUnreferenced.setText("Show only unreferenced Attributes?"); //$NON-NLS-1$
97:                 GridDataFactory.fillDefaults().align(SWT.FILL, SWT.BEGINNING).grab(true, false).span(2, 1)
98:                         .applyTo(bUnreferenced);
99:
100:                 final Composite tableComposite = new Composite(composite, SWT.FILL);
101:                 tableComposite.setLayout(GridLayoutFactory.fillDefaults().create());
102:                 GridDataFactory.fillDefaults().align(SWT.FILL, SWT.FILL).grab(true, true).applyTo(tableComposite);
103:                 tableComposite.setBackground(composite.getBackground());
104:
105:                 tvAttributes = CheckboxTableViewer.newCheckList(tableComposite, SWT.NONE);
106:                 tvAttributes.setLabelProvider(labelProvider);
107:                 tvAttributes.setContentProvider(ArrayContentProvider.getInstance());
108:                 GridDataFactory.fillDefaults().align(SWT.FILL, SWT.FILL).grab(true, true).hint(SWT.DEFAULT, 200)
109:                         .applyTo(tvAttributes.getControl());
110:
111:                 tvAttributes.addCheckStateListener(new ICheckStateListener() {
112:
113:                         @Override
114:                         public void checkStateChanged(CheckStateChangedEvent event) {
115:                                 final EStructuralFeature object = (EStructuralFeature) event.getElement();
116:•                                if (event.getChecked()) {
117:                                         selectedFeatures.add(object);
118:
119:                                 } else {
120:                                         selectedFeatures.remove(object);
121:                                 }
122:•                                setPageComplete(!selectedFeatures.isEmpty());
123:
124:                         }
125:                 });
126:
127:                 List<EStructuralFeature> attributes = null;
128:                 if (rootClass != null) {
129:                         if (!bUnreferenced.getSelection()) {
130:                                 attributes = rootClass.getEAllStructuralFeatures();
131:                         } else {
132:                                 attributes = getUnreferencedSegmentAttributes(rootClass);
133:                         }
134:                 }
135:                 tvAttributes.setInput(attributes);
136:
137:                 bUnreferenced.addSelectionListener(new SelectionAdapter() {
138:
139:                         @Override
140:                         public void widgetSelected(SelectionEvent e) {
141:                                 List<EStructuralFeature> attributes = null;
142:                                 if (rootClass != null) {
143:                                         if (!bUnreferenced.getSelection()) {
144:                                                 attributes = rootClass.getEAllStructuralFeatures();
145:                                         } else {
146:                                                 attributes = getUnreferencedSegmentAttributes(rootClass);
147:                                         }
148:                                 }
149:                                 tvAttributes.setInput(attributes);
150:                         }
151:
152:                 });
153:
154:                 final Composite compositeButtons = new Composite(composite, SWT.NONE);
155:                 GridLayoutFactory.fillDefaults().numColumns(2).equalWidth(true).applyTo(compositeButtons);
156:                 GridDataFactory.fillDefaults().align(SWT.FILL, SWT.BEGINNING).grab(true, false).span(2, 1)
157:                         .applyTo(compositeButtons);
158:
159:                 final Button bSelectAll = new Button(compositeButtons, SWT.PUSH);
160:                 bSelectAll.setText("Select All"); //$NON-NLS-1$
161:                 bSelectAll.addSelectionListener(new SelectionAdapter() {
162:
163:                         @Override
164:                         public void widgetSelected(SelectionEvent e) {
165:                                 @SuppressWarnings("unchecked")
166:                                 final List<EStructuralFeature> segments = (List<EStructuralFeature>) tvAttributes.getInput();
167:                                 tvAttributes.setAllChecked(true);
168:                                 selectedFeatures.addAll(segments);
169:                                 setPageComplete(!selectedFeatures.isEmpty());
170:                         }
171:
172:                 });
173:                 GridDataFactory.fillDefaults().align(SWT.FILL, SWT.BEGINNING).grab(true, false).applyTo(bSelectAll);
174:                 final Button bDeSelectAll = new Button(compositeButtons, SWT.PUSH);
175:                 bDeSelectAll.setText("Deselect All"); //$NON-NLS-1$
176:                 bDeSelectAll.addSelectionListener(new SelectionAdapter() {
177:
178:                         @Override
179:                         public void widgetSelected(SelectionEvent e) {
180:                                 @SuppressWarnings("unchecked")
181:                                 final List<EStructuralFeature> segments = (List<EStructuralFeature>) tvAttributes.getInput();
182:                                 tvAttributes.setAllChecked(false);
183:                                 selectedFeatures.removeAll(segments);
184:                                 setPageComplete(!selectedFeatures.isEmpty());
185:                         }
186:
187:                 });
188:                 GridDataFactory.fillDefaults().align(SWT.FILL, SWT.BEGINNING).grab(true, false).applyTo(bDeSelectAll);
189:                 parent.layout(true);
190:         }
191:
192:         public void onEnterPage() {
193:                 /* if (isCurrentPage()) */ {
194:                         // clear composite
195:                         clear();
196:                         composite = new Composite(parent, SWT.NONE);
197:                         final GridLayout layout = new GridLayout();
198:                         composite.setLayout(layout);
199:                         layout.numColumns = 2;
200:                         setControl(composite);
201:                         composedAdapterFactory = new ComposedAdapterFactory(new AdapterFactory[] {
202:                                 new ReflectiveItemProviderAdapterFactory(),
203:                                 new ComposedAdapterFactory(ComposedAdapterFactory.Descriptor.Registry.INSTANCE) });
204:
205:                         labelProvider = new AdapterFactoryLabelProvider(composedAdapterFactory);
206:
207:                         final Button bUnreferenced = new Button(composite, SWT.CHECK);
208:                         bUnreferenced.setText("Show only unreferenced Attributes?"); //$NON-NLS-1$
209:                         GridDataFactory.fillDefaults().align(SWT.FILL, SWT.BEGINNING).grab(true, false).span(2, 1)
210:                                 .applyTo(bUnreferenced);
211:
212:                         final Label labelAttributes = new Label(composite, SWT.NONE);
213:                         labelAttributes.setText("Select Attributes"); //$NON-NLS-1$
214:
215:                         final ScrolledComposite scrolledComposite = new ScrolledComposite(composite, SWT.H_SCROLL | SWT.V_SCROLL
216:                                 | SWT.BORDER);
217:                         scrolledComposite.setShowFocusedControl(true);
218:                         scrolledComposite.setExpandVertical(true);
219:                         scrolledComposite.setExpandHorizontal(true);
220:                         GridDataFactory.fillDefaults().align(SWT.FILL, SWT.FILL).grab(true, true).applyTo(scrolledComposite);
221:                         GridLayoutFactory.fillDefaults().applyTo(scrolledComposite);
222:
223:                         final Composite tableComposite = new Composite(scrolledComposite, SWT.FILL);
224:                         tableComposite.setLayout(GridLayoutFactory.fillDefaults().create());
225:                         GridDataFactory.fillDefaults().align(SWT.FILL, SWT.FILL).grab(true, true).applyTo(tableComposite);
226:                         tableComposite.setBackground(scrolledComposite.getBackground());
227:
228:                         final CheckboxTableViewer tvAttributes = CheckboxTableViewer.newCheckList(tableComposite, SWT.NONE);
229:                         tvAttributes.setLabelProvider(labelProvider);
230:                         tvAttributes.setContentProvider(ArrayContentProvider.getInstance());
231:                         GridDataFactory.fillDefaults().align(SWT.FILL, SWT.BEGINNING).grab(true, false).hint(SWT.DEFAULT, 200)
232:                                 .applyTo(tvAttributes.getControl());
233:
234:                         scrolledComposite.setContent(tableComposite);
235:
236:                         final Point point = tableComposite.computeSize(SWT.DEFAULT, SWT.DEFAULT);
237:                         scrolledComposite.setMinSize(point);
238:
239:                         tvAttributes.addCheckStateListener(new ICheckStateListener() {
240:
241:                                 @Override
242:                                 public void checkStateChanged(CheckStateChangedEvent event) {
243:                                         final EStructuralFeature object = (EStructuralFeature) event.getElement();
244:                                         if (event.getChecked()) {
245:                                                 selectedFeatures.add(object);
246:
247:                                         } else {
248:                                                 selectedFeatures.remove(object);
249:                                         }
250:                                         setPageComplete(!selectedFeatures.isEmpty());
251:
252:                                 }
253:                         });
254:
255:                         List<EStructuralFeature> attributes = null;
256:                         if (!bUnreferenced.getSelection()) {
257:                                 attributes = rootClass.getEAllStructuralFeatures();
258:                         } else {
259:                                 attributes = getUnreferencedSegmentAttributes(rootClass);
260:                         }
261:                         tvAttributes.setInput(attributes);
262:
263:                         bUnreferenced.addSelectionListener(new SelectionAdapter() {
264:
265:                                 @Override
266:                                 public void widgetSelected(SelectionEvent e) {
267:                                         List<EStructuralFeature> attributes = null;
268:                                         if (!bUnreferenced.getSelection()) {
269:                                                 attributes = rootClass.getEAllStructuralFeatures();
270:                                         } else {
271:                                                 attributes = getUnreferencedSegmentAttributes(rootClass);
272:                                         }
273:                                         tvAttributes.setInput(attributes);
274:                                 }
275:
276:                         });
277:
278:                         final Composite compositeButtons = new Composite(composite, SWT.NONE);
279:                         GridLayoutFactory.fillDefaults().numColumns(2).equalWidth(true).applyTo(compositeButtons);
280:                         GridDataFactory.fillDefaults().align(SWT.FILL, SWT.BEGINNING).grab(true, false).span(2, 1)
281:                                 .applyTo(compositeButtons);
282:
283:                         final Button bSelectAll = new Button(compositeButtons, SWT.PUSH);
284:                         bSelectAll.setText("Select All"); //$NON-NLS-1$
285:                         bSelectAll.addSelectionListener(new SelectionAdapter() {
286:
287:                                 @Override
288:                                 public void widgetSelected(SelectionEvent e) {
289:                                         @SuppressWarnings("unchecked")
290:                                         final List<EStructuralFeature> segments = (List<EStructuralFeature>) tvAttributes.getInput();
291:                                         tvAttributes.setAllChecked(true);
292:                                         selectedFeatures.addAll(segments);
293:                                         setPageComplete(!selectedFeatures.isEmpty());
294:                                 }
295:
296:                         });
297:                         GridDataFactory.fillDefaults().align(SWT.FILL, SWT.BEGINNING).grab(true, false).applyTo(bSelectAll);
298:                         final Button bDeSelectAll = new Button(compositeButtons, SWT.PUSH);
299:                         bDeSelectAll.setText("Deselect All"); //$NON-NLS-1$
300:                         bDeSelectAll.addSelectionListener(new SelectionAdapter() {
301:
302:                                 @Override
303:                                 public void widgetSelected(SelectionEvent e) {
304:                                         @SuppressWarnings("unchecked")
305:                                         final List<EStructuralFeature> segments = (List<EStructuralFeature>) tvAttributes.getInput();
306:                                         tvAttributes.setAllChecked(false);
307:                                         selectedFeatures.removeAll(segments);
308:                                         setPageComplete(!selectedFeatures.isEmpty());
309:                                 }
310:
311:                         });
312:                         GridDataFactory.fillDefaults().align(SWT.FILL, SWT.BEGINNING).grab(true, false).applyTo(bDeSelectAll);
313:                         parent.layout(true);
314:                         scrolledComposite.layout(true);
315:
316:                 }
317:         }
318:
319:         /**
320:          * {@inheritDoc}
321:          *
322:          * @see org.eclipse.jface.wizard.WizardPage#isPageComplete()
323:          */
324:         @Override
325:         public boolean isPageComplete() {
326:                 return super.isPageComplete();
327:         }
328:
329:         /**
330:          *
331:          */
332:         private void clear() {
333:                 if (parent != null && !parent.isDisposed()) {
334:                         for (final Control control : parent.getChildren()) {
335:                                 control.dispose();
336:                         }
337:                 }
338:         }
339:
340:         /**
341:          * @param eClass the EClass to show attributes from
342:          */
343:         public void setRootClass(EClass eClass) {
344:                 rootClass = eClass;
345:                 selectedFeatures.clear();
346:                 if (tvAttributes != null) {
347:                         List<EStructuralFeature> attributes = null;
348:                         attributes = rootClass.getEAllStructuralFeatures();
349:
350:                         tvAttributes.setInput(attributes);
351:                 }
352:         }
353:
354:         private List<EStructuralFeature> getUnreferencedSegmentAttributes(EClass eClass) {
355:                 final List<EStructuralFeature> result = new ArrayList<EStructuralFeature>();
356:                 final List<EStructuralFeature> allStructuralFeatures = new ArrayList<EStructuralFeature>(
357:                         eClass.getEAllStructuralFeatures());
358:
359:                 final TreeIterator<EObject> eAllContents = view.eAllContents();
360:                 while (eAllContents.hasNext()) {
361:                         final EObject eObject = eAllContents.next();
362:                         if (org.eclipse.emf.ecp.view.spi.model.VControl.class.isInstance(eObject)) {
363:                                 final org.eclipse.emf.ecp.view.spi.model.VControl control = (org.eclipse.emf.ecp.view.spi.model.VControl) eObject;
364:                                 final VDomainModelReference domainModelReference = control.getDomainModelReference();
365:                                 if (domainModelReference == null) {
366:                                         continue;
367:                                 }
368:
369:                                 IValueProperty valueProperty;
370:                                 try {
371:                                         valueProperty = Activator.getDefault().getEMFFormsDatabinding()
372:                                                 .getValueProperty(domainModelReference, eClass);
373:                                 } catch (final DatabindingFailedException ex) {
374:                                         Activator.getDefault().getReportService().report(new DatabindingFailedReport(ex));
375:                                         continue;
376:                                 }
377:                                 final EStructuralFeature feature = (EStructuralFeature) valueProperty.getValueType();
378:                                 if (feature != null && feature.getEContainingClass() != null
379:                                         && feature.getEContainingClass().isSuperTypeOf(eClass)) {
380:                                         result.add(feature);
381:                                 }
382:                         }
383:
384:                 }
385:
386:                 allStructuralFeatures.removeAll(result);
387:                 return allStructuralFeatures;
388:         }
389:
390:         /** @return the set of features selected in the dialog, for which controls should be generated. */
391:         public Set<EStructuralFeature> getSelectedFeatures() {
392:                 return selectedFeatures;
393:         }
394:
395:         /**
396:          * {@inheritDoc}
397:          *
398:          * @see org.eclipse.jface.wizard.WizardPage#getPreviousPage()
399:          */
400:         @Override
401:         public IWizardPage getPreviousPage() {
402:                 // TODO Auto-generated method stub
403:                 return super.getPreviousPage();
404:         }
405:
406:         /**
407:          * @param view The {@link VView} to select an attribute for. Is used to filter attributes already shown in the view.
408:          */
409:         public void setView(VView view) {
410:                 this.view = view;
411:         }
412:
413:         @Override
414:         public boolean isCurrentPage() {
415:                 return super.isCurrentPage();
416:         }
417:
418:         /**
419:          *
420:          */
421:         public void clearSelection() {
422:                 getSelectedFeatures().clear();
423:                 tvAttributes.setAllChecked(false);
424:
425:         }
426: }