Commit | Line | Data |
---|---|---|
12c155f5 FC |
1 | /******************************************************************************* |
2 | * Copyright (c) 2009, 2010, 2011 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 | * Francois Chouinard - Initial API and implementation | |
11 | *******************************************************************************/ | |
12 | ||
13 | package org.eclipse.linuxtools.tmf.ui.project.handlers; | |
14 | ||
15 | import java.util.Iterator; | |
16 | ||
17 | import org.eclipse.core.commands.AbstractHandler; | |
18 | import org.eclipse.core.commands.ExecutionEvent; | |
19 | import org.eclipse.core.commands.ExecutionException; | |
20 | import org.eclipse.core.resources.IResource; | |
21 | import org.eclipse.core.runtime.CoreException; | |
22 | import org.eclipse.jface.viewers.ISelection; | |
23 | import org.eclipse.jface.viewers.TreeSelection; | |
24 | import org.eclipse.linuxtools.tmf.ui.project.model.TmfExperimentElement; | |
25 | import org.eclipse.swt.SWT; | |
26 | import org.eclipse.swt.widgets.MessageBox; | |
27 | import org.eclipse.swt.widgets.Shell; | |
28 | import org.eclipse.ui.IWorkbenchPage; | |
29 | import org.eclipse.ui.IWorkbenchPart; | |
30 | import org.eclipse.ui.IWorkbenchWindow; | |
31 | import org.eclipse.ui.PlatformUI; | |
32 | ||
33 | /** | |
34 | * <b><u>DeleteExperimentHandler</u></b> | |
35 | * <p> | |
36 | */ | |
37 | public class DeleteExperimentHandler extends AbstractHandler { | |
38 | ||
39 | // ------------------------------------------------------------------------ | |
40 | // Execution | |
41 | // ------------------------------------------------------------------------ | |
42 | ||
43 | @Override | |
44 | public Object execute(ExecutionEvent event) throws ExecutionException { | |
45 | ||
46 | // Check if we are closing down | |
47 | IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow(); | |
48 | if (window == null) | |
49 | return null; | |
50 | ||
51 | // Confirm the operation | |
52 | Shell shell = window.getShell(); | |
53 | MessageBox confirmOperation = new MessageBox(shell, SWT.ICON_QUESTION | SWT.CANCEL | SWT.OK); | |
54 | confirmOperation.setText(Messages.DeleteDialog_Title); | |
55 | confirmOperation.setMessage(Messages.DeleteExperimentHandler_Message); | |
56 | if (confirmOperation.open() != SWT.OK) | |
57 | return null; | |
58 | ||
59 | // Get the selection | |
60 | IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(); | |
61 | IWorkbenchPart part = page.getActivePart(); | |
62 | ISelection selection = part.getSite().getSelectionProvider().getSelection(); | |
63 | ||
64 | if (selection instanceof TreeSelection) { | |
65 | TreeSelection sel = (TreeSelection) selection; | |
66 | @SuppressWarnings("unchecked") | |
67 | Iterator<Object> iterator = sel.iterator(); | |
68 | while (iterator.hasNext()) { | |
69 | Object element = iterator.next(); | |
70 | if (element instanceof TmfExperimentElement) { | |
71 | TmfExperimentElement experiment = (TmfExperimentElement) element; | |
72 | IResource resource = experiment.getResource(); | |
73 | try { | |
74 | resource.delete(true, null); | |
75 | experiment.getProject().refresh(); | |
76 | } catch (CoreException e) { | |
77 | e.printStackTrace(); | |
78 | } | |
79 | } | |
80 | } | |
81 | } | |
82 | ||
83 | return null; | |
84 | } | |
85 | } |