Skip to content

Package: CreateNewInstaceWizard$CreateNewInstanceFileCreationPage

CreateNewInstaceWizard$CreateNewInstanceFileCreationPage

nameinstructionbranchcomplexitylinemethod
CreateNewInstaceWizard.CreateNewInstanceFileCreationPage(CreateNewInstaceWizard, String, IStructuredSelection)
M: 8 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
getModelFile()
M: 9 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
validatePage()
M: 25 C: 0
0%
M: 6 C: 0
0%
M: 4 C: 0
0%
M: 7 C: 0
0%
M: 1 C: 0
0%

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: * Lucas Koehler - initial API and implementation
13: ******************************************************************************/
14: package org.eclipse.emfforms.internal.editor.ecore.actions;
15:
16: import java.io.IOException;
17: import java.lang.reflect.InvocationTargetException;
18: import java.util.Collections;
19: import java.util.HashMap;
20: import java.util.List;
21: import java.util.Map;
22:
23: import org.eclipse.core.resources.IFile;
24: import org.eclipse.core.resources.ResourcesPlugin;
25: import org.eclipse.core.runtime.IProgressMonitor;
26: import org.eclipse.core.runtime.IStatus;
27: import org.eclipse.core.runtime.Path;
28: import org.eclipse.core.runtime.Status;
29: import org.eclipse.emf.common.util.URI;
30: import org.eclipse.emf.ecore.EClass;
31: import org.eclipse.emf.ecore.EObject;
32: import org.eclipse.emf.ecore.resource.Resource;
33: import org.eclipse.emf.ecore.resource.ResourceSet;
34: import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
35: import org.eclipse.emf.ecore.util.EcoreUtil;
36: import org.eclipse.emf.ecore.xmi.XMLResource;
37: import org.eclipse.jface.dialogs.ErrorDialog;
38: import org.eclipse.jface.viewers.IStructuredSelection;
39: import org.eclipse.jface.viewers.StructuredSelection;
40: import org.eclipse.jface.wizard.Wizard;
41: import org.eclipse.ui.actions.WorkspaceModifyOperation;
42: import org.eclipse.ui.dialogs.WizardNewFileCreationPage;
43:
44: /**
45: * A wizard to create a new instance of an {@link EClass} and save it into a file.
46: *
47: * @author Lucas Koehler
48: *
49: */
50: public class CreateNewInstaceWizard extends Wizard {
51:
52:         /**
53:          * The supported extensions for created files.
54:          */
55:         public static final List<String> FILE_EXTENSIONS = Collections.singletonList("xmi");
56:
57:         /**
58:          * A formatted list of supported file extensions, suitable for display.
59:          */
60:         public static final String FORMATTED_FILE_EXTENSIONS = ".xmi";
61:
62:         private CreateNewInstanceFileCreationPage newFileCreationPage;
63:
64:         /**
65:          * The EClass for which a new instance is created and saved.
66:          */
67:         private final EClass eClass;
68:
69:         /**
70:          * Default constructor.
71:          *
72:          * @param eClass The {@link EClass} for which a new instance is created and saved in a file
73:          */
74:         public CreateNewInstaceWizard(EClass eClass) {
75:                 this.eClass = eClass;
76:         }
77:
78:         /**
79:          * {@inheritDoc}
80:          */
81:         @Override
82:         public boolean performFinish() {
83:
84:                 // Remember the file.
85:                 final IFile modelFile = getModelFile();
86:
87:                 // Do the work within an operation.
88:                 final WorkspaceModifyOperation operation = new WorkspaceModifyOperation() {
89:                         @Override
90:                         protected void execute(IProgressMonitor progressMonitor) {
91:                                 // Create a resource set
92:                                 final ResourceSet resourceSet = new ResourceSetImpl();
93:
94:                                 // Get the URI of the model file.
95:                                 final URI fileURI = URI.createPlatformResourceURI(modelFile.getFullPath().toString(), true);
96:
97:                                 // Create a resource for this file.
98:                                 final Resource resource = resourceSet.createResource(fileURI);
99:
100:                                 final EObject instance = EcoreUtil.create(eClass);
101:                                 resource.getContents().add(instance);
102:
103:                                 // Save the contents of the resource to the file system.
104:                                 final Map<Object, Object> options = new HashMap<Object, Object>();
105:                                 options.put(XMLResource.OPTION_ENCODING, "UTF-8");
106:                                 options.put(XMLResource.OPTION_SCHEMA_LOCATION, Boolean.TRUE);
107:                                 options.put(XMLResource.OPTION_LINE_WIDTH, 80);
108:                                 try {
109:                                         resource.save(options);
110:                                 } catch (final IOException exception) {
111:                                         openErrorDialog(exception);
112:                                         return;
113:                                 } finally {
114:                                         progressMonitor.done();
115:                                 }
116:                         }
117:                 };
118:
119:                 try {
120:                         getContainer().run(false, false, operation);
121:                         return true;
122:                 } catch (final InvocationTargetException exception) {
123:                         openErrorDialog(exception);
124:                         return false;
125:                 } catch (final InterruptedException exception) {
126:                         openErrorDialog(exception);
127:                         return false;
128:                 }
129:         }
130:
131:         /**
132:          * @param exception The {@link Exception} that caused the error.
133:          */
134:         private void openErrorDialog(final Exception exception) {
135:                 final Status status = new Status(IStatus.ERROR, "org.eclipse.emfforms.editor.ecore",
136:                         "Could not create instance file.", exception);
137:                 ErrorDialog.openError(getShell(), "Error", null, status);
138:         }
139:
140:         /**
141:          * The wizard page to select in which file the new instance will be saved.
142:          */
143:         public class CreateNewInstanceFileCreationPage extends WizardNewFileCreationPage {
144:                 /**
145:                  * Default constructor.
146:                  *
147:                  * @param pageId This page's id
148:                  * @param selection The current selection
149:                  */
150:                 public CreateNewInstanceFileCreationPage(String pageId, IStructuredSelection selection) {
151:                         super(pageId, selection);
152:                 }
153:
154:                 /**
155:                  * The framework calls this to see if the file is correct.
156:                  * <!-- begin-user-doc -->
157:                  * <!-- end-user-doc -->
158:                  *
159:                  * @generated
160:                  */
161:                 @Override
162:                 protected boolean validatePage() {
163:•                        if (super.validatePage()) {
164:                                 final String extension = new Path(getFileName()).getFileExtension();
165:•                                if (extension == null || !FILE_EXTENSIONS.contains(extension)) {
166:                                         setErrorMessage("Wrong file extension: Use " + FORMATTED_FILE_EXTENSIONS);
167:                                         return false;
168:                                 }
169:                                 return true;
170:                         }
171:                         return false;
172:                 }
173:
174:                 public IFile getModelFile() {
175:                         return ResourcesPlugin.getWorkspace().getRoot().getFile(getContainerFullPath().append(getFileName()));
176:                 }
177:         }
178:
179:         /**
180:          * {@inheritDoc}
181:          */
182:         @Override
183:         public void addPages() {
184:                 // Create a page, set the title, and the initial model file name.
185:                 newFileCreationPage = new CreateNewInstanceFileCreationPage("CreatePage", new StructuredSelection(eClass));
186:                 final String className = eClass.getName();
187:                 newFileCreationPage.setTitle("Create New Instance In File");
188:                 newFileCreationPage.setDescription("Create a new file to save a new instance of EClass " + className + ".");
189:                 newFileCreationPage.setFileName(className + "." + FILE_EXTENSIONS.get(0));
190:                 addPage(newFileCreationPage);
191:         }
192:
193:         /**
194:          * @return the file from the file creation page.
195:          */
196:         public IFile getModelFile() {
197:                 return newFileCreationPage.getModelFile();
198:         }
199:
200: }