Search in sources :

Example 1 with Lock

use of org.osate.ge.internal.services.ModelChangeNotifier.Lock in project osate2 by osate.

the class DefaultAadlModificationService method performModifications.

// Assumes that the modification notifier is already locked
private void performModifications(final List<? extends Modification<?, ?>> modifications, final ModificationPostprocessor postProcessor) {
    class ModificationAction implements AgeAction {

        @Override
        public boolean canExecute() {
            return true;
        }

        @Override
        public AgeAction execute() {
            try (Lock lock = modelChangeNotifier.lock()) {
                final Set<IProject> projectsToBuild = new HashSet<>();
                boolean allSuccessful = true;
                final List<ModificationResult> modificationResults = new ArrayList<>();
                // Iterate over the input objects
                for (final Modification<?, ?> modification : modifications) {
                    final ModificationResult modificationResult = performModification(modification, projectsToBuild);
                    allSuccessful = modificationResult.modificationSuccessful;
                    if (!allSuccessful) {
                        break;
                    }
                    modificationResults.add(modificationResult);
                }
                // Build projects before unlocking. This will cause the post build notifications to be sent out before the lock is released.
                // This is desired to avoid multiple diagram updates for the same change.
                buildProjects(projectsToBuild);
                if (postProcessor != null) {
                    postProcessor.modificationCompleted(allSuccessful);
                }
                return modificationResults.isEmpty() ? null : new UndoAction(modificationResults);
            }
        }
    }
    actionService.execute("Modify Model", ActionExecutor.ExecutionMode.NORMAL, new ModificationAction());
}
Also used : AgeAction(org.osate.ge.internal.services.AgeAction) ArrayList(java.util.ArrayList) IProject(org.eclipse.core.resources.IProject) Lock(org.osate.ge.internal.services.ModelChangeNotifier.Lock) HashSet(java.util.HashSet)

Example 2 with Lock

use of org.osate.ge.internal.services.ModelChangeNotifier.Lock in project osate2 by osate.

the class LtkRenameAction method renameWithLtk.

/**
 * Renames the specified model element using an LTK rename refactoring.
 * @param bo the model element to rename
 * @param value the new name for the model element
 * @return true if the rename occurred
 */
private boolean renameWithLtk(final EObject bo, final String value) {
    // Prevent model notification changes from being sent until after the refactoring
    try (Lock lock = modelChangeNotifier.lock()) {
        // Rename the element using LTK
        final ProcessorBasedRefactoring renameRefactoring = RenameUtil.getRenameRefactoring(bo);
        final RefactoringStatus refactoringStatus = prepareAndCheck(renameRefactoring, value);
        if (!refactoringStatus.isOK()) {
            final Dialog dlg = RefactoringUI.createRefactoringStatusDialog(refactoringStatus, Display.getCurrent().getActiveShell(), "Refactoring", false);
            if (dlg.open() != Window.OK) {
                // Abort
                return false;
            }
        }
        try {
            final Change change = renameRefactoring.createChange(new NullProgressMonitor());
            new WorkspaceModifyOperation() {

                @Override
                protected void execute(IProgressMonitor monitor) throws CoreException {
                    // Perform the modification
                    change.perform(monitor);
                    // Build the project, reconcile all open AADL text editors and then build again.
                    // This seems to be the best way to ensure that all the model change events have been
                    // queued before the model change notification lock is released
                    buildProject();
                    ensureReconciled();
                    buildProject();
                }
            }.run(null);
        } catch (final InterruptedException e) {
            Thread.currentThread().interrupt();
            throw new GraphicalEditorException(e);
        } catch (final RuntimeException | InvocationTargetException | CoreException e) {
            throw new GraphicalEditorException(e);
        }
    }
    return true;
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) WorkspaceModifyOperation(org.eclipse.ui.actions.WorkspaceModifyOperation) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) Change(org.eclipse.ltk.core.refactoring.Change) ProcessorBasedRefactoring(org.eclipse.ltk.core.refactoring.participants.ProcessorBasedRefactoring) InvocationTargetException(java.lang.reflect.InvocationTargetException) Lock(org.osate.ge.internal.services.ModelChangeNotifier.Lock) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) CoreException(org.eclipse.core.runtime.CoreException) Dialog(org.eclipse.jface.dialogs.Dialog) GraphicalEditorException(org.osate.ge.internal.GraphicalEditorException)

Aggregations

Lock (org.osate.ge.internal.services.ModelChangeNotifier.Lock)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 IProject (org.eclipse.core.resources.IProject)1 CoreException (org.eclipse.core.runtime.CoreException)1 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)1 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)1 Dialog (org.eclipse.jface.dialogs.Dialog)1 Change (org.eclipse.ltk.core.refactoring.Change)1 RefactoringStatus (org.eclipse.ltk.core.refactoring.RefactoringStatus)1 ProcessorBasedRefactoring (org.eclipse.ltk.core.refactoring.participants.ProcessorBasedRefactoring)1 WorkspaceModifyOperation (org.eclipse.ui.actions.WorkspaceModifyOperation)1 GraphicalEditorException (org.osate.ge.internal.GraphicalEditorException)1 AgeAction (org.osate.ge.internal.services.AgeAction)1