]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - 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
diff --git a/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/modelBrowser/handlers/DeleteNodeHandler.java b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/modelBrowser/handlers/DeleteNodeHandler.java
new file mode 100644 (file)
index 0000000..f3ae81c
--- /dev/null
@@ -0,0 +1,174 @@
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ *     VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.modeling.ui.modelBrowser.handlers;\r
+\r
+import java.lang.reflect.InvocationTargetException;\r
+import java.util.Set;\r
+\r
+import org.eclipse.core.commands.AbstractHandler;\r
+import org.eclipse.core.commands.ExecutionEvent;\r
+import org.eclipse.core.commands.ExecutionException;\r
+import org.eclipse.core.runtime.IProgressMonitor;\r
+import org.eclipse.core.runtime.IStatus;\r
+import org.eclipse.core.runtime.Status;\r
+import org.eclipse.jface.dialogs.ProgressMonitorDialog;\r
+import org.eclipse.jface.operation.IRunnableWithProgress;\r
+import org.eclipse.jface.viewers.ISelection;\r
+import org.eclipse.swt.widgets.Shell;\r
+import org.eclipse.ui.handlers.HandlerUtil;\r
+import org.simantics.browsing.ui.NodeContext;\r
+import org.simantics.browsing.ui.common.node.DeleteException;\r
+import org.simantics.browsing.ui.common.node.IDeletable;\r
+import org.simantics.browsing.ui.common.node.IDeletableNode;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.WriteGraph;\r
+import org.simantics.db.common.request.WriteRequest;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.layer0.exception.CannotRemoveException;\r
+import org.simantics.db.layer0.util.RemoverUtil;\r
+import org.simantics.modeling.ui.Activator;\r
+import org.simantics.modeling.ui.modelBrowser.model.INode;\r
+import org.simantics.modeling.ui.modelBrowser.model.INode2;\r
+import org.simantics.ui.SimanticsUI;\r
+import org.simantics.utils.logging.TimeLogger;\r
+import org.simantics.utils.ui.ErrorLogger;\r
+import org.simantics.utils.ui.ISelectionUtils;\r
+import org.simantics.utils.ui.dialogs.ShowMessage;\r
+\r
+/**\r
+ * @author Tuukka Lehtonen\r
+ */\r
+public class DeleteNodeHandler extends AbstractHandler {\r
+\r
+    @Override\r
+    public Object execute(ExecutionEvent event) throws ExecutionException {\r
+        TimeLogger.resetTimeAndLog(getClass(), "execute");\r
+        \r
+        Shell shell = HandlerUtil.getActiveShellChecked(event);\r
+        ISelection sel = HandlerUtil.getCurrentSelection(event);\r
+        Set<NodeContext> ctxs = ISelectionUtils.filterSetSelection(sel, NodeContext.class);\r
+        if (ctxs.isEmpty())\r
+            return null;\r
+\r
+        if (ctxs.size() == 1 && handleLegacyDelete(ctxs.iterator().next())) {\r
+            return null;\r
+        }\r
+\r
+        // If all selected nodes are IDeletableNodes, delete them.\r
+        final Set<IDeletableNode> deletableNodes = ISelectionUtils.filterSetSelection(ctxs, IDeletableNode.class);\r
+        if (!deletableNodes.isEmpty()) {\r
+            // Only delete if all selected nodes are IDeletableNodes\r
+            if (deletableNodes.size() == ctxs.size()) {\r
+                inJob(shell, new IRunnableWithProgress() {\r
+                    @Override\r
+                    public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {\r
+                        monitor.beginTask("Delete selected nodes", IProgressMonitor.UNKNOWN);\r
+                        try {\r
+                            for (IDeletableNode deletableNode : deletableNodes) {\r
+                                deletableNode.delete();\r
+                            }\r
+                        } catch (DeleteException e) {\r
+                            throw new InvocationTargetException(e);\r
+                        }\r
+                    }\r
+                });\r
+            }\r
+        } else {\r
+            final Set<Resource> rs = ISelectionUtils.filterSetSelection(ctxs, Resource.class);\r
+            if (rs.size() == ctxs.size()) {\r
+                inJob(shell, new IRunnableWithProgress() {\r
+                    @Override\r
+                    public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {\r
+                        monitor.beginTask("Delete selected nodes", IProgressMonitor.UNKNOWN);\r
+                        try {\r
+                            RemoverUtil.tryCollectionRemover(rs);\r
+                        } catch (CannotRemoveException e) {\r
+                               ShowMessage.showInformation("Delete Selection Was Denied", e.getLocalizedMessage());\r
+                        } catch (DatabaseException e) {\r
+                            throw new InvocationTargetException(e);\r
+                        }\r
+                    }\r
+                });\r
+            }\r
+        }\r
+\r
+        TimeLogger.log("Node deleted");\r
+        return null;\r
+    }\r
+\r
+    private void inJob(Shell shell, final IRunnableWithProgress runnable) {\r
+        ProgressMonitorDialog dialog = new ProgressMonitorDialog(shell);\r
+        try {\r
+            dialog.run(true, false, runnable);\r
+        } catch (InvocationTargetException e) {\r
+            Activator.getDefault().getLog().log( new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Delete failed, see exception for details.", e.getCause()) );\r
+        } catch (InterruptedException e) {\r
+            Activator.getDefault().getLog().log( new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Delete failed, see exception for details.", e.getCause()) );\r
+        }\r
+//        Job job = new Job("Delete selection") {\r
+//            @Override\r
+//            protected IStatus run(IProgressMonitor monitor) {\r
+//                monitor.beginTask("Delete selected nodes", IProgressMonitor.UNKNOWN);\r
+//                try {\r
+//                    runnable.run(monitor);\r
+//                    return Status.OK_STATUS;\r
+//                } catch (InvocationTargetException e) {\r
+//                    return new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Delete failed, see exception for details.", e.getCause());\r
+//                } catch (Exception e) {\r
+//                    return new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Delete failed, see exception for details.", e);\r
+//                } finally {\r
+//                    monitor.done();\r
+//                }\r
+//            }\r
+//        };\r
+//        job.setUser(true);\r
+//        job.setRule(new ObjectIdentitySchedulingRule(schedulingObject));\r
+//        job.schedule();\r
+    }\r
+\r
+
+    /**\r
+     * @param ctx\r
+     * @return\r
+     */\r
+    private boolean handleLegacyDelete(NodeContext ctx) {\r
+        if (ctx.getAdapter(IDeletable.class) == null) {\r
+            return false;\r
+        }\r
+\r
+        final INode node = (INode) ctx.getAdapter(INode.class);\r
+        if (node != null) {\r
+            SimanticsUI.getSession().asyncRequest(new WriteRequest() {\r
+                @Override\r
+                public void perform(WriteGraph graph) throws DatabaseException {\r
+                    node.handleDelete(graph);\r
+                }\r
+            });\r
+            return true;\r
+        }\r
+\r
+        INode2 node2 = (INode2) ctx.getAdapter(INode2.class);\r
+        if (node2 != null) {\r
+            try {\r
+                node2.handleDelete();\r
+            } catch (DeleteException e) {\r
+                ShowMessage.showError("Delete failed", e.getMessage());\r
+                ErrorLogger.defaultLogError(e);\r
+            }\r
+            return true;\r
+        }\r
+\r
+        // Could not handle with legacy logic.\r
+        return false;\r
+    }\r
+\r
+}\r