| /******************************************************************************* |
| * Copyright (c) 2010 BestSolution.at and others. |
| * All rights reserved. This program and the accompanying materials |
| * are made available under the terms of the Eclipse Public License v1.0 |
| * which accompanies this distribution, and is available at |
| * http://www.eclipse.org/legal/epl-v10.html |
| * |
| * Contributors: |
| * Tom Schindl <tom.schindl@bestsolution.at> - initial API and implementation |
| ******************************************************************************/ |
| package org.eclipse.e4.internal.tools.wizards.model; |
| |
| import java.util.HashMap; |
| import java.util.Map; |
| |
| import org.eclipse.core.resources.IContainer; |
| import org.eclipse.core.resources.IFile; |
| import org.eclipse.core.resources.IResource; |
| import org.eclipse.core.resources.IWorkspaceRoot; |
| import org.eclipse.core.resources.ResourcesPlugin; |
| import org.eclipse.core.runtime.CoreException; |
| import org.eclipse.core.runtime.IProgressMonitor; |
| import org.eclipse.core.runtime.IStatus; |
| import org.eclipse.core.runtime.Path; |
| import org.eclipse.core.runtime.Status; |
| import org.eclipse.emf.common.util.URI; |
| import org.eclipse.emf.ecore.EObject; |
| import org.eclipse.emf.ecore.resource.Resource; |
| import org.eclipse.emf.ecore.resource.ResourceSet; |
| import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl; |
| import org.eclipse.jface.dialogs.MessageDialog; |
| import org.eclipse.jface.viewers.ISelection; |
| import org.eclipse.jface.viewers.IStructuredSelection; |
| import org.eclipse.jface.viewers.StructuredSelection; |
| import org.eclipse.jface.wizard.Wizard; |
| import org.eclipse.ui.INewWizard; |
| import org.eclipse.ui.IWorkbench; |
| import org.eclipse.ui.IWorkbenchPage; |
| import org.eclipse.ui.IWorkbenchPart; |
| import org.eclipse.ui.IWorkbenchWindow; |
| import org.eclipse.ui.IWorkbenchWizard; |
| import org.eclipse.ui.PartInitException; |
| import org.eclipse.ui.actions.WorkspaceModifyOperation; |
| import org.eclipse.ui.part.FileEditorInput; |
| import org.eclipse.ui.part.ISetSelectionTarget; |
| |
| public abstract class BaseApplicationModelWizard extends Wizard implements INewWizard { |
| private NewModelFilePage page; |
| private ISelection selection; |
| |
| protected IWorkbench workbench; |
| |
| /** |
| * Constructor for NewApplicationModelWizard. |
| */ |
| public BaseApplicationModelWizard() { |
| super(); |
| setNeedsProgressMonitor(true); |
| } |
| |
| /** |
| * Adding the page to the wizard. |
| */ |
| |
| public void addPages() { |
| page = new NewModelFilePage(selection,getDefaultFileName()); |
| addPage(page); |
| } |
| |
| public abstract String getDefaultFileName(); |
| |
| @Override |
| public boolean performFinish() { |
| try { |
| // Remember the file. |
| // |
| final IFile modelFile = getModelFile(); |
| |
| // Do the work within an operation. |
| // |
| WorkspaceModifyOperation operation = |
| new WorkspaceModifyOperation() { |
| @Override |
| protected void execute(IProgressMonitor progressMonitor) { |
| try { |
| // Create a resource set |
| // |
| ResourceSet resourceSet = new ResourceSetImpl(); |
| |
| // Get the URI of the model file. |
| // |
| URI fileURI = URI.createPlatformResourceURI(modelFile.getFullPath().toString(), true); |
| |
| // Create a resource for this file. |
| // |
| Resource resource = resourceSet.createResource(fileURI); |
| |
| // Add the initial model object to the contents. |
| // |
| EObject rootObject = createInitialModel(); |
| if (rootObject != null) { |
| resource.getContents().add(rootObject); |
| } |
| |
| // Save the contents of the resource to the file system. |
| // |
| Map<Object, Object> options = new HashMap<Object, Object>(); |
| resource.save(options); |
| } |
| catch (Exception exception) { |
| throw new RuntimeException(exception); |
| } |
| finally { |
| progressMonitor.done(); |
| } |
| } |
| }; |
| |
| getContainer().run(false, false, operation); |
| |
| // Select the new file resource in the current view. |
| // |
| IWorkbenchWindow workbenchWindow = workbench.getActiveWorkbenchWindow(); |
| IWorkbenchPage page = workbenchWindow.getActivePage(); |
| final IWorkbenchPart activePart = page.getActivePart(); |
| if (activePart instanceof ISetSelectionTarget) { |
| final ISelection targetSelection = new StructuredSelection(modelFile); |
| getShell().getDisplay().asyncExec |
| (new Runnable() { |
| public void run() { |
| ((ISetSelectionTarget)activePart).selectReveal(targetSelection); |
| } |
| }); |
| } |
| |
| // Open an editor on the new file. |
| // |
| try { |
| page.openEditor |
| (new FileEditorInput(modelFile), |
| workbench.getEditorRegistry().getDefaultEditor(modelFile.getFullPath().toString()).getId()); |
| } |
| catch (PartInitException exception) { |
| MessageDialog.openError(workbenchWindow.getShell(), "Could not init editor", exception.getMessage()); //$NON-NLS-1$ |
| return false; |
| } |
| |
| return true; |
| } |
| catch (Exception exception) { |
| MessageDialog.openError(getShell(), "Error", exception.getMessage()); |
| return false; |
| } |
| } |
| |
| protected abstract EObject createInitialModel(); |
| |
| private IFile getModelFile() throws CoreException { |
| String containerName = page.getContainerName(); |
| String fileName = page.getFileName(); |
| IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot(); |
| IResource resource = root.findMember(new Path(containerName)); |
| if (!resource.exists() || !(resource instanceof IContainer)) { |
| throwCoreException("Container \"" + containerName |
| + "\" does not exist."); |
| } |
| IContainer container = (IContainer) resource; |
| return container.getFile(new Path(fileName)); |
| } |
| |
| private void throwCoreException(String message) throws CoreException { |
| IStatus status = new Status(IStatus.ERROR, |
| "org.eclipse.e4.tools.emf.editor3x", IStatus.OK, message, null); |
| throw new CoreException(status); |
| } |
| |
| /** |
| * We will accept the selection in the workbench to see if we can initialize |
| * from it. |
| * |
| * @see IWorkbenchWizard#init(IWorkbench, IStructuredSelection) |
| */ |
| public void init(IWorkbench workbench, IStructuredSelection selection) { |
| this.workbench = workbench; |
| this.selection = selection; |
| } |
| } |