2 * Copyright (C) 2007-2012 Argeo GmbH
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org
.argeo
.slc
.client
.ui
.commands
;
18 import java
.util
.HashMap
;
19 import java
.util
.Iterator
;
22 import javax
.jcr
.Node
;
23 import javax
.jcr
.RepositoryException
;
24 import javax
.jcr
.Session
;
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
;
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)";
52 public Object
execute(final ExecutionEvent event
) throws ExecutionException
{
53 final ISelection selection
= HandlerUtil
54 .getActiveWorkbenchWindow(event
).getActivePage().getSelection();
57 StringBuilder buf
= new StringBuilder("");
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(", ");
67 String msg
= "Nothing to delete";
68 // remove last separator
69 if (buf
.lastIndexOf(", ") > -1) {
70 msg
= "Do you want to delete following objects (and their children): "
71 + buf
.substring(0, buf
.lastIndexOf(", ")) + "?";
73 Boolean ok
= MessageDialog
.openConfirm(
74 HandlerUtil
.getActiveShell(event
), "Confirm deletion", msg
);
79 Job job
= new Job("Delete results") {
81 protected IStatus
run(IProgressMonitor monitor
) {
83 && selection
instanceof IStructuredSelection
) {
84 Map
<String
, Node
> nodes
= new HashMap
<String
, Node
>();
85 Iterator
<?
> it
= ((IStructuredSelection
) selection
)
90 while (it
.hasNext()) {
92 if (obj
instanceof ResultFolder
) {
93 Node node
= ((ResultFolder
) obj
).getNode();
94 nodes
.put(node
.getPath(), node
);
95 } else if (obj
instanceof SingleResultNode
) {
96 Node node
= ((SingleResultNode
) obj
).getNode();
97 nodes
.put(node
.getPath(), node
);
100 if (!nodes
.isEmpty()) {
101 Session session
= null;
102 monitor
.beginTask("Delete results", nodes
.size());
103 for (String path
: nodes
.keySet()) {
105 session
= nodes
.get(path
).getSession();
107 // check if the item has not already been
108 // deleted while deleting one of its ancestor
109 if (session
.itemExists(path
)) {
110 Node parent
= nodes
.get(path
).getParent();
111 nodes
.get(path
).remove();
112 ResultParentUtils
.updatePassedStatus(
120 } catch (RepositoryException e
) {
121 throw new SlcException(
122 "Unexpected error while deleteting node(s)", e
);
126 return Status
.OK_STATUS
;