1 /**********************************************************************
2 * Copyright (c) 2013, 2015 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
.jdt
.annotation
.NonNull
;
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
;
21 import org
.eclipse
.swt
.widgets
.Composite
;
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
;
27 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfTrace
;
28 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.TmfTraceManager
;
29 import org
.eclipse
.tracecompass
.tmf
.ui
.signal
.TmfTimeViewAlignmentInfo
;
30 import org
.eclipse
.tracecompass
.tmf
.ui
.signal
.TmfTimeViewAlignmentSignal
;
31 import org
.eclipse
.tracecompass
.tmf
.ui
.viewers
.TmfViewer
;
32 import org
.eclipse
.tracecompass
.tmf
.ui
.viewers
.xycharts
.TmfXYChartViewer
;
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.
39 * @author Bernd Hufmann
41 public abstract class TmfChartView
extends TmfView
implements ITmfTimeAligned
{
43 private static final int[] DEFAULT_WEIGHTS
= {1, 3};
45 // ------------------------------------------------------------------------
47 // ------------------------------------------------------------------------
48 /** The TMF XY Chart reference */
49 private TmfXYChartViewer fChartViewer
;
50 /** The Trace reference */
51 private ITmfTrace fTrace
;
52 /** A composite that allows us to add margins */
53 private Composite fXYViewerContainer
;
54 private TmfViewer fTmfViewer
;
55 private SashForm fSashForm
;
56 private Listener fSashDragListener
;
58 // ------------------------------------------------------------------------
60 // ------------------------------------------------------------------------
62 * Standard Constructor
67 public TmfChartView(String viewName
) {
71 // ------------------------------------------------------------------------
73 // ------------------------------------------------------------------------
75 * Returns the TMF XY chart viewer implementation.
77 * @return the TMF XY chart viewer {@link TmfXYChartViewer}
79 protected TmfXYChartViewer
getChartViewer() {
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.
90 * @return a {@link TmfViewer} instance
93 protected @NonNull TmfViewer
createLeftChildViewer(Composite parent
) {
94 return new EmptyViewer(parent
);
98 * Create the TMF XY chart viewer implementation
103 * @return The TMF XY chart viewer {@link TmfXYChartViewer}
106 abstract protected TmfXYChartViewer
createChartViewer(Composite parent
);
109 * Returns the ITmfTrace implementation
111 * @return the ITmfTrace implementation {@link ITmfTrace}
113 protected ITmfTrace
getTrace() {
118 * Sets the ITmfTrace implementation
121 * The ITmfTrace implementation {@link ITmfTrace}
123 protected void setTrace(ITmfTrace trace
) {
127 // ------------------------------------------------------------------------
129 // ------------------------------------------------------------------------
131 public void createPartControl(Composite parent
) {
132 super.createPartControl(parent
);
133 fSashForm
= new SashForm(parent
, SWT
.NONE
);
134 fTmfViewer
= createLeftChildViewer(fSashForm
);
135 fXYViewerContainer
= new Composite(fSashForm
, SWT
.NONE
);
136 GridLayout layout
= new GridLayout();
137 layout
.marginHeight
= 0;
138 layout
.marginWidth
= 0;
139 fXYViewerContainer
.setLayout(layout
);
141 fChartViewer
= createChartViewer(fXYViewerContainer
);
142 fChartViewer
.setSendTimeAlignSignals(true);
143 fChartViewer
.getControl().setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
145 fChartViewer
.getControl().addPaintListener(new PaintListener() {
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() {
156 public void handleEvent(Event event
) {
157 TmfSignalManager
.dispatchSignal(new TmfTimeViewAlignmentSignal(fSashForm
, getTimeViewAlignmentInfo()));
160 control
.removePaintListener(this);
161 control
.addListener(SWT
.Selection
, fSashDragListener
);
162 // There should be only one sash
169 fSashForm
.setWeights(DEFAULT_WEIGHTS
);
170 ITmfTrace trace
= TmfTraceManager
.getInstance().getActiveTrace();
178 public void dispose() {
179 if (fChartViewer
!= null) {
180 fChartViewer
.dispose();
183 if (fTmfViewer
!= null) {
184 fTmfViewer
.dispose();
189 public void setFocus() {
190 fChartViewer
.getControl().setFocus();
194 * Load the trace into view.
196 protected void loadTrace() {
197 if (fChartViewer
!= null) {
198 fChartViewer
.loadTrace(fTrace
);
206 public TmfTimeViewAlignmentInfo
getTimeViewAlignmentInfo() {
207 if (fChartViewer
== null) {
211 return new TmfTimeViewAlignmentInfo(fChartViewer
.getControl().getShell(), fSashForm
.toDisplay(0, 0), getTimeAxisOffset());
214 private int getTimeAxisOffset() {
215 return fSashForm
.getChildren()[0].getSize().x
+ fSashForm
.getSashWidth() + fChartViewer
.getPointAreaOffset();
222 public int getAvailableWidth(int requestedOffset
) {
223 if (fChartViewer
== null) {
227 int pointAreaWidth
= fChartViewer
.getPointAreaWidth();
228 int curTimeAxisOffset
= getTimeAxisOffset();
229 if (pointAreaWidth
<= 0) {
230 pointAreaWidth
= fSashForm
.getBounds().width
- curTimeAxisOffset
;
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
;
244 public void performAlign(int offset
, int width
) {
245 int total
= fSashForm
.getBounds().width
;
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());
249 if (width1
>= 0 && width2
> 0 || width1
> 0 && width2
>= 0) {
250 fSashForm
.setWeights(new int[] { width1
, width2
});
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
);
262 // ------------------------------------------------------------------------
264 // ------------------------------------------------------------------------
266 * Empty @{link TmfViewer} class.
268 private class EmptyViewer
extends TmfViewer
{
269 private Composite fComposite
;
270 public EmptyViewer(Composite parent
) {
272 fComposite
= new Composite(parent
, SWT
.NONE
);
275 public void refresh() {
278 public Control
getControl() {