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