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