Prevent unnecessary read transaction for synch master typical handle
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / handlers / e4 / SyncCurrentTypicalInstanceWithTemplate.java
1 package org.simantics.modeling.ui.handlers.e4;
2
3 import javax.inject.Named;
4
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;
26
27 /**
28  * @author Tuukka Lehtonen
29  *
30  */
31 public class SyncCurrentTypicalInstanceWithTemplate {
32
33     @CanExecute
34     public boolean canExecute(@Named(IServiceConstants.ACTIVE_PART) MPart part) throws DatabaseException, InterruptedException {
35         // TODO: Fix this when we get rid of CompatibilityEditors
36         IEditorPart activeEditor = null;
37         if (part != null && part.getObject() instanceof CompatibilityEditor) {
38             CompatibilityEditor compEditor = (CompatibilityEditor) part.getObject();
39             activeEditor = compEditor.getEditor();
40         } else {
41             // TODO: This is not good practice with E4 but an OK fallback for now
42             activeEditor = WorkbenchUtils.getActiveEditor();
43         }
44         if (activeEditor == null || (!(activeEditor instanceof DiagramEditor)))
45             return false;
46         if (DatabaseJob.inProgress())
47             return false;
48         if (activeEditor.getEditorInput() instanceof IResourceEditorInput) {
49             IResourceEditorInput input = (IResourceEditorInput) activeEditor.getEditorInput();
50             return TypicalPropertyTester.isTypicalInstanceEditor(Simantics.getSession(), input.getResource());
51         } else {
52             return false;
53         }
54     }
55     
56     @Execute
57     public void execute(@Named(IServiceConstants.ACTIVE_SHELL) Shell shell, @Named(IServiceConstants.ACTIVE_PART) MPart mActiveEditor) {
58
59         // TODO: Fix this when we get rid of CompatibilityEditors
60         IEditorPart activeEditor = null;
61         Object editor = mActiveEditor.getObject();
62         if (editor instanceof CompatibilityEditor) {
63             CompatibilityEditor compEditor = (CompatibilityEditor) editor;
64             activeEditor = compEditor.getEditor();
65         } else {
66             // TODO: This is not good practice with E4 but an OK fallback for now
67             activeEditor = WorkbenchUtils.getActiveEditor();
68         }
69         if (activeEditor == null)
70             return;
71
72         IResourceEditorInput input = (IResourceEditorInput) activeEditor.getEditorInput();
73         Session session = Simantics.getSession();
74
75 //        if (!MessageDialog
76 //                .openConfirm(shell,
77 //                        "Synchronize Typical Instance With Template",
78 //                        "Are you sure you want to synchronize this typical instance with its template?"))
79 //            return null;
80
81         try {
82             if (!TypicalPropertyTester.isTypicalInstanceEditor(session, input.getResource())) {
83                 MessageDialog.openInformation(shell, "Not Synchronizing", "Currently active editor is not a typical diagram instance editor.");
84                 return;
85             }
86
87             RuleChooserDialog.RuleResult result = RuleChooserDialog.choose(shell, "Synchronizing typical instance with its template.", new Resource[] { input.getResource() });
88             if(result == null) return;
89
90             session.markUndoPoint();
91             SyncTypicalTemplatesToInstances req = SyncTypicalTemplatesToInstances.syncSingleInstance(result.selectedRules, input.getResource()).logging(result.logging); 
92             session.syncRequest(req);
93             if (result.logging) {
94                 for(Resource log : req.logs)
95                     new OpenPlainTextDocumentAdapter().openEditor(log);
96             }
97         } catch (Exception e) {
98             ErrorLogger.defaultLogError(e);
99         }
100         return;
101         
102     }
103
104 }