    001/* ===========================================================
    002 * Orson Charts : a 3D chart library for the Java(tm) platform
    003 * ===========================================================
    004 *
    005 * (C)opyright 2013-2016, by Object Refinery Limited.  All rights reserved.
    006 *
    007 * http://www.object-refinery.com/orsoncharts/index.html
    008 *
    009 * This program is free software: you can redistribute it and/or modify
    010 * it under the terms of the GNU General Public License as published by
    011 * the Free Software Foundation, either version 3 of the License, or
    012 * (at your option) any later version.
    013 *
    014 * This program is distributed in the hope that it will be useful,
    015 * but WITHOUT ANY WARRANTY; without even the implied warranty of
    016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    017 * GNU General Public License for more details.
    018 *
    019 * You should have received a copy of the GNU General Public License
    020 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    021 *
    022 * [Oracle and Java are registered trademarks of Oracle and/or its affiliates.
    023 * Other names may be trademarks of their respective owners.]
    024 *
    025 * If you do not wish to be bound by the terms of the GPL, an alternative
    026 * commercial license can be purchased.  For details, please see visit the
    027 * Orson Charts home page:
    028 *
    029 * http://www.object-refinery.com/orsoncharts/index.html
    030 *
    031 */
    032
    033package com.orsoncharts.label;
    034
    035import java.io.Serializable;
    036import java.util.Formatter;
    037
    038import com.orsoncharts.data.category.CategoryDataset3D;
    039import com.orsoncharts.data.KeyedValues3DItemKey;
    040import com.orsoncharts.interaction.KeyedValues3DItemSelection;
    041import com.orsoncharts.interaction.StandardKeyedValues3DItemSelection;
    042import com.orsoncharts.util.ArgChecks;
    043import com.orsoncharts.util.ObjectUtils;
    044
    045/**
    046 * A default implementation of the {@link CategoryItemLabelGenerator} interface.
    047 * The implementation uses a {@link java.util.Formatter} instance to generate
    048 * the labels.  Four values are passed to the formatter: (1) the key for
    049 * the series, (2) the row key, (3) the column key and (4) the data value.
    050 * <br><br>
    051 * NOTE: This class is serializable, but the serialization format is subject
    052 * to change in future releases and should not be relied upon for persisting
    053 * instances of this class.
    054 *
    055 * @since 1.3
    056 */
    057@SuppressWarnings("serial")
    058public class StandardCategoryItemLabelGenerator
    059        implements CategoryItemLabelGenerator, Serializable {
    060
    061    /**
    062     * A template string that will show just the value (to 2 decimal places).
    063     *
    064     * @since 1.3
    065     */
    066    public static final String VALUE_TEMPLATE = "%4$.2f";
    067
    068    /**
    069     * A template string that will show the series, row and column keys plus
    070     * the data value.
    071     *
    072     * @since 1.3
    073     */
    074    public static final String KEYS_AND_VALUE_TEMPLATE = "%s, %s, %s = %4$.3f";
    075
    076    /**
    077     * A template that shows the series key, column key and value (the row
    078     * key is omitted because it is often the same as the series key).
    079     *
    080     * @since 1.3
    081     */
    082    public static final String SERIES_AND_COLUMN_KEYS_AND_VALUE_TEMPLATE
    083            = "%1$s, %3$s = %4$.3f";
    084
    085    /**
    086     * The default template string (used in the default constructor, it is
    087     * equivalent to {@link #SERIES_AND_COLUMN_KEYS_AND_VALUE_TEMPLATE}).
    088     *
    089     * @since 1.3
    090     */
    091    public static final String DEFAULT_TEMPLATE
    092            = SERIES_AND_COLUMN_KEYS_AND_VALUE_TEMPLATE;
    093
    094    /** The template. */
    095    private String template;
    096
    097    /**
    098     * If this object is not-{@code null}, an item label will only be
    099     * returned by this generator if the selection contains the item that
    100     * the label is to be generated for.
    101     */
    102    private KeyedValues3DItemSelection itemSelection;
    103
    104    /**
    105     * The default constructor.
    106     */
    107    public StandardCategoryItemLabelGenerator() {
    108        this(DEFAULT_TEMPLATE);
    109    }
    110
    111    /**
    112     * Creates a new instance with the specified template string (which will
    113     * be passed to a {@code java.util.Formatter} instance when generating
    114     * labels).  See the class description for an explanation of the values
    115     * that are available for use in the template string.
    116     *
    117     * @param template  the template ({@code null} not permitted).
    118     */
    119    public StandardCategoryItemLabelGenerator(String template) {
    120        ArgChecks.nullNotPermitted(template, "template");
    121        this.template = template;
    122        this.itemSelection = null;
    123    }
    124
    125    /**
    126     * Returns the item selection.  The default value is {@code null}.
    127     *
    128     * @return The item selection.
    129     *
    130     * @since 1.3
    131     */
    132    public KeyedValues3DItemSelection getItemSelection() {
    133        return this.itemSelection;
    134    }
    135
    136    /**
    137     * Sets the item selection (labels will be created by this generator only
    138     * for data items that are contained in the collection).  If you set the
    139     * selection to {@code null} then the generator will create labels for
    140     * all data items.
    141     *
    142     * @param selection  the selection ({@code null} permitted).
    143     */
    144    public void setItemSelection(StandardKeyedValues3DItemSelection selection) {
    145        this.itemSelection = selection;
    146    }
    147
    148    /**
    149     * Generates the item label for one data item in a category chart.
    150     *
    151     * @param dataset  the dataset ({@code null} not permitted).
    152     * @param seriesKey  the series key ({@code null} not permitted).
    153     * @param rowKey  the row key ({@code null} not permitted).
    154     * @param columnKey  the column key ({@code null} not permitted).
    155     *
    156     * @return The label (never {@code null} for this implementation).
    157     */
    158    @Override
    159    public String generateItemLabel(CategoryDataset3D dataset,
    160            Comparable<?> seriesKey, Comparable<?> rowKey,
    161            Comparable<?> columnKey) {
    162        ArgChecks.nullNotPermitted(dataset, "dataset");
    163        ArgChecks.nullNotPermitted(seriesKey, "seriesKey");
    164        ArgChecks.nullNotPermitted(rowKey, "rowKey");
    165        ArgChecks.nullNotPermitted(columnKey, "columnKey");
    166        if (this.itemSelection != null) {
    167            KeyedValues3DItemKey key = new KeyedValues3DItemKey(seriesKey,
    168                    rowKey, columnKey);
    169            if (!this.itemSelection.isSelected(key)) {
    170                return null;
    171            }
    172        }
    173        Formatter formatter = new Formatter(new StringBuilder());
    174        Number value = dataset.getValue(seriesKey, rowKey, columnKey);
    175        Double d = null;
    176        if (value != null) {
    177            d = Double.valueOf(value.doubleValue());
    178        }
    179        formatter.format(this.template, seriesKey, rowKey, columnKey, d);
    180        String result = formatter.toString();
    181        formatter.close();
    182        return result;
    183    }
    184
    185    /**
    186     * Tests this label generator for equality with an arbitrary object.
    187     *
    188     * @param obj  the object ({@code null} permitted).
    189     *
    190     * @return A boolean.
    191     */
    192    @Override
    193    public boolean equals(Object obj) {
    194        if (obj == this) {
    195            return true;
    196        }
    197        if (!(obj instanceof StandardCategoryItemLabelGenerator)) {
    198            return false;
    199        }
    200        StandardCategoryItemLabelGenerator that
    201                = (StandardCategoryItemLabelGenerator) obj;
    202        if (!this.template.equals(that.template)) {
    203            return false;
    204        }
    205        if (!ObjectUtils.equals(this.itemSelection, that.itemSelection)) {
    206            return false;
    207        }
    208        return true;
    209    }
    210
    211    @Override
    212    public int hashCode() {
    213        return this.template.hashCode();
    214    }
    215
    216}