Merge branch 'master' into lttng-kepler
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / tmf / ui / views / statistics / TmfStatisticsView.java
1 /*******************************************************************************
2 * Copyright (c) 2011, 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 * 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
14 *
15 *******************************************************************************/
16
17 package org.eclipse.linuxtools.tmf.ui.views.statistics;
18
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;
36
37 /**
38 * The generic Statistics View displays statistics for any kind of traces.
39 *
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.
44 *
45 * @version 2.0
46 * @author Mathieu Denis
47 */
48 public class TmfStatisticsView extends TmfView {
49
50 /**
51 * The ID corresponds to the package in which this class is embedded.
52 */
53 public static final String ID = "org.eclipse.linuxtools.tmf.ui.views.statistics"; //$NON-NLS-1$
54
55 /**
56 * The view name.
57 */
58 public static final String TMF_STATISTICS_VIEW = "StatisticsView"; //$NON-NLS-1$
59
60 /**
61 * The viewer that builds the columns to show the statistics.
62 *
63 * @since 2.0
64 */
65 protected final TmfViewerFolder fStatsViewers;
66
67 /**
68 * Flag to force request the data from trace.
69 */
70 protected boolean fRequestData = false;
71
72 /**
73 * Stores a reference to the selected experiment.
74 */
75 private TmfExperiment fExperiment;
76
77 /**
78 * Constructor of a statistics view.
79 *
80 * @param viewName The name to give to the view.
81 */
82 public TmfStatisticsView(String viewName) {
83 super(viewName);
84 /*
85 * Create a fake parent for initialization purpose, than set the parent
86 * as soon as createPartControl is called.
87 */
88 Composite temporaryParent = new Shell();
89 fStatsViewers = new TmfViewerFolder(temporaryParent);
90 }
91
92 /**
93 * Default constructor.
94 */
95 public TmfStatisticsView() {
96 this(TMF_STATISTICS_VIEW);
97 }
98
99 /*
100 * (non-Javadoc)
101 *
102 * @see
103 * org.eclipse.ui.part.WorkbenchPart#createPartControl(org.eclipse.swt.widgets.Composite)
104 */
105 @Override
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) {
111 fRequestData = true;
112 // Insert the statistics data into the tree
113 TmfExperimentSelectedSignal signal = new TmfExperimentSelectedSignal(this, currentExperiment);
114 experimentSelected(signal);
115 return;
116 }
117 createStatisticsViewers();
118 /*
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.
122 */
123 fExperiment = currentExperiment;
124 }
125
126 /**
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
129 * cached.
130 *
131 * @param signal
132 * Contains the information about the selection.
133 */
134 @TmfSignalHandler
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) {
140 /*
141 * Dispose the current viewer and adapt the new one to the trace
142 * type of the experiment selected
143 */
144 fStatsViewers.clear();
145 // Update the current experiment
146 fExperiment = signal.getExperiment();
147 createStatisticsViewers();
148 fStatsViewers.layout();
149
150 if (fRequestData) {
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$
158 continue;
159 }
160 statsViewer = (TmfStatisticsViewer) viewer;
161 statsViewer.experimentRangeUpdated(updateSignal);
162 }
163 fExperiment.endSynch(new TmfEndSynchSignal(0));
164 fRequestData = false;
165 }
166 } else {
167 /*
168 * If the same experiment is reselected, sends a notification to
169 * the viewers to make sure they reload correctly their partial
170 * event count.
171 */
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$
176 continue;
177 }
178 statsViewer = (TmfStatisticsViewer) viewer;
179 // Will update the partial event count if needed.
180 statsViewer.sendPartialRequestOnNextUpdate();
181 }
182 }
183 }
184 }
185
186 /*
187 * (non-Javadoc)
188 *
189 * @see org.eclipse.linuxtools.tmf.ui.views.TmfView#dispose()
190 */
191 @Override
192 public void dispose() {
193 super.dispose();
194 fStatsViewers.dispose();
195 }
196
197 /*
198 * (non-Javadoc)
199 *
200 * @see org.eclipse.ui.part.WorkbenchPart#setFocus()
201 */
202 @Override
203 public void setFocus() {
204 fStatsViewers.setFocus();
205 }
206
207 /**
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.
211 *
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
216 * still be created.
217 *
218 * @since 2.0
219 */
220 protected void createStatisticsViewers() {
221 // Default style for the tabs that will be created
222 int defaultStyle = SWT.NONE;
223
224 // The folder composite that will contain the tabs
225 Composite folder = fStatsViewers.getParentFolder();
226
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$
233 }
234 fStatsViewers.addTab(globalViewer, Messages.TmfStatisticsView_GlobalTabName, defaultStyle);
235
236 String traceName;
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);
243 /*
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)
247 */
248 if (viewer != null) {
249 viewer.init(folder, traceName, trace);
250 fStatsViewers.addTab(viewer, viewer.getName(), defaultStyle);
251 }
252 }
253 } else {
254 if (globalViewer != null) {
255 // There is no experiment selected. Shows an empty global tab
256 globalViewer.init(folder, Messages.TmfStatisticsView_GlobalTabName, fExperiment);
257 }
258 fStatsViewers.addTab(globalViewer, Messages.TmfStatisticsView_GlobalTabName, defaultStyle);
259 }
260 // Makes the global viewer visible
261 fStatsViewers.setSelection(0);
262 }
263
264 /**
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.
267 *
268 * It only calls the 0-parameter constructor without performing any other
269 * initialization on the viewer.
270 *
271 * @param resource
272 * The resource where to find the information about the trace
273 * properties
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.
276 * @since 2.0
277 */
278 protected static TmfStatisticsViewer getStatisticsViewer(IResource resource) {
279 return (TmfStatisticsViewer) TmfTraceType.getTraceTypeElement(resource, TmfTraceType.STATISTICS_VIEWER_ELEM);
280 }
281
282 /**
283 * @return The class to use to instantiate the global statistics viewer
284 * @since 2.0
285 */
286 protected TmfStatisticsViewer getGlobalViewer() {
287 return new TmfStatisticsViewer();
288 }
289 }
This page took 0.042268 seconds and 6 git commands to generate.