Skip to content

Package: EMFSimpleProjectWizard$1

EMFSimpleProjectWizard$1

nameinstructionbranchcomplexitylinemethod
createControl(Composite)
M: 27 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 6 C: 0
0%
M: 1 C: 0
0%
validatePage()
M: 30 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 8 C: 0
0%
M: 1 C: 0
0%
{...}
M: 7 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2011-2013 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: * David Soto Setzke - initial API and implementation, implementation based
13: * org.eclipse.emf.ecore.presentation.EcoreModelWizard and
14: * org.eclipse.emf.importer.ui.EMFProjectWizard
15: ******************************************************************************/
16: /**
17: *
18: * Copyright (c) 2005-2007 IBM Corporation and others.
19: * All rights reserved. This program and the accompanying materials
20: * are made available under the terms of the Eclipse Public License 2.0
21: * which accompanies this distribution, and is available at
22: * https://www.eclipse.org/legal/epl-2.0/
23: *
24: * SPDX-License-Identifier: EPL-2.0
25: *
26: * Contributors:
27: * IBM - Initial API and implementation
28: *
29: */
30: package org.eclipse.emf.ecp.ecore.editor.ui;
31:
32: import java.io.File;
33: import java.io.IOException;
34: import java.io.PrintStream;
35: import java.io.UnsupportedEncodingException;
36: import java.lang.reflect.InvocationTargetException;
37: import java.util.Collections;
38:
39: import org.eclipse.core.resources.IFile;
40: import org.eclipse.core.resources.IProject;
41: import org.eclipse.core.resources.ResourcesPlugin;
42: import org.eclipse.core.runtime.CoreException;
43: import org.eclipse.core.runtime.IPath;
44: import org.eclipse.core.runtime.IProgressMonitor;
45: import org.eclipse.core.runtime.Path;
46: import org.eclipse.core.runtime.Platform;
47: import org.eclipse.emf.codegen.ecore.Generator;
48: import org.eclipse.emf.codegen.ecore.genmodel.provider.GenModelEditPlugin;
49: import org.eclipse.emf.codegen.util.CodeGenUtil;
50: import org.eclipse.emf.common.util.URI;
51: import org.eclipse.emf.ecore.presentation.EcoreEditorPlugin;
52: import org.eclipse.emf.ecore.resource.URIConverter;
53: import org.eclipse.emf.ecp.ecore.editor.ui.operations.CreateModelsWorkspaceModifyOperation;
54: import org.eclipse.emf.ecp.ecore.editor.util.ProjectHelper;
55: import org.eclipse.emf.edit.ui.provider.ExtendedImageRegistry;
56: import org.eclipse.jface.dialogs.MessageDialog;
57: import org.eclipse.jface.viewers.ISelection;
58: import org.eclipse.jface.viewers.IStructuredSelection;
59: import org.eclipse.jface.viewers.StructuredSelection;
60: import org.eclipse.jface.wizard.IWizardContainer;
61: import org.eclipse.jface.wizard.Wizard;
62: import org.eclipse.swt.widgets.Composite;
63: import org.eclipse.ui.IEditorDescriptor;
64: import org.eclipse.ui.INewWizard;
65: import org.eclipse.ui.IWorkbench;
66: import org.eclipse.ui.IWorkbenchPage;
67: import org.eclipse.ui.IWorkbenchPart;
68: import org.eclipse.ui.IWorkbenchWindow;
69: import org.eclipse.ui.IWorkingSet;
70: import org.eclipse.ui.PartInitException;
71: import org.eclipse.ui.actions.WorkspaceModifyOperation;
72: import org.eclipse.ui.dialogs.WizardNewProjectCreationPage;
73: import org.eclipse.ui.part.FileEditorInput;
74: import org.eclipse.ui.part.ISetSelectionTarget;
75:
76: /**
77: *
78: * A wizard for creating a simple EMF project with an ecore and a genmodel file.
79: *
80: */
81: public class EMFSimpleProjectWizard extends Wizard implements INewWizard {
82:
83:         private IWorkbench workbench;
84:         private IPath genModelProjectLocation;
85:         private IPath genModelContainerPath;
86:         private IProject project;
87:         private String initialProjectName;
88:         private IStructuredSelection selection;
89:         private WizardNewProjectCreationPage newProjectCreationPage;
90:
91:         @Override
92:         public void setContainer(IWizardContainer wizardContainer) {
93:                 super.setContainer(wizardContainer);
94:         }
95:
96:         /**
97:          *
98:          * {@inheritDoc}
99:          *
100:          * @see org.eclipse.ui.IWorkbenchWizard#init(org.eclipse.ui.IWorkbench,
101:          * org.eclipse.jface.viewers.IStructuredSelection)
102:          */
103:         @Override
104:         public void init(IWorkbench workbench, IStructuredSelection selection) {
105:                 this.workbench = workbench;
106:                 this.selection = selection;
107:                 setDefaultPageImageDescriptor(ExtendedImageRegistry.INSTANCE
108:                         .getImageDescriptor(GenModelEditPlugin.INSTANCE
109:                                 .getImage("full/wizban/NewEmptyEMFProject")));
110:                 setWindowTitle(GenModelEditPlugin.INSTANCE
111:                         .getString("_UI_NewEmptyProject_title"));
112:
113:         }
114:
115:         @Override
116:         public void addPages() {
117:                 newProjectCreationPage = new WizardNewProjectCreationPage(
118:                         "NewProjectCreationPage") {
119:                         @Override
120:                         protected boolean validatePage() {
121:•                                if (super.validatePage()) {
122:                                         final IPath locationPath = getLocationPath();
123:                                         genModelProjectLocation = Platform.getLocation().equals(
124:•                                                locationPath) ? null : locationPath;
125:                                         final IPath projectPath = getProjectHandle().getFullPath();
126:                                         genModelContainerPath = projectPath.append("src");
127:                                         return true;
128:                                 }
129:                                 return false;
130:                         }
131:
132:                         @Override
133:                         public void createControl(Composite parent) {
134:                                 super.createControl(parent);
135:                                 createWorkingSetGroup((Composite) getControl(), selection,
136:                                         new String[] { "org.eclipse.jdt.ui.JavaWorkingSetPage",
137:                                                 "org.eclipse.pde.ui.pluginWorkingSet",
138:                                                 "org.eclipse.ui.resourceWorkingSetPage" });
139:                         }
140:                 };
141:                 newProjectCreationPage.setInitialProjectName(initialProjectName);
142:                 newProjectCreationPage.setTitle(GenModelEditPlugin.INSTANCE
143:                         .getString("_UI_EmptyProject_title"));
144:                 newProjectCreationPage.setDescription(GenModelEditPlugin.INSTANCE
145:                         .getString("_UI_EmptyProject_description"));
146:
147:                 addPage(newProjectCreationPage);
148:         }
149:
150:         @Override
151:         public boolean performFinish() {
152:                 final WorkspaceModifyOperation operation = new WorkspaceModifyOperation() {
153:                         @Override
154:                         protected void execute(IProgressMonitor progressMonitor) {
155:                                 try {
156:                                         modifyWorkspace(progressMonitor);
157:                                 } catch (final UnsupportedEncodingException ex) {
158:                                         GenModelEditPlugin.INSTANCE.log(ex);
159:                                 } catch (final CoreException ex) {
160:                                         GenModelEditPlugin.INSTANCE.log(ex);
161:                                 } catch (final IOException ex) {
162:                                         GenModelEditPlugin.INSTANCE.log(ex);
163:                                 } finally {
164:                                         progressMonitor.done();
165:                                 }
166:                         }
167:                 };
168:
169:                 try {
170:                         getContainer().run(false, false, operation);
171:                 } catch (final InvocationTargetException ex) {
172:                         GenModelEditPlugin.INSTANCE.log(ex);
173:                         return false;
174:                 } catch (final InterruptedException ex) {
175:                         GenModelEditPlugin.INSTANCE.log(ex);
176:                         return false;
177:                 }
178:
179:                 if (project != null) {
180:                         final IWorkbenchPage page = workbench.getActiveWorkbenchWindow()
181:                                 .getActivePage();
182:                         final IWorkbenchPart activePart = page.getActivePart();
183:                         if (activePart instanceof ISetSelectionTarget) {
184:                                 final ISelection targetSelection = new StructuredSelection(
185:                                         project);
186:                                 getShell().getDisplay().asyncExec(() -> {
187:                                         ((ISetSelectionTarget) activePart).selectReveal(targetSelection);
188:                                 });
189:                         }
190:                 }
191:                 final ProjectHelper helper = new ProjectHelper(newProjectCreationPage.getProjectName());
192:                 final String filePrefix = helper.getProjectName().length() == 0 ? "model"
193:                         : helper.getProjectName();
194:                 final IFile modelFile = getModelFile(newProjectCreationPage.getProjectName(),
195:                         filePrefix + ".ecore");
196:
197:                 // Do the work within an operation.
198:                 final WorkspaceModifyOperation createModelsOperation = new CreateModelsWorkspaceModifyOperation(
199:                         modelFile, "EPackage", "UTF-8",
200:                         newProjectCreationPage.getProjectName());
201:
202:                 try {
203:                         getContainer().run(false, false, createModelsOperation);
204:                 } catch (final InvocationTargetException ex) {
205:                         EcoreEditorPlugin.INSTANCE.log(ex);
206:                         return false;
207:                 } catch (final InterruptedException ex) {
208:                         EcoreEditorPlugin.INSTANCE.log(ex);
209:                         return false;
210:                 }
211:
212:                 // Select the new file resource in the current view.
213:                 final IWorkbenchWindow workbenchWindow = workbench
214:                         .getActiveWorkbenchWindow();
215:                 final IWorkbenchPage page = workbenchWindow.getActivePage();
216:                 final IWorkbenchPart activePart = page.getActivePart();
217:                 if (activePart instanceof ISetSelectionTarget) {
218:                         final ISelection targetSelection = new StructuredSelection(
219:                                 modelFile);
220:                         getShell().getDisplay().asyncExec(new Runnable() {
221:                                 @Override
222:                                 public void run() {
223:                                         ((ISetSelectionTarget) activePart)
224:                                                 .selectReveal(targetSelection);
225:                                 }
226:                         });
227:                 }
228:
229:                 return openFile(modelFile, workbenchWindow, page);
230:         }
231:
232:         private boolean openFile(final IFile modelFile, final IWorkbenchWindow workbenchWindow, final IWorkbenchPage page) {
233:                 // Open an editor on the new file.
234:                 try {
235:                         final IEditorDescriptor defaultEditor = workbench.getEditorRegistry()
236:                                 .getDefaultEditor(modelFile.getFullPath().toString());
237:                         page.openEditor(
238:                                 new FileEditorInput(modelFile),
239:                                 defaultEditor == null ? "org.eclipse.emf.ecore.presentation.EcoreEditorID"
240:                                         : defaultEditor.getId());
241:                 } catch (final PartInitException exception) {
242:                         MessageDialog.openError(workbenchWindow.getShell(),
243:                                 EcoreEditorPlugin.INSTANCE.getString("_UI_OpenEditorError_label"),
244:                                 exception.getMessage());
245:                         return false;
246:                 }
247:
248:                 return true;
249:         }
250:
251:         private void modifyWorkspace(IProgressMonitor progressMonitor)
252:                 throws CoreException, UnsupportedEncodingException, IOException {
253:                 project = Generator.createEMFProject(
254:                         new Path(genModelContainerPath.toString()),
255:                         genModelProjectLocation, Collections.<IProject> emptyList(),
256:                         progressMonitor, Generator.EMF_MODEL_PROJECT_STYLE
257:                                 | Generator.EMF_PLUGIN_PROJECT_STYLE);
258:
259:                 final IWorkingSet[] workingSets = newProjectCreationPage
260:                         .getSelectedWorkingSets();
261:                 if (workingSets != null) {
262:                         workbench.getWorkingSetManager().addToWorkingSets(project,
263:                                 workingSets);
264:                 }
265:
266:                 CodeGenUtil.EclipseUtil.findOrCreateContainer(new Path("/"
267:                         + genModelContainerPath.segment(0) + "/model"), true,
268:                         genModelProjectLocation, progressMonitor);
269:
270:                 final PrintStream manifest = new PrintStream(
271:                         URIConverter.INSTANCE.createOutputStream(
272:                                 URI.createPlatformResourceURI("/"
273:                                         + genModelContainerPath.segment(0)
274:                                         + "/META-INF/MANIFEST.MF", true),
275:                                 null),
276:                         false,
277:                         "UTF-8");
278:                 manifest.println("Manifest-Version: 1.0");
279:                 manifest.println("Bundle-ManifestVersion: 2");
280:                 manifest.print("Bundle-Name: ");
281:                 manifest.println(genModelContainerPath.segment(0));
282:                 manifest.print("Bundle-SymbolicName: ");
283:                 manifest.print(CodeGenUtil.validPluginID(genModelContainerPath
284:                         .segment(0)));
285:                 manifest.println("; singleton:=true");
286:                 manifest.println("Bundle-Version: 0.1.0.qualifier");
287:                 manifest.print("Require-Bundle: ");
288:                 final String[] requiredBundles = getRequiredBundles();
289:                 for (int i = 0, size = requiredBundles.length; i < size;) {
290:                         manifest.print(requiredBundles[i]);
291:                         if (++i == size) {
292:                                 manifest.println();
293:                                 break;
294:                         }
295:                         manifest.println(",");
296:                         manifest.print(" ");
297:                 }
298:                 manifest.close();
299:         }
300:
301:         private String[] getRequiredBundles() {
302:                 return new String[] { "org.eclipse.emf.ecore" };
303:         }
304:
305:         private IFile getModelFile(String projectName, String fileName) {
306:                 return ResourcesPlugin
307:                         .getWorkspace()
308:                         .getRoot()
309:                         .getFile(
310:                                 new Path(new File(projectName + "/model/" + fileName)
311:                                         .getPath()));
312:         }
313: }