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 CategoryDataset 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 CategoryDataset(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 int oldSize = workingKeys.size();
52 key.processAdd(workingKeys);
53 for (int i = oldSize; i < workingKeys.size() - 1; i++)
54 setValue(new Integer(0), "My Key", workingKeys.get(i));
55 setValue(new Integer(amount), "My Key", key);
56 }
57 }
58 }
59
60 /***
61 * Remove cards to all datasets.
62 *
63 * @param cardModel
64 * the card to remove.
65 * @param amount
66 * the amount of card to remove.
67 */
68 public void removeCard(final CardModel cardModel, final int amount) {
69 try {
70 for (IChartKey key : provider.getKeys(cardModel, filter)) {
71 final int value = Math.max(getValue("My Key", key).intValue() - amount,
72 0);
73 setValue(key, value);
74 super.validateObject();
75 }
76 } catch (Exception e) {
77
78 e.printStackTrace();
79 }
80 }
81
82 public void removeAll() {
83 for (IChartKey key : workingKeys)
84 super.removeColumn(key);
85 workingKeys.clear();
86 }
87
88 public void setValue(IChartKey key, Integer value) {
89 super.setValue(value, "My Key", key);
90 }
91
92 private final List<IChartKey> workingKeys;
93
94 private final IDataProvider provider;
95
96 private final ChartFilter filter;
97
98 }