X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=blobdiff_plain;f=bundles%2Forg.simantics.modeling.ui%2Fsrc%2Forg%2Fsimantics%2Fmodeling%2Fui%2Fhandlers%2Fe4%2FSyncCurrentTypicalInstanceWithTemplate.java;h=714e72eb6c05b7ee19637ff533bc2f6630c5a3ac;hp=86bfdc0bbf97c226e1a6d49c508bf7661658f32c;hb=95144e3b879f0a10715927027bb9a7aa8e05bdeb;hpb=969bd23cab98a79ca9101af33334000879fb60c5 diff --git a/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/handlers/e4/SyncCurrentTypicalInstanceWithTemplate.java b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/handlers/e4/SyncCurrentTypicalInstanceWithTemplate.java index 86bfdc0bb..714e72eb6 100644 --- a/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/handlers/e4/SyncCurrentTypicalInstanceWithTemplate.java +++ b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/handlers/e4/SyncCurrentTypicalInstanceWithTemplate.java @@ -1,95 +1,104 @@ -package org.simantics.modeling.ui.handlers.e4; - -import javax.inject.Named; - -import org.eclipse.e4.core.di.annotations.CanExecute; -import org.eclipse.e4.core.di.annotations.Execute; -import org.eclipse.e4.ui.model.application.ui.basic.MPart; -import org.eclipse.e4.ui.services.IServiceConstants; -import org.eclipse.jface.dialogs.MessageDialog; -import org.eclipse.swt.widgets.Shell; -import org.eclipse.ui.IEditorPart; -import org.eclipse.ui.internal.e4.compatibility.CompatibilityEditor; -import org.simantics.Simantics; -import org.simantics.db.Resource; -import org.simantics.db.Session; -import org.simantics.db.exception.DatabaseException; -import org.simantics.modeling.typicals.SyncTypicalTemplatesToInstances; -import org.simantics.modeling.ui.documents.OpenPlainTextDocumentAdapter; -import org.simantics.modeling.ui.property.TypicalPropertyTester; -import org.simantics.modeling.ui.typicals.RuleChooserDialog; -import org.simantics.ui.workbench.IResourceEditorInput; -import org.simantics.utils.ui.ErrorLogger; -import org.simantics.utils.ui.workbench.WorkbenchUtils; - -/** - * @author Tuukka Lehtonen - * - */ -public class SyncCurrentTypicalInstanceWithTemplate { - - @CanExecute - public boolean canExecute(@Named(IServiceConstants.ACTIVE_PART) MPart part) throws DatabaseException, InterruptedException { - // TODO: Fix this when we get rid of CompatibilityEditors - IEditorPart activeEditor = null; - if (part != null && part.getObject() instanceof CompatibilityEditor) { - CompatibilityEditor compEditor = (CompatibilityEditor) part.getObject(); - activeEditor = compEditor.getEditor(); - } else { - // TODO: This is not good practice with E4 but an OK fallback for now - activeEditor = WorkbenchUtils.getActiveEditor(); - } - if (activeEditor == null) - return false; - IResourceEditorInput input = (IResourceEditorInput) activeEditor.getEditorInput(); - return TypicalPropertyTester.isTypicalInstanceEditor(Simantics.getSession(), input.getResource()); - } - - @Execute - public void execute(@Named(IServiceConstants.ACTIVE_SHELL) Shell shell, @Named(IServiceConstants.ACTIVE_PART) MPart mActiveEditor) { - - // TODO: Fix this when we get rid of CompatibilityEditors - IEditorPart activeEditor = null; - Object editor = mActiveEditor.getObject(); - if (editor instanceof CompatibilityEditor) { - CompatibilityEditor compEditor = (CompatibilityEditor) editor; - activeEditor = compEditor.getEditor(); - } else { - // TODO: This is not good practice with E4 but an OK fallback for now - activeEditor = WorkbenchUtils.getActiveEditor(); - } - if (activeEditor == null) - return; - - IResourceEditorInput input = (IResourceEditorInput) activeEditor.getEditorInput(); - Session session = Simantics.getSession(); - -// if (!MessageDialog -// .openConfirm(shell, -// "Synchronize Typical Instance With Template", -// "Are you sure you want to synchronize this typical instance with its template?")) -// return null; - - try { - if (!TypicalPropertyTester.isTypicalInstanceEditor(session, input.getResource())) { - MessageDialog.openInformation(shell, "Not Synchronizing", "Currently active editor is not a typical diagram instance editor."); - return; - } - - RuleChooserDialog.RuleResult result = RuleChooserDialog.choose(shell, "Synchronizing typical instance with its template.", new Resource[] { input.getResource() }); - if(result == null) return; - - SyncTypicalTemplatesToInstances req = SyncTypicalTemplatesToInstances.syncSingleInstance(result.selectedRules, input.getResource()).logging(result.logging); - session.syncRequest(req); - if (result.logging) { - for(Resource log : req.logs) - new OpenPlainTextDocumentAdapter().openEditor(log); - } - } catch (Exception e) { - ErrorLogger.defaultLogError(e); - } - return; - - } - -} +package org.simantics.modeling.ui.handlers.e4; + +import javax.inject.Named; + +import org.eclipse.e4.core.di.annotations.CanExecute; +import org.eclipse.e4.core.di.annotations.Execute; +import org.eclipse.e4.ui.model.application.ui.basic.MPart; +import org.eclipse.e4.ui.services.IServiceConstants; +import org.eclipse.jface.dialogs.MessageDialog; +import org.eclipse.swt.widgets.Shell; +import org.eclipse.ui.IEditorPart; +import org.eclipse.ui.internal.e4.compatibility.CompatibilityEditor; +import org.simantics.DatabaseJob; +import org.simantics.Simantics; +import org.simantics.db.Resource; +import org.simantics.db.Session; +import org.simantics.db.exception.DatabaseException; +import org.simantics.modeling.typicals.SyncTypicalTemplatesToInstances; +import org.simantics.modeling.ui.diagramEditor.DiagramEditor; +import org.simantics.modeling.ui.documents.OpenPlainTextDocumentAdapter; +import org.simantics.modeling.ui.property.TypicalPropertyTester; +import org.simantics.modeling.ui.typicals.RuleChooserDialog; +import org.simantics.ui.workbench.IResourceEditorInput; +import org.simantics.utils.ui.ErrorLogger; +import org.simantics.utils.ui.workbench.WorkbenchUtils; + +/** + * @author Tuukka Lehtonen + * + */ +public class SyncCurrentTypicalInstanceWithTemplate { + + @CanExecute + public boolean canExecute(@Named(IServiceConstants.ACTIVE_PART) MPart part) throws DatabaseException, InterruptedException { + // TODO: Fix this when we get rid of CompatibilityEditors + IEditorPart activeEditor = null; + if (part != null && part.getObject() instanceof CompatibilityEditor) { + CompatibilityEditor compEditor = (CompatibilityEditor) part.getObject(); + activeEditor = compEditor.getEditor(); + } else { + // TODO: This is not good practice with E4 but an OK fallback for now + activeEditor = WorkbenchUtils.getActiveEditor(); + } + if (activeEditor == null || (!(activeEditor instanceof DiagramEditor))) + return false; + if (DatabaseJob.inProgress()) + return false; + if (activeEditor.getEditorInput() instanceof IResourceEditorInput) { + IResourceEditorInput input = (IResourceEditorInput) activeEditor.getEditorInput(); + return TypicalPropertyTester.isTypicalInstanceEditor(Simantics.getSession(), input.getResource()); + } else { + return false; + } + } + + @Execute + public void execute(@Named(IServiceConstants.ACTIVE_SHELL) Shell shell, @Named(IServiceConstants.ACTIVE_PART) MPart mActiveEditor) { + + // TODO: Fix this when we get rid of CompatibilityEditors + IEditorPart activeEditor = null; + Object editor = mActiveEditor.getObject(); + if (editor instanceof CompatibilityEditor) { + CompatibilityEditor compEditor = (CompatibilityEditor) editor; + activeEditor = compEditor.getEditor(); + } else { + // TODO: This is not good practice with E4 but an OK fallback for now + activeEditor = WorkbenchUtils.getActiveEditor(); + } + if (activeEditor == null) + return; + + IResourceEditorInput input = (IResourceEditorInput) activeEditor.getEditorInput(); + Session session = Simantics.getSession(); + +// if (!MessageDialog +// .openConfirm(shell, +// "Synchronize Typical Instance With Template", +// "Are you sure you want to synchronize this typical instance with its template?")) +// return null; + + try { + if (!TypicalPropertyTester.isTypicalInstanceEditor(session, input.getResource())) { + MessageDialog.openInformation(shell, "Not Synchronizing", "Currently active editor is not a typical diagram instance editor."); + return; + } + + RuleChooserDialog.RuleResult result = RuleChooserDialog.choose(shell, "Synchronizing typical instance with its template.", new Resource[] { input.getResource() }); + if(result == null) return; + + session.markUndoPoint(); + SyncTypicalTemplatesToInstances req = SyncTypicalTemplatesToInstances.syncSingleInstance(result.selectedRules, input.getResource()).logging(result.logging); + session.syncRequest(req); + if (result.logging) { + for(Resource log : req.logs) + new OpenPlainTextDocumentAdapter().openEditor(log); + } + } catch (Exception e) { + ErrorLogger.defaultLogError(e); + } + return; + + } + +}