001/* =========================================================== 002 * JFreeChart : a free chart library for the Java(tm) platform 003 * =========================================================== 004 * 005 * (C) Copyright 2000-2022, 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 * CustomXYToolTipGenerator.java 029 * ----------------------------- 030 * (C) Copyright 2002-2020, by Richard Atkinson and Contributors. 031 * 032 * Original Author: Richard Atkinson; 033 * Contributor(s): David Gilbert; 034 * 035 */ 036 037package org.jfree.chart.labels; 038 039import java.io.Serializable; 040import java.util.ArrayList; 041import java.util.List; 042import org.jfree.chart.api.PublicCloneable; 043 044import org.jfree.data.xy.XYDataset; 045 046/** 047 * A tool tip generator that stores custom tooltips. The dataset passed into 048 * the generateToolTip method is ignored. 049 */ 050public class CustomXYToolTipGenerator implements XYToolTipGenerator, 051 Cloneable, PublicCloneable, Serializable { 052 053 /** For serialization. */ 054 private static final long serialVersionUID = 8636030004670141362L; 055 056 /** Storage for the tooltip lists. */ 057 private List<List<String>> toolTipSeries = new ArrayList<>(); 058 059 /** 060 * Default constructor. 061 */ 062 public CustomXYToolTipGenerator() { 063 super(); 064 } 065 066 /** 067 * Returns the number of tool tip lists stored by the renderer. 068 * 069 * @return The list count. 070 */ 071 public int getListCount() { 072 return this.toolTipSeries.size(); 073 } 074 075 /** 076 * Returns the number of tool tips in a given list. 077 * 078 * @param list the list index (zero based). 079 * 080 * @return The tooltip count. 081 */ 082 public int getToolTipCount(int list) { 083 084 int result = 0; 085 List<String> tooltips = this.toolTipSeries.get(list); 086 if (tooltips != null) { 087 result = tooltips.size(); 088 } 089 return result; 090 } 091 092 /** 093 * Returns the tool tip text for an item. 094 * 095 * @param series the series index. 096 * @param item the item index. 097 * 098 * @return The tool tip text (possibly {@code null}). 099 */ 100 public String getToolTipText(int series, int item) { 101 String result = null; 102 if (series < getListCount()) { 103 List<String> tooltips = this.toolTipSeries.get(series); 104 if (tooltips != null) { 105 if (item < tooltips.size()) { 106 result = tooltips.get(item); 107 } 108 } 109 } 110 return result; 111 } 112 113 /** 114 * Adds a list of tooltips for a series. 115 * 116 * @param toolTips the list of tool tips. 117 */ 118 public void addToolTipSeries(List<String> toolTips) { 119 this.toolTipSeries.add(toolTips); 120 } 121 122 /** 123 * Generates a tool tip text item for a particular item within a series. 124 * 125 * @param data the dataset (ignored in this implementation). 126 * @param series the series (zero-based index). 127 * @param item the item (zero-based index). 128 * 129 * @return The tooltip text. 130 */ 131 @Override 132 public String generateToolTip(XYDataset data, int series, int item) { 133 return getToolTipText(series, item); 134 } 135 136 /** 137 * Returns an independent copy of the generator. 138 * 139 * @return A clone. 140 * 141 * @throws CloneNotSupportedException if cloning is not supported. 142 */ 143 @Override 144 public Object clone() throws CloneNotSupportedException { 145 CustomXYToolTipGenerator clone 146 = (CustomXYToolTipGenerator) super.clone(); 147 if (this.toolTipSeries != null) { 148 clone.toolTipSeries = new java.util.ArrayList(this.toolTipSeries); 149 } 150 return clone; 151 } 152 153 /** 154 * Tests if this object is equal to another. 155 * 156 * @param obj the other object. 157 * 158 * @return A boolean. 159 */ 160 @Override 161 public boolean equals(Object obj) { 162 if (obj == this) { 163 return true; 164 } 165 if (obj instanceof CustomXYToolTipGenerator) { 166 CustomXYToolTipGenerator generator = (CustomXYToolTipGenerator) obj; 167 boolean result = true; 168 for (int series = 0; series < getListCount(); series++) { 169 for (int item = 0; item < getToolTipCount(series); item++) { 170 String t1 = getToolTipText(series, item); 171 String t2 = generator.getToolTipText(series, item); 172 if (t1 != null) { 173 result = result && t1.equals(t2); 174 } 175 else { 176 result = result && (t2 == null); 177 } 178 } 179 } 180 return result; 181 } 182 return false; 183 } 184 185}