| 1 | /********************************************************************** |
| 2 | * Copyright (c) 2013, 2015 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 | * Bernd Hufmann - Initial API and implementation |
| 11 | **********************************************************************/ |
| 12 | package org.eclipse.tracecompass.tmf.ui.views; |
| 13 | |
| 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; |
| 31 | |
| 32 | /** |
| 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. |
| 36 | * |
| 37 | * @author Bernd Hufmann |
| 38 | */ |
| 39 | public abstract class TmfChartView extends TmfView implements ITmfTimeAligned { |
| 40 | |
| 41 | private static final int[] DEFAULT_WEIGHTS = {1, 3}; |
| 42 | |
| 43 | // ------------------------------------------------------------------------ |
| 44 | // Attributes |
| 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; |
| 54 | |
| 55 | // ------------------------------------------------------------------------ |
| 56 | // Constructors |
| 57 | // ------------------------------------------------------------------------ |
| 58 | /** |
| 59 | * Standard Constructor |
| 60 | * |
| 61 | * @param viewName |
| 62 | * The view name |
| 63 | */ |
| 64 | public TmfChartView(String viewName) { |
| 65 | super(viewName); |
| 66 | } |
| 67 | |
| 68 | // ------------------------------------------------------------------------ |
| 69 | // Accessors |
| 70 | // ------------------------------------------------------------------------ |
| 71 | /** |
| 72 | * Returns the TMF XY chart viewer implementation. |
| 73 | * |
| 74 | * @return the TMF XY chart viewer {@link TmfXYChartViewer} |
| 75 | */ |
| 76 | protected TmfXYChartViewer getChartViewer() { |
| 77 | return fChartViewer; |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Create the TMF XY chart viewer implementation |
| 82 | * |
| 83 | * @param parent |
| 84 | * the parent control |
| 85 | * |
| 86 | * @return The TMF XY chart viewer {@link TmfXYChartViewer} |
| 87 | * @since 1.0 |
| 88 | */ |
| 89 | abstract protected TmfXYChartViewer createChartViewer(Composite parent); |
| 90 | |
| 91 | /** |
| 92 | * Returns the ITmfTrace implementation |
| 93 | * |
| 94 | * @return the ITmfTrace implementation {@link ITmfTrace} |
| 95 | */ |
| 96 | protected ITmfTrace getTrace() { |
| 97 | return fTrace; |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Sets the ITmfTrace implementation |
| 102 | * |
| 103 | * @param trace |
| 104 | * The ITmfTrace implementation {@link ITmfTrace} |
| 105 | */ |
| 106 | protected void setTrace(ITmfTrace trace) { |
| 107 | fTrace = trace; |
| 108 | } |
| 109 | |
| 110 | // ------------------------------------------------------------------------ |
| 111 | // Operations |
| 112 | // ------------------------------------------------------------------------ |
| 113 | @Override |
| 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); |
| 123 | |
| 124 | fChartViewer = createChartViewer(fXYViewerContainer); |
| 125 | fChartViewer.setSendTimeAlignSignals(true); |
| 126 | fChartViewer.getControl().setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); |
| 127 | |
| 128 | fChartViewer.getControl().addPaintListener(new PaintListener() { |
| 129 | @Override |
| 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() { |
| 137 | |
| 138 | @Override |
| 139 | public void handleEvent(Event event) { |
| 140 | TmfSignalManager.dispatchSignal(new TmfTimeViewAlignmentSignal(fSashForm, getTimeViewAlignmentInfo())); |
| 141 | } |
| 142 | }; |
| 143 | control.removePaintListener(this); |
| 144 | control.addListener(SWT.Selection, fSashDragListener); |
| 145 | // There should be only one sash |
| 146 | break; |
| 147 | } |
| 148 | } |
| 149 | } |
| 150 | } |
| 151 | }); |
| 152 | fSashForm.setWeights(DEFAULT_WEIGHTS); |
| 153 | ITmfTrace trace = TmfTraceManager.getInstance().getActiveTrace(); |
| 154 | if (trace != null) { |
| 155 | setTrace(trace); |
| 156 | loadTrace(); |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | @Override |
| 161 | public void dispose() { |
| 162 | if (fChartViewer != null) { |
| 163 | fChartViewer.dispose(); |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | @Override |
| 168 | public void setFocus() { |
| 169 | fChartViewer.getControl().setFocus(); |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * Load the trace into view. |
| 174 | */ |
| 175 | protected void loadTrace() { |
| 176 | if (fChartViewer != null) { |
| 177 | fChartViewer.loadTrace(fTrace); |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * @since 1.0 |
| 183 | */ |
| 184 | @Override |
| 185 | public TmfTimeViewAlignmentInfo getTimeViewAlignmentInfo() { |
| 186 | if (fChartViewer == null) { |
| 187 | return null; |
| 188 | } |
| 189 | |
| 190 | return new TmfTimeViewAlignmentInfo(fChartViewer.getControl().getShell(), fSashForm.toDisplay(0, 0), getTimeAxisOffset()); |
| 191 | } |
| 192 | |
| 193 | private int getTimeAxisOffset() { |
| 194 | return fSashForm.getChildren()[0].getSize().x + fSashForm.getSashWidth() + fChartViewer.getPointAreaOffset(); |
| 195 | } |
| 196 | |
| 197 | /** |
| 198 | * @since 1.0 |
| 199 | */ |
| 200 | @Override |
| 201 | public int getAvailableWidth(int requestedOffset) { |
| 202 | if (fChartViewer == null) { |
| 203 | return 0; |
| 204 | } |
| 205 | |
| 206 | int pointAreaWidth = fChartViewer.getPointAreaWidth(); |
| 207 | int curTimeAxisOffset = getTimeAxisOffset(); |
| 208 | if (pointAreaWidth <= 0) { |
| 209 | pointAreaWidth = fSashForm.getBounds().width - curTimeAxisOffset; |
| 210 | } |
| 211 | |
| 212 | // 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 |
| 213 | int endOffset = curTimeAxisOffset + pointAreaWidth; |
| 214 | GridLayout layout = (GridLayout) fXYViewerContainer.getLayout(); |
| 215 | int endOffsetWithoutMargin = endOffset + layout.marginRight; |
| 216 | int availableWidth = endOffsetWithoutMargin - requestedOffset; |
| 217 | availableWidth = Math.min(fSashForm.getBounds().width, Math.max(0, availableWidth)); |
| 218 | return availableWidth; |
| 219 | } |
| 220 | |
| 221 | /** |
| 222 | * @since 1.0 |
| 223 | */ |
| 224 | @Override |
| 225 | public void performAlign(int offset, int width) { |
| 226 | int total = fSashForm.getBounds().width; |
| 227 | int plotAreaOffset = fChartViewer.getPointAreaOffset(); |
| 228 | int width1 = Math.max(0, offset - plotAreaOffset - fSashForm.getSashWidth()); |
| 229 | int width2 = Math.max(0, total - width1 - fSashForm.getSashWidth()); |
| 230 | fSashForm.setWeights(new int[] { width1, width2 }); |
| 231 | fSashForm.layout(); |
| 232 | |
| 233 | Composite composite = fXYViewerContainer; |
| 234 | GridLayout layout = (GridLayout) composite.getLayout(); |
| 235 | int timeAxisWidth = getAvailableWidth(offset); |
| 236 | int marginSize = timeAxisWidth - width; |
| 237 | layout.marginRight = Math.max(0, marginSize); |
| 238 | composite.layout(); |
| 239 | } |
| 240 | } |