/** @generated */
 public static void setCharset(IFile file) {
   if (file == null) {
     return;
   }
   try {
     file.setCharset("UTF-8", new NullProgressMonitor()); // $NON-NLS-1$
   } catch (CoreException e) {
     edu.toronto.cs.se.modelepedia.statemachine_mavo.diagram.part
         .StateMachine_MAVODiagramEditorPlugin.getInstance()
         .logError("Unable to set charset for file " + file.getFullPath(), e); // $NON-NLS-1$
   }
 }
 /**
  * Runs the wizard in a dialog.
  *
  * @generated
  */
 public static void runWizard(Shell shell, Wizard wizard, String settingsKey) {
   IDialogSettings pluginDialogSettings =
       edu.toronto.cs.se.modelepedia.statemachine_mavo.diagram.part
           .StateMachine_MAVODiagramEditorPlugin.getInstance()
           .getDialogSettings();
   IDialogSettings wizardDialogSettings = pluginDialogSettings.getSection(settingsKey);
   if (wizardDialogSettings == null) {
     wizardDialogSettings = pluginDialogSettings.addNewSection(settingsKey);
   }
   wizard.setDialogSettings(wizardDialogSettings);
   WizardDialog dialog = new WizardDialog(shell, wizard);
   dialog.create();
   dialog.getShell().setSize(Math.max(500, dialog.getShell().getSize().x), 500);
   dialog.open();
 }
  /**
   * This method should be called within a workspace modify operation since it creates resources.
   *
   * @generated
   */
  public static Resource createDiagram(
      URI diagramURI, URI modelURI, IProgressMonitor progressMonitor) {
    TransactionalEditingDomain editingDomain =
        GMFEditingDomainFactory.INSTANCE.createEditingDomain();
    progressMonitor.beginTask(
        edu.toronto.cs.se.modelepedia.statemachine_mavo.diagram.part.Messages
            .StateMachine_MAVODiagramEditorUtil_CreateDiagramProgressTask,
        3);
    final Resource diagramResource = editingDomain.getResourceSet().createResource(diagramURI);
    final Resource modelResource = editingDomain.getResourceSet().createResource(modelURI);
    final String diagramName = diagramURI.lastSegment();
    AbstractTransactionalCommand command =
        new AbstractTransactionalCommand(
            editingDomain,
            edu.toronto.cs.se.modelepedia.statemachine_mavo.diagram.part.Messages
                .StateMachine_MAVODiagramEditorUtil_CreateDiagramCommandLabel,
            Collections.EMPTY_LIST) {
          protected CommandResult doExecuteWithResult(IProgressMonitor monitor, IAdaptable info)
              throws ExecutionException {
            edu.toronto.cs.se.modelepedia.statemachine_mavo.StateMachine model =
                createInitialModel();
            attachModelToResource(model, modelResource);

            Diagram diagram =
                ViewService.createDiagram(
                    model,
                    edu.toronto.cs.se.modelepedia.statemachine_mavo.diagram.edit.parts
                        .StateMachineEditPart.MODEL_ID,
                    edu.toronto.cs.se.modelepedia.statemachine_mavo.diagram.part
                        .StateMachine_MAVODiagramEditorPlugin.DIAGRAM_PREFERENCES_HINT);
            if (diagram != null) {
              diagramResource.getContents().add(diagram);
              diagram.setName(diagramName);
              diagram.setElement(model);
            }

            try {
              modelResource.save(
                  edu.toronto.cs.se.modelepedia.statemachine_mavo.diagram.part
                      .StateMachine_MAVODiagramEditorUtil.getSaveOptions());
              diagramResource.save(
                  edu.toronto.cs.se.modelepedia.statemachine_mavo.diagram.part
                      .StateMachine_MAVODiagramEditorUtil.getSaveOptions());
            } catch (IOException e) {

              edu.toronto.cs.se.modelepedia.statemachine_mavo.diagram.part
                  .StateMachine_MAVODiagramEditorPlugin.getInstance()
                  .logError("Unable to store model and diagram resources", e); // $NON-NLS-1$
            }
            return CommandResult.newOKCommandResult();
          }
        };
    try {
      OperationHistoryFactory.getOperationHistory()
          .execute(command, new SubProgressMonitor(progressMonitor, 1), null);
    } catch (ExecutionException e) {
      edu.toronto.cs.se.modelepedia.statemachine_mavo.diagram.part
          .StateMachine_MAVODiagramEditorPlugin.getInstance()
          .logError("Unable to create model and diagram", e); // $NON-NLS-1$
    }
    setCharset(WorkspaceSynchronizer.getFile(modelResource));
    setCharset(WorkspaceSynchronizer.getFile(diagramResource));
    return diagramResource;
  }
  /** @generated */
  public boolean performFinish() {
    LinkedList<IFile> affectedFiles = new LinkedList<IFile>();
    IFile diagramFile = myFileCreationPage.createNewFile();
    edu.toronto.cs.se.modelepedia.statemachine_mavo.diagram.part.StateMachine_MAVODiagramEditorUtil
        .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,
            edu.toronto.cs.se.modelepedia.statemachine_mavo.diagram.part.Messages
                .StateMachine_MAVONewDiagramFileWizard_InitDiagramCommand,
            affectedFiles) {

          protected CommandResult doExecuteWithResult(IProgressMonitor monitor, IAdaptable info)
              throws ExecutionException {
            int diagramVID =
                edu.toronto.cs.se.modelepedia.statemachine_mavo.diagram.part
                    .StateMachine_MAVOVisualIDRegistry.getDiagramVisualID(
                    diagramRootElementSelectionPage.getModelElement());
            if (diagramVID
                != edu.toronto.cs.se.modelepedia.statemachine_mavo.diagram.edit.parts
                    .StateMachineEditPart.VISUAL_ID) {
              return CommandResult.newErrorCommandResult(
                  edu.toronto.cs.se.modelepedia.statemachine_mavo.diagram.part.Messages
                      .StateMachine_MAVONewDiagramFileWizard_IncorrectRootError);
            }
            Diagram diagram =
                ViewService.createDiagram(
                    diagramRootElementSelectionPage.getModelElement(),
                    edu.toronto.cs.se.modelepedia.statemachine_mavo.diagram.edit.parts
                        .StateMachineEditPart.MODEL_ID,
                    edu.toronto.cs.se.modelepedia.statemachine_mavo.diagram.part
                        .StateMachine_MAVODiagramEditorPlugin.DIAGRAM_PREFERENCES_HINT);
            diagramResource.getContents().add(diagram);
            return CommandResult.newOKCommandResult();
          }
        };
    try {
      OperationHistoryFactory.getOperationHistory()
          .execute(command, new NullProgressMonitor(), null);
      diagramResource.save(
          edu.toronto.cs.se.modelepedia.statemachine_mavo.diagram.part
              .StateMachine_MAVODiagramEditorUtil.getSaveOptions());
      edu.toronto.cs.se.modelepedia.statemachine_mavo.diagram.part
          .StateMachine_MAVODiagramEditorUtil.openDiagram(diagramResource);
    } catch (ExecutionException e) {
      edu.toronto.cs.se.modelepedia.statemachine_mavo.diagram.part
          .StateMachine_MAVODiagramEditorPlugin.getInstance()
          .logError("Unable to create model and diagram", e); // $NON-NLS-1$
    } catch (IOException ex) {
      edu.toronto.cs.se.modelepedia.statemachine_mavo.diagram.part
          .StateMachine_MAVODiagramEditorPlugin.getInstance()
          .logError("Save operation failed for: " + diagramModelURI, ex); // $NON-NLS-1$
    } catch (PartInitException ex) {
      edu.toronto.cs.se.modelepedia.statemachine_mavo.diagram.part
          .StateMachine_MAVODiagramEditorPlugin.getInstance()
          .logError("Unable to open editor", ex); // $NON-NLS-1$
    }
    return true;
  }