]> 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
Analyse issue with reasult display
[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.ResultParent;
30 import org.argeo.slc.client.ui.model.ResultParentUtils;
31 import org.argeo.slc.client.ui.model.SingleResultNode;
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 // confirmation
54 StringBuffer buf = new StringBuffer("");
55 Iterator<?> lst = ((IStructuredSelection) selection).iterator();
56 while (lst.hasNext()) {
57 Object obj = lst.next();
58 if (obj instanceof ResultParent) {
59 ResultParent rp = ((ResultParent) obj);
60 buf.append(rp.getName()).append(", ");
61 }
62
63 }
64
65 String msg = "Nothing to delete";
66 // remove last separator
67 if (buf.lastIndexOf(", ") > -1) {
68 msg = "Do you want to delete following objects: "
69 + buf.substring(0, buf.lastIndexOf(", ")) + "?";
70 }
71 Boolean ok = MessageDialog.openConfirm(
72 HandlerUtil.getActiveShell(event), "Confirm deletion", msg);
73
74 if (!ok)
75 return null;
76
77 Job job = new Job("Delete results") {
78 @Override
79 protected IStatus run(IProgressMonitor monitor) {
80 if (selection != null
81 && selection instanceof IStructuredSelection) {
82 List<String> nodes = new ArrayList<String>();
83 Iterator<?> it = ((IStructuredSelection) selection)
84 .iterator();
85 Object obj = null;
86 try {
87 while (it.hasNext()) {
88 obj = it.next();
89 if (obj instanceof ResultFolder) {
90 Node node = ((ResultFolder) obj).getNode();
91 nodes.add(node.getPath());
92 } else if (obj instanceof SingleResultNode) {
93 Node node = ((SingleResultNode) obj).getNode();
94 nodes.add(node.getPath());
95 }
96 }
97 } catch (RepositoryException e) {
98 ErrorFeedback.show("Cannot list nodes", e);
99 return null;
100 }
101 monitor.beginTask("Delete results", nodes.size());
102 Node node = null;
103 try {
104 for (final String path : nodes) {
105 if (session.itemExists(path)) {
106 node = session.getNode(path);
107 Node parent = node.getParent();
108 node.remove();
109 ResultParentUtils.updateStatusOnRemoval(parent);
110 }
111 monitor.worked(1);
112 }
113 session.save();
114 } catch (RepositoryException e) {
115 ErrorFeedback.show("Cannot delete node " + node, e);
116 }
117 monitor.done();
118 }
119 return Status.OK_STATUS;
120 }
121
122 };
123 job.setUser(true);
124 job.schedule();
125 return null;
126 }
127
128 /* DEPENDENCY INJECTION */
129 public void setSession(Session session) {
130 this.session = session;
131 }
132 }