lttng: Add copyright headers to properties files
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / tmf / ui / views / statistics / TmfStatisticsView.java
1 /*******************************************************************************
2 * Copyright (c) 2011, 2013 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.ui.project.model.TmfTraceType;
30 import org.eclipse.linuxtools.tmf.ui.viewers.ITmfViewer;
31 import org.eclipse.linuxtools.tmf.ui.viewers.statistics.TmfStatisticsViewer;
32 import org.eclipse.linuxtools.tmf.ui.views.TmfView;
33 import org.eclipse.linuxtools.tmf.ui.widgets.tabsview.TmfViewerFolder;
34 import org.eclipse.swt.SWT;
35 import org.eclipse.swt.widgets.Composite;
36 import org.eclipse.swt.widgets.Shell;
37
38 /**
39 * The generic Statistics View displays statistics for any kind of traces.
40 *
41 * It is implemented according to the MVC pattern. - The model is a
42 * TmfStatisticsTreeNode built by the State Manager. - The view is built with a
43 * TreeViewer. - The controller that keeps model and view synchronized is an
44 * observer of the model.
45 *
46 * @version 2.0
47 * @author Mathieu Denis
48 */
49 public class TmfStatisticsView extends TmfView {
50
51 /**
52 * The ID corresponds to the package in which this class is embedded.
53 */
54 public static final String ID = "org.eclipse.linuxtools.tmf.ui.views.statistics"; //$NON-NLS-1$
55
56 /**
57 * The view name.
58 */
59 public static final String TMF_STATISTICS_VIEW = "StatisticsView"; //$NON-NLS-1$
60
61 /**
62 * The viewer that builds the columns to show the statistics.
63 *
64 * @since 2.0
65 */
66 protected final TmfViewerFolder fStatsViewers;
67
68 /**
69 * Stores a reference to the selected trace.
70 */
71 private ITmfTrace fTrace;
72
73 /**
74 * Constructor of a statistics view.
75 *
76 * @param viewName The name to give to the view.
77 */
78 public TmfStatisticsView(String viewName) {
79 super(viewName);
80 /*
81 * Create a fake parent for initialization purpose, than set the parent
82 * as soon as createPartControl is called.
83 */
84 Composite temporaryParent = new Shell();
85 fStatsViewers = new TmfViewerFolder(temporaryParent);
86 }
87
88 /**
89 * Default constructor.
90 */
91 public TmfStatisticsView() {
92 this(TMF_STATISTICS_VIEW);
93 }
94
95 @Override
96 public void createPartControl(Composite parent) {
97 fStatsViewers.setParent(parent);
98 createStatisticsViewers();
99
100 ITmfTrace trace = getActiveTrace();
101 if (trace != null) {
102 traceSelected(new TmfTraceSelectedSignal(this, trace));
103 }
104 }
105
106 @Override
107 public void dispose() {
108 super.dispose();
109 fStatsViewers.dispose();
110 }
111
112 /**
113 * Handler called when an trace is opened.
114 *
115 * @param signal
116 * Contains the information about the selection.
117 * @since 2.0
118 */
119 @TmfSignalHandler
120 public void traceOpened(TmfTraceOpenedSignal signal) {
121 /*
122 * Dispose the current viewer and adapt the new one to the trace
123 * type of the trace opened
124 */
125 fStatsViewers.clear();
126 // Update the current trace
127 fTrace = signal.getTrace();
128 createStatisticsViewers();
129 fStatsViewers.layout();
130 }
131
132 /**
133 * Handler called when an trace is selected. Checks if the trace
134 * has changed and requests the selected trace if it has not yet been
135 * cached.
136 *
137 * @param signal
138 * Contains the information about the selection.
139 * @since 2.0
140 */
141 @TmfSignalHandler
142 public void traceSelected(TmfTraceSelectedSignal signal) {
143 // Does not reload the same trace if already opened
144 if (signal.getTrace() != fTrace) {
145 /*
146 * Dispose the current viewer and adapt the new one to the trace
147 * type of the trace selected
148 */
149 fStatsViewers.clear();
150 // Update the current trace
151 fTrace = signal.getTrace();
152 createStatisticsViewers();
153 fStatsViewers.layout();
154
155 TmfTraceRangeUpdatedSignal updateSignal = new TmfTraceRangeUpdatedSignal(this, fTrace, fTrace.getTimeRange());
156
157 // Synchronizes the requests to make them coalesced
158 if (fTrace instanceof TmfDataProvider) {
159 ((TmfDataProvider) fTrace).startSynch(new TmfStartSynchSignal(0));
160 }
161 for (ITmfViewer viewer : fStatsViewers.getViewers()) {
162 TmfStatisticsViewer statsViewer = (TmfStatisticsViewer) viewer;
163 statsViewer.sendPartialRequestOnNextUpdate();
164 statsViewer.traceRangeUpdated(updateSignal);
165 }
166 if (fTrace instanceof TmfDataProvider) {
167 ((TmfDataProvider) fTrace).endSynch(new TmfEndSynchSignal(0));
168 }
169 } else {
170 /*
171 * If the same trace is reselected, sends a notification to
172 * the viewers to make sure they reload correctly their partial
173 * event count.
174 */
175 for (ITmfViewer viewer : fStatsViewers.getViewers()) {
176 TmfStatisticsViewer statsViewer = (TmfStatisticsViewer) viewer;
177 // Will update the partial event count if needed.
178 statsViewer.sendPartialRequestOnNextUpdate();
179 }
180 }
181 }
182
183 /**
184 * @param signal the incoming signal
185 * @since 2.0
186 */
187 @TmfSignalHandler
188 public void traceClosed(TmfTraceClosedSignal signal) {
189 if (signal.getTrace() != fTrace) {
190 return;
191 }
192
193 // Clear the internal data
194 fTrace = null;
195
196 // Clear the UI widgets
197 fStatsViewers.clear(); // Also cancels ongoing requests
198 createStatisticsViewers();
199 fStatsViewers.layout();
200 }
201
202 @Override
203 public void setFocus() {
204 fStatsViewers.setFocus();
205 }
206
207 /**
208 * Creates the statistics viewers for all traces in an 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 there is no trace selected, 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 (fTrace != null) {
230 if (globalViewer != null) {
231 // Shows the name of the trace in the global tab
232 globalViewer.init(folder, Messages.TmfStatisticsView_GlobalTabName + " - " + fTrace.getName(), fTrace); //$NON-NLS-1$
233 }
234 fStatsViewers.addTab(globalViewer, Messages.TmfStatisticsView_GlobalTabName, defaultStyle);
235
236 String traceName;
237 IResource traceResource;
238 ITmfTrace[] traces = fTrace.getTraces();
239 // Creates a statistics viewer for each trace.
240 for (ITmfTrace trace : traces) {
241 traceName = trace.getName();
242 traceResource = trace.getResource();
243 TmfStatisticsViewer viewer = getStatisticsViewer(traceResource);
244 /*
245 * Adds a new viewer only if there is one defined for the
246 * selected trace type, since the global tab already contains
247 * all the basic event counts for the trace(s)
248 */
249 if (viewer != null) {
250 viewer.init(folder, traceName, trace);
251 fStatsViewers.addTab(viewer, viewer.getName(), defaultStyle);
252 }
253 }
254 } else {
255 if (globalViewer != null) {
256 // There is no trace selected. Shows an empty global tab
257 globalViewer.init(folder, Messages.TmfStatisticsView_GlobalTabName, fTrace);
258 }
259 fStatsViewers.addTab(globalViewer, Messages.TmfStatisticsView_GlobalTabName, defaultStyle);
260 }
261 // Makes the global viewer visible
262 fStatsViewers.setSelection(0);
263 }
264
265 /**
266 * Retrieves and instantiates a viewer based on his plug-in definition for a
267 * specific trace type. It is specific to the statistics viewer.
268 *
269 * It only calls the 0-parameter constructor without performing any other
270 * initialization on the viewer.
271 *
272 * @param resource
273 * The resource where to find the information about the trace
274 * properties
275 * @return a new statistics viewer based on his plug-in definition, or null
276 * if no statistics definition was found for the trace type.
277 * @since 2.0
278 */
279 protected static TmfStatisticsViewer getStatisticsViewer(IResource resource) {
280 return (TmfStatisticsViewer) TmfTraceType.getTraceTypeElement(resource, TmfTraceType.STATISTICS_VIEWER_ELEM);
281 }
282
283 /**
284 * @return The class to use to instantiate the global statistics viewer
285 * @since 2.0
286 */
287 protected TmfStatisticsViewer getGlobalViewer() {
288 return new TmfStatisticsViewer();
289 }
290 }
This page took 0.03671 seconds and 5 git commands to generate.