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