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