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
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.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;
40
41 /**
42 * The generic Statistics View displays statistics for any kind of traces.
43 *
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.
48 *
49 * @version 2.0
50 * @author Mathieu Denis
51 */
52 public class TmfStatisticsView extends TmfView {
53
54 /**
55 * The ID corresponds to the package in which this class is embedded.
56 */
57 public static final String ID = "org.eclipse.linuxtools.tmf.ui.views.statistics"; //$NON-NLS-1$
58
59 /**
60 * The view name.
61 */
62 public static final String TMF_STATISTICS_VIEW = "StatisticsView"; //$NON-NLS-1$
63
64 /**
65 * The viewer that builds the columns to show the statistics.
66 *
67 * @since 2.0
68 */
69 protected final TmfViewerFolder fStatsViewers;
70
71 /**
72 * Stores a reference to the selected trace.
73 */
74 private ITmfTrace fTrace;
75
76 /**
77 * Constructor of a statistics view.
78 *
79 * @param viewName The name to give to the view.
80 */
81 public TmfStatisticsView(String viewName) {
82 super(viewName);
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);
89 }
90
91 /**
92 * Default constructor.
93 */
94 public TmfStatisticsView() {
95 this(TMF_STATISTICS_VIEW);
96 }
97
98 /*
99 * (non-Javadoc)
100 *
101 * @see
102 * org.eclipse.ui.part.WorkbenchPart#createPartControl(org.eclipse.swt.widgets.Composite)
103 */
104 @Override
105 public void createPartControl(Composite parent) {
106 fStatsViewers.setParent(parent);
107 createStatisticsViewers();
108
109 IEditorPart editor = getSite().getPage().getActiveEditor();
110 if (editor instanceof ITmfTraceEditor) {
111 ITmfTrace trace = ((ITmfTraceEditor) editor).getTrace();
112 if (trace != null) {
113 traceSelected(new TmfTraceSelectedSignal(this, trace));
114 }
115 }
116 }
117
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();
126 fStatsViewers.dispose();
127 }
128
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
200 /**
201 * @param signal the incoming signal
202 * @since 2.0
203 */
204 @TmfSignalHandler
205 public void traceClosed(TmfTraceClosedSignal signal) {
206 if (signal.getTrace() != fTrace) {
207 return;
208 }
209
210 // Clear the internal data
211 fTrace = null;
212
213 // Clear the UI widgets
214 fStatsViewers.clear(); // Also cancels ongoing requests
215 createStatisticsViewers();
216 fStatsViewers.layout();
217 }
218
219 /*
220 * (non-Javadoc)
221 *
222 * @see org.eclipse.ui.part.WorkbenchPart#setFocus()
223 */
224 @Override
225 public void setFocus() {
226 fStatsViewers.setFocus();
227 }
228
229 /**
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.
233 *
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
238 * still be created.
239 *
240 * @since 2.0
241 */
242 protected void createStatisticsViewers() {
243 // Default style for the tabs that will be created
244 int defaultStyle = SWT.NONE;
245
246 // The folder composite that will contain the tabs
247 Composite folder = fStatsViewers.getParentFolder();
248
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$
255 }
256 fStatsViewers.addTab(globalViewer, Messages.TmfStatisticsView_GlobalTabName, defaultStyle);
257
258 String traceName;
259 IResource traceResource;
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) {
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);
280 }
281 }
282 } else {
283 if (globalViewer != null) {
284 // There is no trace selected. Shows an empty global tab
285 globalViewer.init(folder, Messages.TmfStatisticsView_GlobalTabName, fTrace);
286 }
287 fStatsViewers.addTab(globalViewer, Messages.TmfStatisticsView_GlobalTabName, defaultStyle);
288 }
289 // Makes the global viewer visible
290 fStatsViewers.setSelection(0);
291 }
292
293 /**
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.
296 *
297 * It only calls the 0-parameter constructor without performing any other
298 * initialization on the viewer.
299 *
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.
305 * @since 2.0
306 */
307 protected static TmfStatisticsViewer getStatisticsViewer(IResource resource) {
308 return (TmfStatisticsViewer) TmfTraceType.getTraceTypeElement(resource, TmfTraceType.STATISTICS_VIEWER_ELEM);
309 }
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 }
318 }
This page took 0.037598 seconds and 5 git commands to generate.