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