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 BarDataset 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 BarDataset(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  				workingKeys.add(key);
71  				setValue(Integer.valueOf(amount), "My Key", key);
72  			}
73  		}
74  		fireDatasetChanged();
75  	}
76  
77  	/***
78  	 * Remove cards to all data sets.
79  	 * 
80  	 * @param cardModel
81  	 *          the card to remove.
82  	 * @param amount
83  	 *          the amount of card to remove.
84  	 */
85  	public void removeCard(final CardModel cardModel, final int amount) {
86  		try {
87  			for (IChartKey key : provider.getKeys(cardModel, filter)) {
88  				final int value = Math.max(getValue("My Key", key).intValue() - amount,
89  						0);
90  				if (value == 0) {
91  					workingKeys.remove(key);
92  					removeColumn(key);
93  				} else {
94  					setValue(key, value);
95  				}
96  			}
97  		} catch (Exception e) {
98  			e.printStackTrace();
99  		}
100 		fireDatasetChanged();
101 	}
102 
103 	public void removeAll() {
104 		for (IChartKey key : workingKeys)
105 			super.removeColumn(key);
106 		workingKeys.clear();
107 	}
108 
109 	public void setValue(IChartKey key, Integer value) {
110 		super.setValue(value, "My Key", key);
111 	}
112 
113 	private final List<IChartKey> workingKeys;
114 
115 	private final IDataProvider provider;
116 
117 	private final ChartFilter filter;
118 
119 }