1 /*******************************************************************************
2 * Copyright (c) 2011, 2012 Ericsson
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
10 * Mathieu Denis <mathieu.denis@polymtl.ca> - Generalized version based on LTTng
11 * Bernd Hufmann - Updated to use trace reference in TmfEvent and streaming
12 * Mathieu Denis - New request added to update the statistics from the selected time range
13 * Mathieu Denis - Generalization of the view to instantiate a viewer specific to a trace type
15 *******************************************************************************/
17 package org
.eclipse
.linuxtools
.tmf
.ui
.views
.statistics
;
19 import org
.eclipse
.core
.resources
.IResource
;
20 import org
.eclipse
.linuxtools
.tmf
.core
.component
.TmfDataProvider
;
21 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfEndSynchSignal
;
22 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfSignalHandler
;
23 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfStartSynchSignal
;
24 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfTraceClosedSignal
;
25 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfTraceOpenedSignal
;
26 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfTraceRangeUpdatedSignal
;
27 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfTraceSelectedSignal
;
28 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfTrace
;
29 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfExperiment
;
30 import org
.eclipse
.linuxtools
.tmf
.ui
.editors
.ITmfTraceEditor
;
31 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfTraceType
;
32 import org
.eclipse
.linuxtools
.tmf
.ui
.viewers
.ITmfViewer
;
33 import org
.eclipse
.linuxtools
.tmf
.ui
.viewers
.statistics
.TmfStatisticsViewer
;
34 import org
.eclipse
.linuxtools
.tmf
.ui
.views
.TmfView
;
35 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.tabsview
.TmfViewerFolder
;
36 import org
.eclipse
.swt
.SWT
;
37 import org
.eclipse
.swt
.widgets
.Composite
;
38 import org
.eclipse
.swt
.widgets
.Shell
;
39 import org
.eclipse
.ui
.IEditorPart
;
42 * The generic Statistics View displays statistics for any kind of traces.
44 * It is implemented according to the MVC pattern. - The model is a
45 * TmfStatisticsTreeNode built by the State Manager. - The view is built with a
46 * TreeViewer. - The controller that keeps model and view synchronized is an
47 * observer of the model.
50 * @author Mathieu Denis
52 public class TmfStatisticsView
extends TmfView
{
55 * The ID corresponds to the package in which this class is embedded.
57 public static final String ID
= "org.eclipse.linuxtools.tmf.ui.views.statistics"; //$NON-NLS-1$
62 public static final String TMF_STATISTICS_VIEW
= "StatisticsView"; //$NON-NLS-1$
65 * The viewer that builds the columns to show the statistics.
69 protected final TmfViewerFolder fStatsViewers
;
72 * Stores a reference to the selected trace.
74 private ITmfTrace fTrace
;
77 * Constructor of a statistics view.
79 * @param viewName The name to give to the view.
81 public TmfStatisticsView(String viewName
) {
84 * Create a fake parent for initialization purpose, than set the parent
85 * as soon as createPartControl is called.
87 Composite temporaryParent
= new Shell();
88 fStatsViewers
= new TmfViewerFolder(temporaryParent
);
92 * Default constructor.
94 public TmfStatisticsView() {
95 this(TMF_STATISTICS_VIEW
);
102 * org.eclipse.ui.part.WorkbenchPart#createPartControl(org.eclipse.swt.widgets.Composite)
105 public void createPartControl(Composite parent
) {
106 fStatsViewers
.setParent(parent
);
107 createStatisticsViewers();
109 IEditorPart editor
= getSite().getPage().getActiveEditor();
110 if (editor
instanceof ITmfTraceEditor
) {
111 ITmfTrace trace
= ((ITmfTraceEditor
) editor
).getTrace();
113 traceSelected(new TmfTraceSelectedSignal(this, trace
));
121 * @see org.eclipse.linuxtools.tmf.ui.views.TmfView#dispose()
124 public void dispose() {
126 fStatsViewers
.dispose();
130 * Handler called when an trace is opened.
133 * Contains the information about the selection.
137 public void traceOpened(TmfTraceOpenedSignal signal
) {
139 * Dispose the current viewer and adapt the new one to the trace
140 * type of the trace opened
142 fStatsViewers
.clear();
143 // Update the current trace
144 fTrace
= signal
.getTrace();
145 createStatisticsViewers();
146 fStatsViewers
.layout();
150 * Handler called when an trace is selected. Checks if the trace
151 * has changed and requests the selected trace if it has not yet been
155 * Contains the information about the selection.
159 public void traceSelected(TmfTraceSelectedSignal signal
) {
160 // Does not reload the same trace if already opened
161 if (signal
.getTrace() != fTrace
) {
163 * Dispose the current viewer and adapt the new one to the trace
164 * type of the trace selected
166 fStatsViewers
.clear();
167 // Update the current trace
168 fTrace
= signal
.getTrace();
169 createStatisticsViewers();
170 fStatsViewers
.layout();
172 TmfTraceRangeUpdatedSignal updateSignal
= new TmfTraceRangeUpdatedSignal(this, fTrace
, fTrace
.getTimeRange());
174 // Synchronizes the requests to make them coalesced
175 if (fTrace
instanceof TmfDataProvider
) {
176 ((TmfDataProvider
) fTrace
).startSynch(new TmfStartSynchSignal(0));
178 for (ITmfViewer viewer
: fStatsViewers
.getViewers()) {
179 TmfStatisticsViewer statsViewer
= (TmfStatisticsViewer
) viewer
;
180 statsViewer
.sendPartialRequestOnNextUpdate();
181 statsViewer
.traceRangeUpdated(updateSignal
);
183 if (fTrace
instanceof TmfDataProvider
) {
184 ((TmfDataProvider
) fTrace
).endSynch(new TmfEndSynchSignal(0));
188 * If the same trace is reselected, sends a notification to
189 * the viewers to make sure they reload correctly their partial
192 for (ITmfViewer viewer
: fStatsViewers
.getViewers()) {
193 TmfStatisticsViewer statsViewer
= (TmfStatisticsViewer
) viewer
;
194 // Will update the partial event count if needed.
195 statsViewer
.sendPartialRequestOnNextUpdate();
201 * @param signal the incoming signal
205 public void traceClosed(TmfTraceClosedSignal signal
) {
206 if (signal
.getTrace() != fTrace
) {
210 // Clear the internal data
213 // Clear the UI widgets
214 fStatsViewers
.clear(); // Also cancels ongoing requests
215 createStatisticsViewers();
216 fStatsViewers
.layout();
222 * @see org.eclipse.ui.part.WorkbenchPart#setFocus()
225 public void setFocus() {
226 fStatsViewers
.setFocus();
230 * Creates the statistics viewers for all traces in an experiment and
231 * populates a viewer folder. Each viewer is placed in a different tab and
232 * the first one is selected automatically.
234 * It uses the extension point that defines the statistics viewer to build
235 * from the trace type. If no viewer is defined, another tab won't be
236 * created, since the global viewer already contains all the basic
237 * statistics. If there is no trace selected, a global statistics viewer will
242 protected void createStatisticsViewers() {
243 // Default style for the tabs that will be created
244 int defaultStyle
= SWT
.NONE
;
246 // The folder composite that will contain the tabs
247 Composite folder
= fStatsViewers
.getParentFolder();
249 // Instantiation of the global viewer
250 TmfStatisticsViewer globalViewer
= getGlobalViewer();
251 if (fTrace
!= null) {
252 if (globalViewer
!= null) {
253 // Shows the name of the trace in the global tab
254 globalViewer
.init(folder
, Messages
.TmfStatisticsView_GlobalTabName
+ " - " + fTrace
.getName(), fTrace
); //$NON-NLS-1$
256 fStatsViewers
.addTab(globalViewer
, Messages
.TmfStatisticsView_GlobalTabName
, defaultStyle
);
259 IResource traceResource
;
261 if (fTrace
instanceof TmfExperiment
) {
262 TmfExperiment experiment
= (TmfExperiment
) fTrace
;
263 traces
= experiment
.getTraces();
265 traces
= new ITmfTrace
[] { fTrace
};
267 // Creates a statistics viewer for each trace.
268 for (ITmfTrace trace
: traces
) {
269 traceName
= trace
.getName();
270 traceResource
= trace
.getResource();
271 TmfStatisticsViewer viewer
= getStatisticsViewer(traceResource
);
273 * Adds a new viewer only if there is one defined for the
274 * selected trace type, since the global tab already contains
275 * all the basic event counts for the trace(s)
277 if (viewer
!= null) {
278 viewer
.init(folder
, traceName
, trace
);
279 fStatsViewers
.addTab(viewer
, viewer
.getName(), defaultStyle
);
283 if (globalViewer
!= null) {
284 // There is no trace selected. Shows an empty global tab
285 globalViewer
.init(folder
, Messages
.TmfStatisticsView_GlobalTabName
, fTrace
);
287 fStatsViewers
.addTab(globalViewer
, Messages
.TmfStatisticsView_GlobalTabName
, defaultStyle
);
289 // Makes the global viewer visible
290 fStatsViewers
.setSelection(0);
294 * Retrieves and instantiates a viewer based on his plug-in definition for a
295 * specific trace type. It is specific to the statistics viewer.
297 * It only calls the 0-parameter constructor without performing any other
298 * initialization on the viewer.
301 * The resource where to find the information about the trace
303 * @return a new statistics viewer based on his plug-in definition, or null
304 * if no statistics definition was found for the trace type.
307 protected static TmfStatisticsViewer
getStatisticsViewer(IResource resource
) {
308 return (TmfStatisticsViewer
) TmfTraceType
.getTraceTypeElement(resource
, TmfTraceType
.STATISTICS_VIEWER_ELEM
);
312 * @return The class to use to instantiate the global statistics viewer
315 protected TmfStatisticsViewer
getGlobalViewer() {
316 return new TmfStatisticsViewer();