]> gerrit.simantics Code Review - simantics/platform.git/blob
86bfdc0bbf97c226e1a6d49c508bf7661658f32c
[simantics/platform.git] /
1 package org.simantics.modeling.ui.handlers.e4;\r
2 \r
3 import javax.inject.Named;\r
4 \r
5 import org.eclipse.e4.core.di.annotations.CanExecute;\r
6 import org.eclipse.e4.core.di.annotations.Execute;\r
7 import org.eclipse.e4.ui.model.application.ui.basic.MPart;\r
8 import org.eclipse.e4.ui.services.IServiceConstants;\r
9 import org.eclipse.jface.dialogs.MessageDialog;\r
10 import org.eclipse.swt.widgets.Shell;\r
11 import org.eclipse.ui.IEditorPart;\r
12 import org.eclipse.ui.internal.e4.compatibility.CompatibilityEditor;\r
13 import org.simantics.Simantics;\r
14 import org.simantics.db.Resource;\r
15 import org.simantics.db.Session;\r
16 import org.simantics.db.exception.DatabaseException;\r
17 import org.simantics.modeling.typicals.SyncTypicalTemplatesToInstances;\r
18 import org.simantics.modeling.ui.documents.OpenPlainTextDocumentAdapter;\r
19 import org.simantics.modeling.ui.property.TypicalPropertyTester;\r
20 import org.simantics.modeling.ui.typicals.RuleChooserDialog;\r
21 import org.simantics.ui.workbench.IResourceEditorInput;\r
22 import org.simantics.utils.ui.ErrorLogger;\r
23 import org.simantics.utils.ui.workbench.WorkbenchUtils;\r
24 \r
25 /**\r
26  * @author Tuukka Lehtonen\r
27  *\r
28  */\r
29 public class SyncCurrentTypicalInstanceWithTemplate {\r
30 \r
31     @CanExecute\r
32     public boolean canExecute(@Named(IServiceConstants.ACTIVE_PART) MPart part) throws DatabaseException, InterruptedException {\r
33         // TODO: Fix this when we get rid of CompatibilityEditors\r
34         IEditorPart activeEditor = null;\r
35         if (part != null && part.getObject() instanceof CompatibilityEditor) {\r
36             CompatibilityEditor compEditor = (CompatibilityEditor) part.getObject();\r
37             activeEditor = compEditor.getEditor();\r
38         } else {\r
39             // TODO: This is not good practice with E4 but an OK fallback for now\r
40             activeEditor = WorkbenchUtils.getActiveEditor();\r
41         }\r
42         if (activeEditor == null)\r
43             return false;\r
44         IResourceEditorInput input = (IResourceEditorInput) activeEditor.getEditorInput();\r
45         return TypicalPropertyTester.isTypicalInstanceEditor(Simantics.getSession(), input.getResource());\r
46     }\r
47     \r
48     @Execute\r
49     public void execute(@Named(IServiceConstants.ACTIVE_SHELL) Shell shell, @Named(IServiceConstants.ACTIVE_PART) MPart mActiveEditor) {\r
50 \r
51         // TODO: Fix this when we get rid of CompatibilityEditors\r
52         IEditorPart activeEditor = null;\r
53         Object editor = mActiveEditor.getObject();\r
54         if (editor instanceof CompatibilityEditor) {\r
55             CompatibilityEditor compEditor = (CompatibilityEditor) editor;\r
56             activeEditor = compEditor.getEditor();\r
57         } else {\r
58             // TODO: This is not good practice with E4 but an OK fallback for now\r
59             activeEditor = WorkbenchUtils.getActiveEditor();\r
60         }\r
61         if (activeEditor == null)\r
62             return;\r
63 \r
64         IResourceEditorInput input = (IResourceEditorInput) activeEditor.getEditorInput();\r
65         Session session = Simantics.getSession();\r
66 \r
67 //        if (!MessageDialog\r
68 //                .openConfirm(shell,\r
69 //                        "Synchronize Typical Instance With Template",\r
70 //                        "Are you sure you want to synchronize this typical instance with its template?"))\r
71 //            return null;\r
72 \r
73         try {\r
74             if (!TypicalPropertyTester.isTypicalInstanceEditor(session, input.getResource())) {\r
75                 MessageDialog.openInformation(shell, "Not Synchronizing", "Currently active editor is not a typical diagram instance editor.");\r
76                 return;\r
77             }\r
78 \r
79             RuleChooserDialog.RuleResult result = RuleChooserDialog.choose(shell, "Synchronizing typical instance with its template.", new Resource[] { input.getResource() });\r
80             if(result == null) return;\r
81 \r
82             SyncTypicalTemplatesToInstances req = SyncTypicalTemplatesToInstances.syncSingleInstance(result.selectedRules, input.getResource()).logging(result.logging); \r
83             session.syncRequest(req);\r
84             if (result.logging) {\r
85                 for(Resource log : req.logs)\r
86                     new OpenPlainTextDocumentAdapter().openEditor(log);\r
87             }\r
88         } catch (Exception e) {\r
89             ErrorLogger.defaultLogError(e);\r
90         }\r
91         return;\r
92         \r
93     }\r
94 \r
95 }\r