Commit | Line | Data |
---|---|---|
d7d40e67 | 1 | /********************************************************************** |
abf7b9b0 | 2 | * Copyright (c) 2013, 2015 Ericsson |
d7d40e67 BH |
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 | * Bernd Hufmann - Initial API and implementation | |
11 | **********************************************************************/ | |
2bdf0193 | 12 | package org.eclipse.tracecompass.tmf.ui.views; |
d7d40e67 | 13 | |
e72ac30b | 14 | import org.eclipse.jdt.annotation.NonNull; |
4c5e027b MAL |
15 | import org.eclipse.swt.SWT; |
16 | import org.eclipse.swt.custom.SashForm; | |
17 | import org.eclipse.swt.events.PaintEvent; | |
18 | import org.eclipse.swt.events.PaintListener; | |
19 | import org.eclipse.swt.layout.GridData; | |
20 | import org.eclipse.swt.layout.GridLayout; | |
d7d40e67 | 21 | import org.eclipse.swt.widgets.Composite; |
4c5e027b MAL |
22 | import org.eclipse.swt.widgets.Control; |
23 | import org.eclipse.swt.widgets.Event; | |
24 | import org.eclipse.swt.widgets.Listener; | |
25 | import org.eclipse.swt.widgets.Sash; | |
26 | import org.eclipse.tracecompass.tmf.core.signal.TmfSignalManager; | |
2bdf0193 | 27 | import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace; |
21852dfa | 28 | import org.eclipse.tracecompass.tmf.core.trace.TmfTraceManager; |
4c5e027b MAL |
29 | import org.eclipse.tracecompass.tmf.ui.signal.TmfTimeViewAlignmentInfo; |
30 | import org.eclipse.tracecompass.tmf.ui.signal.TmfTimeViewAlignmentSignal; | |
e72ac30b | 31 | import org.eclipse.tracecompass.tmf.ui.viewers.TmfViewer; |
2bdf0193 | 32 | import org.eclipse.tracecompass.tmf.ui.viewers.xycharts.TmfXYChartViewer; |
d7d40e67 BH |
33 | |
34 | /** | |
35 | * Base class to be used with a chart viewer {@link TmfXYChartViewer}. | |
36 | * It is responsible to instantiate the viewer class and load the trace | |
37 | * into the viewer when the view is created. | |
38 | * | |
39 | * @author Bernd Hufmann | |
d7d40e67 | 40 | */ |
4c5e027b | 41 | public abstract class TmfChartView extends TmfView implements ITmfTimeAligned { |
d7d40e67 | 42 | |
59387be3 BH |
43 | private static final int[] DEFAULT_WEIGHTS = {1, 3}; |
44 | ||
d7d40e67 BH |
45 | // ------------------------------------------------------------------------ |
46 | // Attributes | |
47 | // ------------------------------------------------------------------------ | |
48 | /** The TMF XY Chart reference */ | |
49 | private TmfXYChartViewer fChartViewer; | |
50 | /** The Trace reference */ | |
51 | private ITmfTrace fTrace; | |
4c5e027b MAL |
52 | /** A composite that allows us to add margins */ |
53 | private Composite fXYViewerContainer; | |
e72ac30b | 54 | private TmfViewer fTmfViewer; |
4c5e027b MAL |
55 | private SashForm fSashForm; |
56 | private Listener fSashDragListener; | |
d7d40e67 BH |
57 | |
58 | // ------------------------------------------------------------------------ | |
59 | // Constructors | |
60 | // ------------------------------------------------------------------------ | |
61 | /** | |
62 | * Standard Constructor | |
63 | * | |
64 | * @param viewName | |
65 | * The view name | |
66 | */ | |
67 | public TmfChartView(String viewName) { | |
68 | super(viewName); | |
69 | } | |
70 | ||
71 | // ------------------------------------------------------------------------ | |
72 | // Accessors | |
73 | // ------------------------------------------------------------------------ | |
74 | /** | |
75 | * Returns the TMF XY chart viewer implementation. | |
76 | * | |
77 | * @return the TMF XY chart viewer {@link TmfXYChartViewer} | |
78 | */ | |
79 | protected TmfXYChartViewer getChartViewer() { | |
80 | return fChartViewer; | |
81 | } | |
82 | ||
e72ac30b SG |
83 | /** |
84 | * Create a {@link TmfViewer} instance to be added to the left composite | |
85 | * of the sash. Default implementation provides an empty composite and | |
86 | * don't overwrite this method if not needed. | |
87 | * | |
88 | * @param parent | |
89 | * the parent control | |
90 | * @return a {@link TmfViewer} instance | |
91 | * @since 2.0 | |
92 | */ | |
93 | protected @NonNull TmfViewer createLeftChildViewer(Composite parent) { | |
94 | return new EmptyViewer(parent); | |
95 | } | |
96 | ||
d7d40e67 | 97 | /** |
4c5e027b | 98 | * Create the TMF XY chart viewer implementation |
d7d40e67 | 99 | * |
4c5e027b MAL |
100 | * @param parent |
101 | * the parent control | |
102 | * | |
103 | * @return The TMF XY chart viewer {@link TmfXYChartViewer} | |
104 | * @since 1.0 | |
d7d40e67 | 105 | */ |
4c5e027b | 106 | abstract protected TmfXYChartViewer createChartViewer(Composite parent); |
d7d40e67 BH |
107 | |
108 | /** | |
109 | * Returns the ITmfTrace implementation | |
110 | * | |
111 | * @return the ITmfTrace implementation {@link ITmfTrace} | |
112 | */ | |
113 | protected ITmfTrace getTrace() { | |
114 | return fTrace; | |
115 | } | |
116 | ||
117 | /** | |
118 | * Sets the ITmfTrace implementation | |
119 | * | |
120 | * @param trace | |
121 | * The ITmfTrace implementation {@link ITmfTrace} | |
122 | */ | |
123 | protected void setTrace(ITmfTrace trace) { | |
124 | fTrace = trace; | |
125 | } | |
126 | ||
127 | // ------------------------------------------------------------------------ | |
128 | // Operations | |
129 | // ------------------------------------------------------------------------ | |
130 | @Override | |
131 | public void createPartControl(Composite parent) { | |
4c5e027b MAL |
132 | super.createPartControl(parent); |
133 | fSashForm = new SashForm(parent, SWT.NONE); | |
e72ac30b | 134 | fTmfViewer = createLeftChildViewer(fSashForm); |
4c5e027b MAL |
135 | fXYViewerContainer = new Composite(fSashForm, SWT.NONE); |
136 | GridLayout layout = new GridLayout(); | |
137 | layout.marginHeight = 0; | |
138 | layout.marginWidth = 0; | |
139 | fXYViewerContainer.setLayout(layout); | |
140 | ||
141 | fChartViewer = createChartViewer(fXYViewerContainer); | |
142 | fChartViewer.setSendTimeAlignSignals(true); | |
143 | fChartViewer.getControl().setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); | |
144 | ||
4c5e027b MAL |
145 | fChartViewer.getControl().addPaintListener(new PaintListener() { |
146 | @Override | |
147 | public void paintControl(PaintEvent e) { | |
148 | // Sashes in a SashForm are being created on layout so add the | |
149 | // drag listener here | |
150 | if (fSashDragListener == null) { | |
151 | for (Control control : fSashForm.getChildren()) { | |
152 | if (control instanceof Sash) { | |
153 | fSashDragListener = new Listener() { | |
154 | ||
155 | @Override | |
156 | public void handleEvent(Event event) { | |
157 | TmfSignalManager.dispatchSignal(new TmfTimeViewAlignmentSignal(fSashForm, getTimeViewAlignmentInfo())); | |
158 | } | |
159 | }; | |
160 | control.removePaintListener(this); | |
161 | control.addListener(SWT.Selection, fSashDragListener); | |
162 | // There should be only one sash | |
163 | break; | |
164 | } | |
165 | } | |
166 | } | |
167 | } | |
168 | }); | |
59387be3 | 169 | fSashForm.setWeights(DEFAULT_WEIGHTS); |
21852dfa | 170 | ITmfTrace trace = TmfTraceManager.getInstance().getActiveTrace(); |
d7d40e67 BH |
171 | if (trace != null) { |
172 | setTrace(trace); | |
173 | loadTrace(); | |
174 | } | |
175 | } | |
176 | ||
177 | @Override | |
178 | public void dispose() { | |
179 | if (fChartViewer != null) { | |
180 | fChartViewer.dispose(); | |
181 | } | |
e72ac30b SG |
182 | |
183 | if (fTmfViewer != null) { | |
184 | fTmfViewer.dispose(); | |
185 | } | |
d7d40e67 BH |
186 | } |
187 | ||
abf7b9b0 PT |
188 | @Override |
189 | public void setFocus() { | |
190 | fChartViewer.getControl().setFocus(); | |
191 | } | |
192 | ||
d7d40e67 BH |
193 | /** |
194 | * Load the trace into view. | |
195 | */ | |
196 | protected void loadTrace() { | |
197 | if (fChartViewer != null) { | |
198 | fChartViewer.loadTrace(fTrace); | |
199 | } | |
200 | } | |
201 | ||
4c5e027b MAL |
202 | /** |
203 | * @since 1.0 | |
204 | */ | |
205 | @Override | |
206 | public TmfTimeViewAlignmentInfo getTimeViewAlignmentInfo() { | |
207 | if (fChartViewer == null) { | |
208 | return null; | |
209 | } | |
210 | ||
211 | return new TmfTimeViewAlignmentInfo(fChartViewer.getControl().getShell(), fSashForm.toDisplay(0, 0), getTimeAxisOffset()); | |
212 | } | |
213 | ||
214 | private int getTimeAxisOffset() { | |
2e23a015 | 215 | return fSashForm.getChildren()[0].getSize().x + fSashForm.getSashWidth() + fChartViewer.getPointAreaOffset(); |
4c5e027b MAL |
216 | } |
217 | ||
218 | /** | |
219 | * @since 1.0 | |
220 | */ | |
221 | @Override | |
222 | public int getAvailableWidth(int requestedOffset) { | |
223 | if (fChartViewer == null) { | |
224 | return 0; | |
225 | } | |
226 | ||
227 | int pointAreaWidth = fChartViewer.getPointAreaWidth(); | |
228 | int curTimeAxisOffset = getTimeAxisOffset(); | |
229 | if (pointAreaWidth <= 0) { | |
230 | pointAreaWidth = fSashForm.getBounds().width - curTimeAxisOffset; | |
231 | } | |
4c5e027b MAL |
232 | int endOffset = curTimeAxisOffset + pointAreaWidth; |
233 | GridLayout layout = (GridLayout) fXYViewerContainer.getLayout(); | |
234 | int endOffsetWithoutMargin = endOffset + layout.marginRight; | |
235 | int availableWidth = endOffsetWithoutMargin - requestedOffset; | |
236 | availableWidth = Math.min(fSashForm.getBounds().width, Math.max(0, availableWidth)); | |
237 | return availableWidth; | |
238 | } | |
239 | ||
240 | /** | |
241 | * @since 1.0 | |
242 | */ | |
243 | @Override | |
244 | public void performAlign(int offset, int width) { | |
4c5e027b | 245 | int total = fSashForm.getBounds().width; |
c34ab48a PT |
246 | int plotAreaOffset = fChartViewer.getPointAreaOffset(); |
247 | int width1 = Math.max(0, offset - plotAreaOffset - fSashForm.getSashWidth()); | |
248 | int width2 = Math.max(0, total - width1 - fSashForm.getSashWidth()); | |
40f88571 MAL |
249 | if (width1 >= 0 && width2 > 0 || width1 > 0 && width2 >= 0) { |
250 | fSashForm.setWeights(new int[] { width1, width2 }); | |
251 | fSashForm.layout(); | |
252 | } | |
4c5e027b MAL |
253 | |
254 | Composite composite = fXYViewerContainer; | |
255 | GridLayout layout = (GridLayout) composite.getLayout(); | |
256 | int timeAxisWidth = getAvailableWidth(offset); | |
257 | int marginSize = timeAxisWidth - width; | |
258 | layout.marginRight = Math.max(0, marginSize); | |
259 | composite.layout(); | |
260 | } | |
e72ac30b SG |
261 | |
262 | // ------------------------------------------------------------------------ | |
263 | // Helpers | |
264 | // ------------------------------------------------------------------------ | |
265 | /** | |
266 | * Empty @{link TmfViewer} class. | |
267 | */ | |
268 | private class EmptyViewer extends TmfViewer { | |
269 | private Composite fComposite; | |
270 | public EmptyViewer(Composite parent) { | |
271 | super(parent); | |
272 | fComposite = new Composite(parent, SWT.NONE); | |
273 | } | |
274 | @Override | |
275 | public void refresh() { | |
276 | } | |
277 | @Override | |
278 | public Control getControl() { | |
279 | return fComposite; | |
280 | } | |
281 | } | |
282 | ||
d7d40e67 | 283 | } |