1 43 44 package org.jfree.chart.labels; 45 46 import java.io.Serializable ; 47 import java.text.MessageFormat ; 48 import java.util.HashMap ; 49 import java.util.List ; 50 import java.util.Map ; 51 52 import org.jfree.data.xy.XYDataset; 53 import org.jfree.util.PublicCloneable; 54 55 59 public class MultipleXYSeriesLabelGenerator implements XYSeriesLabelGenerator, 60 Cloneable , 61 PublicCloneable, 62 Serializable { 63 64 65 private static final long serialVersionUID = 138976236941898560L; 66 67 68 public static final String DEFAULT_LABEL_FORMAT = "{0}"; 69 70 71 private String formatPattern; 72 73 74 private String additionalFormatPattern; 75 76 77 private Map seriesLabelLists; 78 79 82 public MultipleXYSeriesLabelGenerator() { 83 this(DEFAULT_LABEL_FORMAT); 84 } 85 86 91 public MultipleXYSeriesLabelGenerator(String format) { 92 if (format == null) { 93 throw new IllegalArgumentException ("Null 'format' argument."); 94 } 95 this.formatPattern = format; 96 this.additionalFormatPattern = "\n{0}"; 97 this.seriesLabelLists = new HashMap (); 98 } 99 100 106 public void addSeriesLabel(int series, String label) { 107 Integer key = new Integer (series); 108 List labelList = (List ) this.seriesLabelLists.get(key); 109 if (labelList == null) { 110 labelList = new java.util.ArrayList (); 111 this.seriesLabelLists.put(key, labelList); 112 } 113 labelList.add(label); 114 } 115 116 121 public void clearSeriesLabels(int series) { 122 Integer key = new Integer (series); 123 this.seriesLabelLists.put(key, null); 124 } 125 126 135 public String generateLabel(XYDataset dataset, int series) { 136 if (dataset == null) { 137 throw new IllegalArgumentException ("Null 'dataset' argument."); 138 } 139 StringBuffer label = new StringBuffer (); 140 label.append( 141 MessageFormat.format( 142 this.formatPattern, createItemArray(dataset, series) 143 ) 144 ); 145 Integer key = new Integer (series); 146 List extraLabels = (List ) this.seriesLabelLists.get(key); 147 if (extraLabels != null) { 148 Object [] temp = new Object [1]; 149 for (int i = 0; i < extraLabels.size(); i++) { 150 temp[0] = extraLabels.get(i); 151 String labelAddition = MessageFormat.format( 152 this.additionalFormatPattern, temp 153 ); 154 label.append(labelAddition); 155 } 156 } 157 return label.toString(); 158 } 159 160 169 protected Object [] createItemArray(XYDataset dataset, int series) { 170 Object [] result = new Object [1]; 171 result[0] = dataset.getSeriesKey(series).toString(); 172 return result; 173 } 174 175 182 public Object clone() throws CloneNotSupportedException { 183 return super.clone(); 184 } 185 186 193 public boolean equals(Object obj) { 194 if (obj == this) { 195 return true; 196 } 197 if (!(obj instanceof StandardXYSeriesLabelGenerator)) { 198 return false; 199 } 200 if (!super.equals(obj)) { 201 return false; 202 } 203 return true; 204 } 205 206 } 207 | Popular Tags |