Commit | Line | Data |
---|---|---|
0b09e0cf | 1 | /********************************************************************** |
60ae41e1 | 2 | * Copyright (c) 2013, 2014 Ericsson |
0b09e0cf 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 | **********************************************************************/ | |
12 | package org.eclipse.linuxtools.tmf.ui.viewers.xycharts; | |
13 | ||
14 | import org.swtchart.Chart; | |
15 | import org.swtchart.IAxis; | |
16 | ||
17 | /** | |
18 | * Base class for any provider such as tool tip, zoom and selection providers. | |
19 | * | |
20 | * @author Bernd Hufmann | |
21 | * @since 3.0 | |
22 | */ | |
23 | abstract public class TmfBaseProvider { | |
24 | ||
25 | // ------------------------------------------------------------------------ | |
26 | // Attributes | |
27 | // ------------------------------------------------------------------------ | |
28 | /** Reference to the chart viewer */ | |
29 | private final ITmfChartTimeProvider fChartViewer; | |
30 | ||
31 | // ------------------------------------------------------------------------ | |
32 | // Constructors | |
33 | // ------------------------------------------------------------------------ | |
34 | ||
35 | /** | |
36 | * Standard constructor. | |
37 | * | |
38 | * @param tmfChartViewer | |
39 | * The parent histogram object | |
40 | */ | |
41 | public TmfBaseProvider(ITmfChartTimeProvider tmfChartViewer) { | |
42 | fChartViewer = tmfChartViewer; | |
43 | } | |
44 | ||
45 | // ------------------------------------------------------------------------ | |
46 | // Accessors | |
47 | // ------------------------------------------------------------------------ | |
48 | /** | |
49 | * Returns the chart viewer reference. | |
50 | * @return the chart viewer reference | |
51 | */ | |
52 | public ITmfChartTimeProvider getChartViewer() { | |
53 | return fChartViewer; | |
54 | } | |
55 | ||
56 | /** | |
57 | * Returns the SWT chart reference | |
58 | * | |
59 | * @return SWT chart reference. | |
60 | */ | |
61 | protected Chart getChart() { | |
62 | return (Chart) fChartViewer.getControl(); | |
63 | } | |
64 | ||
65 | /** | |
66 | * Limits x data coordinate to window start and window end range | |
67 | * | |
68 | * @param x | |
69 | * x to limit | |
70 | * @return x if x >= begin && x <= end | |
71 | * begin if x < begin | |
72 | * end if x > end | |
73 | */ | |
74 | protected long limitXDataCoordinate(double x) { | |
75 | ITmfChartTimeProvider viewer = getChartViewer(); | |
76 | long windowStartTime = viewer.getWindowStartTime() - viewer.getTimeOffset(); | |
77 | long windowEndTime = viewer.getWindowEndTime() - viewer.getTimeOffset(); | |
78 | ||
79 | if (x < windowStartTime) { | |
80 | return windowStartTime; | |
81 | } | |
82 | ||
83 | if (x > windowEndTime) { | |
84 | return windowEndTime; | |
85 | } | |
86 | ||
87 | return (long) x; | |
88 | } | |
89 | ||
90 | /** | |
91 | * Limits x pixel coordinate to window start and window end range | |
92 | * | |
93 | * @param axisIndex | |
94 | * index of x-axis | |
95 | * @param x | |
96 | * x to limit | |
97 | * @return x if x >= begin && x <= end | |
98 | * begin if x < begin | |
99 | * end if x > end | |
100 | */ | |
101 | protected int limitXPixelCoordinate(int axisIndex, int x) { | |
102 | ITmfChartTimeProvider viewer = getChartViewer(); | |
103 | long windowStartTime = viewer.getWindowStartTime() - viewer.getTimeOffset(); | |
104 | long windowEndTime = viewer.getWindowEndTime() - viewer.getTimeOffset(); | |
105 | ||
106 | IAxis xAxis = getChart().getAxisSet().getXAxis(0); | |
107 | int startX = xAxis.getPixelCoordinate(windowStartTime); | |
108 | if (x < startX) { | |
109 | return startX; | |
110 | } | |
111 | ||
112 | int endX = xAxis.getPixelCoordinate(windowEndTime); | |
113 | if (x > endX) { | |
114 | return endX; | |
115 | } | |
116 | ||
117 | return x; | |
118 | } | |
119 | ||
120 | // ------------------------------------------------------------------------ | |
121 | // Operations | |
122 | // ------------------------------------------------------------------------ | |
123 | /** | |
124 | * Method deregisters provider from chart viewer. Subclasses may override this method | |
125 | * to dispose any resources. | |
126 | */ | |
127 | public void dispose() { | |
128 | deregister(); | |
129 | } | |
130 | ||
131 | /** | |
132 | * Method to refresh the viewer. It will redraw the viewer. | |
133 | */ | |
134 | public void refresh() { | |
135 | if (!TmfXYChartViewer.getDisplay().isDisposed()) { | |
136 | TmfXYChartViewer.getDisplay().asyncExec(new Runnable() { | |
137 | @Override | |
138 | public void run() { | |
139 | getChart().redraw(); | |
140 | } | |
141 | }); | |
142 | } | |
143 | } | |
144 | ||
145 | /** | |
146 | * Method to register the provider to chart viewer. | |
147 | */ | |
83842d7d | 148 | protected abstract void register(); |
0b09e0cf BH |
149 | |
150 | /** | |
151 | * Method to deregister the provider from chart viewer. | |
152 | */ | |
83842d7d | 153 | protected abstract void deregister(); |
0b09e0cf BH |
154 | |
155 | } |