| 1 | /********************************************************************** |
| 2 | * Copyright (c) 2012 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 | * Bernd Hufmann - Initial API and implementation |
| 11 | **********************************************************************/ |
| 12 | package org.eclipse.linuxtools.internal.lttng.ui.views.control.handlers; |
| 13 | |
| 14 | import java.util.ArrayList; |
| 15 | import java.util.Iterator; |
| 16 | import java.util.List; |
| 17 | |
| 18 | import org.eclipse.core.commands.ExecutionEvent; |
| 19 | import org.eclipse.core.commands.ExecutionException; |
| 20 | import org.eclipse.core.runtime.IProgressMonitor; |
| 21 | import org.eclipse.core.runtime.IStatus; |
| 22 | import org.eclipse.core.runtime.Status; |
| 23 | import org.eclipse.core.runtime.jobs.Job; |
| 24 | import org.eclipse.jface.viewers.ISelection; |
| 25 | import org.eclipse.jface.viewers.StructuredSelection; |
| 26 | import org.eclipse.linuxtools.internal.lttng.ui.Activator; |
| 27 | import org.eclipse.linuxtools.internal.lttng.ui.views.control.ControlView; |
| 28 | import org.eclipse.linuxtools.internal.lttng.ui.views.control.Messages; |
| 29 | import org.eclipse.linuxtools.internal.lttng.ui.views.control.dialogs.IConfirmDialog; |
| 30 | import org.eclipse.linuxtools.internal.lttng.ui.views.control.dialogs.TraceControlDialogFactory; |
| 31 | import org.eclipse.linuxtools.internal.lttng.ui.views.control.model.TraceSessionState; |
| 32 | import org.eclipse.linuxtools.internal.lttng.ui.views.control.model.impl.TraceSessionComponent; |
| 33 | import org.eclipse.linuxtools.internal.lttng.ui.views.control.model.impl.TraceSessionGroup; |
| 34 | import org.eclipse.ui.IWorkbenchPage; |
| 35 | import org.eclipse.ui.IWorkbenchWindow; |
| 36 | import org.eclipse.ui.PlatformUI; |
| 37 | |
| 38 | /** |
| 39 | * <b><u>DestroySessionHandler</u></b> |
| 40 | * <p> |
| 41 | * Command handler implementation to destroy one or more trace sessions. |
| 42 | * </p> |
| 43 | */ |
| 44 | public class DestroySessionHandler extends BaseControlViewHandler { |
| 45 | |
| 46 | // ------------------------------------------------------------------------ |
| 47 | // Attributes |
| 48 | // ------------------------------------------------------------------------ |
| 49 | /** |
| 50 | * The list of session components the command is to be executed on. |
| 51 | */ |
| 52 | private List<TraceSessionComponent> fSessions = new ArrayList<TraceSessionComponent>(); |
| 53 | |
| 54 | // ------------------------------------------------------------------------ |
| 55 | // Operations |
| 56 | // ------------------------------------------------------------------------ |
| 57 | /* |
| 58 | * (non-Javadoc) |
| 59 | * @see org.eclipse.core.commands.AbstractHandler#execute(org.eclipse.core.commands.ExecutionEvent) |
| 60 | */ |
| 61 | @Override |
| 62 | public Object execute(ExecutionEvent event) throws ExecutionException { |
| 63 | |
| 64 | IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow(); |
| 65 | |
| 66 | if (window == null) { |
| 67 | return false; |
| 68 | } |
| 69 | // Get user confirmation |
| 70 | IConfirmDialog dialog = TraceControlDialogFactory.getInstance().getConfirmDialog(); |
| 71 | if (!dialog.openConfirm(window.getShell(), |
| 72 | Messages.TraceControl_DestroyConfirmationTitle, |
| 73 | Messages.TraceControl_DestroyConfirmationMessage)) { |
| 74 | |
| 75 | return null; |
| 76 | } |
| 77 | |
| 78 | Job job = new Job(Messages.TraceControl_DestroySessionJob) { |
| 79 | @Override |
| 80 | protected IStatus run(IProgressMonitor monitor) { |
| 81 | try { |
| 82 | // Make a copy of the list of sessions to avoid ConcurrentModificationException when iterating |
| 83 | // over fSessions, since fSessions is modified in another thread triggered by the tree viewer refresh |
| 84 | // after removing a session. |
| 85 | TraceSessionComponent[] sessions = (TraceSessionComponent[])fSessions.toArray(new TraceSessionComponent[fSessions.size()]); |
| 86 | |
| 87 | for (int i = 0; i < sessions.length; i++) { |
| 88 | // Destroy all selected sessions |
| 89 | TraceSessionComponent session = sessions[i]; |
| 90 | TraceSessionGroup sessionGroup = (TraceSessionGroup)session.getParent(); |
| 91 | sessionGroup.destroySession(session, monitor); |
| 92 | } |
| 93 | } catch (ExecutionException e) { |
| 94 | return new Status(Status.ERROR, Activator.PLUGIN_ID, e.toString()); |
| 95 | } |
| 96 | return Status.OK_STATUS; |
| 97 | } |
| 98 | }; |
| 99 | job.setUser(true); |
| 100 | job.schedule(); |
| 101 | |
| 102 | return null; |
| 103 | } |
| 104 | |
| 105 | /* |
| 106 | * (non-Javadoc) |
| 107 | * @see org.eclipse.core.commands.AbstractHandler#isEnabled() |
| 108 | */ |
| 109 | @Override |
| 110 | public boolean isEnabled() { |
| 111 | // Get workbench page for the Control View |
| 112 | IWorkbenchPage page = getWorkbenchPage(); |
| 113 | if (page == null) { |
| 114 | return false; |
| 115 | } |
| 116 | fSessions.clear(); |
| 117 | |
| 118 | // Check if one or more session are selected |
| 119 | ISelection selection = page.getSelection(ControlView.ID); |
| 120 | if (selection instanceof StructuredSelection) { |
| 121 | StructuredSelection structered = ((StructuredSelection) selection); |
| 122 | for (Iterator<?> iterator = structered.iterator(); iterator.hasNext();) { |
| 123 | Object element = (Object) iterator.next(); |
| 124 | if (element instanceof TraceSessionComponent) { |
| 125 | // Add only TraceSessionComponents that are inactive and not destroyed |
| 126 | TraceSessionComponent session = (TraceSessionComponent) element; |
| 127 | if ((session.getSessionState() == TraceSessionState.INACTIVE) && (!session.isDestroyed())) { |
| 128 | fSessions.add((TraceSessionComponent)element); |
| 129 | } |
| 130 | } |
| 131 | } |
| 132 | } |
| 133 | return fSessions.size() > 0; |
| 134 | } |
| 135 | } |