1 package net.sf.magicproject.chart.datasets;
2
3 import java.util.ArrayList;
4 import java.util.List;
5
6 import net.sf.magicproject.chart.ChartFilter;
7 import net.sf.magicproject.chart.IChartKey;
8 import net.sf.magicproject.chart.IDataProvider;
9 import net.sf.magicproject.clickable.targetable.card.CardModel;
10
11 import org.jfree.data.category.DefaultCategoryDataset;
12
13 /***
14 *
15 */
16 public class BarDataset extends DefaultCategoryDataset implements Dataset {
17
18 /***
19 * Create a new instance of this class.
20 *
21 * @param provider
22 * the key provider.
23 * @param filter
24 * the filter attached to this dataset.
25 */
26 public BarDataset(IDataProvider provider, ChartFilter filter) {
27 super();
28 this.provider = provider;
29 this.filter = filter;
30 this.workingKeys = new ArrayList<IChartKey>();
31 }
32
33 /***
34 * Add cards to all datasets.
35 *
36 * @param cardModel
37 * the card to add.
38 * @param amount
39 * the amount of card to add.
40 */
41 public void addCard(final CardModel cardModel, final int amount) {
42 for (IChartKey key : provider.getKeys(cardModel, filter)) {
43 if (workingKeys.contains(key)) {
44 try {
45 setValue(key,
46 new Integer(getValue("My Key", key).intValue() + amount));
47 } catch (Exception e) {
48 e.printStackTrace();
49 }
50 } else {
51 workingKeys.add(key);
52 setValue(new Integer(amount), "My Key", key);
53 }
54 }
55 fireDatasetChanged();
56 }
57
58 /***
59 * Remove cards to all datasets.
60 *
61 * @param cardModel
62 * the card to remove.
63 * @param amount
64 * the amount of card to remove.
65 */
66 public void removeCard(final CardModel cardModel, final int amount) {
67 try {
68 for (IChartKey key : provider.getKeys(cardModel, filter)) {
69 final int value = Math.max(getValue("My Key", key).intValue() - amount,
70 0);
71 if (value == 0) {
72 workingKeys.remove(key);
73 removeColumn(key);
74 } else {
75 setValue(key, value);
76 }
77 }
78 } catch (Exception e) {
79 e.printStackTrace();
80 }
81 fireDatasetChanged();
82 }
83
84 public void removeAll() {
85 for (IChartKey key : workingKeys)
86 super.removeColumn(key);
87 workingKeys.clear();
88 }
89
90 public void setValue(IChartKey key, Integer value) {
91 super.setValue(value, "My Key", key);
92 }
93
94 private final List<IChartKey> workingKeys;
95
96 private final IDataProvider provider;
97
98 private final ChartFilter filter;
99
100 }