]> git.argeo.org Git - lgpl/argeo-commons.git/blob - DeleteNodes.java
6cde09e93b790047d0447a0b0dd38916d573ff22
[lgpl/argeo-commons.git] / DeleteNodes.java
1 /*
2 * Copyright (C) 2007-2012 Mathieu Baudier
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.argeo.eclipse.ui.jcr.commands;
17
18 import java.util.Iterator;
19
20 import javax.jcr.Node;
21 import javax.jcr.RepositoryException;
22
23 import org.argeo.ArgeoException;
24 import org.argeo.eclipse.ui.ErrorFeedback;
25 import org.argeo.eclipse.ui.jcr.views.AbstractJcrBrowser;
26 import org.eclipse.core.commands.AbstractHandler;
27 import org.eclipse.core.commands.ExecutionEvent;
28 import org.eclipse.core.commands.ExecutionException;
29 import org.eclipse.jface.viewers.ISelection;
30 import org.eclipse.jface.viewers.IStructuredSelection;
31 import org.eclipse.ui.handlers.HandlerUtil;
32
33 /**
34 * Deletes the selected nodes and refresh the corresponding AbstractJcrView.
35 * Note that no model specific check is done to see if the node can be removed
36 * or not. Extend or override to implement specific behaviour.
37 */
38 public class DeleteNodes extends AbstractHandler {
39 public final static String ID = "org.argeo.eclipse.ui.jcr.deleteNodes";
40 public final static String DEFAULT_LABEL = "Delete selected nodes";
41
42 public Object execute(ExecutionEvent event) throws ExecutionException {
43 ISelection selection = HandlerUtil.getActiveWorkbenchWindow(event)
44 .getActivePage().getSelection();
45 AbstractJcrBrowser view = (AbstractJcrBrowser) HandlerUtil
46 .getActiveWorkbenchWindow(event).getActivePage()
47 .findView(HandlerUtil.getActivePartId(event));
48
49 if (selection != null && selection instanceof IStructuredSelection) {
50 Iterator<?> it = ((IStructuredSelection) selection).iterator();
51 Object obj = null;
52 Node ancestor = null;
53 try {
54 while (it.hasNext()) {
55 obj = it.next();
56 if (obj instanceof Node) {
57 Node node = (Node) obj;
58 Node parentNode = node.getParent();
59 node.remove();
60 node.getSession().save();
61 ancestor = getOlder(ancestor, parentNode);
62 }
63 }
64 if (ancestor != null)
65 view.nodeRemoved(ancestor);
66 } catch (Exception e) {
67 ErrorFeedback.show("Cannot delete node " + obj, e);
68 }
69 }
70 return null;
71 }
72
73 protected Node getOlder(Node A, Node B) {
74 try {
75
76 if (A == null)
77 return B == null ? null : B;
78 // Todo enhanced this method
79 else
80 return A.getDepth() <= B.getDepth() ? A : B;
81 } catch (RepositoryException re) {
82 throw new ArgeoException("Cannot find ancestor", re);
83 }
84 }
85 }