1 /*******************************************************************************
2 * Copyright (c) 2014 Ericsson
4 * All rights reserved. This program and the accompanying materials are
5 * made available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * Patrick Tasse - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.linuxtools
.internal
.tmf
.ui
.project
.handlers
;
15 import java
.lang
.reflect
.InvocationTargetException
;
16 import java
.util
.Iterator
;
18 import org
.eclipse
.core
.commands
.AbstractHandler
;
19 import org
.eclipse
.core
.commands
.ExecutionEvent
;
20 import org
.eclipse
.core
.commands
.ExecutionException
;
21 import org
.eclipse
.core
.resources
.IResource
;
22 import org
.eclipse
.core
.runtime
.CoreException
;
23 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
24 import org
.eclipse
.core
.runtime
.OperationCanceledException
;
25 import org
.eclipse
.jface
.dialogs
.MessageDialog
;
26 import org
.eclipse
.jface
.viewers
.ISelection
;
27 import org
.eclipse
.jface
.viewers
.IStructuredSelection
;
28 import org
.eclipse
.linuxtools
.internal
.tmf
.ui
.Activator
;
29 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfTraceElement
;
30 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfTraceFolder
;
31 import org
.eclipse
.swt
.SWT
;
32 import org
.eclipse
.swt
.widgets
.Display
;
33 import org
.eclipse
.swt
.widgets
.MessageBox
;
34 import org
.eclipse
.swt
.widgets
.Shell
;
35 import org
.eclipse
.ui
.IWorkbenchWindow
;
36 import org
.eclipse
.ui
.PlatformUI
;
37 import org
.eclipse
.ui
.actions
.WorkspaceModifyOperation
;
38 import org
.eclipse
.ui
.handlers
.HandlerUtil
;
41 * Handler for the Delete Folder command.
43 public class DeleteFolderHandler
extends AbstractHandler
{
45 // ------------------------------------------------------------------------
47 // ------------------------------------------------------------------------
50 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
52 // Check if we are closing down
53 IWorkbenchWindow window
= PlatformUI
.getWorkbench().getActiveWorkbenchWindow();
58 // Confirm the operation
59 Shell shell
= window
.getShell();
60 MessageBox confirmOperation
= new MessageBox(shell
, SWT
.ICON_QUESTION
| SWT
.CANCEL
| SWT
.OK
);
61 confirmOperation
.setText(Messages
.DeleteDialog_Title
);
62 confirmOperation
.setMessage(Messages
.DeleteFolderHandler_Message
);
63 if (confirmOperation
.open() != SWT
.OK
) {
68 ISelection selection
= HandlerUtil
.getCurrentSelection(event
);
69 if (!(selection
instanceof IStructuredSelection
)) {
72 final Iterator
<Object
> iterator
= ((IStructuredSelection
) selection
).iterator();
74 WorkspaceModifyOperation operation
= new WorkspaceModifyOperation() {
76 public void execute(IProgressMonitor monitor
) throws CoreException
{
77 monitor
.beginTask("", 1000); //$NON-NLS-1$
78 while (iterator
.hasNext()) {
79 if (monitor
.isCanceled()) {
80 throw new OperationCanceledException();
82 Object element
= iterator
.next();
83 if (element
instanceof TmfTraceFolder
) {
84 final TmfTraceFolder folder
= (TmfTraceFolder
) element
;
85 IResource resource
= folder
.getResource();
88 // delete all traces under this folder
89 for (TmfTraceElement traceElement
: folder
.getTraces()) {
90 traceElement
.delete(null);
93 // Finally, delete the folder
94 resource
.delete(true, monitor
);
95 } catch (final CoreException e
) {
96 Display
.getDefault().asyncExec(new Runnable() {
99 final MessageBox mb
= new MessageBox(PlatformUI
.getWorkbench().getActiveWorkbenchWindow().getShell());
100 mb
.setText(Messages
.DeleteFolderHandler_Error
+ ' ' + folder
.getName());
101 mb
.setMessage(e
.getMessage());
105 Activator
.getDefault().logError("Error deleting folder: " + folder
.getName(), e
); //$NON-NLS-1$
113 PlatformUI
.getWorkbench().getProgressService().busyCursorWhile(operation
);
114 } catch (InterruptedException e
) {
116 } catch (InvocationTargetException e
) {
117 MessageDialog
.openError(window
.getShell(), e
.toString(), e
.getTargetException().toString());