| 1 | /********************************************************************** |
| 2 | * Copyright (c) 2012, 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 | * Bernd Hufmann - Initial API and implementation |
| 11 | **********************************************************************/ |
| 12 | package org.eclipse.linuxtools.internal.lttng2.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.lttng2.core.control.model.TraceSessionState; |
| 27 | import org.eclipse.linuxtools.internal.lttng2.ui.Activator; |
| 28 | import org.eclipse.linuxtools.internal.lttng2.ui.views.control.ControlView; |
| 29 | import org.eclipse.linuxtools.internal.lttng2.ui.views.control.messages.Messages; |
| 30 | import org.eclipse.linuxtools.internal.lttng2.ui.views.control.model.impl.TraceSessionComponent; |
| 31 | import org.eclipse.ui.IWorkbenchPage; |
| 32 | import org.eclipse.ui.IWorkbenchWindow; |
| 33 | import org.eclipse.ui.PlatformUI; |
| 34 | |
| 35 | /** |
| 36 | * <p> |
| 37 | * Abstract command handler implementation to start or stop one or more trace sessions. |
| 38 | * </p> |
| 39 | * |
| 40 | * @author Bernd Hufmann |
| 41 | */ |
| 42 | public abstract class ChangeSessionStateHandler extends BaseControlViewHandler { |
| 43 | |
| 44 | // ------------------------------------------------------------------------ |
| 45 | // Attributes |
| 46 | // ------------------------------------------------------------------------ |
| 47 | /** |
| 48 | * The list of session components the command is to be executed on. |
| 49 | */ |
| 50 | protected List<TraceSessionComponent> fSessions = new ArrayList<>(); |
| 51 | |
| 52 | // ------------------------------------------------------------------------ |
| 53 | // Accessors |
| 54 | // ------------------------------------------------------------------------ |
| 55 | |
| 56 | /** |
| 57 | * @return new required state. |
| 58 | */ |
| 59 | public abstract TraceSessionState getNewState(); |
| 60 | |
| 61 | // ------------------------------------------------------------------------ |
| 62 | // Operations |
| 63 | // ------------------------------------------------------------------------ |
| 64 | |
| 65 | /** |
| 66 | * Performs the state change on given session. |
| 67 | * |
| 68 | * @param session |
| 69 | * - a session which state is to be changed |
| 70 | * @param monitor |
| 71 | * - a progress monitor |
| 72 | * @throws ExecutionException |
| 73 | * If the command fails |
| 74 | */ |
| 75 | public abstract void changeState(TraceSessionComponent session, IProgressMonitor monitor) throws ExecutionException; |
| 76 | |
| 77 | @Override |
| 78 | public Object execute(ExecutionEvent event) throws ExecutionException { |
| 79 | |
| 80 | IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow(); |
| 81 | |
| 82 | if (window == null) { |
| 83 | return false; |
| 84 | } |
| 85 | |
| 86 | fLock.lock(); |
| 87 | try { |
| 88 | |
| 89 | final List<TraceSessionComponent> sessions = new ArrayList<>(); |
| 90 | sessions.addAll(fSessions); |
| 91 | |
| 92 | Job job = new Job(Messages.TraceControl_ChangeSessionStateJob) { |
| 93 | @Override |
| 94 | protected IStatus run(IProgressMonitor monitor) { |
| 95 | try { |
| 96 | for (Iterator<TraceSessionComponent> iterator = sessions.iterator(); iterator.hasNext();) { |
| 97 | |
| 98 | // Start all selected sessions |
| 99 | TraceSessionComponent session = iterator.next(); |
| 100 | changeState(session, monitor); |
| 101 | |
| 102 | // Set Session state |
| 103 | session.setSessionState(getNewState()); |
| 104 | session.fireComponentChanged(session); |
| 105 | } |
| 106 | } catch (ExecutionException e) { |
| 107 | return new Status(IStatus.ERROR, Activator.PLUGIN_ID, Messages.TraceControl_ChangeSessionStateFailure, e); |
| 108 | } |
| 109 | return Status.OK_STATUS; |
| 110 | } |
| 111 | }; |
| 112 | job.setUser(true); |
| 113 | job.schedule(); |
| 114 | } finally { |
| 115 | fLock.unlock(); |
| 116 | } |
| 117 | return null; |
| 118 | } |
| 119 | |
| 120 | @Override |
| 121 | public boolean isEnabled() { |
| 122 | // Get workbench page for the Control View |
| 123 | IWorkbenchPage page = getWorkbenchPage(); |
| 124 | if (page == null) { |
| 125 | return false; |
| 126 | } |
| 127 | |
| 128 | List<TraceSessionComponent> sessions = new ArrayList<>(0); |
| 129 | |
| 130 | // Check if one or more session are selected |
| 131 | ISelection selection = page.getSelection(ControlView.ID); |
| 132 | if (selection instanceof StructuredSelection) { |
| 133 | StructuredSelection structered = ((StructuredSelection) selection); |
| 134 | for (Iterator<?> iterator = structered.iterator(); iterator.hasNext();) { |
| 135 | Object element = iterator.next(); |
| 136 | if (element instanceof TraceSessionComponent) { |
| 137 | // Add only TraceSessionComponents that are inactive and not destroyed |
| 138 | TraceSessionComponent session = (TraceSessionComponent) element; |
| 139 | if ((session.getSessionState() != getNewState()) && (!session.isDestroyed())) { |
| 140 | sessions.add(session); |
| 141 | } |
| 142 | } |
| 143 | } |
| 144 | } |
| 145 | boolean isEnabled = !sessions.isEmpty(); |
| 146 | fLock.lock(); |
| 147 | try { |
| 148 | fSessions = null; |
| 149 | if (isEnabled) { |
| 150 | fSessions = sessions; |
| 151 | } |
| 152 | } finally { |
| 153 | fLock.unlock(); |
| 154 | } |
| 155 | return isEnabled; |
| 156 | } |
| 157 | } |