]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/modelBrowser/handlers/DeleteNodeHandler.java
(refs #7571) Fixed incorrect handling of effects in edo
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / modelBrowser / handlers / DeleteNodeHandler.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.modeling.ui.modelBrowser.handlers;
13
14 import java.lang.reflect.InvocationTargetException;
15 import java.util.Set;
16
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;
46
47 /**
48  * @author Tuukka Lehtonen
49  */
50 public class DeleteNodeHandler extends AbstractHandler {
51
52     @Override
53     public Object execute(ExecutionEvent event) throws ExecutionException {
54         TimeLogger.resetTimeAndLog(getClass(), "execute");
55         
56         Shell shell = HandlerUtil.getActiveShellChecked(event);
57         ISelection sel = HandlerUtil.getCurrentSelection(event);
58         Set<NodeContext> ctxs = ISelectionUtils.filterSetSelection(sel, NodeContext.class);
59         if (ctxs.isEmpty())
60             return null;
61
62         if (ctxs.size() == 1 && handleLegacyDelete(ctxs.iterator().next())) {
63             return null;
64         }
65
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() {
72                     @Override
73                     public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
74                         monitor.beginTask("Delete selected nodes", IProgressMonitor.UNKNOWN);
75                         try {
76                             for (IDeletableNode deletableNode : deletableNodes) {
77                                 deletableNode.delete();
78                             }
79                         } catch (DeleteException e) {
80                             throw new InvocationTargetException(e);
81                         }
82                     }
83                 });
84             }
85         } else {
86             final Set<Resource> rs = ISelectionUtils.filterSetSelection(ctxs, Resource.class);
87             if (rs.size() == ctxs.size()) {
88                 inJob(shell, new IRunnableWithProgress() {
89                     @Override
90                     public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
91                         monitor.beginTask("Delete selected nodes", IProgressMonitor.UNKNOWN);
92                         try {
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);
98                         }
99                     }
100                 });
101             }
102         }
103
104         TimeLogger.log("Node deleted");
105         return null;
106     }
107
108     private void inJob(Shell shell, final IRunnableWithProgress runnable) {
109         ProgressMonitorDialog dialog = new ProgressMonitorDialog(shell);
110         try {
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()) );
116         }
117 //        Job job = new Job("Delete selection") {
118 //            @Override
119 //            protected IStatus run(IProgressMonitor monitor) {
120 //                monitor.beginTask("Delete selected nodes", IProgressMonitor.UNKNOWN);
121 //                try {
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);
128 //                } finally {
129 //                    monitor.done();
130 //                }
131 //            }
132 //        };
133 //        job.setUser(true);
134 //        job.setRule(new ObjectIdentitySchedulingRule(schedulingObject));
135 //        job.schedule();
136     }
137
138
139     /**
140      * @param ctx
141      * @return
142      */
143     private boolean handleLegacyDelete(NodeContext ctx) {
144         if (ctx.getAdapter(IDeletable.class) == null) {
145             return false;
146         }
147
148         final INode node = (INode) ctx.getAdapter(INode.class);
149         if (node != null) {
150             SimanticsUI.getSession().asyncRequest(new WriteRequest() {
151                 @Override
152                 public void perform(WriteGraph graph) throws DatabaseException {
153                     node.handleDelete(graph);
154                 }
155             });
156             return true;
157         }
158
159         INode2 node2 = (INode2) ctx.getAdapter(INode2.class);
160         if (node2 != null) {
161             try {
162                 node2.handleDelete();
163             } catch (DeleteException e) {
164                 ShowMessage.showError("Delete failed", e.getMessage());
165                 ErrorLogger.defaultLogError(e);
166             }
167             return true;
168         }
169
170         // Could not handle with legacy logic.
171         return false;
172     }
173
174 }