View Javadoc

1   /*
2    *   Firemox is a turn based strategy simulator
3    *   Copyright (C) 2003-2007 Fabrice Daugan
4    *
5    *   This program is free software; you can redistribute it and/or modify it 
6    * under the terms of the GNU General Public License as published by the Free 
7    * Software Foundation; either version 2 of the License, or (at your option) any
8    * later version.
9    *
10   *   This program is distributed in the hope that it will be useful, but WITHOUT 
11   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12   * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more 
13   * details.
14   *
15   *   You should have received a copy of the GNU General Public License along  
16   * with this program; if not, write to the Free Software Foundation, Inc., 
17   * 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18   * 
19   */
20  package net.sf.firemox.chart.datasets;
21  
22  import java.util.Collection;
23  
24  import org.jfree.data.statistics.HistogramType;
25  
26  import net.sf.firemox.chart.ChartFilter;
27  import net.sf.firemox.chart.IChartKey;
28  import net.sf.firemox.chart.IDataProvider;
29  import net.sf.firemox.clickable.target.card.CardModel;
30  
31  /***
32   * 
33   */
34  public class HistogramDataset extends
35  		org.jfree.data.statistics.HistogramDataset implements Dataset {
36  
37  	private int[] values = new int[0];
38  
39  	/***
40  	 * Create a new instance of this class.
41  	 * 
42  	 * @param provider
43  	 *          the key provider.
44  	 * @param filter
45  	 *          the filter attached to this data set.
46  	 */
47  	public HistogramDataset(IDataProvider provider, ChartFilter filter) {
48  		super();
49  		this.provider = provider;
50  		this.filter = filter;
51  		this.setType(HistogramType.FREQUENCY);
52  	}
53  
54  	/***
55  	 * Add cards to all data sets.
56  	 * 
57  	 * @param cardModel
58  	 *          the card to add.
59  	 * @param amount
60  	 *          the amount of card to add.
61  	 */
62  	public void addCard(final CardModel cardModel, final int amount) {
63  		Collection<IChartKey> keys = provider.getKeys(cardModel, filter);
64  		for (IChartKey key : keys) {
65  			if (key.getIntegerKey() + 1 > values.length) {
66  				int[] tmp = new int[key.getIntegerKey() + 1];
67  				System.arraycopy(values, 0, tmp, 0, values.length);
68  				values = tmp;
69  			}
70  			values[key.getIntegerKey()] += amount;
71  		}
72  		list.clear();
73  		int max = 0;
74  		for (int value : values) {
75  			if (value > max)
76  				max = value;
77  		}
78  		double[] amounts = new double[max + 1];
79  		for (int index = max + 1; index-- > 0;) {
80  			amounts[values[index]] = index;
81  		}
82  		try {
83  			addSeries("data1", amounts, amounts.length, 0, amounts.length);
84  		} catch (Exception e) {
85  			e.printStackTrace();
86  		}
87  		fireDatasetChanged();
88  	}
89  
90  	/***
91  	 * Remove cards to all data sets.
92  	 * 
93  	 * @param cardModel
94  	 *          the card to remove.
95  	 * @param amount
96  	 *          the amount of card to remove.
97  	 */
98  	public void removeCard(final CardModel cardModel, final int amount) {
99  		Collection<IChartKey> keys = provider.getKeys(cardModel, filter);
100 		for (IChartKey key : keys) {
101 			values[key.getIntegerKey()] -= amount;
102 			double max = 0;
103 			for (double value : values)
104 				if (value > max)
105 					max = value;
106 			list.clear();
107 			try {
108 				// addSeries("data1", values, 100, 0, max);
109 			} catch (Exception e) {
110 				e.printStackTrace();
111 			}
112 		}
113 		fireDatasetChanged();
114 	}
115 
116 	public void setValue(IChartKey key, Integer value) {
117 		// super.setValue(value, "My Key", key);
118 	}
119 
120 	public void removeAll() {
121 		//
122 	}
123 
124 	private final IDataProvider provider;
125 
126 	private final ChartFilter filter;
127 
128 }