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 / ChartTimeStampFormat.java
1 /*******************************************************************************
2 * Copyright (c) 2015, 2016 EfficiOS Inc. and others
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.math.BigDecimal;
15 import java.text.FieldPosition;
16 import java.text.Format;
17 import java.text.ParsePosition;
18
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.eclipse.tracecompass.internal.tmf.chart.ui.data.ChartRange;
21 import org.eclipse.tracecompass.internal.tmf.chart.ui.data.ChartRangeMap;
22 import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimestampFormat;
23
24 /**
25 * Formatter for timestamps.
26 *
27 * @author Michael Jeanson
28 * @author Gabriel-Andrew Pollo-Guilbert
29 */
30 public class ChartTimeStampFormat extends Format {
31
32 // ------------------------------------------------------------------------
33 // Constants
34 // ------------------------------------------------------------------------
35
36 private static final long serialVersionUID = 8102026791684954897L;
37
38 // ------------------------------------------------------------------------
39 // Members
40 // ------------------------------------------------------------------------
41
42 private final TmfTimestampFormat fFormat;
43 private @Nullable ChartRangeMap fRangeMap;
44
45 // ------------------------------------------------------------------------
46 // Constructors
47 // ------------------------------------------------------------------------
48
49 /**
50 * Constructor with a range map supplied.
51 *
52 * @param map
53 * A internal and external ranges map
54 */
55 public ChartTimeStampFormat(@Nullable ChartRangeMap map) {
56 fFormat = checkNotNull(TmfTimestampFormat.getDefaulTimeFormat());
57 fRangeMap = map;
58 }
59
60 /**
61 * Constructor with a pattern and a range map supplied.
62 *
63 * @param pattern
64 * The format pattern
65 * @param map
66 * A chart range map for mapping values
67 */
68 public ChartTimeStampFormat(String pattern, @Nullable ChartRangeMap map) {
69 fFormat = new TmfTimestampFormat(pattern);
70 fRangeMap = map;
71 }
72
73 // ------------------------------------------------------------------------
74 // Mutators
75 // ------------------------------------------------------------------------
76
77 /**
78 * Mutators that sets the chart range map of this formatter.
79 *
80 * @param map
81 * The new chart range map
82 */
83 public void setRangeMap(ChartRangeMap map) {
84 fRangeMap = map;
85 }
86
87 // ------------------------------------------------------------------------
88 // Operations
89 // ------------------------------------------------------------------------
90
91 @Override
92 public StringBuffer format(@Nullable Object obj, @Nullable StringBuffer toAppendTo, @Nullable FieldPosition pos) {
93 if (!(obj instanceof Number) || toAppendTo == null) {
94 throw new IllegalArgumentException("Cannot format given Object as a Number: " + obj); //$NON-NLS-1$
95 }
96
97 Number number = (Number) obj;
98
99 /* If no range was provided, format with the number unchanged */
100 ChartRangeMap rangeMap = fRangeMap;
101 if (rangeMap == null) {
102 long time = ((Number) obj).longValue();
103 return checkNotNull(toAppendTo.append(fFormat.format(time)));
104 }
105
106 ChartRange internalRange = rangeMap.getPlottedRange();
107 ChartRange externalRange = rangeMap.getInputDataRange();
108
109 /* If any range's delta is null, format with the external bounds */
110 if (internalRange.isDeltaNull() || externalRange.isDeltaNull()) {
111 return checkNotNull(toAppendTo.append(fFormat.format(externalRange.getMinimum().doubleValue())));
112 }
113
114 /* Find external value before formatting */
115 BigDecimal externalValue = checkNotNull(fRangeMap).getExternalValue(number);
116 return checkNotNull(toAppendTo.append(fFormat.format(externalValue.longValue())));
117 }
118
119 @Override
120 public @Nullable Object parseObject(@Nullable String source, @Nullable ParsePosition pos) {
121 return null;
122 }
123
124 }
This page took 0.036104 seconds and 5 git commands to generate.