]> gerrit.simantics Code Review - simantics/platform.git/blob
df049a6719009e4998bd6d6a36cb0cd25e79db25
[simantics/platform.git] /
1 package org.simantics.modeling.ui.typicals;\r
2 \r
3 import org.eclipse.core.commands.AbstractHandler;\r
4 import org.eclipse.core.commands.ExecutionEvent;\r
5 import org.eclipse.core.commands.ExecutionException;\r
6 import org.eclipse.jface.dialogs.MessageDialog;\r
7 import org.eclipse.swt.widgets.Shell;\r
8 import org.eclipse.ui.IEditorPart;\r
9 import org.eclipse.ui.handlers.HandlerUtil;\r
10 import org.simantics.Simantics;\r
11 import org.simantics.db.Resource;\r
12 import org.simantics.db.Session;\r
13 import org.simantics.modeling.typicals.SyncTypicalTemplatesToInstances;\r
14 import org.simantics.modeling.ui.documents.OpenPlainTextDocumentAdapter;\r
15 import org.simantics.modeling.ui.property.TypicalPropertyTester;\r
16 import org.simantics.ui.workbench.IResourceEditorInput;\r
17 import org.simantics.utils.ui.ErrorLogger;\r
18 \r
19 /**\r
20  * @author Tuukka Lehtonen\r
21  *\r
22  */\r
23 public class SyncCurrentTypicalInstanceWithTemplate extends AbstractHandler {\r
24 \r
25     @Override\r
26     public Object execute(ExecutionEvent event) throws ExecutionException {\r
27         Shell shell = HandlerUtil.getActiveShell(event);\r
28         IEditorPart activeEditor = HandlerUtil.getActiveEditorChecked(event);\r
29         IResourceEditorInput input = (IResourceEditorInput) activeEditor.getEditorInput();\r
30         Session session = Simantics.getSession();\r
31 \r
32 //        if (!MessageDialog\r
33 //                .openConfirm(shell,\r
34 //                        "Synchronize Typical Instance With Template",\r
35 //                        "Are you sure you want to synchronize this typical instance with its template?"))\r
36 //            return null;\r
37 \r
38         try {\r
39             if (!TypicalPropertyTester.isTypicalInstanceEditor(session, input.getResource())) {\r
40                 MessageDialog.openInformation(shell, "Not Synchronizing", "Currently active editor is not a typical diagram instance editor.");\r
41                 return null;\r
42             }\r
43 \r
44             RuleChooserDialog.RuleResult result = RuleChooserDialog.choose(shell, "Synchronizing typical instance with its template.", new Resource[] { input.getResource() });\r
45             if(result == null) return null;\r
46 \r
47             SyncTypicalTemplatesToInstances req = SyncTypicalTemplatesToInstances.syncSingleInstance(result.selectedRules, input.getResource()).logging(result.logging); \r
48             session.syncRequest(req);\r
49             if (result.logging) {\r
50                 for(Resource log : req.logs)\r
51                     new OpenPlainTextDocumentAdapter().openEditor(log);\r
52             }\r
53         } catch (Exception e) {\r
54             ErrorLogger.defaultLogError(e);\r
55         }\r
56         return null;\r
57         \r
58     }\r
59 \r
60 }\r