2 * Copyright (C) 2007-2012 Mathieu Baudier
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
.jcr
.ui
.explorer
.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
.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
;
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,
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
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
));
53 if (selection
!= null && selection
instanceof IStructuredSelection
) {
54 Iterator
<?
> it
= ((IStructuredSelection
) selection
).iterator();
56 SingleJcrNode ancestor
= null;
57 WorkspaceNode rootAncestor
= null;
59 while (it
.hasNext()) {
61 if (obj
instanceof SingleJcrNode
) {
63 SingleJcrNode sjn
= (SingleJcrNode
) obj
;
64 TreeParent tp
= (TreeParent
) sjn
.getParent();
65 Node node
= sjn
.getNode();
69 node
.getSession().save();
73 // Check if the parent is the root node
74 if (tp
instanceof WorkspaceNode
)
75 rootAncestor
= (WorkspaceNode
) tp
;
77 ancestor
= getOlder(ancestor
, (SingleJcrNode
) tp
);
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
);
91 private SingleJcrNode
getOlder(SingleJcrNode A
, SingleJcrNode B
) {
94 return B
== null ?
null : B
;
95 // Todo enhanced this method
97 return A
.getNode().getDepth() <= B
.getNode().getDepth() ? A
: B
;
98 } catch (RepositoryException re
) {
99 throw new ArgeoException("Cannot find ancestor", re
);