@Override
  public void doSave(IProgressMonitor monitor) {
    super.doSave(monitor);

    final ActivitiDiagramEditorInput adei = (ActivitiDiagramEditorInput) getEditorInput();

    try {
      final IFile dataFile = adei.getDataFile();
      final String diagramFileString = dataFile.getLocationURI().getPath();
      BpmnMemoryModel model =
          ModelHandler.getModel(EcoreUtil.getURI(getDiagramTypeProvider().getDiagram()));

      // Save the bpmn diagram file
      doSaveToBpmn(model, diagramFileString);

      // Save an image of the diagram
      doSaveImage(diagramFileString, model);

      // Refresh the resources in the workspace before invoking export
      // marshallers, as they may need access to resources
      dataFile.getProject().refreshLocal(IResource.DEPTH_INFINITE, null);

      // Invoke export marshallers to produce additional output
      doInvokeExportMarshallers(model);

    } catch (Exception e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }

    ((BasicCommandStack) getEditingDomain().getCommandStack()).saveIsDone();
    updateDirtyState();
  }
  @Override
  public void dispose() {
    super.dispose();

    final ActivitiDiagramEditorInput adei = (ActivitiDiagramEditorInput) getEditorInput();

    ModelHandler.removeModel(EcoreUtil.getURI(getDiagramTypeProvider().getDiagram()));
    Bpmn2DiagramCreator.dispose(adei.getDiagramFile());
  }
  @Override
  protected void setInput(IEditorInput input) {
    super.setInput(input);

    final ActivitiDiagramEditorInput adei = (ActivitiDiagramEditorInput) input;
    final IFile dataFile = adei.getDataFile();

    final BpmnMemoryModel model =
        new BpmnMemoryModel(getDiagramTypeProvider().getFeatureProvider(), dataFile);
    ModelHandler.addModel(EcoreUtil.getURI(getDiagramTypeProvider().getDiagram()), model);

    String filePath = dataFile.getLocationURI().getPath();
    File bpmnFile = new File(filePath);
    try {
      if (bpmnFile.exists() == false) {
        model.setBpmnModel(new BpmnModel());
        model.addMainProcess();
        bpmnFile.createNewFile();
        dataFile.refreshLocal(IResource.DEPTH_INFINITE, null);
      } else {
        FileInputStream fileStream = new FileInputStream(bpmnFile);
        XMLInputFactory xif = XMLInputFactory.newInstance();
        InputStreamReader in = new InputStreamReader(fileStream, "UTF-8");
        XMLStreamReader xtr = xif.createXMLStreamReader(in);
        BpmnXMLConverter bpmnConverter = new BpmnXMLConverter();
        bpmnConverter.setUserTaskFormTypes(
            PreferencesUtil.getStringArray(Preferences.ALFRESCO_FORMTYPES_USERTASK));
        bpmnConverter.setStartEventFormTypes(
            PreferencesUtil.getStringArray(Preferences.ALFRESCO_FORMTYPES_STARTEVENT));
        BpmnModel bpmnModel = null;
        try {
          bpmnModel = bpmnConverter.convertToBpmnModel(xtr);
        } catch (Exception e) {
          bpmnModel = new BpmnModel();
        }
        model.setBpmnModel(bpmnModel);

        if (bpmnModel.getLocationMap().size() == 0) {
          BpmnAutoLayout layout = new BpmnAutoLayout(bpmnModel);
          layout.execute();
        }

        BasicCommandStack basicCommandStack =
            (BasicCommandStack) getEditingDomain().getCommandStack();

        if (input instanceof DiagramEditorInput) {

          basicCommandStack.execute(
              new RecordingCommand(getEditingDomain()) {

                @Override
                protected void doExecute() {
                  importDiagram(model);
                }
              });
        }
        basicCommandStack.saveIsDone();
        basicCommandStack.flush();
      }

    } catch (Exception e) {
      e.printStackTrace();
    }
  }