Skip to content

Package: SelectEcorePage

SelectEcorePage

nameinstructionbranchcomplexitylinemethod
SelectEcorePage(String)
M: 0 C: 13
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
createControl(Composite)
M: 0 C: 122
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 25
100%
M: 0 C: 1
100%
getContainerName()
M: 31 C: 0
0%
M: 6 C: 0
0%
M: 4 C: 0
0%
M: 7 C: 0
0%
M: 1 C: 0
0%
getPackageSelectionDialog()
M: 0 C: 24
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 6
100%
M: 0 C: 1
100%
getSelectedContainer()
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%
getText1()
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getWorkspaceEcoreSelectionDialog()
M: 0 C: 30
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 7
100%
M: 0 C: 1
100%
setSelectedContainer(Object)
M: 16 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
setVisible(boolean)
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2011-2016 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.core.resources.IFile;
17: import org.eclipse.core.resources.ResourcesPlugin;
18: import org.eclipse.core.runtime.IStatus;
19: import org.eclipse.core.runtime.Status;
20: import org.eclipse.emf.ecore.EPackage;
21: import org.eclipse.emf.ecore.provider.EcoreEditPlugin;
22: import org.eclipse.emf.ecp.ide.spi.util.EcoreHelper;
23: import org.eclipse.emf.edit.ui.provider.ExtendedImageRegistry;
24: import org.eclipse.jface.layout.GridDataFactory;
25: import org.eclipse.jface.layout.GridLayoutFactory;
26: import org.eclipse.jface.viewers.LabelProvider;
27: import org.eclipse.jface.window.Window;
28: import org.eclipse.jface.wizard.WizardPage;
29: import org.eclipse.swt.SWT;
30: import org.eclipse.swt.events.SelectionEvent;
31: import org.eclipse.swt.events.SelectionListener;
32: import org.eclipse.swt.graphics.Image;
33: import org.eclipse.swt.layout.GridData;
34: import org.eclipse.swt.layout.GridLayout;
35: import org.eclipse.swt.widgets.Button;
36: import org.eclipse.swt.widgets.Composite;
37: import org.eclipse.swt.widgets.Display;
38: import org.eclipse.swt.widgets.Label;
39: import org.eclipse.swt.widgets.Text;
40: import org.eclipse.ui.dialogs.ElementListSelectionDialog;
41: import org.eclipse.ui.dialogs.ElementTreeSelectionDialog;
42: import org.eclipse.ui.dialogs.ISelectionStatusValidator;
43: import org.eclipse.ui.model.BaseWorkbenchContentProvider;
44: import org.eclipse.ui.model.WorkbenchLabelProvider;
45:
46: /**
47: * Wizard page for selecting an Ecore.
48: */
49: public class SelectEcorePage extends WizardPage {
50:         private Text text1;
51:         private Composite container;
52:         private Button browseWorkspaceBtn;
53:         private final String pluginId;
54:         private Button browsePackageRegistryBtn;
55:         /** The container of the EClass. It can either be an IFile or an EPackage. */
56:         private Object selectedContainer;
57:
58:         /**
59:          * Creates a new Ecore selection wizard page.
60:          *
61:          * @param pageName the name of the page
62:          */
63:         public SelectEcorePage(String pageName) {
64:                 super("Select Model"); //$NON-NLS-1$
65:                 setTitle("Select Model"); //$NON-NLS-1$
66:                 setDescription("Select a model file for the new View Model"); //$NON-NLS-1$
67:                 pluginId = pageName;
68:         }
69:
70:         @Override
71:         public void createControl(Composite parent) {
72:                 container = new Composite(parent, SWT.NONE);
73:                 final GridLayout layout = new GridLayout();
74:                 container.setLayout(layout);
75:                 layout.numColumns = 2;
76:                 final Label label1 = new Label(container, SWT.NONE);
77:                 label1.setText("Selected Ecore:"); //$NON-NLS-1$
78:
79:                 text1 = new Text(container, SWT.BORDER | SWT.SINGLE);
80:                 text1.setText(""); //$NON-NLS-1$
81:
82:                 final GridData gd = new GridData(GridData.FILL_HORIZONTAL);
83:                 text1.setLayoutData(gd);
84:
85:                 @SuppressWarnings("unused")
86:                 final Composite dummy = new Composite(container, SWT.None);
87:
88:                 final Composite btnsComposite = new Composite(container, SWT.NONE);
89:                 final GridLayout gridLayout = GridLayoutFactory.fillDefaults().create();
90:                 gridLayout.numColumns = 2;
91:                 btnsComposite.setLayout(gridLayout);
92:                 GridDataFactory.fillDefaults().grab(true, false).applyTo(btnsComposite);
93:
94:                 browseWorkspaceBtn = new Button(btnsComposite, SWT.PUSH);
95:                 browseWorkspaceBtn.setText("Browse Workspace"); //$NON-NLS-1$
96:                 browseWorkspaceBtn.addSelectionListener(new SelectionListener() {
97:
98:                         @Override
99:                         public void widgetSelected(SelectionEvent e) {
100:                                 final ElementTreeSelectionDialog dialog = getWorkspaceEcoreSelectionDialog();
101:
102:                                 if (dialog.open() == Window.OK) {
103:                                         // get Ecore
104:                                         final IFile selectedEcore = (IFile) dialog.getFirstResult();
105:                                         text1.setText(selectedEcore.getFullPath().toString());
106:                                         selectedContainer = selectedEcore;
107:                                         setPageComplete(selectedContainer != null);
108:                                 }
109:                         }
110:
111:                         @Override
112:                         public void widgetDefaultSelected(SelectionEvent e) {
113:                                 // do nothing
114:                         }
115:                 });
116:
117:                 browsePackageRegistryBtn = new Button(btnsComposite, SWT.PUSH);
118:                 browsePackageRegistryBtn.setText("Browse Package Registry"); //$NON-NLS-1$
119:                 browsePackageRegistryBtn.addSelectionListener(new SelectionListener() {
120:
121:                         @Override
122:                         public void widgetSelected(SelectionEvent e) {
123:                                 final ElementListSelectionDialog selectRegisteredPackageDialog = getPackageSelectionDialog();
124:                                 selectRegisteredPackageDialog.open();
125:                                 final Object[] result = selectRegisteredPackageDialog.getResult();
126:                                 if (result == null) {
127:                                         return;
128:                                 }
129:                                 final String selectedEPackageURI = (String) result[0];
130:                                 selectedContainer = EPackage.Registry.INSTANCE.getEPackage(selectedEPackageURI);
131:                                 text1.setText(selectedEPackageURI);
132:                                 setPageComplete(selectedContainer != null);
133:                         }
134:
135:                         @Override
136:                         public void widgetDefaultSelected(SelectionEvent e) {
137:                                 // do nothing
138:                         }
139:                 });
140:
141:                 setControl(container);
142:                 setPageComplete(false);
143:         }
144:
145:         private ElementListSelectionDialog getPackageSelectionDialog() {
146:                 final ElementListSelectionDialog dialog = new ElementListSelectionDialog(getShell(), new LabelProvider() {
147:                         @Override
148:                         public Image getImage(Object element) {
149:                                 return ExtendedImageRegistry.getInstance()
150:                                         .getImage(EcoreEditPlugin.INSTANCE.getImage("full/obj16/EPackage")); //$NON-NLS-1$
151:                         }
152:                 });
153:                 dialog.setMultipleSelection(false);
154:                 dialog.setTitle("Package Selection"); //$NON-NLS-1$
155:                 dialog.setMessage("Select a package:"); //$NON-NLS-1$
156:                 dialog.setElements(EcoreHelper.getDefaultPackageRegistryContents());
157:                 return dialog;
158:         }
159:
160:         private ElementTreeSelectionDialog getWorkspaceEcoreSelectionDialog() {
161:                 final ElementTreeSelectionDialog dialog = new ElementTreeSelectionDialog(Display.getDefault()
162:                         .getActiveShell(), new WorkbenchLabelProvider(), new BaseWorkbenchContentProvider());
163:                 dialog.setInput(ResourcesPlugin.getWorkspace().getRoot());
164:                 dialog.setAllowMultiple(false);
165:                 dialog.setValidator(new ISelectionStatusValidator() {
166:
167:                         @Override
168:                         public IStatus validate(Object[] selection) {
169:                                 if (selection.length == 1) {
170:                                         if (selection[0] instanceof IFile) {
171:                                                 final IFile file = (IFile) selection[0];
172:                                                 if (file.getFileExtension().equals("ecore")) { //$NON-NLS-1$
173:                                                         return new Status(IStatus.OK, pluginId, IStatus.OK, null, null);
174:                                                 }
175:                                         }
176:                                 }
177:                                 return new Status(IStatus.ERROR, pluginId, IStatus.ERROR, "Please Select a Model file", //$NON-NLS-1$
178:                                         null);
179:                         }
180:                 });
181:                 dialog.setTitle("Select Model"); //$NON-NLS-1$
182:                 return dialog;
183:         }
184:
185:         /**
186:          * Returns the path of the selected Ecore as specified in the text field of the page.
187:          *
188:          * @return the path as a String
189:          */
190:         public String getText1() {
191:                 return text1.getText();
192:         }
193:
194:         /**
195:          * {@inheritDoc}
196:          *
197:          * @see org.eclipse.jface.dialogs.DialogPage#setVisible(boolean)
198:          */
199:         @Override
200:         public void setVisible(boolean visible) {
201:                 super.setVisible(visible);
202:                 // if (visible) {
203:                 // selectedEClass = null;
204:                 // }
205:         }
206:
207:         /**
208:          * Sets the selected Ecore container.
209:          *
210:          * @param object the container
211:          */
212:         public void setSelectedContainer(Object object) {
213:                 selectedContainer = object;
214:•                setPageComplete(object != null);
215:                 text1.setText(getContainerName());
216:         }
217:
218:         /**
219:          * @return the container of the EClass. It can either be an IFile or an EPackage.
220:          */
221:         public Object getSelectedContainer() {
222:                 return selectedContainer;
223:         }
224:
225:         /**
226:          * @return
227:          */
228:         private String getContainerName() {
229:                 String containerName = ""; //$NON-NLS-1$
230:•                if (selectedContainer != null) {
231:•                        if (EPackage.class.isInstance(selectedContainer)) {
232:                                 containerName = EPackage.class.cast(selectedContainer).getName();
233:•                        } else if (IFile.class.isInstance(selectedContainer)) {
234:                                 containerName = ((IFile) selectedContainer).getFullPath().toString();
235:                         }
236:                 }
237:                 return containerName;
238:         }
239:
240: }