1 package org
.argeo
.cms
.ui
.workbench
.internal
.jcr
.commands
;
3 import java
.util
.Iterator
;
6 import javax
.jcr
.RepositoryException
;
8 import org
.argeo
.cms
.ui
.jcr
.model
.SingleJcrNodeElem
;
9 import org
.argeo
.cms
.ui
.jcr
.model
.WorkspaceElem
;
10 import org
.argeo
.cms
.ui
.workbench
.jcr
.JcrBrowserView
;
11 import org
.argeo
.eclipse
.ui
.EclipseUiException
;
12 import org
.argeo
.eclipse
.ui
.TreeParent
;
13 import org
.argeo
.eclipse
.ui
.dialogs
.ErrorFeedback
;
14 import org
.eclipse
.core
.commands
.AbstractHandler
;
15 import org
.eclipse
.core
.commands
.ExecutionEvent
;
16 import org
.eclipse
.core
.commands
.ExecutionException
;
17 import org
.eclipse
.jface
.dialogs
.MessageDialog
;
18 import org
.eclipse
.jface
.viewers
.ISelection
;
19 import org
.eclipse
.jface
.viewers
.IStructuredSelection
;
20 import org
.eclipse
.ui
.handlers
.HandlerUtil
;
23 * Delete the selected nodes: both in the JCR repository and in the UI view.
24 * Warning no check is done, except implementation dependent native checks,
27 * This handler is still 'hard linked' to a GenericJcrBrowser view to enable
28 * correct tree refresh when a node is added. This must be corrected in future
31 public class DeleteNodes
extends AbstractHandler
{
32 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
33 ISelection selection
= HandlerUtil
.getActiveWorkbenchWindow(event
)
34 .getActivePage().getSelection();
35 if (selection
== null || !(selection
instanceof IStructuredSelection
))
38 JcrBrowserView view
= (JcrBrowserView
) HandlerUtil
39 .getActiveWorkbenchWindow(event
).getActivePage()
40 .findView(HandlerUtil
.getActivePartId(event
));
43 StringBuffer buf
= new StringBuffer("");
44 Iterator
<?
> lst
= ((IStructuredSelection
) selection
).iterator();
45 while (lst
.hasNext()) {
46 SingleJcrNodeElem sjn
= ((SingleJcrNodeElem
) lst
.next());
47 buf
.append(sjn
.getName()).append(' ');
49 Boolean doRemove
= MessageDialog
.openConfirm(
50 HandlerUtil
.getActiveShell(event
), "Confirm deletion",
51 "Do you want to delete " + buf
+ "?");
55 Iterator
<?
> it
= ((IStructuredSelection
) selection
).iterator();
57 SingleJcrNodeElem ancestor
= null;
58 WorkspaceElem rootAncestor
= null;
60 while (it
.hasNext()) {
62 if (obj
instanceof SingleJcrNodeElem
) {
64 SingleJcrNodeElem sjn
= (SingleJcrNodeElem
) obj
;
65 TreeParent tp
= (TreeParent
) sjn
.getParent();
66 Node node
= sjn
.getNode();
70 node
.getSession().save();
74 // Check if the parent is the root node
75 if (tp
instanceof WorkspaceElem
)
76 rootAncestor
= (WorkspaceElem
) tp
;
78 ancestor
= getOlder(ancestor
, (SingleJcrNodeElem
) tp
);
81 if (rootAncestor
!= null)
82 view
.nodeRemoved(rootAncestor
);
83 else if (ancestor
!= null)
84 view
.nodeRemoved(ancestor
);
85 } catch (Exception e
) {
86 ErrorFeedback
.show("Cannot delete selected node ", e
);
92 private SingleJcrNodeElem
getOlder(SingleJcrNodeElem A
, SingleJcrNodeElem B
) {
95 return B
== null ?
null : B
;
96 // Todo enhanced this method
98 return A
.getNode().getDepth() <= B
.getNode().getDepth() ? A
: B
;
99 } catch (RepositoryException re
) {
100 throw new EclipseUiException("Cannot find ancestor", re
);