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