2 * Copyright (C) 2007-2012 Argeo GmbH
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org
.argeo
.eclipse
.ui
.workbench
.commands
;
18 import java
.util
.Iterator
;
20 import javax
.jcr
.Node
;
21 import javax
.jcr
.RepositoryException
;
23 import org
.argeo
.ArgeoException
;
24 import org
.argeo
.eclipse
.ui
.ErrorFeedback
;
25 import org
.argeo
.eclipse
.ui
.TreeParent
;
26 import org
.argeo
.eclipse
.ui
.workbench
.jcr
.JcrBrowserView
;
27 import org
.argeo
.eclipse
.ui
.workbench
.jcr
.internal
.model
.SingleJcrNodeElem
;
28 import org
.argeo
.eclipse
.ui
.workbench
.jcr
.internal
.model
.WorkspaceElem
;
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
.dialogs
.MessageDialog
;
33 import org
.eclipse
.jface
.viewers
.ISelection
;
34 import org
.eclipse
.jface
.viewers
.IStructuredSelection
;
35 import org
.eclipse
.ui
.handlers
.HandlerUtil
;
38 * Deletes the selected nodes: both in the JCR repository and in the UI view.
39 * Warning no check is done, except implementation dependent native checks,
42 * This handler is still 'hard linked' to a GenericJcrBrowser view to enable
43 * correct tree refresh when a node is added. This must be corrected in future
46 public class DeleteNodes
extends AbstractHandler
{
47 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
48 ISelection selection
= HandlerUtil
.getActiveWorkbenchWindow(event
)
49 .getActivePage().getSelection();
50 if (selection
== null || !(selection
instanceof IStructuredSelection
))
53 JcrBrowserView view
= (JcrBrowserView
) HandlerUtil
54 .getActiveWorkbenchWindow(event
).getActivePage()
55 .findView(HandlerUtil
.getActivePartId(event
));
58 StringBuffer buf
= new StringBuffer("");
59 Iterator
<?
> lst
= ((IStructuredSelection
) selection
).iterator();
60 while (lst
.hasNext()) {
61 SingleJcrNodeElem sjn
= ((SingleJcrNodeElem
) lst
.next());
62 buf
.append(sjn
.getName()).append(' ');
64 Boolean ok
= MessageDialog
.openConfirm(
65 HandlerUtil
.getActiveShell(event
), "Confirm deletion",
66 "Do you want to delete " + buf
+ "?");
70 Iterator
<?
> it
= ((IStructuredSelection
) selection
).iterator();
72 SingleJcrNodeElem ancestor
= null;
73 WorkspaceElem rootAncestor
= null;
75 while (it
.hasNext()) {
77 if (obj
instanceof SingleJcrNodeElem
) {
79 SingleJcrNodeElem sjn
= (SingleJcrNodeElem
) obj
;
80 TreeParent tp
= (TreeParent
) sjn
.getParent();
81 Node node
= sjn
.getNode();
85 node
.getSession().save();
89 // Check if the parent is the root node
90 if (tp
instanceof WorkspaceElem
)
91 rootAncestor
= (WorkspaceElem
) tp
;
93 ancestor
= getOlder(ancestor
, (SingleJcrNodeElem
) tp
);
96 if (rootAncestor
!= null)
97 view
.nodeRemoved(rootAncestor
);
98 else if (ancestor
!= null)
99 view
.nodeRemoved(ancestor
);
100 } catch (Exception e
) {
101 ErrorFeedback
.show("Cannot delete selected node ", e
);
107 private SingleJcrNodeElem
getOlder(SingleJcrNodeElem A
, SingleJcrNodeElem B
) {
110 return B
== null ?
null : B
;
111 // Todo enhanced this method
113 return A
.getNode().getDepth() <= B
.getNode().getDepth() ? A
: B
;
114 } catch (RepositoryException re
) {
115 throw new ArgeoException("Cannot find ancestor", re
);