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
.internal
.tmf
.ui
.Activator
;
21 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfEndSynchSignal
;
22 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfExperimentRangeUpdatedSignal
;
23 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfExperimentSelectedSignal
;
24 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfSignalHandler
;
25 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfStartSynchSignal
;
26 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfTrace
;
27 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfExperiment
;
28 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfTraceType
;
29 import org
.eclipse
.linuxtools
.tmf
.ui
.viewers
.ITmfViewer
;
30 import org
.eclipse
.linuxtools
.tmf
.ui
.viewers
.statistics
.TmfStatisticsViewer
;
31 import org
.eclipse
.linuxtools
.tmf
.ui
.views
.TmfView
;
32 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.tabsview
.TmfViewerFolder
;
33 import org
.eclipse
.swt
.SWT
;
34 import org
.eclipse
.swt
.widgets
.Composite
;
35 import org
.eclipse
.swt
.widgets
.Shell
;
38 * The generic Statistics View displays statistics for any kind of traces.
40 * It is implemented according to the MVC pattern. - The model is a
41 * TmfStatisticsTreeNode built by the State Manager. - The view is built with a
42 * TreeViewer. - The controller that keeps model and view synchronized is an
43 * observer of the model.
46 * @author Mathieu Denis
48 public class TmfStatisticsView
extends TmfView
{
51 * The ID corresponds to the package in which this class is embedded.
53 public static final String ID
= "org.eclipse.linuxtools.tmf.ui.views.statistics"; //$NON-NLS-1$
58 public static final String TMF_STATISTICS_VIEW
= "StatisticsView"; //$NON-NLS-1$
61 * The viewer that builds the columns to show the statistics.
65 protected final TmfViewerFolder fStatsViewers
;
68 * Flag to force request the data from trace.
70 protected boolean fRequestData
= false;
73 * Stores a reference to the selected experiment.
75 private TmfExperiment fExperiment
;
78 * Constructor of a statistics view.
80 * @param viewName The name to give to the view.
82 public TmfStatisticsView(String viewName
) {
85 * Create a fake parent for initialization purpose, than set the parent
86 * as soon as createPartControl is called.
88 Composite temporaryParent
= new Shell();
89 fStatsViewers
= new TmfViewerFolder(temporaryParent
);
93 * Default constructor.
95 public TmfStatisticsView() {
96 this(TMF_STATISTICS_VIEW
);
103 * org.eclipse.ui.part.WorkbenchPart#createPartControl(org.eclipse.swt.widgets.Composite)
106 public void createPartControl(Composite parent
) {
107 fStatsViewers
.setParent(parent
);
108 TmfExperiment currentExperiment
= TmfExperiment
.getCurrentExperiment();
109 // Read current data if any available
110 if (currentExperiment
!= null) {
112 // Insert the statistics data into the tree
113 TmfExperimentSelectedSignal signal
= new TmfExperimentSelectedSignal(this, currentExperiment
);
114 experimentSelected(signal
);
117 createStatisticsViewers();
119 * Updates the experiment field only at the end because
120 * experimentSelected signal verifies the old selected experiment to
121 * avoid reloading the same trace.
123 fExperiment
= currentExperiment
;
127 * Handler called when an experiment is selected. Checks if the experiment
128 * has changed and requests the selected experiment if it has not yet been
132 * Contains the information about the selection.
135 public void experimentSelected(TmfExperimentSelectedSignal signal
) {
136 if (signal
!= null) {
137 // Does not reload the same trace if already opened
138 if (fExperiment
== null
139 || signal
.getExperiment().toString().compareTo(fExperiment
.toString()) != 0) {
141 * Dispose the current viewer and adapt the new one to the trace
142 * type of the experiment selected
144 fStatsViewers
.clear();
145 // Update the current experiment
146 fExperiment
= signal
.getExperiment();
147 createStatisticsViewers();
148 fStatsViewers
.layout();
151 TmfExperimentRangeUpdatedSignal updateSignal
= new TmfExperimentRangeUpdatedSignal(this, fExperiment
, fExperiment
.getTimeRange());
152 TmfStatisticsViewer statsViewer
;
153 // Synchronizes the request to make them coalesced
154 fExperiment
.startSynch(new TmfStartSynchSignal(0));
155 for (ITmfViewer viewer
: fStatsViewers
.getViewers()) {
156 if (!(viewer
instanceof TmfStatisticsViewer
)) {
157 Activator
.getDefault().logError("Error - cannot cast viewer to a statistics viewer"); //$NON-NLS-1$
160 statsViewer
= (TmfStatisticsViewer
) viewer
;
161 statsViewer
.experimentRangeUpdated(updateSignal
);
163 fExperiment
.endSynch(new TmfEndSynchSignal(0));
164 fRequestData
= false;
168 * If the same experiment is reselected, sends a notification to
169 * the viewers to make sure they reload correctly their partial
172 TmfStatisticsViewer statsViewer
;
173 for (ITmfViewer viewer
: fStatsViewers
.getViewers()) {
174 if (!(viewer
instanceof TmfStatisticsViewer
)) {
175 Activator
.getDefault().logError("Error - cannot cast viewer to a statistics viewer"); //$NON-NLS-1$
178 statsViewer
= (TmfStatisticsViewer
) viewer
;
179 // Will update the partial event count if needed.
180 statsViewer
.sendPartialRequestOnNextUpdate();
189 * @see org.eclipse.linuxtools.tmf.ui.views.TmfView#dispose()
192 public void dispose() {
194 fStatsViewers
.dispose();
200 * @see org.eclipse.ui.part.WorkbenchPart#setFocus()
203 public void setFocus() {
204 fStatsViewers
.setFocus();
208 * Creates the statistics viewers for all traces in the experiment and
209 * populates a viewer folder. Each viewer is placed in a different tab and
210 * the first one is selected automatically.
212 * It uses the extension point that defines the statistics viewer to build
213 * from the trace type. If no viewer is defined, another tab won't be
214 * created, since the global viewer already contains all the basic
215 * statistics. If the experiment is empty, a global statistics viewer will
220 protected void createStatisticsViewers() {
221 // Default style for the tabs that will be created
222 int defaultStyle
= SWT
.NONE
;
224 // The folder composite that will contain the tabs
225 Composite folder
= fStatsViewers
.getParentFolder();
227 // Instantiation of the global viewer
228 TmfStatisticsViewer globalViewer
= getGlobalViewer();
229 if (fExperiment
!= null) {
230 if (globalViewer
!= null) {
231 // Shows the name of the experiment in the global tab
232 globalViewer
.init(folder
, Messages
.TmfStatisticsView_GlobalTabName
+ " - " + fExperiment
.getName(), fExperiment
); //$NON-NLS-1$
234 fStatsViewers
.addTab(globalViewer
, Messages
.TmfStatisticsView_GlobalTabName
, defaultStyle
);
237 IResource traceResource
;
238 // Creates a statistics viewer for each traces.
239 for (ITmfTrace trace
: fExperiment
.getTraces()) {
240 traceName
= trace
.getName();
241 traceResource
= trace
.getResource();
242 TmfStatisticsViewer viewer
= getStatisticsViewer(traceResource
);
244 * Adds a new viewer only if there is one defined for the
245 * selected trace type, since the global tab already contains
246 * all the basic event counts for the trace(s)
248 if (viewer
!= null) {
249 viewer
.init(folder
, traceName
, trace
);
250 fStatsViewers
.addTab(viewer
, viewer
.getName(), defaultStyle
);
254 if (globalViewer
!= null) {
255 // There is no experiment selected. Shows an empty global tab
256 globalViewer
.init(folder
, Messages
.TmfStatisticsView_GlobalTabName
, fExperiment
);
258 fStatsViewers
.addTab(globalViewer
, Messages
.TmfStatisticsView_GlobalTabName
, defaultStyle
);
260 // Makes the global viewer visible
261 fStatsViewers
.setSelection(0);
265 * Retrieves and instantiates a viewer based on his plug-in definition for a
266 * specific trace type. It is specific to the statistics viewer.
268 * It only calls the 0-parameter constructor without performing any other
269 * initialization on the viewer.
272 * The resource where to find the information about the trace
274 * @return a new statistics viewer based on his plug-in definition, or null
275 * if no statistics definition was found for the trace type.
278 protected static TmfStatisticsViewer
getStatisticsViewer(IResource resource
) {
279 return (TmfStatisticsViewer
) TmfTraceType
.getTraceTypeElement(resource
, TmfTraceType
.STATISTICS_VIEWER_ELEM
);
283 * @return The class to use to instantiate the global statistics viewer
286 protected TmfStatisticsViewer
getGlobalViewer() {
287 return new TmfStatisticsViewer();