Skip to content

Package: CreateModelsWorkspaceModifyOperation

CreateModelsWorkspaceModifyOperation

nameinstructionbranchcomplexitylinemethod
CreateModelsWorkspaceModifyOperation(IFile, String, String, String)
M: 30 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 9 C: 0
0%
M: 1 C: 0
0%
createInitialModel()
M: 21 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
execute(IProgressMonitor)
M: 132 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 36 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.operations;
31:
32: import java.io.IOException;
33: import java.util.HashMap;
34: import java.util.Map;
35:
36: import org.eclipse.core.resources.IFile;
37: import org.eclipse.core.resources.ResourcesPlugin;
38: import org.eclipse.core.runtime.IProgressMonitor;
39: import org.eclipse.emf.codegen.ecore.genmodel.provider.GenModelEditPlugin;
40: import org.eclipse.emf.common.util.URI;
41: import org.eclipse.emf.ecore.EClass;
42: import org.eclipse.emf.ecore.ENamedElement;
43: import org.eclipse.emf.ecore.EObject;
44: import org.eclipse.emf.ecore.EPackage;
45: import org.eclipse.emf.ecore.EcoreFactory;
46: import org.eclipse.emf.ecore.EcorePackage;
47: import org.eclipse.emf.ecore.plugin.EcorePlugin;
48: import org.eclipse.emf.ecore.resource.Resource;
49: import org.eclipse.emf.ecore.resource.ResourceSet;
50: import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
51: import org.eclipse.emf.ecore.xmi.XMLResource;
52: import org.eclipse.emf.ecp.ecore.editor.IEcoreGenModelLinker;
53: import org.eclipse.emf.ecp.ecore.editor.factory.EcoreGenModelLinkerFactory;
54: import org.eclipse.emf.ecp.ecore.editor.util.EcoreGenException;
55: import org.eclipse.emf.ecp.ecore.editor.util.ProjectHelper;
56: import org.eclipse.ui.actions.WorkspaceModifyOperation;
57:
58: /**
59: * Operation that modifies the workspace.
60: */
61: public class CreateModelsWorkspaceModifyOperation extends
62:         WorkspaceModifyOperation {
63:
64:         private final IFile modelFile;
65:         private final ProjectHelper projectHelper = new ProjectHelper("");
66:         private final String initialObjectName;
67:         private final String encoding;
68:         private final EcorePackage ecorePackage = EcorePackage.eINSTANCE;
69:         private final EcoreFactory ecoreFactory = ecorePackage.getEcoreFactory();
70:
71:         /**
72:          * Constructor.
73:          *
74:          * @param modelFile
75:          * the model file
76:          * @param initialObjectName
77:          * initial object name
78:          * @param encoding
79:          * the encoding
80:          * @param projectFullName
81:          * name of the project
82:          */
83:         public CreateModelsWorkspaceModifyOperation(IFile modelFile,
84:                 String initialObjectName, String encoding, String projectFullName) {
85:                 this.modelFile = modelFile;
86:                 this.initialObjectName = initialObjectName;
87:                 this.encoding = encoding;
88:                 projectHelper.setProjectFullName(projectFullName);
89:         }
90:
91:         private EObject createInitialModel() {
92:                 final EClass eClass = (EClass) ecorePackage.getEClassifier(initialObjectName);
93:                 final EObject rootObject = ecoreFactory.create(eClass);
94:
95:                 // We can't have a null name, in case we're using EMOF serialization.
96:•                if (rootObject instanceof ENamedElement) {
97:                         ((ENamedElement) rootObject).setName("");
98:                 }
99:                 return rootObject;
100:         }
101:
102:         @Override
103:         protected void execute(IProgressMonitor progressMonitor) {
104:                 try {
105:                         // Create a resource set
106:                         //
107:                         final ResourceSet resourceSet = new ResourceSetImpl();
108:                         resourceSet.getURIConverter().getURIMap()
109:                                 .putAll(EcorePlugin.computePlatformURIMap(true));
110:
111:                         // Get the URI of the model file.
112:                         //
113:                         final URI fileURI = URI.createPlatformResourceURI(modelFile.getFullPath()
114:                                 .toString(), true);
115:
116:                         // Create a resource for this file. Don't specify a
117:                         // content type, as it could be Ecore or EMOF.
118:                         //
119:                         final Resource resource = resourceSet.createResource(fileURI);
120:
121:                         // Add the initial model object to the contents.
122:                         //
123:                         final EObject rootObject = createInitialModel();
124:•                        if (rootObject != null) {
125:                                 resource.getContents().add(rootObject);
126:                         }
127:
128:                         // Save the contents of the resource to the file system.
129:                         final Map<Object, Object> options = new HashMap<Object, Object>();
130:                         options.put(XMLResource.OPTION_ENCODING, encoding);
131:                         options.put(Resource.OPTION_LINE_DELIMITER,
132:                                 Resource.OPTION_LINE_DELIMITER_UNSPECIFIED);
133:                         EPackage ecorepack = EcorePackage.eINSTANCE;
134:                         ecorepack = (EPackage) resource.getContents().get(0);
135:                         ecorepack.setName(projectHelper.getProjectName());
136:                         ecorepack.setNsPrefix(projectHelper.getNSPrefix());
137:                         ecorepack.setNsURI(projectHelper.getNSURL());
138:                         resource.save(options);
139:                         final IEcoreGenModelLinker linker = EcoreGenModelLinkerFactory
140:                                 .getEcoreGenModelLinker();
141:                         final String genModelPath = ResourcesPlugin.getWorkspace().getRoot()
142:                                 .getFile(modelFile.getLocation()).getFullPath()
143:                                 .removeLastSegments(1).toOSString()
144:                                 + "/model.genmodel";
145:                         final String ecorePath = modelFile.getLocation().toFile().getAbsolutePath(); // ResourcesPlugin.getWorkspace().getRoot()
146:                         // .getFile(modelFile.getLocation()).getFullPath()
147:                         // .toOSString();
148:                         final String projectPath = "/"
149:                                 + modelFile.getLocation().segment(
150:                                         modelFile.getLocation().segmentCount() - 3);
151:                         linker.generateGenModel(ecorePath, genModelPath, projectPath);
152:                 } catch (final EcoreGenException ex) {
153:                         GenModelEditPlugin.INSTANCE.log(ex);
154:                 } catch (final IOException ex) {
155:                         GenModelEditPlugin.INSTANCE.log(ex);
156:                 } finally {
157:                         progressMonitor.done();
158:                 }
159:         }
160:
161: }