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