Skip to content

Package: ViewModelWizard$1

ViewModelWizard$1

nameinstructionbranchcomplexitylinemethod
execute(IProgressMonitor)
M: 14 C: 81
85%
M: 2 C: 6
75%
M: 2 C: 3
60%
M: 4 C: 21
84%
M: 0 C: 1
100%
{...}
M: 0 C: 6
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
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: * Eugen Neufeld - initial API and implementation
13: */
14: package org.eclipse.emf.ecp.view.model.presentation;
15:
16: import java.io.BufferedReader;
17: import java.io.ByteArrayInputStream;
18: import java.io.FileWriter;
19: import java.io.IOException;
20: import java.io.InputStreamReader;
21: import java.text.MessageFormat;
22: import java.util.ArrayList;
23: import java.util.Arrays;
24: import java.util.Collection;
25: import java.util.Collections;
26: import java.util.List;
27:
28: import org.eclipse.core.resources.IFile;
29: import org.eclipse.core.resources.IProject;
30: import org.eclipse.core.resources.IResource;
31: import org.eclipse.core.runtime.CoreException;
32: import org.eclipse.core.runtime.IProgressMonitor;
33: import org.eclipse.core.runtime.IStatus;
34: import org.eclipse.core.runtime.Status;
35: import org.eclipse.emf.common.CommonPlugin;
36: import org.eclipse.emf.common.util.EList;
37: import org.eclipse.emf.common.util.URI;
38: import org.eclipse.emf.ecore.EClass;
39: import org.eclipse.emf.ecore.EClassifier;
40: import org.eclipse.emf.ecore.EObject;
41: import org.eclipse.emf.ecore.EPackage;
42: import org.eclipse.emf.ecore.plugin.EcorePlugin;
43: import org.eclipse.emf.ecore.resource.Resource;
44: import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
45: import org.eclipse.emf.ecore.xmi.XMLResource;
46: import org.eclipse.emf.ecp.ide.spi.util.ViewModelHelper;
47: import org.eclipse.emf.ecp.ide.view.service.IDEViewModelRegistry;
48: import org.eclipse.emf.ecp.view.internal.editor.handler.ControlGenerator;
49: import org.eclipse.emf.ecp.view.internal.editor.handler.SelectEcorePage;
50: import org.eclipse.emf.ecp.view.spi.model.VView;
51: import org.eclipse.emf.ecp.view.spi.model.VViewFactory;
52: import org.eclipse.emf.ecp.view.spi.model.VViewPackage;
53: import org.eclipse.emf.edit.ui.provider.ExtendedImageRegistry;
54: import org.eclipse.jface.viewers.IStructuredSelection;
55: import org.eclipse.jface.wizard.IWizardPage;
56: import org.eclipse.jface.wizard.Wizard;
57: import org.eclipse.ui.INewWizard;
58: import org.eclipse.ui.IWorkbench;
59: import org.eclipse.ui.IWorkbenchPage;
60: import org.eclipse.ui.actions.WorkspaceModifyOperation;
61: import org.eclipse.ui.part.FileEditorInput;
62: import org.osgi.framework.ServiceReference;
63:
64: /**
65: * This is a simple wizard for creating a new model file. <!-- begin-user-doc
66: * --> <!-- end-user-doc -->
67: *
68: * @generated
69: */
70: public class ViewModelWizard extends Wizard implements INewWizard {
71:
72:         private static final String PLUGIN_ID = "org.eclipse.emf.ecp.view.model.presentation"; //$NON-NLS-1$
73:         private Object selectedContainer;
74:         private List<EClass> selectedEClasses;
75:         private List<ViewModelWizardNewFileCreationPage> fileCreationPages;
76:
77:         /**
78:          * @param selectedContainer the selectedContainer to set
79:          */
80:         public void setSelectedContainer(Object selectedContainer) {
81:                 this.selectedContainer = selectedContainer;
82:         }
83:
84:         public void clearSelectedContainer() {
85:                 selectedContainer = null;
86:                 if (selectEcorePage != null) {
87:                         selectEcorePage.setSelectedContainer(null);
88:                 }
89:         }
90:
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:          * A formatted list of supported file extensions, suitable for display. <!--
104:          * begin-user-doc --> <!-- end-user-doc -->
105:          *
106:          * @generated
107:          */
108:         public static final String FORMATTED_FILE_EXTENSIONS = ViewEditorPlugin.INSTANCE
109:                 .getString("_UI_ViewEditorFilenameExtensions").replaceAll( //$NON-NLS-1$
110:                         "\\s*,\\s*", ", "); //$NON-NLS-1$ //$NON-NLS-2$
111:
112:         /**
113:          * This caches an instance of the model package. <!-- begin-user-doc -->
114:          * <!-- end-user-doc -->
115:          *
116:          * @generated
117:          */
118:         protected VViewPackage viewPackage = VViewPackage.eINSTANCE;
119:
120:         /**
121:          * This caches an instance of the model factory. <!-- begin-user-doc -->
122:          * <!-- end-user-doc -->
123:          *
124:          * @generated
125:          */
126:         protected VViewFactory viewFactory = viewPackage.getViewFactory();
127:
128:         /**
129:          * This is the file creation page. <!-- begin-user-doc --> <!-- end-user-doc
130:          * -->
131:          *
132:          * @generated
133:          */
134:
135:         private IncludeViewModelProviderXmiFileExtensionPage contributeToFileExtensionPage;
136:
137:         /**
138:          * Remember the selection during initialization for populating the default
139:          * container. <!-- begin-user-doc --> <!-- end-user-doc -->
140:          *
141:          * @generated
142:          */
143:         protected IStructuredSelection selection;
144:
145:         /**
146:          * @param selection the selection to set
147:          */
148:         public void setSelection(IStructuredSelection selection) {
149:                 this.selection = selection;
150:         }
151:
152:         /**
153:          * Remember the workbench during initialization. <!-- begin-user-doc -->
154:          * <!-- end-user-doc -->
155:          *
156:          * @generated
157:          */
158:         protected IWorkbench workbench;
159:
160:         /**
161:          * Caches the names of the types that can be created as the root object.
162:          * <!-- begin-user-doc --> <!-- end-user-doc -->
163:          *
164:          * @generated
165:          */
166:         protected List<String> initialObjectNames;
167:
168:         private SelectEcorePage selectEcorePage;
169:         private SelectEClassForViewWizardPage selectEClassPage;
170:
171:         /**
172:          * This just records the information. <!-- begin-user-doc --> <!--
173:          * end-user-doc -->
174:          *
175:          * @generated
176:          */
177:         @Override
178:         public void init(IWorkbench workbench, IStructuredSelection selection) {
179:                 this.workbench = workbench;
180:                 this.selection = selection;
181:                 setWindowTitle(ViewEditorPlugin.INSTANCE.getString("_UI_Wizard_label")); //$NON-NLS-1$
182:                 setDefaultPageImageDescriptor(ExtendedImageRegistry.INSTANCE
183:                         .getImageDescriptor(ViewEditorPlugin.INSTANCE
184:                                 .getImage("full/wizban/NewView"))); //$NON-NLS-1$
185:         }
186:
187:         /**
188:          * Returns the names of the types that can be created as the root object.
189:          * <!-- begin-user-doc --> <!-- end-user-doc -->
190:          *
191:          * @generated
192:          */
193:         protected Collection<String> getInitialObjectNames() {
194:                 if (initialObjectNames == null) {
195:                         initialObjectNames = new ArrayList<String>();
196:                         for (final EClassifier eClassifier : viewPackage.getEClassifiers()) {
197:                                 if (eClassifier instanceof EClass) {
198:                                         final EClass eClass = (EClass) eClassifier;
199:                                         if (!eClass.isAbstract()) {
200:                                                 initialObjectNames.add(eClass.getName());
201:                                         }
202:                                 }
203:                         }
204:                         Collections.sort(initialObjectNames,
205:                                 CommonPlugin.INSTANCE.getComparator());
206:                 }
207:                 return initialObjectNames;
208:         }
209:
210:         /**
211:          * The framework calls this to create the contents of the wizard. <!--
212:          * begin-user-doc --> <!-- end-user-doc -->
213:          *
214:          * @generated
215:          */
216:         @Override
217:         public void addPages() {
218:                 selectEcorePage = new SelectEcorePage(PLUGIN_ID);
219:                 addPage(selectEcorePage);
220:
221:                 contributeToFileExtensionPage = new IncludeViewModelProviderXmiFileExtensionPage(PLUGIN_ID);
222:                 addPage(contributeToFileExtensionPage);
223:         }
224:
225:         @Override
226:         public IWizardPage getStartingPage() {
227:                 if (selectedContainer == null) {
228:                         return selectEcorePage;
229:                 }
230:
231:                 selectEClassPage = new SelectEClassForViewWizardPage();
232:                 selectEClassPage.setSelectedEPackage(getEPackage());
233:                 selectEClassPage.setPageComplete(true);
234:                 addPage(selectEClassPage);
235:                 return selectEClassPage;
236:         }
237:
238:         @Override
239:         public IWizardPage getNextPage(IWizardPage page) {
240:                 if (page == selectEcorePage) {
241:                         selectedEClasses = null;
242:                         selectedContainer = selectEcorePage.getSelectedContainer();
243:                         if (selectedContainer != null) {
244:                                 if (selectEClassPage == null) {
245:                                         selectEClassPage = new SelectEClassForViewWizardPage();
246:                                         selectEClassPage.setPageComplete(true);
247:                                         addPage(selectEClassPage);
248:                                 }
249:                                 selectEClassPage.setSelectedEPackage(getEPackage());
250:                                 return selectEClassPage;
251:                         }
252:                         return null;
253:                 } else if (page == selectEClassPage) {
254:                         selectedEClasses = selectEClassPage.getSelectedEClasses();
255:                         fileCreationPages = new ArrayList<ViewModelWizardNewFileCreationPage>();
256:                         if (selectedEClasses != null && !selectedEClasses.isEmpty()) {
257:                                 for (final EClass eclass : selectedEClasses) {
258:                                         final ViewModelWizardNewFileCreationPage fileCreationPage = getNewFileCreationPage();
259:                                         fileCreationPage.setEClass(eclass);
260:                                         fileCreationPage.setWizard(this);
261:                                         fileCreationPages.add(fileCreationPage);
262:                                 }
263:                                 if (fileCreationPages.isEmpty()) {
264:                                         return null;
265:                                 }
266:                                 return fileCreationPages.get(0);
267:                         }
268:                 } else if (ViewModelWizardNewFileCreationPage.class.isInstance(page)) {
269:                         final int i = fileCreationPages.indexOf(page);
270:                         if (i < fileCreationPages.size() - 1) {
271:                                 return fileCreationPages.get(i + 1);
272:                         }
273:                         if (shouldShowFileExtensionPage()) {
274:                                 return contributeToFileExtensionPage;
275:                         }
276:                 }
277:                 return null;
278:         }
279:
280:         /**
281:          * @return <code>true</code> if at least one of the locations in which the viewmodels are created is a Plugin
282:          * Project
283:          */
284:         private boolean shouldShowFileExtensionPage() {
285:                 for (final ViewModelWizardNewFileCreationPage page : fileCreationPages) {
286:                         if (viewModelContainerIsPluginProject(page)) {
287:                                 return true;
288:                         }
289:                 }
290:                 return false;
291:         }
292:
293:         private EPackage getEPackage() {
294:                 EPackage ePackage = null;
295:                 if (EPackage.class.isInstance(selectedContainer)) {
296:                         ePackage = EPackage.class.cast(selectedContainer);
297:                 } else if (IFile.class.isInstance(selectedContainer)) {
298:                         final ResourceSetImpl resourceSet = new ResourceSetImpl();
299:                         // needed to be able to resolve resource paths to plugin paths and thus load referenced ecores
300:                         resourceSet.getURIConverter().getURIMap().putAll(EcorePlugin.computePlatformURIMap(true));
301:                         resourceSet.getLoadOptions().put(XMLResource.OPTION_DEFER_IDREF_RESOLUTION, Boolean.TRUE);
302:
303:                         final String path = ((IFile) selectedContainer).getFullPath().toString();
304:                         final URI uri = URI.createPlatformResourceURI(path, true);
305:
306:                         final Resource resource = resourceSet.getResource(uri, true);
307:                         if (resource != null) {
308:                                 final EList<EObject> contents = resource.getContents();
309:                                 if (contents.size() != 1) {
310:                                         return null;
311:                                 }
312:                                 final EObject object = contents.get(0);
313:                                 if (!(object instanceof EPackage)) {
314:                                         return null;
315:                                 }
316:                                 ePackage = (EPackage) object;
317:                         }
318:                 }
319:                 return ePackage;
320:         }
321:
322:         private ViewModelWizardNewFileCreationPage getNewFileCreationPage() {
323:                 // Create page, set title and the initial model file name.
324:                 final ViewModelWizardNewFileCreationPage newFileCreationPage = new ViewModelWizardNewFileCreationPage(
325:                         "Whatever", selection); //$NON-NLS-1$
326:                 newFileCreationPage.setTitle(ViewEditorPlugin.INSTANCE
327:                         .getString("_UI_ViewModelWizard_label")); //$NON-NLS-1$
328:                 newFileCreationPage.setDescription(ViewEditorPlugin.INSTANCE
329:                         .getString("_UI_ViewModelWizard_description")); //$NON-NLS-1$
330:                 newFileCreationPage.setFileName(ViewEditorPlugin.INSTANCE
331:                         .getString("_UI_ViewEditorFilenameDefaultBase") //$NON-NLS-1$
332:                         + "." //$NON-NLS-1$
333:                         + FILE_EXTENSIONS.get(0));
334:
335:                 return newFileCreationPage;
336:         }
337:
338:         @Override
339:         public IWizardPage getPreviousPage(IWizardPage page) {
340:                 if (page == selectEClassPage) {
341:                         return selectEcorePage;
342:                 } else if (ViewModelWizardNewFileCreationPage.class.isInstance(page)) {
343:                         return selectEClassPage;
344:                 }
345:                 return null;
346:         }
347:
348:         @Override
349:         public boolean canFinish() {
350:                 if (selectEClassPage != null) {
351:                         final List<EClass> selectedClasses = selectEClassPage.getSelectedEClasses();
352:                         if (selectedClasses == null || selectedClasses.isEmpty()) {
353:                                 return false;
354:                         }
355:                         if (fileCreationPages == null || fileCreationPages.isEmpty()) {
356:                                 return false;
357:                         }
358:                         for (final ViewModelWizardNewFileCreationPage page : fileCreationPages) {
359:                                 if (page.getModelFile() == null) {
360:                                         return false;
361:                                 }
362:                         }
363:                         return true;
364:                 }
365:                 return false;
366:         }
367:
368:         /**
369:          * Checks whether the project where the view model will be created is a plugin project.
370:          *
371:          * @return true if the container project is a plugin project
372:          */
373:         private boolean viewModelContainerIsPluginProject(ViewModelWizardNewFileCreationPage page) {
374:                 if (fileCreationPages == null || fileCreationPages.isEmpty()) {
375:                         return false;
376:                 }
377:                 if (page.getModelFile() == null || page.getModelFile().getProject() == null) {
378:                         return false;
379:                 }
380:                 final IProject project = page.getModelFile().getProject();
381:                 return ContributionUtil.isPluginProject(project);
382:         }
383:
384:         /**
385:          * Do the work after everything is specified. <!-- begin-user-doc --> <!--
386:          * end-user-doc -->
387:          *
388:          * @generated
389:          */
390:         @Override
391:         public boolean performFinish() {
392:                 try {
393:                         // Do the work within an operation.
394:                         final WorkspaceModifyOperation operation = new WorkspaceModifyOperation() {
395:                                 @Override
396:                                 protected void execute(IProgressMonitor progressMonitor) {
397:                                         try {
398:                                                 final boolean generateViewModelControls = selectEClassPage
399:                                                         .isGenerateViewModelOptionSelected();
400:
401:                                                 final IDEViewModelRegistry registry = getViewModelRegistry();
402:•                                                if (registry == null) {
403:                                                         ViewEditorPlugin.INSTANCE.log(new Status(IStatus.ERROR, PLUGIN_ID,
404:                                                                 "No View Model Registry", null)); //$NON-NLS-1$
405:                                                         return;
406:                                                 }
407:•                                                for (final ViewModelWizardNewFileCreationPage filePage : fileCreationPages) {
408:                                                         // Remember the file.
409:                                                         final IFile modelFile = filePage.getModelFile();
410:                                                         // create view
411:                                                         final VView view = ViewModelHelper.createViewModel(modelFile, filePage.getEClass(),
412:                                                                 getSelectedEcore());
413:                                                         // generate controls
414:•                                                        if (generateViewModelControls) {
415:                                                                 ControlGenerator.generateAllControls(view);
416:                                                         }
417:                                                         // contribute to ..provider.xmi.file extension point
418:•                                                        if (contributeToFileExtensionPage.isContributeToExtensionOptionSelected()) {
419:                                                                 addContribution(modelFile);
420:                                                         }
421:                                                         // add containing folder to the build.properties file
422:                                                         addToBuildProperties(modelFile);
423:
424:                                                         // Open the view
425:                                                         final IWorkbenchPage page = workbench.getActiveWorkbenchWindow().getActivePage();
426:                                                         page.openEditor(new FileEditorInput(modelFile),
427:                                                                 workbench.getEditorRegistry().getDefaultEditor(modelFile.getFullPath().toString())
428:                                                                         .getId());
429:                                                         modelFile.getProject().refreshLocal(IResource.DEPTH_INFINITE, progressMonitor);
430:                                                 }
431:
432:                                         } catch (final Exception exception) {
433:                                                 ViewEditorPlugin.INSTANCE.log(exception);
434:                                         } finally {
435:                                                 progressMonitor.done();
436:                                         }
437:                                 }
438:                         };
439:
440:                         getContainer().run(false, false, operation);
441:                         return true;
442:                 } catch (final Exception exception) {
443:                         ViewEditorPlugin.INSTANCE.log(exception);
444:                         return false;
445:                 }
446:         }
447:
448:         /**
449:          * @return the Ecore container
450:          */
451:         protected IFile getSelectedEcore() {
452:                 if (IFile.class.isInstance(selectedContainer)) {
453:                         return (IFile) selectedContainer;
454:                 }
455:                 return null;
456:         }
457:
458:         /**
459:          * Adds the folder where the view model is created to the project's build.properties file.
460:          *
461:          * @param modelFile the view model IFile
462:          */
463:         protected void addToBuildProperties(IFile modelFile) {
464:                 final IProject project = modelFile.getProject();
465:                 final String projectRelPath = modelFile.getProjectRelativePath().toString();
466:                 final int lastPathDelimiter = projectRelPath.lastIndexOf("/"); //$NON-NLS-1$
467:                 final String pluginXml = "plugin.xml"; //$NON-NLS-1$
468:                 final String path;
469:                 if (lastPathDelimiter == -1) {
470:                         path = projectRelPath;
471:                 } else {
472:                         path = projectRelPath.substring(0, projectRelPath.lastIndexOf("/") + 1); //$NON-NLS-1$
473:                 }
474:                 final String includes = "bin.includes"; //$NON-NLS-1$
475:                 final IFile buildFile = project.getFile("build.properties"); //$NON-NLS-1$
476:                 try {
477:                         if (!buildFile.exists()) {
478:                                 return;
479:                         }
480:                         final BufferedReader in = new BufferedReader(new InputStreamReader(buildFile.getContents()));
481:                         String line = null;
482:                         final StringBuffer contents = new StringBuffer();
483:                         while ((line = in.readLine()) != null) {
484:                                 if (line.contains(includes)) {
485:                                         boolean found = false;
486:                                         boolean foundPlugin = false;
487:                                         while (line.contains(",\\")) //$NON-NLS-1$
488:                                         {
489:                                                 // entry start
490:                                                 final int start = line.indexOf("="); //$NON-NLS-1$
491:
492:                                                 final String entry = line.substring(start + 1, line.indexOf(",\\")).trim(); //$NON-NLS-1$
493:
494:                                                 if (entry.equals(path)) {
495:                                                         found = true;
496:                                                 }
497:                                                 if (entry.equals(pluginXml)) {
498:                                                         foundPlugin = true;
499:                                                 }
500:                                                 if (found && foundPlugin) {
501:                                                         break;
502:                                                 }
503:                                                 contents.append(line + "\n"); //$NON-NLS-1$
504:                                                 line = in.readLine();
505:
506:                                         }
507:                                         if (!found) {
508:                                                 // check last line
509:                                                 int start = line.indexOf("="); //$NON-NLS-1$
510:                                                 if (start == -1) {
511:                                                         start = 0;
512:                                                 }
513:                                                 final String entry = line.substring(start).trim();
514:                                                 if (!entry.equals(path)) {
515:                                                         contents.append(" " + path + ",\\\n"); //$NON-NLS-1$ //$NON-NLS-2$
516:                                                 }
517:                                         }
518:                                         if (!foundPlugin) {
519:                                                 // check last line
520:                                                 int start = line.indexOf("="); //$NON-NLS-1$
521:                                                 if (start == -1) {
522:                                                         start = 0;
523:                                                 }
524:                                                 final String entry = line.substring(start).trim();
525:                                                 if (!entry.equals(pluginXml)) {
526:                                                         contents.append(" " + pluginXml + ",\\\n"); //$NON-NLS-1$ //$NON-NLS-2$
527:                                                 }
528:                                         }
529:                                 }
530:                                 contents.append(line + "\n"); //$NON-NLS-1$
531:                         }
532:                         in.close();
533:
534:                         final FileWriter out = new FileWriter(buildFile.getRawLocation().makeAbsolute().toFile());
535:                         out.write(String.valueOf(contents));
536:                         out.flush();
537:                         out.close();
538:
539:                         project.refreshLocal(IResource.DEPTH_INFINITE, null);
540:                 } catch (final CoreException e) {
541:                         ViewEditorPlugin.INSTANCE.log(new Status(IStatus.ERROR, PLUGIN_ID, e.getMessage(), e));
542:                 } catch (final IOException e) {
543:                         ViewEditorPlugin.INSTANCE.log(new Status(IStatus.ERROR, PLUGIN_ID, e.getMessage(), e));
544:                 }
545:         }
546:
547:         /**
548:          * Contributes the view model via the "..provider.xmi" extension point. If the project doesn't have a plugin.xml
549:          * file,
550:          * one will be created.
551:          *
552:          * @param modelFile the view model IFile
553:          */
554:         protected void addContribution(IFile modelFile) {
555:                 final IProject project = modelFile.getProject();
556:                 if (!ContributionUtil.isPluginProject(project)) {
557:                         return;
558:                 }
559:                 final boolean isFragmentProject = ContributionUtil.isFragmentProject(project);
560:                 final String contributionFileName = isFragmentProject ? "fragment.xml" : "plugin.xml"; //$NON-NLS-1$ //$NON-NLS-2$
561:                 final IFile pluginFile = project.getFile(contributionFileName);
562:                 try {
563:                         if (!pluginFile.exists()) {
564:                                 final String xmlContents = MessageFormat.format(
565:                                         "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<?eclipse version=\"3.4\"?>\n<{0}>\n</{0}>", //$NON-NLS-1$
566:                                         isFragmentProject ? "fragment" : "plugin"); //$NON-NLS-1$//$NON-NLS-2$
567:                                 pluginFile.create(new ByteArrayInputStream(xmlContents.getBytes()), true, null);
568:                         }
569:                         final BufferedReader in = new BufferedReader(new InputStreamReader(pluginFile.getContents()));
570:                         final String extension = "org.eclipse.emf.ecp.view.model.provider.xmi.file"; //$NON-NLS-1$
571:                         String line = null;
572:                         final StringBuffer contents = new StringBuffer();
573:                         boolean extensionAdded = false;
574:                         while ((line = in.readLine()) != null) {
575:                                 if (line.contains(extension)) {
576:                                         final int end = line.indexOf("/>"); //$NON-NLS-1$
577:                                         if (end != -1) {
578:                                                 final String filePathAttribute = "<file filePath=\"" //$NON-NLS-1$
579:                                                         + modelFile.getProjectRelativePath().toString() + "\"/>"; //$NON-NLS-1$
580:                                                 line = line.substring(0, end)
581:                                                         + ">\n" + filePathAttribute + "\n</extension>\n" + line.substring(end + 2, line.length()); //$NON-NLS-1$ //$NON-NLS-2$
582:                                         } else {
583:                                                 final String filePathAttribute = "<file filePath=\"" //$NON-NLS-1$
584:                                                         + modelFile.getProjectRelativePath().toString() + "\"/>"; //$NON-NLS-1$
585:                                                 line = line.concat("\n" + filePathAttribute + "\n"); //$NON-NLS-1$ //$NON-NLS-2$
586:                                         }
587:                                         extensionAdded = true;
588:                                 }
589:                                 final String eof = isFragmentProject ? "</fragment>" : "</plugin>"; //$NON-NLS-1$ //$NON-NLS-2$
590:                                 if (line.contains(eof) && !extensionAdded) {
591:                                         final int end = line.indexOf(eof);
592:                                         line = line.substring(0, end)
593:                                                 + "\n<extension point=\"org.eclipse.emf.ecp.view.model.provider.xmi.file\">\n<file filePath=\"" //$NON-NLS-1$
594:                                                 + modelFile.getProjectRelativePath().toString()
595:                                                 + "\"/>\n</extension>\n" + line.substring(end); //$NON-NLS-1$
596:                                 }
597:                                 contents.append(line + "\n"); //$NON-NLS-1$
598:                         }
599:                         in.close();
600:
601:                         final FileWriter out = new FileWriter(pluginFile.getRawLocation().makeAbsolute().toFile());
602:                         out.write(String.valueOf(contents));
603:                         out.flush();
604:                         out.close();
605:
606:                 } catch (final CoreException e) {
607:                         ViewEditorPlugin.INSTANCE.log(new Status(IStatus.ERROR, PLUGIN_ID, e.getMessage(), e));
608:                 } catch (final IOException e) {
609:                         ViewEditorPlugin.INSTANCE.log(new Status(IStatus.ERROR, PLUGIN_ID, e.getMessage(), e));
610:                 }
611:         }
612:
613:         /**
614:          * Return the {@link IDEViewModelRegistry}.
615:          *
616:          * @return the {@link IDEViewModelRegistry}
617:          */
618:         public static IDEViewModelRegistry getViewModelRegistry() {
619:                 final ServiceReference<IDEViewModelRegistry> serviceReference = ViewEditorPlugin.getPlugin().getBundle()
620:                         .getBundleContext()
621:                         .getServiceReference(IDEViewModelRegistry.class);
622:                 if (serviceReference == null) {
623:                         return null;
624:                 }
625:                 return ViewEditorPlugin.getPlugin().getBundle().getBundleContext().getService(serviceReference);
626:         }
627:
628:         /**
629:          * @param workbench the workbench to set
630:          */
631:         public void setWorkbench(IWorkbench workbench) {
632:                 this.workbench = workbench;
633:         }
634: }