Skip to content

Package: GenerateTableColumnsUtil

GenerateTableColumnsUtil

nameinstructionbranchcomplexitylinemethod
selectSubClass(EClass)
M: 0 C: 73
100%
M: 1 C: 3
75%
M: 1 C: 2
67%
M: 0 C: 22
100%
M: 0 C: 1
100%

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 Koehler - initial API and implementation
13: ******************************************************************************/
14: package org.eclipse.emf.ecp.view.internal.editor.handler;
15:
16: import java.util.Collection;
17: import java.util.Optional;
18:
19: import org.eclipse.emf.common.notify.AdapterFactory;
20: import org.eclipse.emf.ecore.EClass;
21: import org.eclipse.emf.ecp.common.spi.EMFUtils;
22: import org.eclipse.emf.edit.provider.ComposedAdapterFactory;
23: import org.eclipse.emf.edit.provider.ReflectiveItemProviderAdapterFactory;
24: import org.eclipse.emf.edit.ui.provider.AdapterFactoryLabelProvider;
25: import org.eclipse.jface.window.Window;
26: import org.eclipse.swt.widgets.Display;
27: import org.eclipse.ui.dialogs.ElementListSelectionDialog;
28:
29: /**
30: * Utility class for common functionality of table column generators.
31: *
32: * @author Lucas Koehler
33: *
34: */
35: final class GenerateTableColumnsUtil {
36:
37:         private GenerateTableColumnsUtil() {
38:                 // utility class
39:         }
40:
41:         /**
42:          * Opens a dialog to select a sub class of the given EClass in order to generate table columns for the sub class.
43:          *
44:          * @param baseEClass The base EClass to select a sub class of
45:          * @return The selected sub class or nothing if the dialog is cancelled or closed
46:          */
47:         static Optional<EClass> selectSubClass(EClass baseEClass) {
48:                 EClass eclass = baseEClass;
49:                 final Collection<EClass> subClasses = EMFUtils.getSubClasses(eclass);
50:
51:                 final ComposedAdapterFactory adapterFactory = new ComposedAdapterFactory(new AdapterFactory[] {
52:                         new ReflectiveItemProviderAdapterFactory(),
53:                         new ComposedAdapterFactory(ComposedAdapterFactory.Descriptor.Registry.INSTANCE) });
54:                 final AdapterFactoryLabelProvider labelProvider = new AdapterFactoryLabelProvider(
55:                         adapterFactory);
56:
57:                 final ElementListSelectionDialog dialog = new ElementListSelectionDialog(Display.getDefault().getActiveShell(),
58:                         labelProvider);
59:                 dialog.setTitle("Select EClass"); //$NON-NLS-1$
60:                 dialog.setMessage("Select a subclass to generate columns for all attributes from this EClass."); //$NON-NLS-1$
61:                 dialog.setMultipleSelection(false);
62:                 dialog.setElements(subClasses.toArray(new EClass[subClasses.size()]));
63:
64:•                if (dialog.open() == Window.OK) {
65:                         final Object firstResult = dialog.getFirstResult();
66:•                        if (firstResult != null) {
67:                                 eclass = (EClass) firstResult;
68:                         }
69:                 } else {
70:                         eclass = null;
71:                 }
72:
73:                 labelProvider.dispose();
74:                 adapterFactory.dispose();
75:
76:                 return Optional.ofNullable(eclass);
77:         }
78: }