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.e4.jcr.handlers;
17  
18  import java.util.List;
19  
20  import javax.inject.Named;
21  import javax.jcr.Node;
22  import javax.jcr.RepositoryException;
23  
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;
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 {
47  	@Execute
48  	public void execute(@Named(IServiceConstants.ACTIVE_PART) MPart part, ESelectionService selectionService) {
49  		List<?> selection = (List<?>) selectionService.getSelection();
50  		if (selection == null)
51  			return;
52  
53  		JcrBrowserView view = (JcrBrowserView) part.getObject();
54  
55  		// confirmation
56  		StringBuffer buf = new StringBuffer("");
57  		for (Object o : selection) {
58  			SingleJcrNodeElem sjn = (SingleJcrNodeElem) o;
59  			buf.append(sjn.getName()).append(' ');
60  		}
61  		Boolean doRemove = MessageDialog.openConfirm(Display.getCurrent().getActiveShell(), "Confirm deletion",
62  				"Do you want to delete " + buf + "?");
63  
64  		// operation
65  		if (doRemove) {
66  			SingleJcrNodeElem ancestor = null;
67  			WorkspaceElem rootAncestor = null;
68  			try {
69  				for (Object obj : selection) {
70  					if (obj instanceof SingleJcrNodeElem) {
71  						// Cache objects
72  						SingleJcrNodeElem sjn = (SingleJcrNodeElem) obj;
73  						TreeParent tp = (TreeParent) sjn.getParent();
74  						Node node = sjn.getNode();
75  
76  						// Jcr Remove
77  						node.remove();
78  						node.getSession().save();
79  						// UI remove
80  						tp.removeChild(sjn);
81  
82  						// Check if the parent is the root node
83  						if (tp instanceof WorkspaceElem)
84  							rootAncestor = (WorkspaceElem) tp;
85  						else
86  							ancestor = getOlder(ancestor, (SingleJcrNodeElem) tp);
87  					}
88  				}
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);
95  			}
96  		}
97  	}
98  
99  	private SingleJcrNodeElem getOlder(SingleJcrNodeElem A, SingleJcrNodeElem B) {
100 		try {
101 			if (A == null)
102 				return B == null ? null : B;
103 			// Todo enhanced this method
104 			else
105 				return A.getNode().getDepth() <= B.getNode().getDepth() ? A : B;
106 		} catch (RepositoryException re) {
107 			throw new EclipseUiException("Cannot find ancestor", re);
108 		}
109 	}
110 }