Package: SelectModelElementWizardFactory
SelectModelElementWizardFactory
name | instruction | branch | complexity | line | method | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
SelectModelElementWizardFactory() |
|
|
|
|
|
||||||||||||||||||||
openCreateNewModelElementDialog(SelectionComposite) |
|
|
|
|
|
||||||||||||||||||||
openModelElementSelectionDialog(SelectionComposite) |
|
|
|
|
|
||||||||||||||||||||
openModelElementSelectionDialog(Set, boolean) |
|
|
|
|
|
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: * Jonas - initial API and implementation
13: * Christian W. Damus - bug 547787
14: ******************************************************************************/
15: package org.eclipse.emf.ecp.spi.common.ui;
16:
17: import java.util.HashSet;
18: import java.util.Set;
19:
20: import org.eclipse.emf.ecore.EClass;
21: import org.eclipse.emf.ecore.EObject;
22: import org.eclipse.emf.ecore.EPackage;
23: import org.eclipse.emf.ecp.internal.common.ui.MessageKeys;
24: import org.eclipse.emf.ecp.spi.common.ui.composites.SelectionComposite;
25: import org.eclipse.emfforms.spi.localization.LocalizationServiceHelper;
26: import org.eclipse.jface.viewers.StructuredViewer;
27: import org.eclipse.jface.viewers.TableViewer;
28: import org.eclipse.jface.viewers.TreeViewer;
29: import org.eclipse.jface.window.Window;
30: import org.eclipse.jface.wizard.WizardDialog;
31: import org.eclipse.swt.widgets.Display;
32:
33: /**
34: * @author Jonas
35: * @since 1.5
36: *
37: */
38: public abstract class SelectModelElementWizardFactory {
39:
40:         /**
41:          * @param elements The elements to be selected
42:          * @param isMany whether multi selection is allowed
43:          * @return The selected elements
44:          *
45:          * @param <T> the type to select
46:          */
47:         public static <T extends EObject> Set<T> openModelElementSelectionDialog(
48:                 final Set<T> elements, boolean isMany) {
49:
50:                 final SelectionComposite<TableViewer> tableSelectionComposite = CompositeFactory
51:                         .getTableSelectionComposite(elements.toArray(), isMany);
52:
53:                 return openModelElementSelectionDialog(tableSelectionComposite);
54:         }
55:
56:         /**
57:          * Open a model element selection dialog using a specific table selection composite.
58:          *
59:          * @param tableSelectionComposite a table selection composite to use in the dialog
60:          *
61:          * @return the selected elements
62:          *
63:          * @param <T> the type of object to select
64:          * @param <V> the type of viewer to present
65:          *
66:          * @since 1.22
67:          */
68:         @SuppressWarnings("unchecked")
69:         public static <T extends EObject, V extends StructuredViewer> Set<T> openModelElementSelectionDialog(
70:                 SelectionComposite<V> tableSelectionComposite) {
71:
72:                 final SelectModelElementWizard wizard = new SelectModelElementWizard(
73:                         LocalizationServiceHelper.getString(SelectModelElementWizardFactory.class,
74:                                 MessageKeys.SelectModelElementWizardFactory_ModelelementSelectionDialog_WindowTitle),
75:                         LocalizationServiceHelper.getString(SelectModelElementWizardFactory.class,
76:                                 MessageKeys.NewModelElementWizard_WizardTitle_AddModelElement),
77:                         LocalizationServiceHelper.getString(SelectModelElementWizardFactory.class,
78:                                 MessageKeys.ModelelementSelectionDialog_DialogTitle),
79:                         LocalizationServiceHelper.getString(SelectModelElementWizardFactory.class,
80:                                 MessageKeys.ModelelementSelectionDialog_DialogMessage_SearchPattern),
81:                         EObject.class);
82:
83:                 final HashSet<T> selectedElements = new HashSet<T>();
84:                 wizard.setCompositeProvider(tableSelectionComposite);
85:
86:                 final WizardDialog wd = new WizardDialog(Display.getDefault().getActiveShell(), wizard);
87:                 final int result = wd.open();
88:•                if (result == Window.OK) {
89:                         final Object[] selection = tableSelectionComposite.getSelection();
90:•                        if (selection == null || selection.length == 0) {
91:                                 return selectedElements;
92:                         }
93:
94:•                        for (final Object object : selection) {
95:•                                if (object instanceof EObject) {
96:                                         selectedElements.add((T) object);
97:                                 }
98:                         }
99:
100:                 }
101:                 return selectedElements;
102:         }
103:
104:         /**
105:          * @param selectionComposite the composite to select the Eclass on
106:          * @return an new EObject or null, if canceled
107:          */
108:         public static EObject openCreateNewModelElementDialog(final SelectionComposite<TreeViewer> selectionComposite) {
109:                 final SelectModelElementWizard wizard = new SelectModelElementWizard("New Reference Element", //$NON-NLS-1$
110:                         LocalizationServiceHelper.getString(SelectModelElementWizardFactory.class,
111:                                 MessageKeys.NewModelElementWizard_WizardTitle_AddModelElement),
112:                         LocalizationServiceHelper
113:                                 .getString(SelectModelElementWizardFactory.class,
114:                                         MessageKeys.NewModelElementWizard_PageTitle_AddModelElement),
115:                         LocalizationServiceHelper.getString(SelectModelElementWizardFactory.class,
116:                                 MessageKeys.NewModelElementWizard_PageDescription_AddModelElement));
117:
118:                 wizard.setCompositeProvider(selectionComposite);
119:
120:                 final WizardDialog wd = new WizardDialog(Display.getDefault().getActiveShell(), wizard);
121:                 // wizard.setWindowTitle("New Reference Element");
122:                 EObject newMEInstance = null;
123:                 final int result = wd.open();
124:
125:•                if (result == Window.OK) {
126:                         final Object[] selection = selectionComposite.getSelection();
127:•                        if (selection == null || selection.length == 0) {
128:                                 return null;
129:                         }
130:                         final EClass eClasse = (EClass) selection[0];
131:                         // 1.create ME
132:                         final EPackage ePackage = eClasse.getEPackage();
133:                         newMEInstance = ePackage.getEFactoryInstance().create(eClasse);
134:                 }
135:                 return newMEInstance;
136:         }
137:
138: }