1 /**********************************************************************
2 * Copyright (c) 2013, 2014 Ericsson
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
10 * Bernd Hufmann - Initial API and implementation
11 **********************************************************************/
12 package org
.eclipse
.tracecompass
.tmf
.ui
.views
;
14 import org
.eclipse
.swt
.SWT
;
15 import org
.eclipse
.swt
.custom
.SashForm
;
16 import org
.eclipse
.swt
.events
.PaintEvent
;
17 import org
.eclipse
.swt
.events
.PaintListener
;
18 import org
.eclipse
.swt
.layout
.GridData
;
19 import org
.eclipse
.swt
.layout
.GridLayout
;
20 import org
.eclipse
.swt
.widgets
.Composite
;
21 import org
.eclipse
.swt
.widgets
.Control
;
22 import org
.eclipse
.swt
.widgets
.Event
;
23 import org
.eclipse
.swt
.widgets
.Listener
;
24 import org
.eclipse
.swt
.widgets
.Sash
;
25 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfSignalManager
;
26 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfTrace
;
27 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.TmfTraceManager
;
28 import org
.eclipse
.tracecompass
.tmf
.ui
.signal
.TmfTimeViewAlignmentInfo
;
29 import org
.eclipse
.tracecompass
.tmf
.ui
.signal
.TmfTimeViewAlignmentSignal
;
30 import org
.eclipse
.tracecompass
.tmf
.ui
.viewers
.xycharts
.TmfXYChartViewer
;
33 * Base class to be used with a chart viewer {@link TmfXYChartViewer}.
34 * It is responsible to instantiate the viewer class and load the trace
35 * into the viewer when the view is created.
37 * @author Bernd Hufmann
39 public abstract class TmfChartView
extends TmfView
implements ITmfTimeAligned
{
41 private static final int[] DEFAULT_WEIGHTS
= {1, 3};
43 // ------------------------------------------------------------------------
45 // ------------------------------------------------------------------------
46 /** The TMF XY Chart reference */
47 private TmfXYChartViewer fChartViewer
;
48 /** The Trace reference */
49 private ITmfTrace fTrace
;
50 /** A composite that allows us to add margins */
51 private Composite fXYViewerContainer
;
52 private SashForm fSashForm
;
53 private Listener fSashDragListener
;
55 // ------------------------------------------------------------------------
57 // ------------------------------------------------------------------------
59 * Standard Constructor
64 public TmfChartView(String viewName
) {
68 // ------------------------------------------------------------------------
70 // ------------------------------------------------------------------------
72 * Returns the TMF XY chart viewer implementation.
74 * @return the TMF XY chart viewer {@link TmfXYChartViewer}
76 protected TmfXYChartViewer
getChartViewer() {
81 * Create the TMF XY chart viewer implementation
86 * @return The TMF XY chart viewer {@link TmfXYChartViewer}
89 abstract protected TmfXYChartViewer
createChartViewer(Composite parent
);
92 * Returns the ITmfTrace implementation
94 * @return the ITmfTrace implementation {@link ITmfTrace}
96 protected ITmfTrace
getTrace() {
101 * Sets the ITmfTrace implementation
104 * The ITmfTrace implementation {@link ITmfTrace}
106 protected void setTrace(ITmfTrace trace
) {
110 // ------------------------------------------------------------------------
112 // ------------------------------------------------------------------------
114 public void createPartControl(Composite parent
) {
115 super.createPartControl(parent
);
116 fSashForm
= new SashForm(parent
, SWT
.NONE
);
117 new Composite(fSashForm
, SWT
.NONE
);
118 fXYViewerContainer
= new Composite(fSashForm
, SWT
.NONE
);
119 GridLayout layout
= new GridLayout();
120 layout
.marginHeight
= 0;
121 layout
.marginWidth
= 0;
122 fXYViewerContainer
.setLayout(layout
);
124 fChartViewer
= createChartViewer(fXYViewerContainer
);
125 fChartViewer
.setSendTimeAlignSignals(true);
126 fChartViewer
.getControl().setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
128 fChartViewer
.getControl().addPaintListener(new PaintListener() {
130 public void paintControl(PaintEvent e
) {
131 // Sashes in a SashForm are being created on layout so add the
132 // drag listener here
133 if (fSashDragListener
== null) {
134 for (Control control
: fSashForm
.getChildren()) {
135 if (control
instanceof Sash
) {
136 fSashDragListener
= new Listener() {
139 public void handleEvent(Event event
) {
140 TmfSignalManager
.dispatchSignal(new TmfTimeViewAlignmentSignal(fSashForm
, getTimeViewAlignmentInfo()));
143 control
.removePaintListener(this);
144 control
.addListener(SWT
.Selection
, fSashDragListener
);
145 // There should be only one sash
152 fSashForm
.setWeights(DEFAULT_WEIGHTS
);
153 ITmfTrace trace
= TmfTraceManager
.getInstance().getActiveTrace();
161 public void dispose() {
162 if (fChartViewer
!= null) {
163 fChartViewer
.dispose();
168 * Load the trace into view.
170 protected void loadTrace() {
171 if (fChartViewer
!= null) {
172 fChartViewer
.loadTrace(fTrace
);
180 public TmfTimeViewAlignmentInfo
getTimeViewAlignmentInfo() {
181 if (fChartViewer
== null) {
185 return new TmfTimeViewAlignmentInfo(fChartViewer
.getControl().getShell(), fSashForm
.toDisplay(0, 0), getTimeAxisOffset());
188 private int getTimeAxisOffset() {
189 int[] weights
= fSashForm
.getWeights();
190 int width
= (int) (((float) weights
[0] / (weights
[0] + weights
[1])) * fSashForm
.getBounds().width
);
191 int curTimeAxisOffset
= width
+ fSashForm
.getSashWidth() + fChartViewer
.getPointAreaOffset();
192 return curTimeAxisOffset
;
199 public int getAvailableWidth(int requestedOffset
) {
200 if (fChartViewer
== null) {
204 int pointAreaWidth
= fChartViewer
.getPointAreaWidth();
205 int curTimeAxisOffset
= getTimeAxisOffset();
206 if (pointAreaWidth
<= 0) {
207 pointAreaWidth
= fSashForm
.getBounds().width
- curTimeAxisOffset
;
210 // TODO this is just an approximation that assumes that the end will be at the same position but that can change for a different data range/scaling
211 int endOffset
= curTimeAxisOffset
+ pointAreaWidth
;
212 GridLayout layout
= (GridLayout
) fXYViewerContainer
.getLayout();
213 int endOffsetWithoutMargin
= endOffset
+ layout
.marginRight
;
214 int availableWidth
= endOffsetWithoutMargin
- requestedOffset
;
215 availableWidth
= Math
.min(fSashForm
.getBounds().width
, Math
.max(0, availableWidth
));
216 return availableWidth
;
223 public void performAlign(int offset
, int width
) {
224 int plotAreaOffset
= fChartViewer
.getPointAreaOffset();
225 int sashOffset
= Math
.max(1, offset
- plotAreaOffset
);
226 int total
= fSashForm
.getBounds().width
;
227 int width1
= (int) (sashOffset
/ (float) total
* 1000);
228 int width2
= (int) ((total
- sashOffset
) / (float) total
* 1000);
229 fSashForm
.setWeights(new int[] { width1
, width2
});
232 Composite composite
= fXYViewerContainer
;
233 GridLayout layout
= (GridLayout
) composite
.getLayout();
234 int timeAxisWidth
= getAvailableWidth(offset
);
235 int marginSize
= timeAxisWidth
- width
;
236 layout
.marginRight
= Math
.max(0, marginSize
);