001/* =========================================================== 002 * JFreeChart : a free chart library for the Java(tm) platform 003 * =========================================================== 004 * 005 * (C) Copyright 2000-present, by David Gilbert and Contributors. 006 * 007 * Project Info: http://www.jfree.org/jfreechart/index.html 008 * 009 * This library is free software; you can redistribute it and/or modify it 010 * under the terms of the GNU Lesser General Public License as published by 011 * the Free Software Foundation; either version 2.1 of the License, or 012 * (at your option) any later version. 013 * 014 * This library is distributed in the hope that it will be useful, but 015 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 016 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 017 * License for more details. 018 * 019 * You should have received a copy of the GNU Lesser General Public 020 * License along with this library; if not, write to the Free Software 021 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 022 * USA. 023 * 024 * [Oracle and Java are registered trademarks of Oracle and/or its affiliates. 025 * Other names may be trademarks of their respective owners.] 026 * 027 * ------------------------------------ 028 * StandardCrosshairLabelGenerator.java 029 * ------------------------------------ 030 * (C) Copyright 2009-present, by David Gilbert. 031 * 032 * Original Author: David Gilbert; 033 * Contributor(s): -; 034 * 035 */ 036 037package org.jfree.chart.labels; 038 039import java.io.Serializable; 040import java.text.MessageFormat; 041import java.text.NumberFormat; 042import org.jfree.chart.plot.Crosshair; 043 044/** 045 * A default label generator. 046 */ 047public class StandardCrosshairLabelGenerator implements CrosshairLabelGenerator, 048 Serializable { 049 050 /** The label format string. */ 051 private String labelTemplate; 052 053 /** A number formatter for the value. */ 054 private NumberFormat numberFormat; 055 056 /** 057 * Creates a new instance with default attributes. 058 */ 059 public StandardCrosshairLabelGenerator() { 060 this("{0}", NumberFormat.getNumberInstance()); 061 } 062 063 /** 064 * Creates a new instance with the specified attributes. 065 * 066 * @param labelTemplate the label template ({@code null} not 067 * permitted). 068 * @param numberFormat the number formatter ({@code null} not 069 * permitted). 070 */ 071 public StandardCrosshairLabelGenerator(String labelTemplate, 072 NumberFormat numberFormat) { 073 super(); 074 if (labelTemplate == null) { 075 throw new IllegalArgumentException( 076 "Null 'labelTemplate' argument."); 077 } 078 if (numberFormat == null) { 079 throw new IllegalArgumentException( 080 "Null 'numberFormat' argument."); 081 } 082 this.labelTemplate = labelTemplate; 083 this.numberFormat = numberFormat; 084 } 085 086 /** 087 * Returns the label template string. 088 * 089 * @return The label template string (never {@code null}). 090 */ 091 public String getLabelTemplate() { 092 return this.labelTemplate; 093 } 094 095 /** 096 * Returns the number formatter. 097 * 098 * @return The formatter (never {@code null}). 099 */ 100 public NumberFormat getNumberFormat() { 101 return this.numberFormat; 102 } 103 104 /** 105 * Returns a string that can be used as the label for a crosshair. 106 * 107 * @param crosshair the crosshair ({@code null} not permitted). 108 * 109 * @return The label (possibly {@code null}). 110 */ 111 @Override 112 public String generateLabel(Crosshair crosshair) { 113 Object[] v = new Object[] {this.numberFormat.format( 114 crosshair.getValue())}; 115 String result = MessageFormat.format(this.labelTemplate, v); 116 return result; 117 } 118 119 /** 120 * Tests this generator for equality with an arbitrary object. 121 * 122 * @param obj the object ({@code null} permitted). 123 * 124 * @return A boolean. 125 */ 126 @Override 127 public boolean equals(Object obj) { 128 if (obj == this) { 129 return true; 130 } 131 if (!(obj instanceof StandardCrosshairLabelGenerator)) { 132 return false; 133 } 134 StandardCrosshairLabelGenerator that 135 = (StandardCrosshairLabelGenerator) obj; 136 if (!this.labelTemplate.equals(that.labelTemplate)) { 137 return false; 138 } 139 if (!this.numberFormat.equals(that.numberFormat)) { 140 return false; 141 } 142 return true; 143 } 144 145 /** 146 * Returns a hash code for this instance. 147 * 148 * @return A hash code for this instance. 149 */ 150 @Override 151 public int hashCode() { 152 return this.labelTemplate.hashCode(); 153 } 154 155}