View Javadoc
1   /*
2    * Copyright (C) 2007-2012 Argeo GmbH
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.cms.ui.workbench.internal.jcr.commands;
17  
18  import java.util.Iterator;
19  
20  import javax.jcr.Node;
21  import javax.jcr.RepositoryException;
22  
23  import org.argeo.cms.ui.jcr.model.SingleJcrNodeElem;
24  import org.argeo.cms.ui.jcr.model.WorkspaceElem;
25  import org.argeo.cms.ui.workbench.jcr.JcrBrowserView;
26  import org.argeo.eclipse.ui.EclipseUiException;
27  import org.argeo.eclipse.ui.TreeParent;
28  import org.argeo.eclipse.ui.dialogs.ErrorFeedback;
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;
36  
37  /**
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,
40   * handle with care.
41   * 
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
44   * versions.
45   */
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))
51  			return null;
52  
53  		JcrBrowserView view = (JcrBrowserView) HandlerUtil
54  				.getActiveWorkbenchWindow(event).getActivePage()
55  				.findView(HandlerUtil.getActivePartId(event));
56  
57  		// confirmation
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(' ');
63  		}
64  		Boolean doRemove = MessageDialog.openConfirm(
65  				HandlerUtil.getActiveShell(event), "Confirm deletion",
66  				"Do you want to delete " + buf + "?");
67  
68  		// operation
69  		if (doRemove) {
70  			Iterator<?> it = ((IStructuredSelection) selection).iterator();
71  			Object obj = null;
72  			SingleJcrNodeElem ancestor = null;
73  			WorkspaceElem rootAncestor = null;
74  			try {
75  				while (it.hasNext()) {
76  					obj = it.next();
77  					if (obj instanceof SingleJcrNodeElem) {
78  						// Cache objects
79  						SingleJcrNodeElem sjn = (SingleJcrNodeElem) obj;
80  						TreeParent tp = (TreeParent) sjn.getParent();
81  						Node node = sjn.getNode();
82  
83  						// Jcr Remove
84  						node.remove();
85  						node.getSession().save();
86  						// UI remove
87  						tp.removeChild(sjn);
88  
89  						// Check if the parent is the root node
90  						if (tp instanceof WorkspaceElem)
91  							rootAncestor = (WorkspaceElem) tp;
92  						else
93  							ancestor = getOlder(ancestor, (SingleJcrNodeElem) tp);
94  					}
95  				}
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);
102 			}
103 		}
104 		return null;
105 	}
106 
107 	private SingleJcrNodeElem getOlder(SingleJcrNodeElem A, SingleJcrNodeElem B) {
108 		try {
109 			if (A == null)
110 				return B == null ? null : B;
111 			// Todo enhanced this method
112 			else
113 				return A.getNode().getDepth() <= B.getNode().getDepth() ? A : B;
114 		} catch (RepositoryException re) {
115 			throw new EclipseUiException("Cannot find ancestor", re);
116 		}
117 	}
118 }