View Javadoc

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.general.DefaultPieDataset;
12  
13  /***
14   * 
15   */
16  public class PieDataset extends DefaultPieDataset 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 PieDataset(IDataProvider provider, ChartFilter filter) {
27  		this.provider = provider;
28  		this.filter = filter;
29  		this.workingKeys = new ArrayList<IChartKey>();
30  	}
31  
32  	/***
33  	 * Add cards to all datasets.
34  	 * 
35  	 * @param cardModel
36  	 *          the card to add.
37  	 * @param amount
38  	 *          the amount of card to add.
39  	 */
40  	public void addCard(final CardModel cardModel, final int amount) {
41  		for (IChartKey key : provider.getKeys(cardModel, filter)) {
42  			if (workingKeys.contains(key)) {
43  				try {
44  					setValue(key, new Integer(getValue(key).intValue() + amount));
45  				} catch (Exception e) {
46  					e.printStackTrace();
47  				}
48  			} else {
49  				workingKeys.add(key);
50  				setValue(key, new Integer(amount));
51  			}
52  		}
53  		fireDatasetChanged();
54  	}
55  
56  	/***
57  	 * Remove cards to all datasets.
58  	 * 
59  	 * @param cardModel
60  	 *          the card to remove.
61  	 * @param amount
62  	 *          the amount of card to remove.
63  	 */
64  	public void removeCard(final CardModel cardModel, final int amount) {
65  		try {
66  			for (IChartKey key : provider.getKeys(cardModel, filter)) {
67  				final int value = Math.max(getValue(key).intValue() - amount, 0);
68  				setValue(key, value);
69  				if (value == 0) {
70  					setValue(key, value);
71  					workingKeys.remove(key);
72  					remove(key);
73  				}
74  			}
75  		} catch (Exception e) {
76  			//
77  			e.printStackTrace();
78  		}
79  		fireDatasetChanged();
80  	}
81  
82  	public void removeAll() {
83  		for (IChartKey key : workingKeys)
84  			super.remove(key);
85  		workingKeys.clear();
86  	}
87  
88  	@Override
89  	public List<IChartKey> getKeys() {
90  		return workingKeys;
91  	}
92  
93  	public void setValue(IChartKey key, Integer value) {
94  		super.setValue(key, value);
95  	}
96  
97  	private final List<IChartKey> workingKeys;
98  
99  	private final IDataProvider provider;
100 
101 	private final ChartFilter filter;
102 
103 }