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