Commit | Line | Data |
---|---|---|
589d0d33 BH |
1 | /********************************************************************** |
2 | * Copyright (c) 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 | * 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.MultiStatus; | |
23 | import org.eclipse.core.runtime.Status; | |
24 | import org.eclipse.core.runtime.jobs.Job; | |
25 | import org.eclipse.jface.viewers.ISelection; | |
26 | import org.eclipse.jface.viewers.StructuredSelection; | |
27 | import org.eclipse.linuxtools.internal.lttng2.core.control.model.TraceSessionState; | |
28 | import org.eclipse.linuxtools.internal.lttng2.ui.Activator; | |
29 | import org.eclipse.linuxtools.internal.lttng2.ui.views.control.ControlView; | |
30 | import org.eclipse.linuxtools.internal.lttng2.ui.views.control.messages.Messages; | |
31 | import org.eclipse.linuxtools.internal.lttng2.ui.views.control.model.impl.TraceSessionComponent; | |
32 | import org.eclipse.ui.IWorkbenchPage; | |
33 | ||
34 | /** | |
35 | * <p> | |
36 | * Command handler implementation to record a snapshot. | |
37 | * </p> | |
38 | * | |
39 | * @author Bernd Hufmann | |
40 | */ | |
41 | public 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 | } |