]> git.argeo.org Git - gpl/argeo-slc.git/blob - plugins/org.argeo.slc.client.ui/src/main/java/org/argeo/slc/client/ui/commands/DeleteItems.java
prevent adding a subfolder when user typed name contains a slash
[gpl/argeo-slc.git] / plugins / org.argeo.slc.client.ui / src / main / java / org / argeo / slc / client / ui / commands / DeleteItems.java
1 /*
2 * Copyright (C) 2007-2012 Argeo GmbH
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 package org.argeo.slc.client.ui.commands;
17
18 import java.util.ArrayList;
19 import java.util.Iterator;
20 import java.util.List;
21
22 import javax.jcr.Node;
23 import javax.jcr.RepositoryException;
24 import javax.jcr.Session;
25
26 import org.argeo.slc.SlcException;
27 import org.argeo.slc.client.ui.ClientUiPlugin;
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.resource.ImageDescriptor;
41 import org.eclipse.jface.viewers.ISelection;
42 import org.eclipse.jface.viewers.IStructuredSelection;
43 import org.eclipse.ui.handlers.HandlerUtil;
44
45 /** Deletes one or many results */
46 public class DeleteItems extends AbstractHandler {
47 public final static String ID = ClientUiPlugin.ID + ".deleteItems";
48 public final static ImageDescriptor DEFAULT_IMG_DESCRIPTOR = ClientUiPlugin
49 .getImageDescriptor("icons/removeAll.png");
50 public final static String DEFAULT_LABEL = "Delete selected item(s)";
51
52 public Object execute(final ExecutionEvent event) throws ExecutionException {
53 final ISelection selection = HandlerUtil
54 .getActiveWorkbenchWindow(event).getActivePage().getSelection();
55
56 // confirmation
57 StringBuffer buf = new StringBuffer("");
58 Iterator<?> lst = ((IStructuredSelection) selection).iterator();
59 while (lst.hasNext()) {
60 Object obj = lst.next();
61 if (obj instanceof ResultParent) {
62 ResultParent rp = ((ResultParent) obj);
63 buf.append(rp.getName()).append(", ");
64 }
65 }
66
67 String msg = "Nothing to delete";
68 // remove last separator
69 if (buf.lastIndexOf(", ") > -1) {
70 msg = "Do you want to delete following objects: "
71 + buf.substring(0, buf.lastIndexOf(", ")) + "?";
72 }
73 Boolean ok = MessageDialog.openConfirm(
74 HandlerUtil.getActiveShell(event), "Confirm deletion", msg);
75
76 if (!ok)
77 return null;
78
79 Job job = new Job("Delete results") {
80 @Override
81 protected IStatus run(IProgressMonitor monitor) {
82 if (selection != null
83 && selection instanceof IStructuredSelection) {
84 List<Node> nodes = new ArrayList<Node>();
85 Iterator<?> it = ((IStructuredSelection) selection)
86 .iterator();
87 Object obj = null;
88 while (it.hasNext()) {
89 obj = it.next();
90 if (obj instanceof ResultFolder) {
91 Node node = ((ResultFolder) obj).getNode();
92 nodes.add(node);
93 } else if (obj instanceof SingleResultNode) {
94 Node node = ((SingleResultNode) obj).getNode();
95 nodes.add(node);
96 }
97 }
98 try {
99 if (!nodes.isEmpty()) {
100 Session session = nodes.get(0).getSession();
101 monitor.beginTask("Delete results", nodes.size());
102 for (Node node : nodes) {
103 Node parent = node.getParent();
104 node.remove();
105 ResultParentUtils.updatePassedStatus(parent,
106 true);
107 monitor.worked(1);
108 }
109 session.save();
110 }
111
112 } catch (RepositoryException e) {
113 throw new SlcException(
114 "Unexpected error while deleteting node(s)", e);
115 }
116 monitor.done();
117 }
118 return Status.OK_STATUS;
119 }
120
121 };
122 job.setUser(true);
123 job.schedule();
124 return null;
125 }
126 }