-/*\r
- * Copyright (C) 2007-2012 Mathieu Baudier\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
-\r
-package org.argeo.slc.client.ui.commands;\r
-\r
-import java.util.ArrayList;\r
-import java.util.Iterator;\r
-import java.util.List;\r
-\r
-import javax.jcr.Node;\r
-import javax.jcr.RepositoryException;\r
-import javax.jcr.Session;\r
-\r
-import org.argeo.eclipse.ui.ErrorFeedback;\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.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 DeleteResult extends AbstractHandler {\r
- /* DEPENDENCY INJECTION */\r
- private Session session;\r
-\r
- public Object execute(final ExecutionEvent event) throws ExecutionException {\r
- final ISelection selection = HandlerUtil\r
- .getActiveWorkbenchWindow(event).getActivePage().getSelection();\r
-\r
- // confirmation\r
- StringBuffer buf = new StringBuffer("");\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
-\r
- String msg = "Nothing to delete";\r
- // remove last separator\r
- if (buf.lastIndexOf(", ") > -1) {\r
- msg = "Do you want to delete following objects: "\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
- List<String> nodes = new ArrayList<String>();\r
- Iterator<?> it = ((IStructuredSelection) selection)\r
- .iterator();\r
- Object obj = null;\r
- try {\r
- while (it.hasNext()) {\r
- obj = it.next();\r
- if (obj instanceof ResultFolder) {\r
- Node node = ((ResultFolder) obj).getNode();\r
- nodes.add(node.getPath());\r
- } else if (obj instanceof SingleResultNode) {\r
- Node node = ((SingleResultNode) obj).getNode();\r
- nodes.add(node.getPath());\r
- }\r
- }\r
- } catch (RepositoryException e) {\r
- ErrorFeedback.show("Cannot list nodes", e);\r
- return null;\r
- }\r
- monitor.beginTask("Delete results", nodes.size());\r
- Node node = null;\r
- try {\r
- for (final String path : nodes) {\r
- if (session.itemExists(path)) {\r
- node = session.getNode(path);\r
- Node parent = node.getParent();\r
- node.remove();\r
- ResultParentUtils.updateStatusOnRemoval(parent);\r
- }\r
- monitor.worked(1);\r
- }\r
- session.save();\r
- } catch (RepositoryException e) {\r
- ErrorFeedback.show("Cannot delete node " + node, 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
-\r
- /* DEPENDENCY INJECTION */\r
- public void setSession(Session session) {\r
- this.session = session;\r
- }\r
-}\r