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
.cms
.e4
.jcr
.handlers
;
18 import java
.util
.List
;
20 import javax
.inject
.Named
;
21 import javax
.jcr
.Node
;
22 import javax
.jcr
.RepositoryException
;
24 import org
.argeo
.cms
.e4
.jcr
.JcrBrowserView
;
25 import org
.argeo
.cms
.ui
.jcr
.model
.SingleJcrNodeElem
;
26 import org
.argeo
.cms
.ui
.jcr
.model
.WorkspaceElem
;
27 import org
.argeo
.eclipse
.ui
.EclipseUiException
;
28 import org
.argeo
.eclipse
.ui
.TreeParent
;
29 import org
.argeo
.eclipse
.ui
.dialogs
.ErrorFeedback
;
30 import org
.eclipse
.e4
.core
.di
.annotations
.Execute
;
31 import org
.eclipse
.e4
.ui
.model
.application
.ui
.basic
.MPart
;
32 import org
.eclipse
.e4
.ui
.services
.IServiceConstants
;
33 import org
.eclipse
.e4
.ui
.workbench
.modeling
.ESelectionService
;
34 import org
.eclipse
.jface
.dialogs
.MessageDialog
;
35 import org
.eclipse
.swt
.widgets
.Display
;
38 * Delete 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
{
48 public void execute(@Named(IServiceConstants
.ACTIVE_PART
) MPart part
, ESelectionService selectionService
) {
49 List
<?
> selection
= (List
<?
>) selectionService
.getSelection();
50 if (selection
== null)
53 JcrBrowserView view
= (JcrBrowserView
) part
.getObject();
56 StringBuffer buf
= new StringBuffer("");
57 for (Object o
: selection
) {
58 SingleJcrNodeElem sjn
= (SingleJcrNodeElem
) o
;
59 buf
.append(sjn
.getName()).append(' ');
61 Boolean doRemove
= MessageDialog
.openConfirm(Display
.getCurrent().getActiveShell(), "Confirm deletion",
62 "Do you want to delete " + buf
+ "?");
66 SingleJcrNodeElem ancestor
= null;
67 WorkspaceElem rootAncestor
= null;
69 for (Object obj
: selection
) {
70 if (obj
instanceof SingleJcrNodeElem
) {
72 SingleJcrNodeElem sjn
= (SingleJcrNodeElem
) obj
;
73 TreeParent tp
= (TreeParent
) sjn
.getParent();
74 Node node
= sjn
.getNode();
78 node
.getSession().save();
82 // Check if the parent is the root node
83 if (tp
instanceof WorkspaceElem
)
84 rootAncestor
= (WorkspaceElem
) tp
;
86 ancestor
= getOlder(ancestor
, (SingleJcrNodeElem
) tp
);
89 if (rootAncestor
!= null)
90 view
.nodeRemoved(rootAncestor
);
91 else if (ancestor
!= null)
92 view
.nodeRemoved(ancestor
);
93 } catch (Exception e
) {
94 ErrorFeedback
.show("Cannot delete selected node ", e
);
99 private SingleJcrNodeElem
getOlder(SingleJcrNodeElem A
, SingleJcrNodeElem B
) {
102 return B
== null ?
null : B
;
103 // Todo enhanced this method
105 return A
.getNode().getDepth() <= B
.getNode().getDepth() ? A
: B
;
106 } catch (RepositoryException re
) {
107 throw new EclipseUiException("Cannot find ancestor", re
);