/** @generated */
 protected void doSynchronize(Object element, IProgressMonitor monitor) throws CoreException {
   ResourceSetInfo info = getResourceSetInfo(element);
   if (info != null) {
     for (Iterator it = info.getResourceSet().getResources().iterator(); it.hasNext(); ) {
       Resource nextResource = (Resource) it.next();
       handleElementChanged(info, nextResource, monitor);
     }
     return;
   }
   super.doSynchronize(element, monitor);
 }
 /** @generated */
 protected ISchedulingRule getSynchronizeRule(Object element) {
   ResourceSetInfo info = getResourceSetInfo(element);
   if (info != null) {
     Collection rules = new ArrayList();
     for (Iterator it = info.getResourceSet().getResources().iterator(); it.hasNext(); ) {
       Resource nextResource = (Resource) it.next();
       IFile file = WorkspaceSynchronizer.getFile(nextResource);
       if (file != null) {
         rules.add(ResourcesPlugin.getWorkspace().getRuleFactory().refreshRule(file));
       }
     }
     return new MultiRule((ISchedulingRule[]) rules.toArray(new ISchedulingRule[rules.size()]));
   }
   return null;
 }
 /** @generated */
 private long computeModificationStamp(ResourceSetInfo info) {
   int result = 0;
   for (Iterator it = info.getResourceSet().getResources().iterator(); it.hasNext(); ) {
     Resource nextResource = (Resource) it.next();
     IFile file = WorkspaceSynchronizer.getFile(nextResource);
     if (file != null) {
       if (file.getLocation() != null) {
         result += file.getLocation().toFile().lastModified();
       } else {
         result += file.getModificationStamp();
       }
     }
   }
   return result;
 }
 /** @generated */
 protected ISchedulingRule getValidateStateRule(Object element) {
   ResourceSetInfo info = getResourceSetInfo(element);
   if (info != null) {
     Collection files = new ArrayList();
     for (Iterator it = info.getResourceSet().getResources().iterator(); it.hasNext(); ) {
       Resource nextResource = (Resource) it.next();
       IFile file = WorkspaceSynchronizer.getFile(nextResource);
       if (file != null) {
         files.add(file);
       }
     }
     return ResourcesPlugin.getWorkspace()
         .getRuleFactory()
         .validateEditRule((IFile[]) files.toArray(new IFile[files.size()]));
   }
   return null;
 }
 /** @generated */
 protected void updateCache(Object element) throws CoreException {
   ResourceSetInfo info = getResourceSetInfo(element);
   if (info != null) {
     for (Iterator it = info.getResourceSet().getResources().iterator(); it.hasNext(); ) {
       Resource nextResource = (Resource) it.next();
       IFile file = WorkspaceSynchronizer.getFile(nextResource);
       if (file != null && file.isReadOnly()) {
         info.setReadOnly(true);
         info.setModifiable(false);
         return;
       }
     }
     info.setReadOnly(false);
     info.setModifiable(true);
     return;
   }
 }
  /** @generated */
  protected void doValidateState(Object element, Object computationContext) throws CoreException {
    ResourceSetInfo info = getResourceSetInfo(element);
    if (info != null) {
      Collection files2Validate = new ArrayList();
      for (Iterator it = info.getResourceSet().getResources().iterator(); it.hasNext(); ) {
        Resource nextResource = (Resource) it.next();
        IFile file = WorkspaceSynchronizer.getFile(nextResource);
        if (file != null && file.isReadOnly()) {
          files2Validate.add(file);
        }
      }
      ResourcesPlugin.getWorkspace()
          .validateEdit(
              (IFile[]) files2Validate.toArray(new IFile[files2Validate.size()]),
              computationContext);
    }

    super.doValidateState(element, computationContext);
  }
    /** @generated */
    public void notifyChanged(Notification notification) {
      if (notification.getNotifier() instanceof ResourceSet) {
        super.notifyChanged(notification);
      }
      if (!notification.isTouch() && myModifiedFilter.matches(notification)) {
        if (notification.getNotifier() instanceof Resource) {
          Resource resource = (Resource) notification.getNotifier();
          if (resource.isLoaded()) {
            boolean modified = false;
            for (Iterator it = myInfo.getResourceSet().getResources().iterator();
                it.hasNext() && !modified; ) {
              Resource nextResource = (Resource) it.next();
              if (nextResource.isLoaded()) {
                modified = nextResource.isModified();
              }
            }
            boolean dirtyStateChanged = false;
            synchronized (myInfo) {
              if (modified != myInfo.fCanBeSaved) {
                myInfo.fCanBeSaved = modified;
                dirtyStateChanged = true;
              }
              if (!resource.isModified()) {
                myInfo.setSynchronized(resource);
              }
            }
            if (dirtyStateChanged) {
              fireElementDirtyStateChanged(myInfo.getEditorInput(), modified);

              if (!modified) {
                myInfo.setModificationStamp(computeModificationStamp(myInfo));
              }
            }
          }
        }
      }
    }
Ejemplo n.º 8
0
 /** @generated */
 protected void doSaveDocument(
     IProgressMonitor monitor, Object element, IDocument document, boolean overwrite)
     throws CoreException {
   ResourceSetInfo info = getResourceSetInfo(element);
   if (info != null) {
     if (!overwrite && !info.isSynchronized()) {
       throw new CoreException(
           new Status(
               IStatus.ERROR,
               StateChartDiagramEditorPlugin.ID,
               IStatus.ERROR,
               Messages.StateChartDocumentProvider_UnsynchronizedFileSaveError,
               null));
     }
     fireElementStateChanging(element);
     try {
       monitor.beginTask(
           Messages.StateChartDocumentProvider_SaveDiagramTask,
           info.getResourceSet().getResources().size() + 1); // "Saving diagram"
       for (Iterator<Resource> it = info.getLoadedResourcesIterator(); it.hasNext(); ) {
         Resource nextResource = it.next();
         monitor.setTaskName(
             NLS.bind(
                 Messages.StateChartDocumentProvider_SaveNextResourceTask, nextResource.getURI()));
         if (nextResource.isLoaded() && !info.getEditingDomain().isReadOnly(nextResource)) {
           try {
             nextResource.save(StateChartDiagramEditorUtil.getSaveOptions());
           } catch (IOException e) {
             fireElementStateChangeFailed(element);
             throw new CoreException(
                 new Status(
                     IStatus.ERROR,
                     StateChartDiagramEditorPlugin.ID,
                     EditorStatusCodes.RESOURCE_FAILURE,
                     e.getLocalizedMessage(),
                     null));
           }
         }
         monitor.worked(1);
       }
       monitor.done();
       info.setModificationStamp(computeModificationStamp(info));
     } catch (RuntimeException x) {
       fireElementStateChangeFailed(element);
       throw x;
     }
   } else {
     URI newResoruceURI;
     List<IFile> affectedFiles = null;
     if (element instanceof URIEditorInput) {
       newResoruceURI = ((URIEditorInput) element).getURI();
     } else {
       fireElementStateChangeFailed(element);
       throw new CoreException(
           new Status(
               IStatus.ERROR,
               StateChartDiagramEditorPlugin.ID,
               0,
               NLS.bind(
                   Messages.StateChartDocumentProvider_IncorrectInputError,
                   new Object[] {
                     element, "org.eclipse.emf.common.ui.URIEditorInput"
                   }), //$NON-NLS-1$
               null));
     }
     if (false == document instanceof IDiagramDocument) {
       fireElementStateChangeFailed(element);
       throw new CoreException(
           new Status(
               IStatus.ERROR,
               StateChartDiagramEditorPlugin.ID,
               0,
               "Incorrect document used: "
                   + document
                   + " instead of org.eclipse.gmf.runtime.diagram.ui.resources.editor.document.IDiagramDocument",
               null)); //$NON-NLS-1$ //$NON-NLS-2$
     }
     IDiagramDocument diagramDocument = (IDiagramDocument) document;
     final Resource newResource =
         diagramDocument.getEditingDomain().getResourceSet().createResource(newResoruceURI);
     final Diagram diagramCopy = (Diagram) EcoreUtil.copy(diagramDocument.getDiagram());
     try {
       new AbstractTransactionalCommand(
           diagramDocument.getEditingDomain(),
           NLS.bind(Messages.StateChartDocumentProvider_SaveAsOperation, diagramCopy.getName()),
           affectedFiles) {
         protected CommandResult doExecuteWithResult(IProgressMonitor monitor, IAdaptable info)
             throws ExecutionException {
           newResource.getContents().add(diagramCopy);
           return CommandResult.newOKCommandResult();
         }
       }.execute(monitor, null);
       newResource.save(StateChartDiagramEditorUtil.getSaveOptions());
     } catch (ExecutionException e) {
       fireElementStateChangeFailed(element);
       throw new CoreException(
           new Status(
               IStatus.ERROR, StateChartDiagramEditorPlugin.ID, 0, e.getLocalizedMessage(), null));
     } catch (IOException e) {
       fireElementStateChangeFailed(element);
       throw new CoreException(
           new Status(
               IStatus.ERROR, StateChartDiagramEditorPlugin.ID, 0, e.getLocalizedMessage(), null));
     }
     newResource.unload();
   }
 }
  @Override
  protected void doSaveDocument(
      IProgressMonitor monitor, Object element, IDocument document, boolean overwrite)
      throws CoreException {

    try {
      // Automatic validation with result
      IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
      CustomValidateAction action = new CustomValidateAction(page, (IEditorInput) element);
      monitor.setTaskName("Validation");
      action.run(monitor);
      if (!action.result) {
        // If validation failed, do not save
        ProblemsView problem = (ProblemsView) page.findView("org.eclipse.ui.views.ProblemView");
        page.activate(problem);
        monitor.setCanceled(true);
        return;
      }
    } catch (Exception e) {
      MindDiagramEditorPlugin.getInstance().logError("Problems occured during validation", e);
      return;
    }

    MindProxy mindRoot = MindProxyFactory.INSTANCE.getRootProxy();
    HashMap<String, Rectangle> boundsMemory = new HashMap<String, Rectangle>();
    HashMap<String, Bendpoints> bendpointsMemory = new HashMap<String, Bendpoints>();
    if (mindRoot != null) {
      SaveUtil.saveBounds(mindRoot.getEditPart(), boundsMemory, bendpointsMemory);
    }

    // Prepare the transaction that will prepare the main definition
    // All merged items are deleted because they must not be serialized
    TransactionalEditingDomain domain = getDiagramDocument(element).getEditingDomain();
    TransactionImpl transaction = new TransactionImpl(domain, false);
    EObject root = ((DiagramImpl) document.getContent()).getElement();

    ResourceSetInfo info = getResourceSetInfo(element);
    if (info != null) {
      if (!overwrite && !info.isSynchronized()) {
        throw new CoreException(
            new Status(
                IStatus.ERROR,
                MindDiagramEditorPlugin.ID,
                IResourceStatus.OUT_OF_SYNC_LOCAL,
                Messages.MindDocumentProvider_UnsynchronizedFileSaveError,
                null));
      }
      info.stopResourceListening();
      fireElementStateChanging(element);
      try {
        monitor.beginTask(
            Messages.MindDocumentProvider_SaveDiagramTask,
            info.getResourceSet().getResources().size() + 1); // "Saving diagram"

        LazyLinkingResource xtextResource = null;
        GMFResource gmfResource = null;

        for (Iterator /*<org.eclipse.emf.ecore.resource.Resource>*/<?> it =
                info.getLoadedResourcesIterator();
            it.hasNext(); ) {
          Resource nextResource = (Resource) it.next();
          monitor.setTaskName(
              NLS.bind(Messages.MindDocumentProvider_SaveNextResourceTask, nextResource.getURI()));
          if (nextResource.isLoaded() && !info.getEditingDomain().isReadOnly(nextResource)) {
            if (nextResource instanceof LazyLinkingResource) {
              // Serialize later
              xtextResource = (LazyLinkingResource) nextResource;
            } else {
              // Save GMF
              try {
                if (nextResource instanceof GMFResource) gmfResource = (GMFResource) nextResource;
                nextResource.save(MindDiagramEditorUtil.getSaveOptions());
              } catch (IOException e) {
                fireElementStateChangeFailed(element);
                throw new CoreException(
                    new Status(
                        IStatus.ERROR,
                        MindDiagramEditorPlugin.ID,
                        EditorStatusCodes.RESOURCE_FAILURE,
                        e.getLocalizedMessage(),
                        null));
              }
            }
          }
          monitor.worked(1);
        }

        // Delete merged elements from the model before saving
        if (root instanceof AdlDefinitionCustomImpl) {
          try {
            transaction.start();
            prepareMainDefinitionBeforeSave((AdlDefinitionCustomImpl) root);
            transaction.commit();
          } catch (InterruptedException e) {
            MindDiagramEditorPlugin.getInstance().logError("Clean merge failed", e);
          } catch (RollbackException e) {
            MindDiagramEditorPlugin.getInstance().logError("Clean merge failed", e);
            transaction.rollback();
          } finally {
          }
        }

        // Merged items are deleted, they will not to be serialized
        // We can save xtext resource
        try {
          xtextResource.save(MindDiagramEditorUtil.getSaveOptions());
        } catch (IOException e) {
          fireElementStateChangeFailed(element);
          throw new CoreException(
              new Status(
                  IStatus.ERROR,
                  MindDiagramEditorPlugin.ID,
                  EditorStatusCodes.RESOURCE_FAILURE,
                  e.getLocalizedMessage(),
                  null));
        }

        // Restore merged elements
        if (root instanceof AdlDefinitionCustomImpl) {
          try {
            transaction.start();
            restoreMainDefinitionAfterSave((AdlDefinitionCustomImpl) root);
            transaction.commit();
          } catch (InterruptedException e) {
            MindDiagramEditorPlugin.getInstance().logError("Restore merge failed", e);
          } catch (RollbackException e) {
            e.printStackTrace();
            MindDiagramEditorPlugin.getInstance().logError("Restore merge failed", e);
            transaction.rollback();
          } finally {
          }
        }

        // Update diagram
        try {
          MindDiagramUpdateCommand update = new MindDiagramUpdateAllCommand(false);
          update.execute(new ExecutionEvent());
        } catch (ExecutionException e) {
          MindDiagramEditorPlugin.getInstance().logError("Update failed", e);
        } finally {
        }

        // Now that editParts have been recreated by the update,
        // we are able to restore the saved bounds
        if (mindRoot != null) {
          SaveUtil.restoreBounds(mindRoot.getEditPart(), boundsMemory, bendpointsMemory);
          boundsMemory.clear();
        }

        // DocumentProvider thinks resource is modified because of restoreMainDefinition
        // Set value to false
        gmfResource.setModified(false);

        monitor.done();
        info.setModificationStamp(computeModificationStamp(info));
      } catch (RuntimeException x) {
        fireElementStateChangeFailed(element);
        throw x;
      } finally {
        info.startResourceListening();
      }
    } else {
      URI newResoruceURI;
      List<IFile> affectedFiles = null;
      if (element instanceof FileEditorInput) {
        IFile newFile = ((FileEditorInput) element).getFile();
        affectedFiles = Collections.singletonList(newFile);
        newResoruceURI = URI.createPlatformResourceURI(newFile.getFullPath().toString(), true);
      } else if (element instanceof URIEditorInput) {
        newResoruceURI = ((URIEditorInput) element).getURI();
      } else {
        fireElementStateChangeFailed(element);
        throw new CoreException(
            new Status(
                IStatus.ERROR,
                MindDiagramEditorPlugin.ID,
                0,
                NLS.bind(
                    Messages.MindDocumentProvider_IncorrectInputError,
                    new Object[] {
                      element,
                      "org.eclipse.ui.part.FileEditorInput",
                      "org.eclipse.emf.common.ui.URIEditorInput"
                    }), //$NON-NLS-1$ //$NON-NLS-2$
                null));
      }
      if (false == document instanceof IDiagramDocument) {
        fireElementStateChangeFailed(element);
        throw new CoreException(
            new Status(
                IStatus.ERROR,
                MindDiagramEditorPlugin.ID,
                0,
                "Incorrect document used: "
                    + document
                    + " instead of org.eclipse.gmf.runtime.diagram.ui.resources.editor.document.IDiagramDocument",
                null)); //$NON-NLS-1$ //$NON-NLS-2$
      }
      IDiagramDocument diagramDocument = (IDiagramDocument) document;
      final Resource newResource =
          diagramDocument.getEditingDomain().getResourceSet().createResource(newResoruceURI);
      final Diagram diagramCopy = (Diagram) EcoreUtil.copy(diagramDocument.getDiagram());
      try {
        new AbstractTransactionalCommand(
            diagramDocument.getEditingDomain(),
            NLS.bind(Messages.MindDocumentProvider_SaveAsOperation, diagramCopy.getName()),
            affectedFiles) {
          protected CommandResult doExecuteWithResult(IProgressMonitor monitor, IAdaptable info)
              throws ExecutionException {
            newResource.getContents().add(diagramCopy);
            return CommandResult.newOKCommandResult();
          }
        }.execute(monitor, null);
        newResource.save(MindDiagramEditorUtil.getSaveOptions());
      } catch (ExecutionException e) {
        fireElementStateChangeFailed(element);
        throw new CoreException(
            new Status(
                IStatus.ERROR, MindDiagramEditorPlugin.ID, 0, e.getLocalizedMessage(), null));
      } catch (IOException e) {
        fireElementStateChangeFailed(element);
        throw new CoreException(
            new Status(
                IStatus.ERROR, MindDiagramEditorPlugin.ID, 0, e.getLocalizedMessage(), null));
      }
      newResource.unload();
    }
  }