1 /*******************************************************************************
2 * Copyright (c) 2016 École Polytechnique de Montréal
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 *******************************************************************************/
10 package org
.eclipse
.tracecompass
.internal
.tmf
.chart
.ui
.format
;
12 import java
.text
.FieldPosition
;
13 import java
.text
.Format
;
14 import java
.text
.ParsePosition
;
16 import org
.eclipse
.jdt
.annotation
.Nullable
;
18 import com
.google
.common
.collect
.BiMap
;
21 * Format label based on a given Map<String, Integer>.
23 * TODO: See if this formatter is specific to the swtchart charts that are
24 * implemented in this plugin or if they can be re-used in another other
25 * charting scheme. We'll probably know when we actually have another
26 * implementation. If swtchart specific, the name of the class and package
27 * should make it clear.
29 * @author Jonathan Rajotte-Julien
30 * @author Gabriel-Andrew Pollo-Guilbert
32 public class LabelFormat
extends Format
{
34 // ------------------------------------------------------------------------
36 // ------------------------------------------------------------------------
38 private static final long serialVersionUID
= -7046922375212377647L;
39 private static final String CHART_EMPTY_LABEL
= " "; //$NON-NLS-1$
41 // ------------------------------------------------------------------------
43 // ------------------------------------------------------------------------
45 private final BiMap
<String
, Integer
> fMap
;
47 // ------------------------------------------------------------------------
49 // ------------------------------------------------------------------------
55 * Map of labels and indices
57 public LabelFormat(BiMap
<String
, Integer
> map
) {
63 // ------------------------------------------------------------------------
65 // ------------------------------------------------------------------------
68 public @Nullable StringBuffer
format(@Nullable Object obj
, @Nullable StringBuffer toAppendTo
, @Nullable FieldPosition pos
) {
69 if (obj
== null || toAppendTo
== null) {
70 return new StringBuffer(CHART_EMPTY_LABEL
);
73 Double doubleObj
= (Double
) obj
;
75 /* If the key is not contained in the map, format a empty label */
76 if ((doubleObj
% 1 != 0) || !fMap
.containsValue((doubleObj
.intValue()))) {
77 return new StringBuffer(CHART_EMPTY_LABEL
);
80 /* If the value is null in the map, format an unknown label */
81 String key
= fMap
.inverse().get(doubleObj
.intValue());
83 return toAppendTo
.append(key
);
87 public @Nullable Object
parseObject(@Nullable String source
, @Nullable ParsePosition pos
) {
88 return fMap
.get(source
);
This page took 0.033878 seconds and 5 git commands to generate.