custom charts: Add formatter for the charts
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.chart.ui / src / org / eclipse / tracecompass / internal / tmf / chart / ui / format / ChartDecimalUnitFormat.java
1 /*******************************************************************************
2 * Copyright (c) 2016 EfficiOS Inc., Jonathan Rajotte-Julien
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
10 package org.eclipse.tracecompass.internal.tmf.chart.ui.format;
11
12 import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;
13
14 import java.text.FieldPosition;
15
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.eclipse.tracecompass.common.core.format.DecimalUnitFormat;
18 import org.eclipse.tracecompass.internal.tmf.chart.ui.data.ChartRange;
19 import org.eclipse.tracecompass.internal.tmf.chart.ui.data.ChartRangeMap;
20
21 /**
22 * Decimal formatter for graph
23 *
24 * Since the graph use normalized internal value the initial (external)
25 * representation needs to be obtained. Subsequent formatting is done based on a
26 * Double. Loss of precision could occurs based on the size. For now, loss of
27 * precision for decimal values is not a big concern. If it ever become one the
28 * use of Long while formatting might come in handy.
29 *
30 * @author Jonathan Rajotte-Julien
31 * @author Gabriel-Andrew Pollo-Guilbert
32 */
33 public class ChartDecimalUnitFormat extends DecimalUnitFormat {
34
35 // ------------------------------------------------------------------------
36 // Constants
37 // ------------------------------------------------------------------------
38
39 private static final long serialVersionUID = -4288059349658845257L;
40
41 // ------------------------------------------------------------------------
42 // Members
43 // ------------------------------------------------------------------------
44
45 private @Nullable ChartRangeMap fRangeMap;
46
47 // ------------------------------------------------------------------------
48 // Constructors
49 // ------------------------------------------------------------------------
50
51 /**
52 * Constructor with a range map supplied.
53 *
54 * @param map
55 * A chart range map for mapping values
56 */
57 public ChartDecimalUnitFormat(@Nullable ChartRangeMap map) {
58 super();
59 fRangeMap = map;
60 }
61
62 /**
63 * Constructor with a multiplication factor and range map.
64 *
65 * @param factor
66 * A multiplication factor to apply to the value
67 * @param map
68 * A chart range map for mapping values
69 */
70 public ChartDecimalUnitFormat(double factor, @Nullable ChartRangeMap map) {
71 super(factor);
72 fRangeMap = map;
73 }
74
75 // ------------------------------------------------------------------------
76 // Mutators
77 // ------------------------------------------------------------------------
78
79 /**
80 * Mutators that sets the chart range map of this formatter.
81 *
82 * @param map
83 * The new chart range map
84 */
85 public void setRangeMap(ChartRangeMap map) {
86 fRangeMap = map;
87 }
88
89 // ------------------------------------------------------------------------
90 // Operations
91 // ------------------------------------------------------------------------
92
93 @Override
94 public StringBuffer format(@Nullable Object obj, @Nullable StringBuffer toAppendTo, @Nullable FieldPosition pos) {
95 if (!(obj instanceof Number) || toAppendTo == null) {
96 throw new IllegalArgumentException("Cannot format given Object as a Number: " + obj); //$NON-NLS-1$
97 }
98
99 Number number = (Number) obj;
100
101 /* If no map was provided, format with the number unchanged */
102 ChartRangeMap rangeMap = fRangeMap;
103 if (rangeMap == null) {
104 StringBuffer buffer = super.format(number, toAppendTo, pos);
105 return (buffer == null ? new StringBuffer() : buffer);
106 }
107
108 ChartRange internalRange = rangeMap.getPlottedRange();
109 ChartRange externalRange = rangeMap.getInputDataRange();
110
111 /* If any range's delta is null, format with the external bounds */
112 if (internalRange.isDeltaNull() || externalRange.isDeltaNull()) {
113 StringBuffer buffer = super.format(externalRange.getMinimum().doubleValue(), toAppendTo, pos);
114 return (buffer == null ? new StringBuffer() : buffer);
115 }
116
117 /* Find external value before formatting */
118 Double externalValue = checkNotNull(fRangeMap).getExternalValue(number).doubleValue();
119 StringBuffer buffer = super.format(externalValue, toAppendTo, pos);
120 return (buffer == null ? new StringBuffer() : buffer);
121 }
122
123 }
This page took 0.037442 seconds and 5 git commands to generate.