001/* ===========================================================
002 * Orson Charts : a 3D chart library for the Java(tm) platform
003 * ===========================================================
004 * 
005 * (C)opyright 2013-2022, by David Gilbert.  All rights reserved.
006 * 
007 * https://github.com/jfree/orson-charts
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 org.jfree.chart3d.label;
034
035import java.io.Serializable;
036import java.util.Formatter;
037
038import org.jfree.chart3d.data.KeyedValues3DItemKey;
039import org.jfree.chart3d.data.category.CategoryDataset3D;
040import org.jfree.chart3d.interaction.KeyedValues3DItemSelection;
041import org.jfree.chart3d.interaction.StandardKeyedValues3DItemSelection;
042import org.jfree.chart3d.internal.Args;
043import org.jfree.chart3d.internal.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        Args.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    @SuppressWarnings("unchecked")
160    public String generateItemLabel(CategoryDataset3D dataset, 
161            Comparable<?> seriesKey, Comparable<?> rowKey, 
162            Comparable<?> columnKey) {
163        Args.nullNotPermitted(dataset, "dataset");
164        Args.nullNotPermitted(seriesKey, "seriesKey");
165        Args.nullNotPermitted(rowKey, "rowKey");
166        Args.nullNotPermitted(columnKey, "columnKey");
167        if (this.itemSelection != null) {
168            KeyedValues3DItemKey key = new KeyedValues3DItemKey(seriesKey, 
169                    rowKey, columnKey);
170            if (!this.itemSelection.isSelected(key)) {
171                return null;
172            }
173        }
174        Formatter formatter = new Formatter(new StringBuilder());
175        Number value = (Number) dataset.getValue(seriesKey, rowKey, columnKey);
176        Double d = null;
177        if (value != null) {
178            d = value.doubleValue();
179        }
180        formatter.format(this.template, seriesKey, rowKey, columnKey, d);
181        String result = formatter.toString();
182        formatter.close();
183        return result;
184    }
185 
186    /**
187     * Tests this label generator for equality with an arbitrary object.
188     * 
189     * @param obj  the object ({@code null} permitted).
190     * 
191     * @return A boolean. 
192     */
193    @Override
194    public boolean equals(Object obj) {
195        if (obj == this) {
196            return true;
197        }
198        if (!(obj instanceof StandardCategoryItemLabelGenerator)) {
199            return false;
200        }
201        StandardCategoryItemLabelGenerator that 
202                = (StandardCategoryItemLabelGenerator) obj;
203        if (!this.template.equals(that.template)) {
204            return false;
205        }
206        if (!ObjectUtils.equals(this.itemSelection, that.itemSelection)) {
207            return false;
208        }
209        return true;
210    }
211
212    @Override
213    public int hashCode() {
214        return this.template.hashCode();
215    }
216
217}