1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.modeling.ui.modelBrowser.handlers;
14 import java.lang.reflect.InvocationTargetException;
17 import org.eclipse.core.commands.AbstractHandler;
18 import org.eclipse.core.commands.ExecutionEvent;
19 import org.eclipse.core.commands.ExecutionException;
20 import org.eclipse.core.runtime.IProgressMonitor;
21 import org.eclipse.core.runtime.IStatus;
22 import org.eclipse.core.runtime.Status;
23 import org.eclipse.jface.dialogs.ProgressMonitorDialog;
24 import org.eclipse.jface.operation.IRunnableWithProgress;
25 import org.eclipse.jface.viewers.ISelection;
26 import org.eclipse.swt.widgets.Shell;
27 import org.eclipse.ui.handlers.HandlerUtil;
28 import org.simantics.browsing.ui.NodeContext;
29 import org.simantics.browsing.ui.common.node.DeleteException;
30 import org.simantics.browsing.ui.common.node.IDeletable;
31 import org.simantics.browsing.ui.common.node.IDeletableNode;
32 import org.simantics.db.Resource;
33 import org.simantics.db.WriteGraph;
34 import org.simantics.db.common.request.WriteRequest;
35 import org.simantics.db.exception.DatabaseException;
36 import org.simantics.db.layer0.exception.CannotRemoveException;
37 import org.simantics.db.layer0.util.RemoverUtil;
38 import org.simantics.modeling.ui.Activator;
39 import org.simantics.modeling.ui.modelBrowser.model.INode;
40 import org.simantics.modeling.ui.modelBrowser.model.INode2;
41 import org.simantics.ui.SimanticsUI;
42 import org.simantics.utils.logging.TimeLogger;
43 import org.simantics.utils.ui.ErrorLogger;
44 import org.simantics.utils.ui.ISelectionUtils;
45 import org.simantics.utils.ui.dialogs.ShowMessage;
48 * @author Tuukka Lehtonen
50 public class DeleteNodeHandler extends AbstractHandler {
53 public Object execute(ExecutionEvent event) throws ExecutionException {
54 TimeLogger.resetTimeAndLog(getClass(), "execute");
56 Shell shell = HandlerUtil.getActiveShellChecked(event);
57 ISelection sel = HandlerUtil.getCurrentSelection(event);
58 Set<NodeContext> ctxs = ISelectionUtils.filterSetSelection(sel, NodeContext.class);
62 if (ctxs.size() == 1 && handleLegacyDelete(ctxs.iterator().next())) {
66 // If all selected nodes are IDeletableNodes, delete them.
67 final Set<IDeletableNode> deletableNodes = ISelectionUtils.filterSetSelection(ctxs, IDeletableNode.class);
68 if (!deletableNodes.isEmpty()) {
69 // Only delete if all selected nodes are IDeletableNodes
70 if (deletableNodes.size() == ctxs.size()) {
71 inJob(shell, new IRunnableWithProgress() {
73 public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
74 monitor.beginTask("Delete selected nodes", IProgressMonitor.UNKNOWN);
76 for (IDeletableNode deletableNode : deletableNodes) {
77 deletableNode.delete();
79 } catch (DeleteException e) {
80 throw new InvocationTargetException(e);
86 final Set<Resource> rs = ISelectionUtils.filterSetSelection(ctxs, Resource.class);
87 if (rs.size() == ctxs.size()) {
88 inJob(shell, new IRunnableWithProgress() {
90 public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
91 monitor.beginTask("Delete selected nodes", IProgressMonitor.UNKNOWN);
93 RemoverUtil.tryCollectionRemover(rs);
94 } catch (CannotRemoveException e) {
95 ShowMessage.showInformation("Delete Selection Was Denied", e.getLocalizedMessage());
96 } catch (DatabaseException e) {
97 throw new InvocationTargetException(e);
104 TimeLogger.log("Node deleted");
108 private void inJob(Shell shell, final IRunnableWithProgress runnable) {
109 ProgressMonitorDialog dialog = new ProgressMonitorDialog(shell);
111 dialog.run(true, false, runnable);
112 } catch (InvocationTargetException e) {
113 Activator.getDefault().getLog().log( new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Delete failed, see exception for details.", e.getCause()) );
114 } catch (InterruptedException e) {
115 Activator.getDefault().getLog().log( new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Delete failed, see exception for details.", e.getCause()) );
117 // Job job = new Job("Delete selection") {
119 // protected IStatus run(IProgressMonitor monitor) {
120 // monitor.beginTask("Delete selected nodes", IProgressMonitor.UNKNOWN);
122 // runnable.run(monitor);
123 // return Status.OK_STATUS;
124 // } catch (InvocationTargetException e) {
125 // return new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Delete failed, see exception for details.", e.getCause());
126 // } catch (Exception e) {
127 // return new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Delete failed, see exception for details.", e);
133 // job.setUser(true);
134 // job.setRule(new ObjectIdentitySchedulingRule(schedulingObject));
143 private boolean handleLegacyDelete(NodeContext ctx) {
144 if (ctx.getAdapter(IDeletable.class) == null) {
148 final INode node = (INode) ctx.getAdapter(INode.class);
150 SimanticsUI.getSession().asyncRequest(new WriteRequest() {
152 public void perform(WriteGraph graph) throws DatabaseException {
153 node.handleDelete(graph);
159 INode2 node2 = (INode2) ctx.getAdapter(INode2.class);
162 node2.handleDelete();
163 } catch (DeleteException e) {
164 ShowMessage.showError("Delete failed", e.getMessage());
165 ErrorLogger.defaultLogError(e);
170 // Could not handle with legacy logic.