]> git.argeo.org Git - lgpl/argeo-commons.git/blob - server/plugins/org.argeo.jcr.ui.explorer/src/main/java/org/argeo/jcr/ui/explorer/commands/DeleteNodes.java
fix bug 85 : JCR explorer refresh was not implemented for TreeParent objects of type...
[lgpl/argeo-commons.git] / server / plugins / org.argeo.jcr.ui.explorer / src / main / java / org / argeo / jcr / ui / explorer / commands / 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.jcr.ui.explorer.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.TreeParent;
26 import org.argeo.jcr.ui.explorer.model.SingleJcrNode;
27 import org.argeo.jcr.ui.explorer.model.WorkspaceNode;
28 import org.argeo.jcr.ui.explorer.views.GenericJcrBrowser;
29 import org.eclipse.core.commands.AbstractHandler;
30 import org.eclipse.core.commands.ExecutionEvent;
31 import org.eclipse.core.commands.ExecutionException;
32 import org.eclipse.jface.viewers.ISelection;
33 import org.eclipse.jface.viewers.IStructuredSelection;
34 import org.eclipse.ui.handlers.HandlerUtil;
35
36 /**
37 * Deletes the selected nodes: both in the JCR repository and in the UI view.
38 * Warning no check is done, except implementation dependent native checks,
39 * handle with care.
40 *
41 * This handler is still 'hard linked' to a GenericJcrBrowser view to enable
42 * correct tree refresh when a node is added. This must be corrected in future
43 * versions.
44 */
45 public class DeleteNodes extends AbstractHandler {
46 public Object execute(ExecutionEvent event) throws ExecutionException {
47 ISelection selection = HandlerUtil.getActiveWorkbenchWindow(event)
48 .getActivePage().getSelection();
49 GenericJcrBrowser view = (GenericJcrBrowser) HandlerUtil
50 .getActiveWorkbenchWindow(event).getActivePage()
51 .findView(HandlerUtil.getActivePartId(event));
52
53 if (selection != null && selection instanceof IStructuredSelection) {
54 Iterator<?> it = ((IStructuredSelection) selection).iterator();
55 Object obj = null;
56 SingleJcrNode ancestor = null;
57 WorkspaceNode rootAncestor = null;
58 try {
59 while (it.hasNext()) {
60 obj = it.next();
61 if (obj instanceof SingleJcrNode) {
62 // Cache objects
63 SingleJcrNode sjn = (SingleJcrNode) obj;
64 TreeParent tp = (TreeParent) sjn.getParent();
65 Node node = sjn.getNode();
66
67 // Jcr Remove
68 node.remove();
69 node.getSession().save();
70 // UI remove
71 tp.removeChild(sjn);
72
73 // Check if the parent is the root node
74 if (tp instanceof WorkspaceNode)
75 rootAncestor = (WorkspaceNode) tp;
76 else
77 ancestor = getOlder(ancestor, (SingleJcrNode) tp);
78 }
79 }
80 if (rootAncestor != null)
81 view.nodeRemoved(rootAncestor);
82 else if (ancestor != null)
83 view.nodeRemoved(ancestor);
84 } catch (Exception e) {
85 ErrorFeedback.show("Cannot delete selected node ", e);
86 }
87 }
88 return null;
89 }
90
91 private SingleJcrNode getOlder(SingleJcrNode A, SingleJcrNode B) {
92 try {
93 if (A == null)
94 return B == null ? null : B;
95 // Todo enhanced this method
96 else
97 return A.getNode().getDepth() <= B.getNode().getDepth() ? A : B;
98 } catch (RepositoryException re) {
99 throw new ArgeoException("Cannot find ancestor", re);
100 }
101 }
102 }