Update default weight for the SashForm in time-aligned views
[deliverable/tracecompass.git] / org.eclipse.tracecompass.tmf.ui / src / org / eclipse / tracecompass / tmf / ui / views / TmfChartView.java
1 /**********************************************************************
2 * Copyright (c) 2013, 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 * 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 /**
168 * Load the trace into view.
169 */
170 protected void loadTrace() {
171 if (fChartViewer != null) {
172 fChartViewer.loadTrace(fTrace);
173 }
174 }
175
176 /**
177 * @since 1.0
178 */
179 @Override
180 public TmfTimeViewAlignmentInfo getTimeViewAlignmentInfo() {
181 if (fChartViewer == null) {
182 return null;
183 }
184
185 return new TmfTimeViewAlignmentInfo(fChartViewer.getControl().getShell(), fSashForm.toDisplay(0, 0), getTimeAxisOffset());
186 }
187
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;
193 }
194
195 /**
196 * @since 1.0
197 */
198 @Override
199 public int getAvailableWidth(int requestedOffset) {
200 if (fChartViewer == null) {
201 return 0;
202 }
203
204 int pointAreaWidth = fChartViewer.getPointAreaWidth();
205 int curTimeAxisOffset = getTimeAxisOffset();
206 if (pointAreaWidth <= 0) {
207 pointAreaWidth = fSashForm.getBounds().width - curTimeAxisOffset;
208 }
209
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;
217 }
218
219 /**
220 * @since 1.0
221 */
222 @Override
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 });
230 fSashForm.layout();
231
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);
237 composite.layout();
238 }
239 }
This page took 0.051204 seconds and 5 git commands to generate.