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
.ArrayList
;
19 import java
.util
.Iterator
;
20 import java
.util
.List
;
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 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(", ");
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(", ")) + "?";
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 List
<Node
> nodes
= new ArrayList
<Node
>();
85 Iterator
<?
> it
= ((IStructuredSelection
) selection
)
88 while (it
.hasNext()) {
90 if (obj
instanceof ResultFolder
) {
91 Node node
= ((ResultFolder
) obj
).getNode();
93 } else if (obj
instanceof SingleResultNode
) {
94 Node node
= ((SingleResultNode
) obj
).getNode();
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();
105 ResultParentUtils
.updateStatusOnRemoval(parent
);
111 } catch (RepositoryException e
) {
112 throw new SlcException(
113 "Unexpected error while deleteting node(s)", e
);
117 return Status
.OK_STATUS
;