Commit | Line | Data |
---|---|---|
b720ac44 | 1 | /********************************************************************** |
68d1057b | 2 | * Copyright (c) 2012, 2015 Ericsson |
cfdb727a | 3 | * |
b720ac44 BH |
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 | |
cfdb727a AM |
8 | * |
9 | * Contributors: | |
b720ac44 BH |
10 | * Bernd Hufmann - Initial API and implementation |
11 | **********************************************************************/ | |
9bc60be7 | 12 | package org.eclipse.tracecompass.internal.lttng2.control.ui.views.handlers; |
b720ac44 | 13 | |
68d1057b BH |
14 | import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull; |
15 | ||
b720ac44 BH |
16 | import java.util.Iterator; |
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; | |
9bc60be7 AM |
26 | import org.eclipse.tracecompass.internal.lttng2.control.ui.Activator; |
27 | import org.eclipse.tracecompass.internal.lttng2.control.ui.views.ControlView; | |
28 | import org.eclipse.tracecompass.internal.lttng2.control.ui.views.messages.Messages; | |
29 | import org.eclipse.tracecompass.internal.lttng2.control.ui.views.model.impl.TraceDomainComponent; | |
30 | import org.eclipse.tracecompass.internal.lttng2.control.ui.views.model.impl.TraceSessionComponent; | |
b720ac44 BH |
31 | import org.eclipse.ui.IWorkbenchPage; |
32 | import org.eclipse.ui.IWorkbenchWindow; | |
33 | import org.eclipse.ui.PlatformUI; | |
34 | ||
35 | /** | |
b720ac44 BH |
36 | * <p> |
37 | * Command handler implementation to execute command calibrate to quantify LTTng overhead. | |
38 | * </p> | |
cfdb727a | 39 | * |
dbd4432d | 40 | * @author Bernd Hufmann |
b720ac44 BH |
41 | */ |
42 | public class CalibrateHandler extends BaseControlViewHandler { | |
43 | ||
44 | // ------------------------------------------------------------------------ | |
45 | // Attributes | |
46 | // ------------------------------------------------------------------------ | |
47 | /** | |
48 | * The command execution parameter. | |
49 | */ | |
50 | protected DomainCommandParameter fParam = null; | |
51 | ||
52 | // ------------------------------------------------------------------------ | |
53 | // Operations | |
54 | // ------------------------------------------------------------------------ | |
cfdb727a | 55 | |
b720ac44 BH |
56 | @Override |
57 | public Object execute(ExecutionEvent event) throws ExecutionException { | |
58 | ||
59 | final IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow(); | |
60 | ||
61 | if (window == null) { | |
62 | return false; | |
63 | } | |
64 | fLock.lock(); | |
65 | try { | |
68d1057b BH |
66 | |
67 | DomainCommandParameter tmpParam = fParam; | |
68 | if (tmpParam == null) { | |
69 | return null; | |
70 | } | |
71 | ||
b720ac44 | 72 | // Make a copy for thread safety |
b8dbc09c | 73 | final DomainCommandParameter param = tmpParam.clone(); |
b720ac44 BH |
74 | |
75 | Job addJob = new Job(Messages.TraceControl_AddCalibrateJob) { | |
76 | @Override | |
77 | protected IStatus run(IProgressMonitor monitor) { | |
78 | try { | |
79 | param.getDomain().calibrate(monitor); | |
80 | } catch (ExecutionException e) { | |
cfdb727a | 81 | return new Status(IStatus.ERROR, Activator.PLUGIN_ID, Messages.TraceControl_AddCalibrateFailure, e); |
b720ac44 BH |
82 | } |
83 | ||
84 | return Status.OK_STATUS; | |
85 | } | |
86 | }; | |
87 | addJob.setUser(true); | |
88 | addJob.schedule(); | |
89 | ||
90 | } finally { | |
91 | fLock.unlock(); | |
92 | } | |
93 | return Status.OK_STATUS; | |
94 | } | |
95 | ||
b720ac44 BH |
96 | @Override |
97 | public boolean isEnabled() { | |
cfdb727a | 98 | |
b720ac44 BH |
99 | // Get workbench page for the Control View |
100 | IWorkbenchPage page = getWorkbenchPage(); | |
101 | if (page == null) { | |
102 | return false; | |
103 | } | |
104 | ||
105 | TraceDomainComponent domain = null; | |
106 | TraceSessionComponent session = null; | |
107 | ||
108 | // Check if one domain is selected | |
109 | ISelection selection = page.getSelection(ControlView.ID); | |
110 | if (selection instanceof StructuredSelection) { | |
111 | StructuredSelection structered = ((StructuredSelection) selection); | |
112 | for (Iterator<?> iterator = structered.iterator(); iterator.hasNext();) { | |
cfdb727a | 113 | Object element = iterator.next(); |
b720ac44 BH |
114 | if (element instanceof TraceDomainComponent) { |
115 | TraceDomainComponent tmpDomain = (TraceDomainComponent) element; | |
116 | session = (TraceSessionComponent) tmpDomain.getParent(); | |
cfdb727a | 117 | |
b720ac44 BH |
118 | // Add only TraceDomainComponent whose TraceSessionComponent parent is not destroyed |
119 | if ((!session.isDestroyed())) { | |
120 | domain = tmpDomain; | |
121 | } | |
122 | } | |
123 | } | |
124 | } | |
125 | ||
126 | boolean isEnabled = domain != null; | |
127 | ||
128 | fLock.lock(); | |
129 | try { | |
130 | fParam = null; | |
131 | if (isEnabled) { | |
68d1057b | 132 | fParam = new DomainCommandParameter(checkNotNull(session), checkNotNull(domain)); |
b720ac44 BH |
133 | } |
134 | } finally { | |
135 | fLock.unlock(); | |
136 | } | |
137 | ||
138 | return isEnabled; | |
139 | } | |
140 | } |