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
.resources
.IResourceVisitor
;
23 import org
.eclipse
.core
.runtime
.CoreException
;
24 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
25 import org
.eclipse
.core
.runtime
.OperationCanceledException
;
26 import org
.eclipse
.jface
.dialogs
.MessageDialog
;
27 import org
.eclipse
.jface
.viewers
.ISelection
;
28 import org
.eclipse
.jface
.viewers
.IStructuredSelection
;
29 import org
.eclipse
.linuxtools
.internal
.tmf
.ui
.Activator
;
30 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfTraceElement
;
31 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfTraceFolder
;
32 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfTracesFolder
;
33 import org
.eclipse
.swt
.SWT
;
34 import org
.eclipse
.swt
.widgets
.Display
;
35 import org
.eclipse
.swt
.widgets
.MessageBox
;
36 import org
.eclipse
.swt
.widgets
.Shell
;
37 import org
.eclipse
.ui
.IWorkbenchWindow
;
38 import org
.eclipse
.ui
.PlatformUI
;
39 import org
.eclipse
.ui
.actions
.WorkspaceModifyOperation
;
40 import org
.eclipse
.ui
.handlers
.HandlerUtil
;
43 * Handler for the Delete Folder command.
45 public class DeleteFolderHandler
extends AbstractHandler
{
47 // ------------------------------------------------------------------------
49 // ------------------------------------------------------------------------
52 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
54 // Check if we are closing down
55 IWorkbenchWindow window
= PlatformUI
.getWorkbench().getActiveWorkbenchWindow();
61 ISelection selection
= HandlerUtil
.getCurrentSelection(event
);
62 if (!(selection
instanceof IStructuredSelection
)) {
65 final boolean isTracesFolder
= ((IStructuredSelection
) selection
).iterator().next() instanceof TmfTracesFolder
;
67 // Confirm the operation
68 Shell shell
= window
.getShell();
69 MessageBox confirmOperation
= new MessageBox(shell
, SWT
.ICON_QUESTION
| SWT
.CANCEL
| SWT
.OK
);
70 confirmOperation
.setText(isTracesFolder ? Messages
.ClearDialog_Title
: Messages
.DeleteDialog_Title
);
71 confirmOperation
.setMessage(isTracesFolder ? Messages
.DeleteFolderHandlerClear_Message
: Messages
.DeleteFolderHandler_Message
);
72 if (confirmOperation
.open() != SWT
.OK
) {
76 final Iterator
<Object
> iterator
= ((IStructuredSelection
) selection
).iterator();
78 WorkspaceModifyOperation operation
= new WorkspaceModifyOperation() {
80 public void execute(final IProgressMonitor monitor
) throws CoreException
{
81 monitor
.beginTask("", 1000); //$NON-NLS-1$
82 while (iterator
.hasNext()) {
83 if (monitor
.isCanceled()) {
84 throw new OperationCanceledException();
86 Object element
= iterator
.next();
87 if (element
instanceof TmfTraceFolder
) {
88 final TmfTraceFolder folder
= (TmfTraceFolder
) element
;
89 final IResource resource
= folder
.getResource();
92 // delete all traces under this folder
93 for (TmfTraceElement traceElement
: folder
.getTraces()) {
94 traceElement
.delete(null);
97 // Finally, delete the folder. For the Traces
98 // folder, we only delete the children since the
99 // folder should always be there.
100 if (folder
instanceof TmfTracesFolder
) {
101 resource
.accept(new IResourceVisitor() {
103 public boolean visit(IResource visitedResource
) throws CoreException
{
104 if (visitedResource
!= resource
) {
105 visitedResource
.delete(true, monitor
);
109 }, IResource
.DEPTH_ONE
, 0);
111 resource
.delete(true, monitor
);
113 } catch (final CoreException e
) {
114 Display
.getDefault().asyncExec(new Runnable() {
117 final MessageBox mb
= new MessageBox(PlatformUI
.getWorkbench().getActiveWorkbenchWindow().getShell());
118 mb
.setText(isTracesFolder ? Messages
.DeleteFolderHandlerClear_Error
: Messages
.DeleteFolderHandler_Error
+ ' ' + folder
.getName());
119 mb
.setMessage(e
.getMessage());
123 Activator
.getDefault().logError("Error deleting folder: " + folder
.getName(), e
); //$NON-NLS-1$
131 PlatformUI
.getWorkbench().getProgressService().busyCursorWhile(operation
);
132 } catch (InterruptedException e
) {
134 } catch (InvocationTargetException e
) {
135 MessageDialog
.openError(window
.getShell(), e
.toString(), e
.getTargetException().toString());