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