]> git.argeo.org Git - gpl/argeo-slc.git/blob - legacy/org.argeo.slc.client.ui/src/org/argeo/slc/client/ui/commands/DeleteItems.java
Clarify overall project structure.
[gpl/argeo-slc.git] / legacy / org.argeo.slc.client.ui / src / org / argeo / slc / client / ui / commands / DeleteItems.java
1 package org.argeo.slc.client.ui.commands;
2
3 import java.util.HashMap;
4 import java.util.Iterator;
5 import java.util.Map;
6
7 import javax.jcr.Node;
8 import javax.jcr.RepositoryException;
9 import javax.jcr.Session;
10
11 import org.argeo.slc.SlcException;
12 import org.argeo.slc.client.ui.ClientUiPlugin;
13 import org.argeo.slc.client.ui.model.ResultFolder;
14 import org.argeo.slc.client.ui.model.ResultParent;
15 import org.argeo.slc.client.ui.model.ResultParentUtils;
16 import org.argeo.slc.client.ui.model.SingleResultNode;
17 import org.eclipse.core.commands.AbstractHandler;
18 import org.eclipse.core.commands.ExecutionEvent;
19 import org.eclipse.core.commands.ExecutionException;
20 import org.eclipse.core.runtime.IProgressMonitor;
21 import org.eclipse.core.runtime.IStatus;
22 import org.eclipse.core.runtime.Status;
23 import org.eclipse.core.runtime.jobs.Job;
24 import org.eclipse.jface.dialogs.MessageDialog;
25 import org.eclipse.jface.resource.ImageDescriptor;
26 import org.eclipse.jface.viewers.ISelection;
27 import org.eclipse.jface.viewers.IStructuredSelection;
28 import org.eclipse.ui.handlers.HandlerUtil;
29
30 /** Deletes one or many results */
31 public class DeleteItems extends AbstractHandler {
32 public final static String ID = ClientUiPlugin.ID + ".deleteItems";
33 public final static ImageDescriptor DEFAULT_IMG_DESCRIPTOR = ClientUiPlugin
34 .getImageDescriptor("icons/removeAll.png");
35 public final static String DEFAULT_LABEL = "Delete selected item(s)";
36
37 public Object execute(final ExecutionEvent event) throws ExecutionException {
38 final ISelection selection = HandlerUtil
39 .getActiveWorkbenchWindow(event).getActivePage().getSelection();
40
41 // confirmation
42 StringBuilder buf = new StringBuilder("");
43 Iterator<?> lst = ((IStructuredSelection) selection).iterator();
44 while (lst.hasNext()) {
45 Object obj = lst.next();
46 if (obj instanceof ResultParent) {
47 ResultParent rp = ((ResultParent) obj);
48 buf.append(rp.getName()).append(", ");
49 }
50 }
51
52 String msg = "Nothing to delete";
53 // remove last separator
54 if (buf.lastIndexOf(", ") > -1) {
55 msg = "Do you want to delete following objects (and their children): "
56 + buf.substring(0, buf.lastIndexOf(", ")) + "?";
57 }
58 Boolean ok = MessageDialog.openConfirm(
59 HandlerUtil.getActiveShell(event), "Confirm deletion", msg);
60
61 if (!ok)
62 return null;
63
64 Job job = new Job("Delete results") {
65 @Override
66 protected IStatus run(IProgressMonitor monitor) {
67 if (selection != null
68 && selection instanceof IStructuredSelection) {
69 Map<String, Node> nodes = new HashMap<String, Node>();
70 Iterator<?> it = ((IStructuredSelection) selection)
71 .iterator();
72 Object obj = null;
73 try {
74
75 while (it.hasNext()) {
76 obj = it.next();
77 if (obj instanceof ResultFolder) {
78 Node node = ((ResultFolder) obj).getNode();
79 nodes.put(node.getPath(), node);
80 } else if (obj instanceof SingleResultNode) {
81 Node node = ((SingleResultNode) obj).getNode();
82 nodes.put(node.getPath(), node);
83 }
84 }
85 if (!nodes.isEmpty()) {
86 Session session = null;
87 monitor.beginTask("Delete results", nodes.size());
88 for (String path : nodes.keySet()) {
89 if (session == null)
90 session = nodes.get(path).getSession();
91
92 // check if the item has not already been
93 // deleted while deleting one of its ancestor
94 if (session.itemExists(path)) {
95 Node parent = nodes.get(path).getParent();
96 nodes.get(path).remove();
97 ResultParentUtils.updatePassedStatus(
98 parent, true);
99 }
100 monitor.worked(1);
101 }
102 session.save();
103 }
104
105 } catch (RepositoryException e) {
106 throw new SlcException(
107 "Unexpected error while deleteting node(s)", e);
108 }
109 monitor.done();
110 }
111 return Status.OK_STATUS;
112 }
113
114 };
115 job.setUser(true);
116 job.schedule();
117 return null;
118 }
119 }