tmf: Handle 'null' active pages in views
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / tmf / ui / views / statistics / TmfStatisticsView.java
CommitLineData
79e08fd0 1/*******************************************************************************
09667aa4 2 * Copyright (c) 2011, 2012 Ericsson
20ff3b75 3 *
79e08fd0
BH
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
20ff3b75 8 *
79e08fd0 9 * Contributors:
09667aa4 10 * Mathieu Denis <mathieu.denis@polymtl.ca> - Generalized version based on LTTng
79e08fd0 11 * Bernd Hufmann - Updated to use trace reference in TmfEvent and streaming
25a042b3 12 * Mathieu Denis - New request added to update the statistics from the selected time range
cfd22ad0 13 * Mathieu Denis - Generalization of the view to instantiate a viewer specific to a trace type
20ff3b75 14 *
79e08fd0
BH
15 *******************************************************************************/
16
17package org.eclipse.linuxtools.tmf.ui.views.statistics;
18
cfd22ad0 19import org.eclipse.core.resources.IResource;
faa38350 20import org.eclipse.linuxtools.tmf.core.component.TmfDataProvider;
05627bda 21import org.eclipse.linuxtools.tmf.core.signal.TmfEndSynchSignal;
6c13869b 22import org.eclipse.linuxtools.tmf.core.signal.TmfSignalHandler;
05627bda 23import org.eclipse.linuxtools.tmf.core.signal.TmfStartSynchSignal;
faa38350
PT
24import org.eclipse.linuxtools.tmf.core.signal.TmfTraceClosedSignal;
25import org.eclipse.linuxtools.tmf.core.signal.TmfTraceOpenedSignal;
26import org.eclipse.linuxtools.tmf.core.signal.TmfTraceRangeUpdatedSignal;
27import org.eclipse.linuxtools.tmf.core.signal.TmfTraceSelectedSignal;
6c13869b 28import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace;
9e0640dc 29import org.eclipse.linuxtools.tmf.core.trace.TmfExperiment;
faa38350 30import org.eclipse.linuxtools.tmf.ui.editors.ITmfTraceEditor;
cfd22ad0 31import org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceType;
05627bda 32import org.eclipse.linuxtools.tmf.ui.viewers.ITmfViewer;
cfd22ad0 33import org.eclipse.linuxtools.tmf.ui.viewers.statistics.TmfStatisticsViewer;
79e08fd0 34import org.eclipse.linuxtools.tmf.ui.views.TmfView;
05627bda
MD
35import org.eclipse.linuxtools.tmf.ui.widgets.tabsview.TmfViewerFolder;
36import org.eclipse.swt.SWT;
79e08fd0 37import org.eclipse.swt.widgets.Composite;
05627bda 38import org.eclipse.swt.widgets.Shell;
faa38350 39import org.eclipse.ui.IEditorPart;
79e08fd0
BH
40
41/**
79e08fd0 42 * The generic Statistics View displays statistics for any kind of traces.
20ff3b75 43 *
09667aa4
MD
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.
20ff3b75 48 *
25a042b3 49 * @version 2.0
09667aa4 50 * @author Mathieu Denis
79e08fd0
BH
51 */
52public class TmfStatisticsView extends TmfView {
09667aa4 53
79e08fd0 54 /**
05627bda 55 * The ID corresponds to the package in which this class is embedded.
79e08fd0
BH
56 */
57 public static final String ID = "org.eclipse.linuxtools.tmf.ui.views.statistics"; //$NON-NLS-1$
09667aa4 58
d26274e7 59 /**
09667aa4 60 * The view name.
d26274e7 61 */
66711dc8 62 public static final String TMF_STATISTICS_VIEW = "StatisticsView"; //$NON-NLS-1$
09667aa4 63
d26274e7 64 /**
05627bda 65 * The viewer that builds the columns to show the statistics.
25a042b3
MD
66 *
67 * @since 2.0
68 */
05627bda 69 protected final TmfViewerFolder fStatsViewers;
25a042b3 70
d26274e7 71 /**
faa38350 72 * Stores a reference to the selected trace.
cfd22ad0 73 */
faa38350 74 private ITmfTrace fTrace;
09667aa4 75
79e08fd0
BH
76 /**
77 * Constructor of a statistics view.
20ff3b75 78 *
cfd22ad0 79 * @param viewName The name to give to the view.
79e08fd0
BH
80 */
81 public TmfStatisticsView(String viewName) {
82 super(viewName);
05627bda
MD
83 /*
84 * Create a fake parent for initialization purpose, than set the parent
85 * as soon as createPartControl is called.
86 */
87 Composite temporaryParent = new Shell();
88 fStatsViewers = new TmfViewerFolder(temporaryParent);
79e08fd0
BH
89 }
90
91 /**
92 * Default constructor.
93 */
94 public TmfStatisticsView() {
95 this(TMF_STATISTICS_VIEW);
96 }
97
98 /*
99 * (non-Javadoc)
09667aa4
MD
100 *
101 * @see
102 * org.eclipse.ui.part.WorkbenchPart#createPartControl(org.eclipse.swt.widgets.Composite)
79e08fd0
BH
103 */
104 @Override
105 public void createPartControl(Composite parent) {
05627bda 106 fStatsViewers.setParent(parent);
05627bda 107 createStatisticsViewers();
79e08fd0 108
f28d404e 109 IEditorPart editor = getSite().getPage().getActiveEditor();
faa38350
PT
110 if (editor instanceof ITmfTraceEditor) {
111 ITmfTrace trace = ((ITmfTraceEditor) editor).getTrace();
112 if (trace != null) {
113 traceSelected(new TmfTraceSelectedSignal(this, trace));
cfd22ad0
MD
114 }
115 }
116 }
117
cfd22ad0
MD
118 /*
119 * (non-Javadoc)
120 *
121 * @see org.eclipse.linuxtools.tmf.ui.views.TmfView#dispose()
122 */
123 @Override
124 public void dispose() {
125 super.dispose();
05627bda 126 fStatsViewers.dispose();
cfd22ad0
MD
127 }
128
faa38350
PT
129 /**
130 * Handler called when an trace is opened.
131 *
132 * @param signal
133 * Contains the information about the selection.
134 * @since 2.0
135 */
136 @TmfSignalHandler
137 public void traceOpened(TmfTraceOpenedSignal signal) {
138 /*
139 * Dispose the current viewer and adapt the new one to the trace
140 * type of the trace opened
141 */
142 fStatsViewers.clear();
143 // Update the current trace
144 fTrace = signal.getTrace();
145 createStatisticsViewers();
146 fStatsViewers.layout();
147 }
148
149 /**
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
152 * cached.
153 *
154 * @param signal
155 * Contains the information about the selection.
156 * @since 2.0
157 */
158 @TmfSignalHandler
159 public void traceSelected(TmfTraceSelectedSignal signal) {
160 // Does not reload the same trace if already opened
161 if (signal.getTrace() != fTrace) {
162 /*
163 * Dispose the current viewer and adapt the new one to the trace
164 * type of the trace selected
165 */
166 fStatsViewers.clear();
167 // Update the current trace
168 fTrace = signal.getTrace();
169 createStatisticsViewers();
170 fStatsViewers.layout();
171
172 TmfTraceRangeUpdatedSignal updateSignal = new TmfTraceRangeUpdatedSignal(this, fTrace, fTrace.getTimeRange());
173
174 // Synchronizes the requests to make them coalesced
175 if (fTrace instanceof TmfDataProvider) {
176 ((TmfDataProvider) fTrace).startSynch(new TmfStartSynchSignal(0));
177 }
178 for (ITmfViewer viewer : fStatsViewers.getViewers()) {
179 TmfStatisticsViewer statsViewer = (TmfStatisticsViewer) viewer;
180 statsViewer.sendPartialRequestOnNextUpdate();
181 statsViewer.traceRangeUpdated(updateSignal);
182 }
183 if (fTrace instanceof TmfDataProvider) {
184 ((TmfDataProvider) fTrace).endSynch(new TmfEndSynchSignal(0));
185 }
186 } else {
187 /*
188 * If the same trace is reselected, sends a notification to
189 * the viewers to make sure they reload correctly their partial
190 * event count.
191 */
192 for (ITmfViewer viewer : fStatsViewers.getViewers()) {
193 TmfStatisticsViewer statsViewer = (TmfStatisticsViewer) viewer;
194 // Will update the partial event count if needed.
195 statsViewer.sendPartialRequestOnNextUpdate();
196 }
197 }
198 }
199
ea279a69
FC
200 /**
201 * @param signal the incoming signal
202 * @since 2.0
203 */
204 @TmfSignalHandler
faa38350
PT
205 public void traceClosed(TmfTraceClosedSignal signal) {
206 if (signal.getTrace() != fTrace) {
207 return;
208 }
ea279a69
FC
209
210 // Clear the internal data
faa38350 211 fTrace = null;
ea279a69
FC
212
213 // Clear the UI widgets
214 fStatsViewers.clear(); // Also cancels ongoing requests
215 createStatisticsViewers();
216 fStatsViewers.layout();
217 }
218
cfd22ad0
MD
219 /*
220 * (non-Javadoc)
221 *
222 * @see org.eclipse.ui.part.WorkbenchPart#setFocus()
223 */
224 @Override
225 public void setFocus() {
05627bda 226 fStatsViewers.setFocus();
cfd22ad0
MD
227 }
228
c0341b86 229 /**
faa38350 230 * Creates the statistics viewers for all traces in an experiment and
eeb388b1 231 * populates a viewer folder. Each viewer is placed in a different tab and
05627bda 232 * the first one is selected automatically.
09667aa4 233 *
05627bda
MD
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
faa38350 237 * statistics. If there is no trace selected, a global statistics viewer will
05627bda 238 * still be created.
c0341b86 239 *
25a042b3
MD
240 * @since 2.0
241 */
05627bda
MD
242 protected void createStatisticsViewers() {
243 // Default style for the tabs that will be created
244 int defaultStyle = SWT.NONE;
25a042b3 245
05627bda
MD
246 // The folder composite that will contain the tabs
247 Composite folder = fStatsViewers.getParentFolder();
25a042b3 248
05627bda 249 // Instantiation of the global viewer
73fbf6be 250 TmfStatisticsViewer globalViewer = getGlobalViewer();
faa38350 251 if (fTrace != null) {
73fbf6be 252 if (globalViewer != null) {
faa38350
PT
253 // Shows the name of the trace in the global tab
254 globalViewer.init(folder, Messages.TmfStatisticsView_GlobalTabName + " - " + fTrace.getName(), fTrace); //$NON-NLS-1$
73fbf6be 255 }
05627bda
MD
256 fStatsViewers.addTab(globalViewer, Messages.TmfStatisticsView_GlobalTabName, defaultStyle);
257
258 String traceName;
259 IResource traceResource;
faa38350
PT
260 ITmfTrace[] traces;
261 if (fTrace instanceof TmfExperiment) {
262 TmfExperiment experiment = (TmfExperiment) fTrace;
263 traces = experiment.getTraces();
264 } else {
265 traces = new ITmfTrace[] { fTrace };
266 }
267 // Creates a statistics viewer for each trace.
268 for (ITmfTrace trace : traces) {
05627bda
MD
269 traceName = trace.getName();
270 traceResource = trace.getResource();
271 TmfStatisticsViewer viewer = getStatisticsViewer(traceResource);
272 /*
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)
276 */
277 if (viewer != null) {
278 viewer.init(folder, traceName, trace);
279 fStatsViewers.addTab(viewer, viewer.getName(), defaultStyle);
79e08fd0
BH
280 }
281 }
05627bda 282 } else {
73fbf6be 283 if (globalViewer != null) {
faa38350
PT
284 // There is no trace selected. Shows an empty global tab
285 globalViewer.init(folder, Messages.TmfStatisticsView_GlobalTabName, fTrace);
73fbf6be 286 }
05627bda 287 fStatsViewers.addTab(globalViewer, Messages.TmfStatisticsView_GlobalTabName, defaultStyle);
79e08fd0 288 }
05627bda
MD
289 // Makes the global viewer visible
290 fStatsViewers.setSelection(0);
79e08fd0
BH
291 }
292
25a042b3 293 /**
05627bda
MD
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.
25a042b3 296 *
05627bda
MD
297 * It only calls the 0-parameter constructor without performing any other
298 * initialization on the viewer.
25a042b3 299 *
05627bda
MD
300 * @param resource
301 * The resource where to find the information about the trace
302 * properties
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.
25a042b3
MD
305 * @since 2.0
306 */
05627bda
MD
307 protected static TmfStatisticsViewer getStatisticsViewer(IResource resource) {
308 return (TmfStatisticsViewer) TmfTraceType.getTraceTypeElement(resource, TmfTraceType.STATISTICS_VIEWER_ELEM);
79e08fd0 309 }
73fbf6be
MD
310
311 /**
312 * @return The class to use to instantiate the global statistics viewer
313 * @since 2.0
314 */
315 protected TmfStatisticsViewer getGlobalViewer() {
316 return new TmfStatisticsViewer();
317 }
79e08fd0 318}
This page took 0.0522 seconds and 5 git commands to generate.