Skip to content

Package: EMFFormsRuleRepositoryWizard$2

EMFFormsRuleRepositoryWizard$2

nameinstructionbranchcomplexitylinemethod
run()
M: 24 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 7 C: 0
0%
M: 1 C: 0
0%
{...}
M: 12 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-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.emfforms.internal.rulerepository.tooling.wizard;
15:
16: import java.io.IOException;
17: import java.lang.reflect.InvocationTargetException;
18:
19: import org.eclipse.core.resources.IContainer;
20: import org.eclipse.core.resources.IFile;
21: import org.eclipse.core.resources.IResource;
22: import org.eclipse.core.resources.IWorkspaceRoot;
23: import org.eclipse.core.resources.ResourcesPlugin;
24: import org.eclipse.core.runtime.CoreException;
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.resource.Resource;
31: import org.eclipse.emf.ecore.resource.ResourceSet;
32: import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
33: import org.eclipse.emfforms.common.Optional;
34: import org.eclipse.emfforms.spi.rulerepository.model.VRuleRepository;
35: import org.eclipse.emfforms.spi.rulerepository.model.VRulerepositoryFactory;
36: import org.eclipse.jface.dialogs.MessageDialog;
37: import org.eclipse.jface.operation.IRunnableWithProgress;
38: import org.eclipse.jface.viewers.ISelection;
39: import org.eclipse.jface.viewers.IStructuredSelection;
40: import org.eclipse.jface.wizard.IWizardPage;
41: import org.eclipse.jface.wizard.Wizard;
42: import org.eclipse.ui.INewWizard;
43: import org.eclipse.ui.IWorkbench;
44: import org.eclipse.ui.IWorkbenchPage;
45: import org.eclipse.ui.PartInitException;
46: import org.eclipse.ui.PlatformUI;
47: import org.eclipse.ui.ide.IDE;
48:
49: /**
50: * This is a sample new wizard. Its role is to create a new file
51: * resource in the provided container. If the container resource
52: * (a folder or a project) is selected in the workspace
53: * when the wizard is opened, it will accept it as the target
54: * container. The wizard creates one file with the extension
55: * "rulerepository". If a sample multi-page editor (also available
56: * as a template) is registered for the same extension, it will
57: * be able to open it.
58: */
59:
60: public class EMFFormsRuleRepositoryWizard extends Wizard implements INewWizard {
61:
62:         private ISelection selection;
63:
64:         private Optional<EMFFormsNewRuleRepositoryWizardPage> newPage = Optional.empty();
65:
66:         /**
67:          * Constructor for EMFFormsRuleRepositoryWizard. This wizard allows you to create a new rulerepository model.
68:          */
69:         public EMFFormsRuleRepositoryWizard() {
70:                 super();
71:                 setNeedsProgressMonitor(true);
72:         }
73:
74:         @Override
75:         public void addPages() {
76:
77:                 newPage = Optional.of(new EMFFormsNewRuleRepositoryWizardPage(selection));
78:                 addPage(newPage.get());
79:
80:         }
81:
82:         @Override
83:         public IWizardPage getNextPage(IWizardPage page) {
84:
85:                 /* if we allow to create a style (selector page is present) show the selector page after the new/select page */
86:                 if (newPage.isPresent() && page == newPage.get()) {
87:                         return null;
88:                 }
89:
90:                 return super.getNextPage(page);
91:         }
92:
93:         @Override
94:         public boolean canFinish() {
95:                 return super.canFinish();
96:         }
97:
98:         @Override
99:         public boolean performFinish() {
100:                 return performFinishNewPage();
101:         }
102:
103:         private boolean performFinishNewPage() {
104:                 final String containerName = newPage.get().getContainerName();
105:                 final String fileName = newPage.get().getFileName();
106:                 final IRunnableWithProgress op = new IRunnableWithProgress() {
107:                         @Override
108:                         public void run(IProgressMonitor monitor) throws InvocationTargetException {
109:                                 try {
110:                                         doFinish(containerName, fileName, monitor);
111:                                 } catch (final CoreException e) {
112:                                         throw new InvocationTargetException(e);
113:                                 } finally {
114:                                         monitor.done();
115:                                 }
116:                         }
117:                 };
118:                 try {
119:                         getContainer().run(true, false, op);
120:                 } catch (final InterruptedException e) {
121:                         return false;
122:                 } catch (final InvocationTargetException e) {
123:                         final Throwable realException = e.getTargetException();
124:                         MessageDialog.openError(getShell(), Messages.EMFFormsRuleRepositoryWizard_errorTitle,
125:                                 realException.getMessage());
126:                         return false;
127:                 }
128:                 return true;
129:         }
130:
131:         /**
132:          * The worker method. It will find the container, create the
133:          * file if missing or just replace its contents, and open
134:          * the editor on the newly created file.
135:          */
136:
137:         private void doFinish(String containerName, String fileName, final IProgressMonitor monitor) throws CoreException {
138:                 // create a sample file
139:                 monitor.beginTask(Messages.EMFFormsRuleRepositoryWizard_creatingTask + fileName, 2);
140:                 final IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
141:                 final IResource resource = root.findMember(new Path(containerName));
142:                 if (!resource.exists() || !(resource instanceof IContainer)) {
143:                         throwCoreException(String.format("Container \"%s\" does not exist.", containerName)); //$NON-NLS-1$
144:                 }
145:                 final IContainer container = (IContainer) resource;
146:                 final IFile file = container.getFile(new Path(fileName));
147:                 try {
148:                         final VRuleRepository ruleRepository = VRulerepositoryFactory.eINSTANCE.createRuleRepository();
149:
150:                         final ResourceSet rs = new ResourceSetImpl();
151:                         final Resource ruleRepositoryResource = rs.createResource(URI.createURI(file.getLocationURI().toString()));
152:                         ruleRepositoryResource.getContents().add(ruleRepository);
153:                         ruleRepositoryResource.save(null);
154:                         container.refreshLocal(IResource.DEPTH_ONE, monitor);
155:                 } catch (final IOException e) {
156:                         MessageDialog.openError(getShell(), "Error", e.getMessage()); //$NON-NLS-1$
157:                         monitor.done();
158:                         return;
159:                 }
160:                 monitor.worked(1);
161:                 monitor.setTaskName(Messages.EMFFormsRuleRepositoryWizard_editingTask);
162:                 getShell().getDisplay().asyncExec(new Runnable() {
163:                         @Override
164:                         public void run() {
165:                                 final IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
166:                                 try {
167:                                         IDE.openEditor(page, file, true);
168:                                 } catch (final PartInitException e) {
169:                                         MessageDialog.openError(getShell(), "Error", e.getMessage()); //$NON-NLS-1$
170:                                         monitor.done();
171:                                         return;
172:                                 }
173:                         }
174:                 });
175:                 monitor.worked(1);
176:         }
177:
178:         private void throwCoreException(String message) throws CoreException {
179:                 final IStatus status = new Status(IStatus.ERROR, "org.eclipse.emfforms.rulerepository.tooling", IStatus.OK, //$NON-NLS-1$
180:                         message, null);
181:                 throw new CoreException(status);
182:         }
183:
184:         @Override
185:         public void init(IWorkbench workbench, IStructuredSelection selection) {
186:                 this.selection = selection;
187:         }
188:
189: }