pcap: Rename packages to org.eclipse.tracecompass.*
[deliverable/tracecompass.git] / org.eclipse.tracecompass.lttng2.control.ui / src / org / eclipse / linuxtools / internal / lttng2 / control / ui / views / handlers / SnaphshotHandler.java
CommitLineData
589d0d33 1/**********************************************************************
60ae41e1 2 * Copyright (c) 2013, 2014 Ericsson
589d0d33
BH
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 **********************************************************************/
8e8c0226 12package org.eclipse.linuxtools.internal.lttng2.control.ui.views.handlers;
589d0d33
BH
13
14import java.util.ArrayList;
15import java.util.Iterator;
16import java.util.List;
17
18import org.eclipse.core.commands.ExecutionEvent;
19import org.eclipse.core.commands.ExecutionException;
20import org.eclipse.core.runtime.IProgressMonitor;
21import org.eclipse.core.runtime.IStatus;
22import org.eclipse.core.runtime.MultiStatus;
23import org.eclipse.core.runtime.Status;
24import org.eclipse.core.runtime.jobs.Job;
25import org.eclipse.jface.viewers.ISelection;
26import org.eclipse.jface.viewers.StructuredSelection;
8e8c0226
AM
27import org.eclipse.linuxtools.internal.lttng2.control.core.model.TraceSessionState;
28import org.eclipse.linuxtools.internal.lttng2.control.ui.Activator;
29import org.eclipse.linuxtools.internal.lttng2.control.ui.views.ControlView;
30import org.eclipse.linuxtools.internal.lttng2.control.ui.views.messages.Messages;
31import org.eclipse.linuxtools.internal.lttng2.control.ui.views.model.impl.TraceSessionComponent;
589d0d33
BH
32import org.eclipse.ui.IWorkbenchPage;
33
34/**
35 * <p>
36 * Command handler implementation to record a snapshot.
37 * </p>
38 *
39 * @author Bernd Hufmann
40 */
41public class SnaphshotHandler extends BaseControlViewHandler {
42
43 // ------------------------------------------------------------------------
44 // Attributes
45 // ------------------------------------------------------------------------
46 /**
47 * The list of session components the command is to be executed on.
48 */
e0838ca1 49 protected List<TraceSessionComponent> fSessions = new ArrayList<>();
589d0d33
BH
50
51 // ------------------------------------------------------------------------
52 // Operations
53 // ------------------------------------------------------------------------
54
55 @Override
56 public Object execute(ExecutionEvent event) throws ExecutionException {
57 fLock.lock();
58 try {
59 // Make a copy for thread safety
e0838ca1 60 final List<TraceSessionComponent> sessions = new ArrayList<>();
589d0d33
BH
61 sessions.addAll(fSessions);
62
63 Job job = new Job(Messages.TraceControl_RecordSnapshotJob) {
64 @Override
65 protected IStatus run(IProgressMonitor monitor) {
66 MultiStatus status = new MultiStatus(Activator.PLUGIN_ID, IStatus.OK, null, null);
67 for (Iterator<TraceSessionComponent> iterator = sessions.iterator(); iterator.hasNext();) {
68 try {
69 // record snapshot for all selected sessions sequentially
70 TraceSessionComponent session = iterator.next();
71 session.recordSnapshot(monitor);
72 if (monitor.isCanceled()) {
73 status.add(Status.CANCEL_STATUS);
74 break;
75 }
76 } catch (ExecutionException e) {
77 status.add(new Status(IStatus.ERROR, Activator.PLUGIN_ID, Messages.TraceControl_RecordSnapshotFailure, e));
78 }
79 }
80 return status;
81 }
82 };
83 job.setUser(true);
84 job.schedule();
85 } finally {
86 fLock.unlock();
87 }
88 return null;
89 }
90
91 @Override
92 public boolean isEnabled() {
93 // Get workbench page for the Control View
94 IWorkbenchPage page = getWorkbenchPage();
95 if (page == null) {
96 return false;
97 }
98
e0838ca1 99 List<TraceSessionComponent> sessions = new ArrayList<>(0);
589d0d33
BH
100
101 // Check if one session is selected
102 ISelection selection = page.getSelection(ControlView.ID);
103 if (selection instanceof StructuredSelection) {
104 StructuredSelection structered = ((StructuredSelection) selection);
105 for (Iterator<?> iterator = structered.iterator(); iterator.hasNext();) {
106 Object element = iterator.next();
107 if (element instanceof TraceSessionComponent) {
108 // Add only if corresponding TraceSessionComponent is an active snapshot session and not destroyed
109 TraceSessionComponent session = (TraceSessionComponent) element;
110 if(session.isSnapshotSession() &&
111 session.getSessionState() == TraceSessionState.ACTIVE &&
112 !session.isDestroyed()) {
113 sessions.add(session);
114 }
115 }
116 }
117 }
118 boolean isEnabled = !sessions.isEmpty();
119 fLock.lock();
120 try {
121 fSessions = null;
122 if (isEnabled) {
123 fSessions = sessions;
124 }
125 } finally {
126 fLock.unlock();
127 }
128 return isEnabled;
129 }
130}
This page took 0.042766 seconds and 5 git commands to generate.