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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.modeling.ui.modelBrowser.handlers;
\r
14 import java.lang.reflect.InvocationTargetException;
\r
15 import java.util.Set;
\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
48 * @author Tuukka Lehtonen
\r
50 public class DeleteNodeHandler extends AbstractHandler {
\r
53 public Object execute(ExecutionEvent event) throws ExecutionException {
\r
54 TimeLogger.resetTimeAndLog(getClass(), "execute");
\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
62 if (ctxs.size() == 1 && handleLegacyDelete(ctxs.iterator().next())) {
\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
73 public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
\r
74 monitor.beginTask("Delete selected nodes", IProgressMonitor.UNKNOWN);
\r
76 for (IDeletableNode deletableNode : deletableNodes) {
\r
77 deletableNode.delete();
\r
79 } catch (DeleteException e) {
\r
80 throw new InvocationTargetException(e);
\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
90 public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
\r
91 monitor.beginTask("Delete selected nodes", IProgressMonitor.UNKNOWN);
\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
104 TimeLogger.log("Node deleted");
\r
108 private void inJob(Shell shell, final IRunnableWithProgress runnable) {
\r
109 ProgressMonitorDialog dialog = new ProgressMonitorDialog(shell);
\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
117 // Job job = new Job("Delete selection") {
\r
119 // protected IStatus run(IProgressMonitor monitor) {
\r
120 // monitor.beginTask("Delete selected nodes", IProgressMonitor.UNKNOWN);
\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
133 // job.setUser(true);
\r
134 // job.setRule(new ObjectIdentitySchedulingRule(schedulingObject));
\r
143 private boolean handleLegacyDelete(NodeContext ctx) {
\r
144 if (ctx.getAdapter(IDeletable.class) == null) {
\r
148 final INode node = (INode) ctx.getAdapter(INode.class);
\r
149 if (node != null) {
\r
150 SimanticsUI.getSession().asyncRequest(new WriteRequest() {
\r
152 public void perform(WriteGraph graph) throws DatabaseException {
\r
153 node.handleDelete(graph);
\r
159 INode2 node2 = (INode2) ctx.getAdapter(INode2.class);
\r
160 if (node2 != null) {
\r
162 node2.handleDelete();
\r
163 } catch (DeleteException e) {
\r
164 ShowMessage.showError("Delete failed", e.getMessage());
\r
165 ErrorLogger.defaultLogError(e);
\r
170 // Could not handle with legacy logic.
\r