Skip to content

Package: NewPluginProjectWizard

NewPluginProjectWizard

nameinstructionbranchcomplexitylinemethod
NewPluginProjectWizard()
M: 0 C: 8
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
addContribution(IFile)
M: 86 C: 110
56%
M: 2 C: 8
80%
M: 2 C: 4
67%
M: 12 C: 25
68%
M: 0 C: 1
100%
addPages()
M: 0 C: 42
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 10
100%
M: 0 C: 1
100%
canFinish()
M: 2 C: 19
90%
M: 2 C: 4
67%
M: 2 C: 2
50%
M: 1 C: 4
80%
M: 0 C: 1
100%
createViewModelFiles()
M: 14 C: 70
83%
M: 1 C: 5
83%
M: 1 C: 3
75%
M: 2 C: 15
88%
M: 0 C: 1
100%
getEPackage()
M: 4 C: 77
95%
M: 4 C: 8
67%
M: 4 C: 3
43%
M: 2 C: 19
90%
M: 0 C: 1
100%
getInitialProjectName()
M: 22 C: 59
73%
M: 3 C: 5
63%
M: 2 C: 3
60%
M: 3 C: 13
81%
M: 0 C: 1
100%
getNextPage(IWizardPage)
M: 0 C: 78
100%
M: 0 C: 6
100%
M: 0 C: 4
100%
M: 0 C: 19
100%
M: 0 C: 1
100%
getPreviousPage(IWizardPage)
M: 3 C: 8
73%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 1 C: 2
67%
M: 0 C: 1
100%
getSelectedEcore()
M: 0 C: 11
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
getViewModelRegistry()
M: 2 C: 15
88%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 1 C: 5
83%
M: 0 C: 1
100%
getWindowTitle()
M: 0 C: 2
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
init(IWorkbench, IStructuredSelection)
M: 0 C: 7
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
loadFromExtensionPoints()
M: 0 C: 156
100%
M: 7 C: 13
65%
M: 7 C: 4
36%
M: 0 C: 38
100%
M: 0 C: 1
100%
performFinish()
M: 13 C: 51
80%
M: 3 C: 5
63%
M: 3 C: 2
40%
M: 2 C: 9
82%
M: 0 C: 1
100%
renamePlugin()
M: 28 C: 161
85%
M: 2 C: 8
80%
M: 2 C: 4
67%
M: 4 C: 34
89%
M: 0 C: 1
100%
setSelectedContainer(Object)
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%
setSelection(IStructuredSelection)
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%
setWorkbench(IWorkbench)
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%
static {...}
M: 0 C: 9
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
updateProjectDescriptionFile(IProject)
M: 77 C: 96
55%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 11 C: 26
70%
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:
15: package org.eclipse.emf.ecp.view.model.internal.project.installer;
16:
17: import java.io.BufferedReader;
18: import java.io.FileWriter;
19: import java.io.IOException;
20: import java.io.InputStream;
21: import java.io.InputStreamReader;
22: import java.io.StringWriter;
23: import java.util.ArrayList;
24: import java.util.Arrays;
25: import java.util.Collections;
26: import java.util.List;
27:
28: import javax.xml.parsers.DocumentBuilder;
29: import javax.xml.parsers.DocumentBuilderFactory;
30: import javax.xml.parsers.ParserConfigurationException;
31: import javax.xml.transform.OutputKeys;
32: import javax.xml.transform.Transformer;
33: import javax.xml.transform.TransformerConfigurationException;
34: import javax.xml.transform.TransformerException;
35: import javax.xml.transform.TransformerFactory;
36: import javax.xml.transform.dom.DOMSource;
37: import javax.xml.transform.stream.StreamResult;
38:
39: import org.eclipse.core.resources.IFile;
40: import org.eclipse.core.resources.IProject;
41: import org.eclipse.core.resources.IResource;
42: import org.eclipse.core.resources.ResourcesPlugin;
43: import org.eclipse.core.runtime.CoreException;
44: import org.eclipse.core.runtime.IConfigurationElement;
45: import org.eclipse.core.runtime.IExtensionPoint;
46: import org.eclipse.core.runtime.IPath;
47: import org.eclipse.core.runtime.IStatus;
48: import org.eclipse.core.runtime.Platform;
49: import org.eclipse.core.runtime.Status;
50: import org.eclipse.emf.common.ui.CommonUIPlugin;
51: import org.eclipse.emf.common.ui.wizard.AbstractExampleInstallerWizard;
52: import org.eclipse.emf.common.ui.wizard.ExampleInstallerWizard;
53: import org.eclipse.emf.common.util.EList;
54: import org.eclipse.emf.common.util.URI;
55: import org.eclipse.emf.ecore.EClass;
56: import org.eclipse.emf.ecore.EObject;
57: import org.eclipse.emf.ecore.EPackage;
58: import org.eclipse.emf.ecore.plugin.EcorePlugin;
59: import org.eclipse.emf.ecore.resource.Resource;
60: import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
61: import org.eclipse.emf.ecore.xmi.XMLResource;
62: import org.eclipse.emf.ecp.ide.spi.util.ViewModelHelper;
63: import org.eclipse.emf.ecp.ide.view.service.IDEViewModelRegistry;
64: import org.eclipse.emf.ecp.view.internal.editor.handler.ControlGenerator;
65: import org.eclipse.emf.ecp.view.internal.editor.handler.SelectEcorePage;
66: import org.eclipse.emf.ecp.view.model.presentation.SelectEClassForViewWizardPage;
67: import org.eclipse.emf.ecp.view.model.presentation.ViewEditorPlugin;
68: import org.eclipse.emf.ecp.view.spi.model.VView;
69: import org.eclipse.jface.viewers.IStructuredSelection;
70: import org.eclipse.jface.wizard.IWizardPage;
71: import org.eclipse.ui.IWorkbench;
72: import org.eclipse.ui.IWorkbenchPage;
73: import org.eclipse.ui.PartInitException;
74: import org.eclipse.ui.dialogs.WizardNewProjectCreationPage;
75: import org.eclipse.ui.part.FileEditorInput;
76: import org.osgi.framework.ServiceReference;
77: import org.w3c.dom.Document;
78: import org.w3c.dom.Element;
79: import org.w3c.dom.NodeList;
80: import org.xml.sax.SAXException;
81:
82: /**
83: * @author Alexandra Buzila
84: *
85: */
86: public class NewPluginProjectWizard extends ExampleInstallerWizard {
87:         private static final String PLUGIN_ID = "org.eclipse.emf.ecp.view.model.internal.project.installer"; //$NON-NLS-1$
88:         private SelectEClassForViewWizardPage selectEClassPage;
89:         private List<EClass> selectedEClasses;
90:         private Object selectedContainer;
91:         /**
92:          * The supported extensions for created files. <!-- begin-user-doc --> <!--
93:          * end-user-doc -->
94:          *
95:          * @generated
96:          */
97:         public static final List<String> FILE_EXTENSIONS = Collections
98:                 .unmodifiableList(Arrays.asList(ViewEditorPlugin.INSTANCE
99:                         .getString("_UI_ViewEditorFilenameExtensions").split( //$NON-NLS-1$
100:                                 "\\s*,\\s*"))); //$NON-NLS-1$
101:
102:         /**
103:          * @param selectedContainer
104:          * the selectedContainer to set
105:          */
106:         public void setSelectedContainer(Object selectedContainer) {
107:                 this.selectedContainer = selectedContainer;
108:         }
109:
110:         private WizardNewProjectCreationPage firstPage;
111:         private SelectEcorePage selectEcorePage;
112:         private String projectName;
113:         private final List<IFile> viewModelFiles = new ArrayList<IFile>();
114:         /**
115:          * Remember the selection during initialization for populating the default
116:          * container. <!-- begin-user-doc --> <!-- end-user-doc -->
117:          *
118:          * @generated
119:          */
120:         protected IStructuredSelection selection;
121:
122:         @Override
123:         public void init(IWorkbench workbench, IStructuredSelection selection) {
124:                 this.workbench = workbench;
125:                 this.selection = selection;
126:         }
127:
128:         /**
129:          * @param workbench
130:          * the workbench to set
131:          */
132:         public void setWorkbench(IWorkbench workbench) {
133:                 this.workbench = workbench;
134:         }
135:
136:         @Override
137:         public String getWindowTitle() {
138:                 return "New View Model Project"; //$NON-NLS-1$
139:         }
140:
141:         /*
142:          * (non-Javadoc)
143:          * @see org.eclipse.emf.common.ui.wizard.ExampleInstallerWizard# loadFromExtensionPoints()
144:          */
145:         @Override
146:         protected void loadFromExtensionPoints() {
147:
148:                 projectDescriptors = new ArrayList<ProjectDescriptor>();
149:                 filesToOpen = new ArrayList<FileToOpen>();
150:                 final String wizardID = "org.eclipse.emf.ecp.view.model.internal.project.installer.NewVMProjectWizard"; //$NON-NLS-1$
151:
152:                 final IExtensionPoint extensionPoint = Platform.getExtensionRegistry().getExtensionPoint(
153:                         CommonUIPlugin.INSTANCE.getSymbolicName(), "examples"); //$NON-NLS-1$
154:                 final IConfigurationElement[] exampleElements = extensionPoint.getConfigurationElements();
155:•                for (int i = 0; i < exampleElements.length; i++) {
156:                         final IConfigurationElement exampleElement = exampleElements[i];
157:•                        if ("example".equals(exampleElement.getName()) //$NON-NLS-1$
158:•                                && wizardID.equals(exampleElement.getAttribute("wizardID"))) { //$NON-NLS-1$
159:                                 final IConfigurationElement[] projectDescriptorElements = exampleElement
160:                                         .getChildren("projectDescriptor"); //$NON-NLS-1$
161:•                                for (int j = 0; j < projectDescriptorElements.length; j++) {
162:
163:                                         final IConfigurationElement projectDescriptorElement = projectDescriptorElements[j];
164:                                         final String contentURI = projectDescriptorElement
165:                                                 .getAttribute("contentURI"); //$NON-NLS-1$
166:•                                        if (projectName != null && contentURI != null) {
167:                                                 final AbstractExampleInstallerWizard.ProjectDescriptor projectDescriptor = new AbstractExampleInstallerWizard.ProjectDescriptor();
168:                                                 projectDescriptor.setName(projectName);
169:
170:                                                 URI uri = URI.createURI(contentURI);
171:•                                                if (uri.isRelative()) {
172:                                                         uri = URI.createPlatformPluginURI(projectDescriptorElement.getContributor().getName()
173:                                                                 + "/" + contentURI, true); //$NON-NLS-1$
174:                                                 }
175:                                                 projectDescriptor.setContentURI(uri);
176:                                                 projectDescriptor.setDescription(projectDescriptorElement.getAttribute("description")); //$NON-NLS-1$
177:                                                 projectDescriptors.add(projectDescriptor);
178:                                         }
179:                                 }
180:
181:•                                if (!projectDescriptors.isEmpty()) {
182:                                         final IConfigurationElement[] openElements = exampleElement.getChildren("fileToOpen"); //$NON-NLS-1$
183:•                                        for (int j = 0; j < openElements.length; j++) {
184:                                                 final IConfigurationElement openElement = openElements[j];
185:                                                 final String location = openElement.getAttribute("location"); //$NON-NLS-1$
186:•                                                if (location != null) {
187:                                                         final AbstractExampleInstallerWizard.FileToOpen fileToOpen = new AbstractExampleInstallerWizard.FileToOpen();
188:                                                         fileToOpen.setLocation(location);
189:                                                         fileToOpen.setEditorID(openElement.getAttribute("editorID")); //$NON-NLS-1$
190:                                                         filesToOpen.add(fileToOpen);
191:                                                 }
192:                                         }
193:
194:                                         // Only one example per wizard
195:                                         break;
196:                                 }
197:                         }
198:                 }
199:         }
200:
201:         /*
202:          * (non-Javadoc)
203:          * @see
204:          * org.eclipse.emf.common.ui.wizard.AbstractExampleInstallerWizard#addPages
205:          * ()
206:          */
207:         @Override
208:         public void addPages() {
209:                 firstPage = new WizardNewProjectCreationPage("basicNewProjectPage"); //$NON-NLS-1$
210:                 firstPage.setTitle("Project"); //$NON-NLS-1$
211:                 firstPage.setDescription("Create a new View Model Project."); //$NON-NLS-1$
212:                 firstPage.setInitialProjectName(getInitialProjectName());
213:                 addPage(firstPage);
214:
215:                 selectEClassPage = new SelectEClassForViewWizardPage();
216:                 selectEClassPage.setSelectedEPackage(getEPackage());
217:                 addPage(selectEClassPage);
218:                 selectEClassPage.setPageComplete(true);
219:
220:         }
221:
222:         private EPackage getEPackage() {
223:                 EPackage ePackage = null;
224:•                if (selectedContainer == null) {
225:                         return null;
226:                 }
227:•                if (EPackage.class.isInstance(selectedContainer)) {
228:                         ePackage = EPackage.class.cast(selectedContainer);
229:•                } else if (IFile.class.isInstance(selectedContainer)) {
230:                         final ResourceSetImpl resourceSet = new ResourceSetImpl();
231:                         // needed to be able to resolve resource paths to plugin paths and thus load referenced ecores
232:                         resourceSet.getURIConverter().getURIMap().putAll(EcorePlugin.computePlatformURIMap(true));
233:                         resourceSet.getLoadOptions().put(XMLResource.OPTION_DEFER_IDREF_RESOLUTION, Boolean.TRUE);
234:
235:                         final String path = ((IFile) selectedContainer).getFullPath().toString();
236:                         final URI uri = URI.createPlatformResourceURI(path, true);
237:
238:                         final Resource resource = resourceSet.getResource(uri, true);
239:•                        if (resource != null) {
240:
241:                                 final EList<EObject> contents = resource.getContents();
242:•                                if (contents.size() != 1) {
243:                                         return null;
244:                                 }
245:
246:                                 final EObject object = contents.get(0);
247:•                                if (!(object instanceof EPackage)) {
248:                                         return null;
249:                                 }
250:
251:                                 ePackage = (EPackage) object;
252:                         }
253:                 }
254:                 return ePackage;
255:
256:         }
257:
258:         /**
259:          * @return
260:          */
261:         private IFile getSelectedEcore() {
262:•                if (IFile.class.isInstance(selectedContainer)) {
263:                         return (IFile) selectedContainer;
264:                 }
265:                 return null;
266:         }
267:
268:         private String getInitialProjectName() {
269:
270:                 projectName = null;
271:•                if (selectedContainer == null) {
272:                         return null;
273:                 }
274:•                if (IFile.class.isInstance(selectedContainer)) {
275:                         final IFile selectedEcore = IFile.class.cast(selectedContainer);
276:                         final IProject project = ((IResource) selectedEcore).getProject();
277:                         projectName = project.getName() + ".viewmodel"; //$NON-NLS-1$
278:                 } else {
279:                         // we have a package selected from the package registry
280:•                        if (EPackage.class.isInstance(selectedContainer)) {
281:                                 final EPackage ePackage = EPackage.class.cast(selectedContainer);
282:                                 projectName = ePackage.getName() + ".viewmodel"; //$NON-NLS-1$
283:                         }
284:                 }
285:
286:                 String name = projectName;
287:                 for (int i = 1; ResourcesPlugin.getWorkspace().getRoot()
288:•                        .getProject(name).exists(); ++i) {
289:                         name = projectName + i;
290:                 }
291:                 return name;
292:         }
293:
294:         /*
295:          * (non-Javadoc)
296:          * @see
297:          * org.eclipse.jface.wizard.Wizard#getNextPage(org.eclipse.jface.wizard.
298:          * IWizardPage)
299:          */
300:         @Override
301:         public IWizardPage getNextPage(IWizardPage page) {
302:
303:•                if (page == firstPage) {
304:                         projectName = firstPage.getProjectName();
305:•                        if (selectedContainer == null) {
306:                                 selectEcorePage = new SelectEcorePage(PLUGIN_ID);
307:                                 addPage(selectEcorePage);
308:                                 return selectEcorePage;
309:                         }
310:                         selectEClassPage = new SelectEClassForViewWizardPage();
311:                         selectEClassPage.setSelectedEPackage(getEPackage());
312:                         addPage(selectEClassPage);
313:                         selectEClassPage.setPageComplete(true);
314:                         return selectEClassPage;
315:•                } else if (page == selectEcorePage) {
316:                         selectedContainer = selectEcorePage.getSelectedContainer();
317:                         selectEClassPage = new SelectEClassForViewWizardPage();
318:                         selectEClassPage.setSelectedEPackage(getEPackage());
319:                         addPage(selectEClassPage);
320:                         selectEClassPage.setPageComplete(true);
321:                         return selectEClassPage;
322:                 }
323:                 return null;
324:         }
325:
326:         /**
327:          * {@inheritDoc}
328:          *
329:          * @see org.eclipse.jface.wizard.Wizard#getPreviousPage(org.eclipse.jface.wizard.IWizardPage)
330:          */
331:         @Override
332:         public IWizardPage getPreviousPage(IWizardPage page) {
333:•                if (page == selectEClassPage) {
334:                         // deactivate Finish button
335:                         selectedEClasses.clear();
336:                 }
337:                 return super.getPreviousPage(page);
338:         }
339:
340:         /*
341:          * (non-Javadoc)
342:          * @see org.eclipse.jface.wizard.Wizard#canFinish()
343:          */
344:         @Override
345:         public boolean canFinish() {
346:•                if (selectEClassPage != null) {
347:                         selectedEClasses = selectEClassPage
348:                                 .getSelectedEClasses();
349:•                        return selectedEClasses != null && !selectedEClasses.isEmpty();
350:                 }
351:                 return false;
352:         }
353:
354:         /*
355:          * (non-Javadoc)
356:          * @see
357:          * org.eclipse.emf.common.ui.wizard.AbstractExampleInstallerWizard#performFinish
358:          * ()
359:          */
360:         @Override
361:         public boolean performFinish() {
362:                 // install the project template in the workspace
363:                 boolean result = super.performFinish();
364:                 // update the project and project files
365:•                result &= createViewModelFiles() && renamePlugin();// updateViewModelPluginFilePath() &&
366:
367:•                if (result) {
368:                         final IWorkbenchPage page = workbench.getActiveWorkbenchWindow().getActivePage();
369:                         try {
370:•                                for (final IFile viewModelFile : viewModelFiles) {
371:                                         page.openEditor(new FileEditorInput(viewModelFile),
372:                                                 workbench.getEditorRegistry().getDefaultEditor(viewModelFile.getFullPath().toString()).getId());
373:                                 }
374:                         } catch (final PartInitException ex) {
375:                                 Activator.getDefault().getLog()
376:                                         .log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, ex.getMessage(), ex));
377:                         }
378:                 }
379:                 return result;
380:         }
381:
382:         private boolean renamePlugin() {
383:
384:                 // get manifest file
385:                 final IProject p = projectDescriptors.get(0).getProject();
386:
387:                 try {
388:                         // rename project description (in .project file)
389:                         updateProjectDescriptionFile(p);
390:                         final IFile manifestFile = ResourcesPlugin.getWorkspace().getRoot()
391:                                 .getFile(p.getFolder("META-INF").getFullPath().append("MANIFEST.MF")); //$NON-NLS-1$ //$NON-NLS-2$
392:                         // change bundle-name and bundle-symbolicname
393:                         final BufferedReader in = new BufferedReader(new InputStreamReader(manifestFile.getContents()));
394:
395:                         final String bundleNameQualifier = "Bundle-Name:"; //$NON-NLS-1$
396:                         final String bundleSymbolicNameQualifier = "Bundle-SymbolicName:"; //$NON-NLS-1$
397:                         String line = null;
398:                         final StringBuffer contents = new StringBuffer();
399:•                        while ((line = in.readLine()) != null) {
400:•                                if (line.contains(bundleNameQualifier)) {
401:                                         final int startIndex = line.indexOf(bundleNameQualifier) + bundleNameQualifier.length();
402:                                         int endIndex = line.indexOf(";", startIndex); //$NON-NLS-1$
403:•                                        if (endIndex == -1) {
404:                                                 endIndex = line.length();
405:                                         }
406:                                         final String oldBundleName = line.substring(startIndex, endIndex);
407:                                         line = line.replace(oldBundleName, " " + p.getName()); //$NON-NLS-1$
408:•                                } else if (line.contains(bundleSymbolicNameQualifier)) {
409:                                         final int startIndex = line.indexOf(bundleSymbolicNameQualifier)
410:                                                 + bundleSymbolicNameQualifier.length();
411:                                         int endIndex = line.indexOf(";", startIndex); //$NON-NLS-1$
412:•                                        if (endIndex == -1) {
413:                                                 endIndex = line.length();
414:                                         }
415:                                         final String oldBundleSymbolicName = line.substring(startIndex, endIndex);
416:                                         line = line.replace(oldBundleSymbolicName, " " + p.getName()); //$NON-NLS-1$
417:                                 }
418:
419:                                 contents.append(line + "\n"); //$NON-NLS-1$
420:                         }
421:                         in.close();
422:
423:                         final FileWriter out = new FileWriter(manifestFile.getRawLocation().makeAbsolute().toFile());
424:                         out.write(String.valueOf(contents));
425:                         out.flush();
426:                         out.close();
427:
428:                         ResourcesPlugin.getWorkspace().getRoot().getProject(p.getName())
429:                                 .refreshLocal(IResource.DEPTH_INFINITE, null);
430:
431:                 } catch (final CoreException e) {
432:                         Activator.getDefault().getLog().log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, e.getMessage(), e));
433:                 } catch (final IOException e) {
434:                         Activator.getDefault().getLog().log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, e.getMessage(), e));
435:                 }
436:                 return true;
437:         }
438:
439:         /**
440:          * Changes the name of the project in the project description file (.project)
441:          */
442:         private void updateProjectDescriptionFile(IProject p) {
443:                 final IPath filePath = p.getFullPath().append(".project"); //$NON-NLS-1$
444:                 final IFile descriptionFile = ResourcesPlugin.getWorkspace().getRoot()
445:                         .getFile(filePath);
446:                 final DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
447:                 DocumentBuilder dBuilder;
448:                 try {
449:                         dBuilder = dbFactory.newDocumentBuilder();
450:                         final InputStream in = descriptionFile.getContents();
451:                         final Document doc = dBuilder.parse(in);
452:
453:                         final NodeList nList = doc.getElementsByTagName("projectDescription"); //$NON-NLS-1$
454:                         final Element eElement = (Element) nList.item(0);
455:                         eElement.getElementsByTagName("name") //$NON-NLS-1$
456:                                 .item(0).getChildNodes().item(0).setNodeValue(p.getName());
457:
458:                         // write the content into xml file
459:                         final TransformerFactory transformerFactory = TransformerFactory.newInstance();
460:                         final Transformer transformer = transformerFactory.newTransformer();
461:                         transformer.setOutputProperty(OutputKeys.INDENT, "yes"); //$NON-NLS-1$
462:                         final DOMSource source = new DOMSource(doc);
463:                         final StreamResult result = new StreamResult(new StringWriter());
464:                         transformer.transform(source, result);
465:
466:                         final String xmlContent = result.getWriter().toString();
467:                         final FileWriter out = new FileWriter(descriptionFile.getRawLocation().makeAbsolute().toFile());
468:                         out.write(xmlContent);
469:                         out.flush();
470:                         out.close();
471:
472:                         ResourcesPlugin.getWorkspace().getRoot().getProject(p.getName())
473:                                 .refreshLocal(IResource.DEPTH_INFINITE, null);
474:                 } catch (final ParserConfigurationException ex) {
475:                         Activator.getDefault().getLog().log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, ex.getMessage(), ex));
476:                 } catch (final CoreException ex) {
477:                         Activator.getDefault().getLog().log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, ex.getMessage(), ex));
478:                 } catch (final SAXException ex) {
479:                         Activator.getDefault().getLog().log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, ex.getMessage(), ex));
480:                 } catch (final IOException ex) {
481:                         Activator.getDefault().getLog().log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, ex.getMessage(), ex));
482:                 } catch (final TransformerConfigurationException ex) {
483:                         Activator.getDefault().getLog().log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, ex.getMessage(), ex));
484:                 } catch (final TransformerException ex) {
485:                         Activator.getDefault().getLog().log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, ex.getMessage(), ex));
486:                 }
487:
488:         }
489:
490:         private boolean createViewModelFiles() {
491:                 final IProject p = projectDescriptors.get(0).getProject();
492:                 final IPath folderPath = p.getFolder("viewmodels").getFullPath(); //$NON-NLS-1$
493:                 final boolean generateViewModelControls = selectEClassPage.isGenerateViewModelOptionSelected();
494:
495:•                for (final EClass eclass : selectedEClasses) {
496:                         final IPath filePath = folderPath.append(eclass.getName() + ".view"); //$NON-NLS-1$
497:                         final IFile viewModelFile = ResourcesPlugin.getWorkspace().getRoot().getFile(filePath);
498:
499:                         try {
500:                                 final IDEViewModelRegistry registry = getViewModelRegistry();
501:•                                if (registry != null) {
502:                                         final VView view = ViewModelHelper.createViewModel(viewModelFile, eclass, getSelectedEcore());
503:•                                        if (generateViewModelControls) {
504:                                                 ControlGenerator.generateAllControls(view);
505:                                         }
506:                                         addContribution(viewModelFile);
507:                                         viewModelFiles.add(viewModelFile);
508:                                 }
509:
510:                         } catch (final IOException e) {
511:                                 Activator.getDefault().getLog().log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, e.getMessage(), e));
512:                                 return false;
513:                         }
514:                 }
515:
516:                 return true;
517:         }
518:
519:         /**
520:          * @param modelFile
521:          */
522:         protected void addContribution(IFile modelFile) {
523:
524:                 final IProject project = modelFile.getProject();
525:                 final IFile pluginFile = project.getFile("plugin.xml"); //$NON-NLS-1$
526:                 try {
527:                         final BufferedReader in = new BufferedReader(new InputStreamReader(pluginFile.getContents()));
528:                         final String extension = "org.eclipse.emf.ecp.view.model.provider.xmi.file"; //$NON-NLS-1$
529:                         String line = null;
530:                         final StringBuffer contents = new StringBuffer();
531:                         boolean extensionAdded = false;
532:•                        while ((line = in.readLine()) != null) {
533:•                                if (line.contains(extension)) {
534:                                         final int end = line.indexOf("/>"); //$NON-NLS-1$
535:•                                        if (end != -1) {
536:                                                 final String filePathAttribute = "<file filePath=\"" //$NON-NLS-1$
537:                                                         + modelFile.getProjectRelativePath().toString() + "\"/>"; //$NON-NLS-1$
538:
539:                                                 line = line.substring(0, end)
540:                                                         + ">\n" + filePathAttribute + "\n</extension>\n" + line.substring(end + 2, line.length()); //$NON-NLS-1$ //$NON-NLS-2$
541:                                         } else {
542:                                                 final String filePathAttribute = "<file filePath=\"" //$NON-NLS-1$
543:                                                         + modelFile.getProjectRelativePath().toString() + "\"/>"; //$NON-NLS-1$
544:                                                 line = line.concat("\n" + filePathAttribute + "\n"); //$NON-NLS-1$ //$NON-NLS-2$
545:                                         }
546:                                         extensionAdded = true;
547:                                 }
548:•                                if (line.contains("</plugin>") && !extensionAdded) { //$NON-NLS-1$
549:                                         final int end = line.indexOf("</plugin>"); //$NON-NLS-1$
550:                                         line = line.substring(0, end)
551:                                                 + "\n<extension point=\"org.eclipse.emf.ecp.view.model.provider.xmi.file\">\n<file filePath=\"" //$NON-NLS-1$
552:                                                 + modelFile.getProjectRelativePath().toString() + "\"/>\n</extension>\n" + line.substring(end); //$NON-NLS-1$
553:                                 }
554:
555:                                 contents.append(line + "\n"); //$NON-NLS-1$
556:                         }
557:                         in.close();
558:
559:                         final FileWriter out = new FileWriter(pluginFile.getRawLocation().makeAbsolute().toFile());
560:                         out.write(String.valueOf(contents));
561:                         out.flush();
562:                         out.close();
563:
564:                         project.refreshLocal(IResource.DEPTH_INFINITE, null);
565:                 } catch (final CoreException e) {
566:                         ViewEditorPlugin.INSTANCE.log(new Status(IStatus.ERROR, PLUGIN_ID, e.getMessage(), e));
567:                 } catch (final IOException e) {
568:                         ViewEditorPlugin.INSTANCE.log(new Status(IStatus.ERROR, PLUGIN_ID, e.getMessage(), e));
569:                 }
570:         }
571:
572:         /**
573:          * @param selection the selection to set
574:          */
575:         public void setSelection(IStructuredSelection selection) {
576:                 this.selection = selection;
577:         }
578:
579:         /**
580:          * Return the {@link IDEViewModelRegistry}.
581:          *
582:          * @return the {@link IDEViewModelRegistry}
583:          */
584:         public static IDEViewModelRegistry getViewModelRegistry() {
585:
586:                 final ServiceReference<IDEViewModelRegistry> serviceReference = ViewEditorPlugin.getPlugin().getBundle()
587:                         .getBundleContext()
588:                         .getServiceReference(IDEViewModelRegistry.class);
589:•                if (serviceReference == null) {
590:                         return null;
591:                 }
592:                 return ViewEditorPlugin.getPlugin().getBundle().getBundleContext().getService(serviceReference);
593:         }
594:
595: }