View Javadoc

1   /*
2    *   Magic-Project 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  package net.sf.magicproject.chart;
20  
21  import java.awt.Color;
22  import java.awt.Paint;
23  import java.util.Map;
24  
25  import net.sf.magicproject.chart.datasets.BarDataset;
26  import net.sf.magicproject.chart.datasets.Dataset;
27  import net.sf.magicproject.ui.i18n.LanguageManager;
28  
29  import org.jfree.chart.axis.CategoryAxis;
30  import org.jfree.chart.axis.CategoryLabelPositions;
31  import org.jfree.chart.axis.NumberAxis;
32  import org.jfree.chart.labels.StandardPieToolTipGenerator;
33  import org.jfree.chart.plot.CategoryPlot;
34  import org.jfree.chart.plot.PiePlot3D;
35  import org.jfree.chart.plot.Plot;
36  import org.jfree.chart.plot.PlotOrientation;
37  import org.jfree.chart.renderer.category.AreaRenderer;
38  import org.jfree.chart.renderer.category.BarRenderer;
39  import org.jfree.data.category.DefaultCategoryDataset;
40  import org.jfree.data.general.PieDataset;
41  import org.jfree.ui.RectangleInsets;
42  import org.jfree.util.Rotation;
43  
44  /***
45   * Appliable filters for charts.
46   * 
47   * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
48   * @since 0.93
49   */
50  public enum ChartFilter {
51  
52  	/***
53  	 * To filter the color.
54  	 */
55  	color,
56  
57  	/***
58  	 * To filter the type.
59  	 */
60  	type,
61  
62  	/***
63  	 * To filter the property.
64  	 */
65  	property,
66  
67  	/***
68  	 * To filter the manacost.
69  	 */
70  	manacost;
71  
72  	/***
73  	 * @param dataSet
74  	 *          the dataset associated to this filter.
75  	 * @param painterMapper
76  	 *          the optional paint mapper to associate to this filter.
77  	 * @return the plot associated to this filter.
78  	 */
79  	public Plot createPlot(Dataset dataSet, Map<Integer, Paint> painterMapper) {
80  		switch (this) {
81  		case color:
82  			return new MPiePlot((PieDataset) dataSet, painterMapper);
83  		case type:
84  		case property:
85  			return new MBarPlot((BarDataset) dataSet, painterMapper);
86  		case manacost:
87  		default:
88  			return new MAreaPlot((DefaultCategoryDataset) dataSet, painterMapper);
89  		}
90  	}
91  
92  	/***
93  	 * This custom PiePlot selects Paint values based on section data.
94  	 */
95  	private class MPiePlot extends PiePlot3D {
96  		private final Map<Integer, Paint> painterMapper;
97  
98  		MPiePlot(PieDataset dataset, Map<Integer, Paint> painterMapper) {
99  			super(dataset);
100 			this.painterMapper = painterMapper;
101 			this.setLabelGenerator(null);
102 			this.setBackgroundPaint(Color.LIGHT_GRAY);
103 			this.setOutlinePaint(Color.BLACK);
104 			this.setStartAngle(290D);
105 			this.setDirection(Rotation.CLOCKWISE);
106 			this.setForegroundAlpha(0.5F);
107 			this.setToolTipGenerator(new StandardPieToolTipGenerator());
108 			this.setInsets(new RectangleInsets(0.0, 5.0, 5.0, 5.0));
109 		}
110 
111 		@Override
112 		public Paint getSectionPaint(int section) {
113 			if (getDataset().getItemCount() == 0)
114 				return Color.LIGHT_GRAY;
115 			if (painterMapper == null)
116 				return super.getSectionPaint(section);
117 			Paint paint = painterMapper.get(((IChartKey) getDataset().getKeys().get(
118 					section)).getIntegerKey());
119 			if (paint == null)
120 				paint = painterMapper.get(DEFAULT_KEY);
121 			if (paint == null)
122 				paint = super.getSectionPaint(section);
123 			return paint;
124 		}
125 	}
126 
127 	/***
128 	 * This custom PiePlot selects Paint values based on section data.
129 	 */
130 	private class MBarPlot extends CategoryPlot {
131 		MBarPlot(BarDataset dataset, Map<Integer, Paint> painterMapper) {
132 			super(dataset, new CategoryAxis(null), new NumberAxis("Amount"),
133 					new BarRenderer());
134 			this.getRangeAxis().setStandardTickUnits(
135 					NumberAxis.createIntegerTickUnits());
136 
137 			CategoryAxis categoryaxis = getDomainAxis();
138 			categoryaxis.setCategoryLabelPositions(CategoryLabelPositions.UP_90);
139 			this.setRangeGridlinePaint(Color.WHITE);
140 			this.setBackgroundPaint(Color.LIGHT_GRAY);
141 			this.setOutlinePaint(Color.BLACK);
142 			this.setForegroundAlpha(0.5F);
143 		}
144 	}
145 
146 	/***
147 	 * This custom PiePlot selects Paint values based on area.
148 	 */
149 	private class MAreaPlot extends CategoryPlot {
150 		MAreaPlot(DefaultCategoryDataset dataset, Map<Integer, Paint> painterMapper) {
151 			super(dataset, new CategoryAxis(LanguageManager.getString("manacost")),
152 					new NumberAxis(LanguageManager.getString("amount")),
153 					new AreaRenderer());
154 			((AreaRenderer) getRenderer()).setFillPaint(Color.BLUE);
155 			((AreaRenderer) getRenderer()).setSeriesFillPaint(0, Color.BLUE);
156 			((AreaRenderer) getRenderer()).setSeriesOutlinePaint(0, Color.BLUE);
157 			((AreaRenderer) getRenderer()).setPaint(Color.BLUE);
158 			((AreaRenderer) getRenderer()).setSeriesFillPaint(0, Color.BLUE);
159 			this.setForegroundAlpha(0.5F);
160 			this.setDomainGridlinesVisible(true);
161 			this.setDomainGridlinePaint(Color.WHITE);
162 			this.setRangeGridlinesVisible(true);
163 			this.setRangeGridlinePaint(Color.WHITE);
164 			CategoryAxis categoryaxis = getDomainAxis();
165 			categoryaxis.setCategoryMargin(0.0);
166 			categoryaxis.setCategoryLabelPositions(CategoryLabelPositions.STANDARD);
167 			categoryaxis.setLowerMargin(0.0D);
168 			categoryaxis.setUpperMargin(0.0D);
169 			// categoryaxis.addCategoryLabelToolTip("Type 1", "The first type.");
170 			NumberAxis numberaxis = (NumberAxis) getRangeAxis();
171 			numberaxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
172 			numberaxis.setLabelAngle(0.0D);
173 			this.setOrientation(PlotOrientation.VERTICAL);
174 			this.setBackgroundPaint(Color.LIGHT_GRAY);
175 			this.setOutlinePaint(Color.BLACK);
176 			this.setForegroundAlpha(0.5F);
177 		}
178 	}
179 
180 	/***
181 	 * 
182 	 */
183 	public static final Integer DEFAULT_KEY = Integer.MAX_VALUE;
184 
185 	/***
186 	 * A new Dataset instance.
187 	 * 
188 	 * @param dataProvider
189 	 * @return a new Dataset instance.
190 	 */
191 	public Dataset createDataSet(IDataProvider dataProvider) {
192 		switch (this) {
193 		case color:
194 			return new net.sf.magicproject.chart.datasets.PieDataset(dataProvider,
195 					this);
196 		case type:
197 		case property:
198 			return new net.sf.magicproject.chart.datasets.BarDataset(dataProvider,
199 					this);
200 		case manacost:
201 		default:
202 			return new net.sf.magicproject.chart.datasets.CategoryDataset(
203 					dataProvider, this);
204 		}
205 	}
206 
207 	/***
208 	 * Return the title of this chart.
209 	 * 
210 	 * @return the title of this chart.
211 	 */
212 	public String getTitle() {
213 		switch (this) {
214 		case color:
215 			return LanguageManager.getString("wiz_input-color.title");
216 		case type:
217 			return LanguageManager.getString("types");
218 		case property:
219 			return LanguageManager.getString("wiz_input-property.title");
220 		case manacost:
221 		default:
222 			return LanguageManager.getString("manacost");
223 		}
224 	}
225 }