]> gerrit.simantics Code Review - simantics/platform.git/blob
fd824f2bc40e0952cd7706bb4e455c3d1bf83de3
[simantics/platform.git] /
1 package org.simantics.modeling.ui.actions;\r
2 \r
3 import org.eclipse.core.runtime.IStatus;\r
4 import org.eclipse.core.runtime.Status;\r
5 import org.eclipse.jface.dialogs.Dialog;\r
6 import org.eclipse.ui.PlatformUI;\r
7 import org.simantics.Simantics;\r
8 import org.simantics.db.ReadGraph;\r
9 import org.simantics.db.Resource;\r
10 import org.simantics.db.WriteGraph;\r
11 import org.simantics.db.common.request.ReadRequest;\r
12 import org.simantics.db.common.request.WriteRequest;\r
13 import org.simantics.db.exception.DatabaseException;\r
14 import org.simantics.db.layer0.adapter.ActionFactory;\r
15 import org.simantics.modeling.ui.Activator;\r
16 import org.simantics.modeling.ui.diagram.renaming.ComponentsRenamingDialog;\r
17 import org.simantics.modeling.ui.diagram.renaming.ComponentsRenamingModel;\r
18 \r
19 /**\r
20  * @author Hannu Niemistö\r
21  */\r
22 public class RenameDiagramComponents implements ActionFactory {\r
23 \r
24     @Override\r
25     public Runnable create(Object target) {\r
26         if (!(target instanceof Resource))\r
27             return null;\r
28         final Resource composite = (Resource)target;\r
29 \r
30         return new Runnable() {\r
31             @Override\r
32             public void run() {\r
33                 final ComponentsRenamingModel model = new ComponentsRenamingModel();\r
34                 try {\r
35                     Simantics.getSession().syncRequest(new ReadRequest() {\r
36                         @Override\r
37                         public void run(ReadGraph graph) throws DatabaseException {\r
38                             model.read(graph, composite);\r
39                         }\r
40                     });\r
41                     ComponentsRenamingDialog dialog = new ComponentsRenamingDialog(\r
42                             PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(),\r
43                             model);\r
44                     if(dialog.open() == Dialog.OK) {\r
45                         Simantics.getSession().syncRequest(new WriteRequest() {\r
46                             @Override\r
47                             public void perform(WriteGraph graph)\r
48                                     throws DatabaseException {\r
49                                 graph.markUndoPoint();\r
50                                 model.write(graph);\r
51                             }\r
52                         });\r
53                     }\r
54                 } catch (DatabaseException e) {\r
55                     Activator.getDefault().getLog().log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, "RenameDiagramComponents action failed, see exception for details", e));\r
56                 }\r
57             }\r
58         };\r
59     }\r
60 \r
61 }\r