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