]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/modelBrowser/handlers/DeleteNodeHandler.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / modelBrowser / handlers / DeleteNodeHandler.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.modeling.ui.modelBrowser.handlers;\r
13 \r
14 import java.lang.reflect.InvocationTargetException;\r
15 import java.util.Set;\r
16 \r
17 import org.eclipse.core.commands.AbstractHandler;\r
18 import org.eclipse.core.commands.ExecutionEvent;\r
19 import org.eclipse.core.commands.ExecutionException;\r
20 import org.eclipse.core.runtime.IProgressMonitor;\r
21 import org.eclipse.core.runtime.IStatus;\r
22 import org.eclipse.core.runtime.Status;\r
23 import org.eclipse.jface.dialogs.ProgressMonitorDialog;\r
24 import org.eclipse.jface.operation.IRunnableWithProgress;\r
25 import org.eclipse.jface.viewers.ISelection;\r
26 import org.eclipse.swt.widgets.Shell;\r
27 import org.eclipse.ui.handlers.HandlerUtil;\r
28 import org.simantics.browsing.ui.NodeContext;\r
29 import org.simantics.browsing.ui.common.node.DeleteException;\r
30 import org.simantics.browsing.ui.common.node.IDeletable;\r
31 import org.simantics.browsing.ui.common.node.IDeletableNode;\r
32 import org.simantics.db.Resource;\r
33 import org.simantics.db.WriteGraph;\r
34 import org.simantics.db.common.request.WriteRequest;\r
35 import org.simantics.db.exception.DatabaseException;\r
36 import org.simantics.db.layer0.exception.CannotRemoveException;\r
37 import org.simantics.db.layer0.util.RemoverUtil;\r
38 import org.simantics.modeling.ui.Activator;\r
39 import org.simantics.modeling.ui.modelBrowser.model.INode;\r
40 import org.simantics.modeling.ui.modelBrowser.model.INode2;\r
41 import org.simantics.ui.SimanticsUI;\r
42 import org.simantics.utils.logging.TimeLogger;\r
43 import org.simantics.utils.ui.ErrorLogger;\r
44 import org.simantics.utils.ui.ISelectionUtils;\r
45 import org.simantics.utils.ui.dialogs.ShowMessage;\r
46 \r
47 /**\r
48  * @author Tuukka Lehtonen\r
49  */\r
50 public class DeleteNodeHandler extends AbstractHandler {\r
51 \r
52     @Override\r
53     public Object execute(ExecutionEvent event) throws ExecutionException {\r
54         TimeLogger.resetTimeAndLog(getClass(), "execute");\r
55         \r
56         Shell shell = HandlerUtil.getActiveShellChecked(event);\r
57         ISelection sel = HandlerUtil.getCurrentSelection(event);\r
58         Set<NodeContext> ctxs = ISelectionUtils.filterSetSelection(sel, NodeContext.class);\r
59         if (ctxs.isEmpty())\r
60             return null;\r
61 \r
62         if (ctxs.size() == 1 && handleLegacyDelete(ctxs.iterator().next())) {\r
63             return null;\r
64         }\r
65 \r
66         // If all selected nodes are IDeletableNodes, delete them.\r
67         final Set<IDeletableNode> deletableNodes = ISelectionUtils.filterSetSelection(ctxs, IDeletableNode.class);\r
68         if (!deletableNodes.isEmpty()) {\r
69             // Only delete if all selected nodes are IDeletableNodes\r
70             if (deletableNodes.size() == ctxs.size()) {\r
71                 inJob(shell, new IRunnableWithProgress() {\r
72                     @Override\r
73                     public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {\r
74                         monitor.beginTask("Delete selected nodes", IProgressMonitor.UNKNOWN);\r
75                         try {\r
76                             for (IDeletableNode deletableNode : deletableNodes) {\r
77                                 deletableNode.delete();\r
78                             }\r
79                         } catch (DeleteException e) {\r
80                             throw new InvocationTargetException(e);\r
81                         }\r
82                     }\r
83                 });\r
84             }\r
85         } else {\r
86             final Set<Resource> rs = ISelectionUtils.filterSetSelection(ctxs, Resource.class);\r
87             if (rs.size() == ctxs.size()) {\r
88                 inJob(shell, new IRunnableWithProgress() {\r
89                     @Override\r
90                     public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {\r
91                         monitor.beginTask("Delete selected nodes", IProgressMonitor.UNKNOWN);\r
92                         try {\r
93                             RemoverUtil.tryCollectionRemover(rs);\r
94                         } catch (CannotRemoveException e) {\r
95                                 ShowMessage.showInformation("Delete Selection Was Denied", e.getLocalizedMessage());\r
96                         } catch (DatabaseException e) {\r
97                             throw new InvocationTargetException(e);\r
98                         }\r
99                     }\r
100                 });\r
101             }\r
102         }\r
103 \r
104         TimeLogger.log("Node deleted");\r
105         return null;\r
106     }\r
107 \r
108     private void inJob(Shell shell, final IRunnableWithProgress runnable) {\r
109         ProgressMonitorDialog dialog = new ProgressMonitorDialog(shell);\r
110         try {\r
111             dialog.run(true, false, runnable);\r
112         } catch (InvocationTargetException e) {\r
113             Activator.getDefault().getLog().log( new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Delete failed, see exception for details.", e.getCause()) );\r
114         } catch (InterruptedException e) {\r
115             Activator.getDefault().getLog().log( new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Delete failed, see exception for details.", e.getCause()) );\r
116         }\r
117 //        Job job = new Job("Delete selection") {\r
118 //            @Override\r
119 //            protected IStatus run(IProgressMonitor monitor) {\r
120 //                monitor.beginTask("Delete selected nodes", IProgressMonitor.UNKNOWN);\r
121 //                try {\r
122 //                    runnable.run(monitor);\r
123 //                    return Status.OK_STATUS;\r
124 //                } catch (InvocationTargetException e) {\r
125 //                    return new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Delete failed, see exception for details.", e.getCause());\r
126 //                } catch (Exception e) {\r
127 //                    return new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Delete failed, see exception for details.", e);\r
128 //                } finally {\r
129 //                    monitor.done();\r
130 //                }\r
131 //            }\r
132 //        };\r
133 //        job.setUser(true);\r
134 //        job.setRule(new ObjectIdentitySchedulingRule(schedulingObject));\r
135 //        job.schedule();\r
136     }\r
137 \r
138
139     /**\r
140      * @param ctx\r
141      * @return\r
142      */\r
143     private boolean handleLegacyDelete(NodeContext ctx) {\r
144         if (ctx.getAdapter(IDeletable.class) == null) {\r
145             return false;\r
146         }\r
147 \r
148         final INode node = (INode) ctx.getAdapter(INode.class);\r
149         if (node != null) {\r
150             SimanticsUI.getSession().asyncRequest(new WriteRequest() {\r
151                 @Override\r
152                 public void perform(WriteGraph graph) throws DatabaseException {\r
153                     node.handleDelete(graph);\r
154                 }\r
155             });\r
156             return true;\r
157         }\r
158 \r
159         INode2 node2 = (INode2) ctx.getAdapter(INode2.class);\r
160         if (node2 != null) {\r
161             try {\r
162                 node2.handleDelete();\r
163             } catch (DeleteException e) {\r
164                 ShowMessage.showError("Delete failed", e.getMessage());\r
165                 ErrorLogger.defaultLogError(e);\r
166             }\r
167             return true;\r
168         }\r
169 \r
170         // Could not handle with legacy logic.\r
171         return false;\r
172     }\r
173 \r
174 }\r