| //------------------------------------------------------------------------------ |
| // Copyright (c) 2005, 2007 IBM Corporation 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: |
| // IBM Corporation - initial implementation |
| //------------------------------------------------------------------------------ |
| package org.eclipse.epf.diagram.wpdd.part; |
| |
| import java.io.IOException; |
| |
| import java.util.Collections; |
| import java.util.LinkedList; |
| import java.util.List; |
| |
| import org.eclipse.core.commands.ExecutionException; |
| |
| import org.eclipse.core.commands.operations.OperationHistoryFactory; |
| |
| import org.eclipse.core.resources.IContainer; |
| import org.eclipse.core.resources.IFile; |
| |
| import org.eclipse.core.runtime.CoreException; |
| import org.eclipse.core.runtime.IAdaptable; |
| import org.eclipse.core.runtime.IPath; |
| import org.eclipse.core.runtime.IProgressMonitor; |
| import org.eclipse.core.runtime.NullProgressMonitor; |
| import org.eclipse.core.runtime.Path; |
| |
| 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.util.FeatureMap; |
| |
| import org.eclipse.emf.edit.provider.IWrapperItemProvider; |
| |
| import org.eclipse.emf.edit.ui.provider.AdapterFactoryContentProvider; |
| import org.eclipse.emf.edit.ui.provider.AdapterFactoryLabelProvider; |
| |
| import org.eclipse.emf.transaction.TransactionalEditingDomain; |
| |
| import org.eclipse.epf.diagram.wpdd.edit.parts.WorkProductDependencyDiagramEditPart; |
| |
| import org.eclipse.gmf.runtime.common.core.command.CommandResult; |
| |
| import org.eclipse.gmf.runtime.diagram.core.services.ViewService; |
| |
| import org.eclipse.gmf.runtime.diagram.core.services.view.CreateDiagramViewOperation; |
| |
| import org.eclipse.gmf.runtime.emf.commands.core.command.AbstractTransactionalCommand; |
| |
| import org.eclipse.gmf.runtime.emf.core.util.EObjectAdapter; |
| |
| import org.eclipse.gmf.runtime.notation.Diagram; |
| |
| import org.eclipse.jface.viewers.ISelectionChangedListener; |
| import org.eclipse.jface.viewers.IStructuredSelection; |
| import org.eclipse.jface.viewers.SelectionChangedEvent; |
| import org.eclipse.jface.viewers.StructuredSelection; |
| import org.eclipse.jface.viewers.TreeViewer; |
| |
| import org.eclipse.jface.wizard.Wizard; |
| import org.eclipse.jface.wizard.WizardPage; |
| |
| import org.eclipse.osgi.util.NLS; |
| import org.eclipse.swt.SWT; |
| |
| import org.eclipse.swt.layout.GridData; |
| import org.eclipse.swt.layout.GridLayout; |
| |
| import org.eclipse.swt.widgets.Composite; |
| import org.eclipse.swt.widgets.Label; |
| |
| import org.eclipse.ui.IWorkbenchPage; |
| import org.eclipse.ui.PartInitException; |
| |
| import org.eclipse.ui.dialogs.WizardNewFileCreationPage; |
| |
| import org.eclipse.ui.ide.IDE; |
| |
| /** |
| * @generated |
| */ |
| public class DiagramNewDiagramFileWizard extends Wizard { |
| |
| /** |
| * @generated |
| */ |
| private TransactionalEditingDomain myEditingDomain; |
| |
| /** |
| * @generated |
| */ |
| public DiagramNewDiagramFileWizard(URI domainModelURI, EObject diagramRoot, |
| TransactionalEditingDomain editingDomain) { |
| assert domainModelURI != null : "Domain model uri must be specified"; //$NON-NLS-1$ |
| assert diagramRoot != null : "Doagram root element must be specified"; //$NON-NLS-1$ |
| assert editingDomain != null : "Editing domain must be specified"; //$NON-NLS-1$ |
| |
| myFileCreationPage = new WizardNewFileCreationPage( |
| Messages.DiagramNewDiagramFileWizard_CreationPageName, |
| StructuredSelection.EMPTY); |
| myFileCreationPage |
| .setTitle(Messages.DiagramNewDiagramFileWizard_CreationPageTitle); |
| myFileCreationPage.setDescription(NLS.bind( |
| Messages.DiagramNewDiagramFileWizard_CreationPageDescription, |
| WorkProductDependencyDiagramEditPart.MODEL_ID)); |
| IPath filePath; |
| String fileName = domainModelURI.trimFileExtension().lastSegment(); |
| if (domainModelURI.isPlatformResource()) { |
| filePath = new Path(domainModelURI.trimSegments(1) |
| .toPlatformString(true)); |
| } else if (domainModelURI.isFile()) { |
| filePath = new Path(domainModelURI.trimSegments(1).toFileString()); |
| } else { |
| // TODO : use some default path |
| throw new IllegalArgumentException( |
| "Unsupported URI: " + domainModelURI); //$NON-NLS-1$ |
| } |
| myFileCreationPage.setContainerFullPath(filePath); |
| myFileCreationPage.setFileName(DiagramDiagramEditorUtil |
| .getUniqueFileName(filePath, fileName, "wpdd")); //$NON-NLS-1$ |
| |
| diagramRootElementSelectionPage = new DiagramRootElementSelectionPage( |
| Messages.DiagramNewDiagramFileWizard_RootSelectionPageName); |
| diagramRootElementSelectionPage |
| .setTitle(Messages.DiagramNewDiagramFileWizard_RootSelectionPageTitle); |
| diagramRootElementSelectionPage |
| .setDescription(Messages.DiagramNewDiagramFileWizard_RootSelectionPageDescription); |
| diagramRootElementSelectionPage.setModelElement(diagramRoot); |
| |
| myEditingDomain = editingDomain; |
| } |
| |
| /** |
| * @generated |
| */ |
| private WizardNewFileCreationPage myFileCreationPage; |
| |
| /** |
| * @generated |
| */ |
| private ModelElementSelectionPage diagramRootElementSelectionPage; |
| |
| /** |
| * @generated |
| */ |
| public void addPages() { |
| addPage(myFileCreationPage); |
| addPage(diagramRootElementSelectionPage); |
| } |
| |
| /** |
| * @generated |
| */ |
| public boolean performFinish() { |
| List affectedFiles = new LinkedList(); |
| IFile diagramFile = myFileCreationPage.createNewFile(); |
| DiagramDiagramEditorUtil.setCharset(diagramFile); |
| affectedFiles.add(diagramFile); |
| URI diagramModelURI = URI.createPlatformResourceURI(diagramFile |
| .getFullPath().toString(), true); |
| ResourceSet resourceSet = myEditingDomain.getResourceSet(); |
| final Resource diagramResource = resourceSet |
| .createResource(diagramModelURI); |
| AbstractTransactionalCommand command = new AbstractTransactionalCommand( |
| myEditingDomain, |
| Messages.DiagramNewDiagramFileWizard_InitDiagramCommand, |
| affectedFiles) { |
| |
| protected CommandResult doExecuteWithResult( |
| IProgressMonitor monitor, IAdaptable info) |
| throws ExecutionException { |
| int diagramVID = DiagramVisualIDRegistry |
| .getDiagramVisualID(diagramRootElementSelectionPage |
| .getModelElement()); |
| if (diagramVID != WorkProductDependencyDiagramEditPart.VISUAL_ID) { |
| return CommandResult |
| .newErrorCommandResult(Messages.DiagramNewDiagramFileWizard_IncorrectRootError); |
| } |
| Diagram diagram = ViewService.createDiagram( |
| diagramRootElementSelectionPage.getModelElement(), |
| WorkProductDependencyDiagramEditPart.MODEL_ID, |
| WPDDiagramEditorPlugin.DIAGRAM_PREFERENCES_HINT); |
| diagramResource.getContents().add(diagram); |
| diagramResource.getContents().add(diagram.getElement()); |
| return CommandResult.newOKCommandResult(); |
| } |
| }; |
| try { |
| OperationHistoryFactory.getOperationHistory().execute(command, |
| new NullProgressMonitor(), null); |
| diagramResource.save(DiagramDiagramEditorUtil.getSaveOptions()); |
| DiagramDiagramEditorUtil.openDiagram(diagramResource); |
| } catch (ExecutionException e) { |
| WPDDiagramEditorPlugin.getInstance().logError( |
| "Unable to create model and diagram", e); //$NON-NLS-1$ |
| } catch (IOException ex) { |
| WPDDiagramEditorPlugin.getInstance().logError( |
| "Save operation failed for: " + diagramModelURI, ex); //$NON-NLS-1$ |
| } catch (PartInitException ex) { |
| WPDDiagramEditorPlugin.getInstance().logError( |
| "Unable to open editor", ex); //$NON-NLS-1$ |
| } |
| return true; |
| } |
| |
| /** |
| * @generated |
| */ |
| private static class DiagramRootElementSelectionPage extends |
| ModelElementSelectionPage { |
| |
| /** |
| * @generated |
| */ |
| protected DiagramRootElementSelectionPage(String pageName) { |
| super(pageName); |
| } |
| |
| /** |
| * @generated |
| */ |
| protected String getSelectionTitle() { |
| return Messages.DiagramNewDiagramFileWizard_RootSelectionPageSelectionTitle; |
| } |
| |
| /** |
| * @generated |
| */ |
| protected boolean validatePage() { |
| if (selectedModelElement == null) { |
| setErrorMessage(Messages.DiagramNewDiagramFileWizard_RootSelectionPageNoSelectionMessage); |
| return false; |
| } |
| boolean result = ViewService.getInstance().provides( |
| new CreateDiagramViewOperation(new EObjectAdapter( |
| selectedModelElement), |
| WorkProductDependencyDiagramEditPart.MODEL_ID, |
| WPDDiagramEditorPlugin.DIAGRAM_PREFERENCES_HINT)); |
| setErrorMessage(result ? null |
| : Messages.DiagramNewDiagramFileWizard_RootSelectionPageInvalidSelectionMessage); |
| return result; |
| } |
| } |
| } |