--- /dev/null
+/*\r
+ * Copyright (C) 2007-2012 Argeo GmbH\r
+ *\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ */\r
+package org.argeo.slc.client.ui.commands;\r
+\r
+import java.util.HashMap;\r
+import java.util.Iterator;\r
+import java.util.Map;\r
+\r
+import javax.jcr.Node;\r
+import javax.jcr.RepositoryException;\r
+import javax.jcr.Session;\r
+\r
+import org.argeo.slc.SlcException;\r
+import org.argeo.slc.client.ui.ClientUiPlugin;\r
+import org.argeo.slc.client.ui.model.ResultFolder;\r
+import org.argeo.slc.client.ui.model.ResultParent;\r
+import org.argeo.slc.client.ui.model.ResultParentUtils;\r
+import org.argeo.slc.client.ui.model.SingleResultNode;\r
+import org.eclipse.core.commands.AbstractHandler;\r
+import org.eclipse.core.commands.ExecutionEvent;\r
+import org.eclipse.core.commands.ExecutionException;\r
+import org.eclipse.core.runtime.IProgressMonitor;\r
+import org.eclipse.core.runtime.IStatus;\r
+import org.eclipse.core.runtime.Status;\r
+import org.eclipse.core.runtime.jobs.Job;\r
+import org.eclipse.jface.dialogs.MessageDialog;\r
+import org.eclipse.jface.resource.ImageDescriptor;\r
+import org.eclipse.jface.viewers.ISelection;\r
+import org.eclipse.jface.viewers.IStructuredSelection;\r
+import org.eclipse.ui.handlers.HandlerUtil;\r
+\r
+/** Deletes one or many results */\r
+public class DeleteItems extends AbstractHandler {\r
+ public final static String ID = ClientUiPlugin.ID + ".deleteItems";\r
+ public final static ImageDescriptor DEFAULT_IMG_DESCRIPTOR = ClientUiPlugin\r
+ .getImageDescriptor("icons/removeAll.png");\r
+ public final static String DEFAULT_LABEL = "Delete selected item(s)";\r
+\r
+ public Object execute(final ExecutionEvent event) throws ExecutionException {\r
+ final ISelection selection = HandlerUtil\r
+ .getActiveWorkbenchWindow(event).getActivePage().getSelection();\r
+\r
+ // confirmation\r
+ StringBuilder buf = new StringBuilder("");\r
+ Iterator<?> lst = ((IStructuredSelection) selection).iterator();\r
+ while (lst.hasNext()) {\r
+ Object obj = lst.next();\r
+ if (obj instanceof ResultParent) {\r
+ ResultParent rp = ((ResultParent) obj);\r
+ buf.append(rp.getName()).append(", ");\r
+ }\r
+ }\r
+\r
+ String msg = "Nothing to delete";\r
+ // remove last separator\r
+ if (buf.lastIndexOf(", ") > -1) {\r
+ msg = "Do you want to delete following objects (and their children): "\r
+ + buf.substring(0, buf.lastIndexOf(", ")) + "?";\r
+ }\r
+ Boolean ok = MessageDialog.openConfirm(\r
+ HandlerUtil.getActiveShell(event), "Confirm deletion", msg);\r
+\r
+ if (!ok)\r
+ return null;\r
+\r
+ Job job = new Job("Delete results") {\r
+ @Override\r
+ protected IStatus run(IProgressMonitor monitor) {\r
+ if (selection != null\r
+ && selection instanceof IStructuredSelection) {\r
+ Map<String, Node> nodes = new HashMap<String, Node>();\r
+ Iterator<?> it = ((IStructuredSelection) selection)\r
+ .iterator();\r
+ Object obj = null;\r
+ try {\r
+\r
+ while (it.hasNext()) {\r
+ obj = it.next();\r
+ if (obj instanceof ResultFolder) {\r
+ Node node = ((ResultFolder) obj).getNode();\r
+ nodes.put(node.getPath(), node);\r
+ } else if (obj instanceof SingleResultNode) {\r
+ Node node = ((SingleResultNode) obj).getNode();\r
+ nodes.put(node.getPath(), node);\r
+ }\r
+ }\r
+ if (!nodes.isEmpty()) {\r
+ Session session = null;\r
+ monitor.beginTask("Delete results", nodes.size());\r
+ for (String path : nodes.keySet()) {\r
+ if (session == null)\r
+ session = nodes.get(path).getSession();\r
+\r
+ // check if the item has not already been\r
+ // deleted while deleting one of its ancestor\r
+ if (session.itemExists(path)) {\r
+ Node parent = nodes.get(path).getParent();\r
+ nodes.get(path).remove();\r
+ ResultParentUtils.updatePassedStatus(\r
+ parent, true);\r
+ }\r
+ monitor.worked(1);\r
+ }\r
+ session.save();\r
+ }\r
+\r
+ } catch (RepositoryException e) {\r
+ throw new SlcException(\r
+ "Unexpected error while deleteting node(s)", e);\r
+ }\r
+ monitor.done();\r
+ }\r
+ return Status.OK_STATUS;\r
+ }\r
+\r
+ };\r
+ job.setUser(true);\r
+ job.schedule();\r
+ return null;\r
+ }\r
+}
\ No newline at end of file