9dda211b23588e2a873876d5dd8e641b0d1ab135
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / internal / tmf / ui / project / handlers / DeleteFolderHandler.java
1 /*******************************************************************************
2 * Copyright (c) 2014 Ericsson
3 *
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
8 *
9 * Contributors:
10 * Patrick Tasse - Initial API and implementation
11 *******************************************************************************/
12
13 package org.eclipse.linuxtools.internal.tmf.ui.project.handlers;
14
15 import java.lang.reflect.InvocationTargetException;
16 import java.util.Iterator;
17
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;
39
40 /**
41 * Handler for the Delete Folder command.
42 */
43 public class DeleteFolderHandler extends AbstractHandler {
44
45 // ------------------------------------------------------------------------
46 // Execution
47 // ------------------------------------------------------------------------
48
49 @Override
50 public Object execute(ExecutionEvent event) throws ExecutionException {
51
52 // Check if we are closing down
53 IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
54 if (window == null) {
55 return null;
56 }
57
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) {
64 return null;
65 }
66
67 // Get the selection
68 ISelection selection = HandlerUtil.getCurrentSelection(event);
69 if (!(selection instanceof IStructuredSelection)) {
70 return null;
71 }
72 final Iterator<Object> iterator = ((IStructuredSelection) selection).iterator();
73
74 WorkspaceModifyOperation operation = new WorkspaceModifyOperation() {
75 @Override
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();
81 }
82 Object element = iterator.next();
83 if (element instanceof TmfTraceFolder) {
84 final TmfTraceFolder folder = (TmfTraceFolder) element;
85 IResource resource = folder.getResource();
86
87 try {
88 // delete all traces under this folder
89 for (TmfTraceElement traceElement : folder.getTraces()) {
90 traceElement.delete(null);
91 }
92
93 // Finally, delete the folder
94 resource.delete(true, monitor);
95 } catch (final CoreException e) {
96 Display.getDefault().asyncExec(new Runnable() {
97 @Override
98 public void run() {
99 final MessageBox mb = new MessageBox(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell());
100 mb.setText(Messages.DeleteFolderHandler_Error + ' ' + folder.getName());
101 mb.setMessage(e.getMessage());
102 mb.open();
103 }
104 });
105 Activator.getDefault().logError("Error deleting folder: " + folder.getName(), e); //$NON-NLS-1$
106 }
107 }
108 }
109 }
110 };
111
112 try {
113 PlatformUI.getWorkbench().getProgressService().busyCursorWhile(operation);
114 } catch (InterruptedException e) {
115 return null;
116 } catch (InvocationTargetException e) {
117 MessageDialog.openError(window.getShell(), e.toString(), e.getTargetException().toString());
118 return null;
119 }
120
121 return null;
122 }
123
124 }
This page took 0.033882 seconds and 4 git commands to generate.