| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2009, 2013 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.internal.tmf.ui.project.handlers; |
| 14 | |
| 15 | import java.util.Iterator; |
| 16 | import java.util.LinkedList; |
| 17 | import java.util.List; |
| 18 | |
| 19 | import org.eclipse.core.commands.AbstractHandler; |
| 20 | import org.eclipse.core.commands.ExecutionEvent; |
| 21 | import org.eclipse.core.commands.ExecutionException; |
| 22 | import org.eclipse.core.resources.IFile; |
| 23 | import org.eclipse.core.resources.IResource; |
| 24 | import org.eclipse.core.runtime.CoreException; |
| 25 | import org.eclipse.core.runtime.IPath; |
| 26 | import org.eclipse.core.runtime.NullProgressMonitor; |
| 27 | import org.eclipse.jface.viewers.ISelection; |
| 28 | import org.eclipse.jface.viewers.ISelectionProvider; |
| 29 | import org.eclipse.jface.viewers.TreeSelection; |
| 30 | import org.eclipse.linuxtools.internal.tmf.ui.Activator; |
| 31 | import org.eclipse.linuxtools.tmf.ui.project.model.ITmfProjectModelElement; |
| 32 | import org.eclipse.linuxtools.tmf.ui.project.model.TmfExperimentElement; |
| 33 | import org.eclipse.linuxtools.tmf.ui.project.model.TmfExperimentFolder; |
| 34 | import org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceElement; |
| 35 | import org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceFolder; |
| 36 | import org.eclipse.swt.SWT; |
| 37 | import org.eclipse.swt.widgets.Display; |
| 38 | import org.eclipse.swt.widgets.MessageBox; |
| 39 | import org.eclipse.swt.widgets.Shell; |
| 40 | import org.eclipse.ui.IEditorReference; |
| 41 | import org.eclipse.ui.IWorkbench; |
| 42 | import org.eclipse.ui.IWorkbenchPage; |
| 43 | import org.eclipse.ui.IWorkbenchPart; |
| 44 | import org.eclipse.ui.IWorkbenchWindow; |
| 45 | import org.eclipse.ui.PlatformUI; |
| 46 | import org.eclipse.ui.part.FileEditorInput; |
| 47 | |
| 48 | /** |
| 49 | * <b><u>DeleteTraceHandler</u></b> |
| 50 | * <p> |
| 51 | */ |
| 52 | public class DeleteTraceHandler extends AbstractHandler { |
| 53 | |
| 54 | private TreeSelection fSelection = null; |
| 55 | |
| 56 | // ------------------------------------------------------------------------ |
| 57 | // Validation |
| 58 | // ------------------------------------------------------------------------ |
| 59 | |
| 60 | @Override |
| 61 | public boolean isEnabled() { |
| 62 | |
| 63 | // Check if we are closing down |
| 64 | IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow(); |
| 65 | if (window == null) { |
| 66 | return false; |
| 67 | } |
| 68 | |
| 69 | // Get the selection |
| 70 | IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(); |
| 71 | IWorkbenchPart part = page.getActivePart(); |
| 72 | if (part == null) { |
| 73 | return false; |
| 74 | } |
| 75 | ISelectionProvider selectionProvider = part.getSite().getSelectionProvider(); |
| 76 | if (selectionProvider == null) { |
| 77 | return false; |
| 78 | } |
| 79 | ISelection selection = selectionProvider.getSelection(); |
| 80 | |
| 81 | // Make sure selection contains only traces |
| 82 | fSelection = null; |
| 83 | if (selection instanceof TreeSelection) { |
| 84 | fSelection = (TreeSelection) selection; |
| 85 | Iterator<Object> iterator = fSelection.iterator(); |
| 86 | while (iterator.hasNext()) { |
| 87 | Object element = iterator.next(); |
| 88 | if (!(element instanceof TmfTraceElement)) { |
| 89 | return false; |
| 90 | } |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | // If we get here, either nothing is selected or everything is a trace |
| 95 | return !selection.isEmpty(); |
| 96 | } |
| 97 | |
| 98 | // ------------------------------------------------------------------------ |
| 99 | // Execution |
| 100 | // ------------------------------------------------------------------------ |
| 101 | |
| 102 | @Override |
| 103 | public Object execute(ExecutionEvent event) throws ExecutionException { |
| 104 | |
| 105 | // Check if we are closing down |
| 106 | IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow(); |
| 107 | if (window == null) { |
| 108 | return null; |
| 109 | } |
| 110 | |
| 111 | // Confirm the operation |
| 112 | Shell shell = window.getShell(); |
| 113 | MessageBox confirmOperation = new MessageBox(shell, SWT.ICON_QUESTION | SWT.CANCEL | SWT.OK); |
| 114 | confirmOperation.setText(Messages.DeleteDialog_Title); |
| 115 | confirmOperation.setMessage(Messages.DeleteTraceHandler_Message); |
| 116 | if (confirmOperation.open() != SWT.OK) { |
| 117 | return null; |
| 118 | } |
| 119 | |
| 120 | Iterator<Object> iterator = fSelection.iterator(); |
| 121 | while (iterator.hasNext()) { |
| 122 | Object element = iterator.next(); |
| 123 | if (element instanceof TmfTraceElement) { |
| 124 | final TmfTraceElement trace = (TmfTraceElement) element; |
| 125 | IResource resource = trace.getResource(); |
| 126 | try { |
| 127 | // Close the trace if open |
| 128 | IFile file = trace.getBookmarksFile(); |
| 129 | FileEditorInput input = new FileEditorInput(file); |
| 130 | IWorkbench wb = PlatformUI.getWorkbench(); |
| 131 | for (IWorkbenchWindow wbWindow : wb.getWorkbenchWindows()) { |
| 132 | for (IWorkbenchPage wbPage : wbWindow.getPages()) { |
| 133 | for (IEditorReference editorReference : wbPage.getEditorReferences()) { |
| 134 | if (editorReference.getEditorInput().equals(input)) { |
| 135 | wbPage.closeEditor(editorReference.getEditor(false), false); |
| 136 | } |
| 137 | } |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | IPath path = resource.getLocation(); |
| 142 | if (path != null && (trace.getParent() instanceof TmfTraceFolder)) { |
| 143 | TmfExperimentFolder experimentFolder = trace.getProject().getExperimentsFolder(); |
| 144 | |
| 145 | // Propagate the removal to traces |
| 146 | for (ITmfProjectModelElement experiment : experimentFolder.getChildren()) { |
| 147 | List<ITmfProjectModelElement> toRemove = new LinkedList<ITmfProjectModelElement>(); |
| 148 | for (ITmfProjectModelElement child : experiment.getChildren()) { |
| 149 | if (child.getName().equals(trace.getName())) { |
| 150 | toRemove.add(child); |
| 151 | } |
| 152 | } |
| 153 | for (ITmfProjectModelElement child : toRemove) { |
| 154 | // Close the experiment if open |
| 155 | file = ((TmfExperimentElement) experiment).getBookmarksFile(); |
| 156 | input = new FileEditorInput(file); |
| 157 | for (IWorkbenchWindow wbWindow : wb.getWorkbenchWindows()) { |
| 158 | for (IWorkbenchPage wbPage : wbWindow.getPages()) { |
| 159 | for (IEditorReference editorReference : wbPage.getEditorReferences()) { |
| 160 | if (editorReference.getEditorInput().equals(input)) { |
| 161 | wbPage.closeEditor(editorReference.getEditor(false), false); |
| 162 | } |
| 163 | } |
| 164 | } |
| 165 | } |
| 166 | experiment.removeChild(child); |
| 167 | child.getResource().delete(true, null); |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | // Delete supplementary files |
| 172 | trace.deleteSupplementaryFolder(); |
| 173 | } |
| 174 | |
| 175 | // Finally, delete the trace |
| 176 | resource.delete(true, new NullProgressMonitor()); |
| 177 | |
| 178 | // Refresh the project |
| 179 | trace.getProject().refresh(); |
| 180 | |
| 181 | } catch (final CoreException e) { |
| 182 | Display.getDefault().asyncExec(new Runnable() { |
| 183 | @Override |
| 184 | public void run() { |
| 185 | final MessageBox mb = new MessageBox(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell()); |
| 186 | mb.setText(Messages.DeleteTraceHandler_Error + ' ' + trace.getName()); |
| 187 | mb.setMessage(e.getMessage()); |
| 188 | mb.open(); |
| 189 | } |
| 190 | }); |
| 191 | Activator.getDefault().logError("Error deleting trace: " + trace.getName(), e); //$NON-NLS-1$ |
| 192 | } |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | return null; |
| 197 | } |
| 198 | } |