tmf: Add clear command on the Traces folder
[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.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;
41
42 /**
43 * Handler for the Delete Folder command.
44 */
45 public class DeleteFolderHandler extends AbstractHandler {
46
47 // ------------------------------------------------------------------------
48 // Execution
49 // ------------------------------------------------------------------------
50
51 @Override
52 public Object execute(ExecutionEvent event) throws ExecutionException {
53
54 // Check if we are closing down
55 IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
56 if (window == null) {
57 return null;
58 }
59
60 // Get the selection
61 ISelection selection = HandlerUtil.getCurrentSelection(event);
62 if (!(selection instanceof IStructuredSelection)) {
63 return null;
64 }
65 final boolean isTracesFolder = ((IStructuredSelection) selection).iterator().next() instanceof TmfTracesFolder;
66
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) {
73 return null;
74 }
75
76 final Iterator<Object> iterator = ((IStructuredSelection) selection).iterator();
77
78 WorkspaceModifyOperation operation = new WorkspaceModifyOperation() {
79 @Override
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();
85 }
86 Object element = iterator.next();
87 if (element instanceof TmfTraceFolder) {
88 final TmfTraceFolder folder = (TmfTraceFolder) element;
89 final IResource resource = folder.getResource();
90
91 try {
92 // delete all traces under this folder
93 for (TmfTraceElement traceElement : folder.getTraces()) {
94 traceElement.delete(null);
95 }
96
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() {
102 @Override
103 public boolean visit(IResource visitedResource) throws CoreException {
104 if (visitedResource != resource) {
105 visitedResource.delete(true, monitor);
106 }
107 return true;
108 }
109 }, IResource.DEPTH_ONE, 0);
110 } else {
111 resource.delete(true, monitor);
112 }
113 } catch (final CoreException e) {
114 Display.getDefault().asyncExec(new Runnable() {
115 @Override
116 public void run() {
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());
120 mb.open();
121 }
122 });
123 Activator.getDefault().logError("Error deleting folder: " + folder.getName(), e); //$NON-NLS-1$
124 }
125 }
126 }
127 }
128 };
129
130 try {
131 PlatformUI.getWorkbench().getProgressService().busyCursorWhile(operation);
132 } catch (InterruptedException e) {
133 return null;
134 } catch (InvocationTargetException e) {
135 MessageDialog.openError(window.getShell(), e.toString(), e.getTargetException().toString());
136 return null;
137 }
138
139 return null;
140 }
141
142 }
This page took 0.041953 seconds and 6 git commands to generate.