]> git.argeo.org Git - gpl/argeo-slc.git/blob - plugins/org.argeo.slc.client.ui/src/main/java/org/argeo/slc/client/ui/commands/DeleteResult.java
Make execution editor more robust
[gpl/argeo-slc.git] / plugins / org.argeo.slc.client.ui / src / main / java / org / argeo / slc / client / ui / commands / DeleteResult.java
1 /*
2 * Copyright (C) 2007-2012 Mathieu Baudier
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
17 package org.argeo.slc.client.ui.commands;
18
19 import java.util.ArrayList;
20 import java.util.Iterator;
21 import java.util.List;
22
23 import javax.jcr.Node;
24 import javax.jcr.RepositoryException;
25 import javax.jcr.Session;
26
27 import org.argeo.eclipse.ui.ErrorFeedback;
28 import org.argeo.slc.client.ui.model.ResultFolder;
29 import org.argeo.slc.client.ui.model.ResultParentUtils;
30 import org.argeo.slc.client.ui.model.SingleResultNode;
31 import org.argeo.slc.jcr.SlcNames;
32 import org.eclipse.core.commands.AbstractHandler;
33 import org.eclipse.core.commands.ExecutionEvent;
34 import org.eclipse.core.commands.ExecutionException;
35 import org.eclipse.core.runtime.IProgressMonitor;
36 import org.eclipse.core.runtime.IStatus;
37 import org.eclipse.core.runtime.Status;
38 import org.eclipse.core.runtime.jobs.Job;
39 import org.eclipse.jface.dialogs.MessageDialog;
40 import org.eclipse.jface.viewers.ISelection;
41 import org.eclipse.jface.viewers.IStructuredSelection;
42 import org.eclipse.ui.handlers.HandlerUtil;
43
44 /** Deletes one or many results */
45 public class DeleteResult extends AbstractHandler {
46 /* DEPENDENCY INJECTION */
47 private Session session;
48
49 public Object execute(final ExecutionEvent event) throws ExecutionException {
50 final ISelection selection = HandlerUtil
51 .getActiveWorkbenchWindow(event).getActivePage().getSelection();
52
53 if (!MessageDialog.openConfirm(HandlerUtil.getActiveShell(event),
54 "Confirm",
55 "Are you sure that you want to delete these results?"))
56 return null;
57
58 Job job = new Job("Delete results") {
59 @Override
60 protected IStatus run(IProgressMonitor monitor) {
61 if (selection != null
62 && selection instanceof IStructuredSelection) {
63 List<String> nodes = new ArrayList<String>();
64 Iterator<?> it = ((IStructuredSelection) selection)
65 .iterator();
66 Object obj = null;
67 try {
68 while (it.hasNext()) {
69 obj = it.next();
70 if (obj instanceof ResultFolder) {
71 Node node = ((ResultFolder) obj).getNode();
72 nodes.add(node.getPath());
73 } else if (obj instanceof SingleResultNode) {
74 Node node = ((SingleResultNode) obj).getNode();
75 nodes.add(node.getPath());
76 }
77 }
78 } catch (RepositoryException e) {
79 ErrorFeedback.show("Cannot list nodes", e);
80 return null;
81 }
82 monitor.beginTask("Delete results", nodes.size());
83 Node node = null;
84 try {
85 for (final String path : nodes) {
86 if (session.itemExists(path)) {
87 node = session.getNode(path);
88 Node parent = node.getParent();
89 node.remove();
90 ResultParentUtils.updateStatusOnRemoval(parent);
91 }
92 monitor.worked(1);
93 }
94 session.save();
95 } catch (RepositoryException e) {
96 ErrorFeedback.show("Cannot delete node " + node, e);
97 }
98 monitor.done();
99 }
100 return Status.OK_STATUS;
101 }
102
103 };
104 job.setUser(true);
105 job.schedule();
106 return null;
107 }
108
109 /* DEPENDENCY INJECTION */
110 public void setSession(Session session) {
111 this.session = session;
112 }
113 }