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 / LabelFormat.java
1 /*******************************************************************************
2 * Copyright (c) 2016 École Polytechnique de Montréal
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 java.text.FieldPosition;
13 import java.text.Format;
14 import java.text.ParsePosition;
15
16 import org.eclipse.jdt.annotation.Nullable;
17
18 import com.google.common.collect.BiMap;
19
20 /**
21 * Format label based on a given Map<String, Integer>.
22 *
23 * @author Jonathan Rajotte-Julien
24 * @author Gabriel-Andrew Pollo-Guilbert
25 */
26 public class LabelFormat extends Format {
27
28 // ------------------------------------------------------------------------
29 // Constants
30 // ------------------------------------------------------------------------
31
32 private static final long serialVersionUID = -7046922375212377647L;
33 private static final String CHART_EMPTY_LABEL = " "; //$NON-NLS-1$
34
35 // ------------------------------------------------------------------------
36 // Members
37 // ------------------------------------------------------------------------
38
39 private final BiMap<String, Integer> fMap;
40
41 // ------------------------------------------------------------------------
42 // Constructors
43 // ------------------------------------------------------------------------
44
45 /**
46 * Constructor.
47 *
48 * @param map
49 * Map of labels and indices
50 */
51 public LabelFormat(BiMap<String, Integer> map) {
52 super();
53
54 fMap = map;
55 }
56
57 // ------------------------------------------------------------------------
58 // Operations
59 // ------------------------------------------------------------------------
60
61 @Override
62 public @Nullable StringBuffer format(@Nullable Object obj, @Nullable StringBuffer toAppendTo, @Nullable FieldPosition pos) {
63 if (obj == null || toAppendTo == null) {
64 return new StringBuffer(CHART_EMPTY_LABEL);
65 }
66
67 Double doubleObj = (Double) obj;
68
69 /* If the key is not contained in the map, format a empty label */
70 if ((doubleObj % 1 != 0) || !fMap.containsValue((doubleObj.intValue()))) {
71 return new StringBuffer(CHART_EMPTY_LABEL);
72 }
73
74 /* If the value is null in the map, format an unknown label */
75 String key = fMap.inverse().get(doubleObj.intValue());
76
77 return toAppendTo.append(key);
78 }
79
80 @Override
81 public @Nullable Object parseObject(@Nullable String source, @Nullable ParsePosition pos) {
82 return fMap.get(source);
83 }
84
85 }
This page took 0.032662 seconds and 5 git commands to generate.