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.ArrayList;
23  import java.util.List;
24  
25  import net.sf.firemox.chart.ChartFilter;
26  import net.sf.firemox.chart.IChartKey;
27  import net.sf.firemox.chart.IDataProvider;
28  import net.sf.firemox.clickable.target.card.CardModel;
29  
30  import org.jfree.data.category.DefaultCategoryDataset;
31  
32  /***
33   * 
34   */
35  public class CategoryDataset extends DefaultCategoryDataset implements Dataset {
36  
37  	/***
38  	 * Create a new instance of this class.
39  	 * 
40  	 * @param provider
41  	 *          the key provider.
42  	 * @param filter
43  	 *          the filter attached to this data set.
44  	 */
45  	public CategoryDataset(IDataProvider provider, ChartFilter filter) {
46  		super();
47  		this.provider = provider;
48  		this.filter = filter;
49  		this.workingKeys = new ArrayList<IChartKey>();
50  	}
51  
52  	/***
53  	 * Add cards to all data sets.
54  	 * 
55  	 * @param cardModel
56  	 *          the card to add.
57  	 * @param amount
58  	 *          the amount of card to add.
59  	 */
60  	public void addCard(final CardModel cardModel, final int amount) {
61  		for (IChartKey key : provider.getKeys(cardModel, filter)) {
62  			if (workingKeys.contains(key)) {
63  				try {
64  					setValue(key, Integer.valueOf(getValue("My Key", key).intValue()
65  							+ amount));
66  				} catch (Exception e) {
67  					e.printStackTrace();
68  				}
69  			} else {
70  				int oldSize = workingKeys.size();
71  				key.processAdd(workingKeys);
72  				for (int i = oldSize; i < workingKeys.size() - 1; i++)
73  					setValue(Integer.valueOf(0), "My Key", workingKeys.get(i));
74  				setValue(Integer.valueOf(amount), "My Key", key);
75  			}
76  		}
77  	}
78  
79  	/***
80  	 * Remove cards to all data sets.
81  	 * 
82  	 * @param cardModel
83  	 *          the card to remove.
84  	 * @param amount
85  	 *          the amount of card to remove.
86  	 */
87  	public void removeCard(final CardModel cardModel, final int amount) {
88  		try {
89  			for (IChartKey key : provider.getKeys(cardModel, filter)) {
90  				final int value = Math.max(getValue("My Key", key).intValue() - amount,
91  						0);
92  				setValue(key, value);
93  				super.validateObject();
94  			}
95  		} catch (Exception e) {
96  			//
97  			e.printStackTrace();
98  		}
99  	}
100 
101 	public void removeAll() {
102 		for (IChartKey key : workingKeys)
103 			super.removeColumn(key);
104 		workingKeys.clear();
105 	}
106 
107 	public void setValue(IChartKey key, Integer value) {
108 		super.setValue(value, "My Key", key);
109 	}
110 
111 	private final List<IChartKey> workingKeys;
112 
113 	private final IDataProvider provider;
114 
115 	private final ChartFilter filter;
116 
117 }