1 package org.simantics.modeling.ui.handlers.e4;
3 import javax.inject.Named;
5 import org.eclipse.e4.core.di.annotations.CanExecute;
6 import org.eclipse.e4.core.di.annotations.Execute;
7 import org.eclipse.e4.ui.model.application.ui.basic.MPart;
8 import org.eclipse.e4.ui.services.IServiceConstants;
9 import org.eclipse.jface.dialogs.MessageDialog;
10 import org.eclipse.swt.widgets.Shell;
11 import org.eclipse.ui.IEditorPart;
12 import org.eclipse.ui.internal.e4.compatibility.CompatibilityEditor;
13 import org.simantics.DatabaseJob;
14 import org.simantics.Simantics;
15 import org.simantics.db.Resource;
16 import org.simantics.db.Session;
17 import org.simantics.db.exception.DatabaseException;
18 import org.simantics.modeling.typicals.SyncTypicalTemplatesToInstances;
19 import org.simantics.modeling.ui.diagramEditor.DiagramEditor;
20 import org.simantics.modeling.ui.documents.OpenPlainTextDocumentAdapter;
21 import org.simantics.modeling.ui.property.TypicalPropertyTester;
22 import org.simantics.modeling.ui.typicals.RuleChooserDialog;
23 import org.simantics.ui.workbench.IResourceEditorInput;
24 import org.simantics.utils.ui.ErrorLogger;
25 import org.simantics.utils.ui.workbench.WorkbenchUtils;
28 * @author Tuukka Lehtonen
30 public class SyncCurrentTypicalTemplateToInstances {
33 public boolean canExecute(@Named(IServiceConstants.ACTIVE_PART) MPart mActiveEditor) throws DatabaseException, InterruptedException {
34 // TODO: Fix this when we get rid of CompatibilityEditors
35 IEditorPart activeEditor = null;
36 if (mActiveEditor != null && mActiveEditor.getObject()instanceof CompatibilityEditor) {
37 CompatibilityEditor compEditor = (CompatibilityEditor) mActiveEditor.getObject();
38 activeEditor = compEditor.getEditor();
40 // TODO: This is not good practice with E4 but an OK fallback for now
41 activeEditor = WorkbenchUtils.getActiveEditor();
43 if (activeEditor == null || (!(activeEditor instanceof DiagramEditor)))
45 if (DatabaseJob.inProgress())
47 if (activeEditor.getEditorInput() instanceof IResourceEditorInput) {
48 IResourceEditorInput input = (IResourceEditorInput) activeEditor.getEditorInput();
49 return TypicalPropertyTester.isTypicalMasterEditor(Simantics.getSession(), input.getResource());
56 public void execute(@Named(IServiceConstants.ACTIVE_SHELL) Shell shell, @Named(IServiceConstants.ACTIVE_PART) MPart mActiveEditor) {
57 // TODO: Fix this when we get rid of CompatibilityEditors
58 IEditorPart activeEditor = null;
59 Object editor = mActiveEditor.getObject();
60 if (editor instanceof CompatibilityEditor) {
61 CompatibilityEditor compEditor = (CompatibilityEditor) editor;
62 activeEditor = compEditor.getEditor();
64 // TODO: This is not good practice with E4 but an OK fallback for now
65 activeEditor = WorkbenchUtils.getActiveEditor();
67 if (activeEditor == null)
70 IResourceEditorInput input = (IResourceEditorInput) activeEditor.getEditorInput();
71 Session session = Simantics.getSession();
74 // .openConfirm(shell,
75 // "Synchronize Typical Template With Instances",
76 // "Are you sure you want to synchronize all instances of this typical template with the template?"))
80 if (!TypicalPropertyTester.isTypicalMasterEditor(session, input.getResource())) {
81 MessageDialog.openInformation(shell, "Not Synchronizing", "Currently active editor is not a typical diagram template editor.");
85 RuleChooserDialog.RuleResult result = RuleChooserDialog.choose(shell, "Synchronizing typical template to all its instances.", new Resource[] { input.getResource() });
86 if(result == null) return;
88 session.markUndoPoint();
89 SyncTypicalTemplatesToInstances req = new SyncTypicalTemplatesToInstances(result.selectedRules, input.getResource()).logging(result.logging);
90 session.syncRequest(req);
92 for(Resource log : req.logs)
93 new OpenPlainTextDocumentAdapter().openEditor(log);
95 } catch (Exception e) {
96 ErrorLogger.defaultLogError(e);