001    package jp.osdl.jbento2.chart;
002    
003    import java.awt.Color;
004    import java.io.File;
005    import java.io.IOException;
006    
007    
008    import org.jfree.chart.ChartUtilities;
009    import org.jfree.chart.JFreeChart;
010    import org.jfree.chart.plot.PlotOrientation;
011    import org.jfree.data.general.AbstractDataset;
012    
013    public class ChartGenerator {
014    
015        private File src;
016    
017        private File dest;
018        
019        private String postfix;
020    
021        private String title;
022    
023        private String xlabel;
024    
025        private String ylabel;
026    
027        private int width = 320;
028    
029        private int height = 240;
030        
031        private long xMin = 0;
032    
033        private long xMax = 0;
034        
035        private long yMin = 0;
036    
037        private long yMax = 0;
038    
039        private boolean shape = true;
040    
041        private String[] categories = null;
042    
043        private String endOfData = "TOTAL";
044    
045        private ChartType chartType = null;
046    
047        private DataSetBuilder dsBuilder = null;
048        
049        private boolean renew = false;
050    
051        /**
052         * Execute
053         * 
054         * @throws IOException
055         *             thrown if error occurred
056         */
057        public void execute() throws IOException {
058    
059            checkStatus();
060            
061            if (!renew && getSrc().lastModified() <= getDest().lastModified()) {
062                return;
063            }
064    
065            dsBuilder.setCategories(categories);
066            dsBuilder.setEndOfData(endOfData);
067    
068            AbstractDataset dataset = dsBuilder.createDataSet(getSrc());
069    
070            JFreeChart chart = chartType.getChart(getTitle(), getXlabel(),
071                    getYlabel(), getXMin(), getXMax(), getYMin(), getYMax(),
072                    dataset, PlotOrientation.VERTICAL, true, true, false,
073                    getShape());
074    
075            chart.setBackgroundPaint(Color.WHITE);
076            
077            ChartUtilities
078                    .saveChartAsPNG(getDest(), chart, getWidth(), getHeight());
079    
080    //        System.out.println(" generated chart " + getDest());
081        }
082    
083        private void checkStatus() {
084            if (getSrc() == null) {
085                throw new IllegalStateException("src is null");
086            }
087            if (getChartType() == null) {
088                throw new IllegalStateException("chartType is null");
089            }
090            if (getDsBuilder() == null) {
091                throw new IllegalStateException("dsBuilder is null");
092            }
093            if (getDest() == null) {
094                String srcName = getSrc().getName();
095                StringBuffer sb = new StringBuffer();
096                sb.append(srcName.substring(0, srcName.lastIndexOf('.')));
097                if (getPostfix() != null) {
098                    sb.append(getPostfix());
099                }
100                sb.append(".png");
101                File dest = new File(getSrc().getParent(), sb.toString());
102                setDest(dest);
103            }
104        }
105    
106        /**
107         * Gets the value of src
108         * 
109         * @return the value of src
110         */
111        public File getSrc() {
112            return this.src;
113        }
114    
115        /**
116         * Sets the value of src
117         * 
118         * @param argSrc
119         *            Value to assign to this.src
120         */
121        public void setSrc(File argSrc) {
122            this.src = argSrc;
123        }
124    
125        /**
126         * Gets the value of dest
127         * 
128         * @return the value of dest
129         */
130        public File getDest() {
131            return this.dest;
132        }
133    
134        /**
135         * Sets the value of dest
136         * 
137         * @param argDest
138         *            Value to assign to this.dest
139         */
140        public void setDest(File argDest) {
141            this.dest = argDest;
142        }
143    
144        /**
145         * Gets the value of title
146         * 
147         * @return the value of title
148         */
149        public String getTitle() {
150            return this.title;
151        }
152    
153        /**
154         * Sets the value of title
155         * 
156         * @param argTitle
157         *            Value to assign to this.title
158         */
159        public void setTitle(String argTitle) {
160            this.title = argTitle;
161        }
162    
163        /**
164         * Gets the value of xlabel
165         * 
166         * @return the value of xlabel
167         */
168        public String getXlabel() {
169            return this.xlabel;
170        }
171    
172        /**
173         * Sets the value of xlabel
174         * 
175         * @param argXlabel
176         *            Value to assign to this.xlabel
177         */
178        public void setXlabel(String argXlabel) {
179            this.xlabel = argXlabel;
180        }
181    
182        /**
183         * Gets the value of ylabel
184         * 
185         * @return the value of ylabel
186         */
187        public String getYlabel() {
188            return this.ylabel;
189        }
190    
191        /**
192         * Sets the value of ylabel
193         * 
194         * @param argYlabel
195         *            Value to assign to this.ylabel
196         */
197        public void setYlabel(String argYlabel) {
198            this.ylabel = argYlabel;
199        }
200    
201        /**
202         * Gets the value of width
203         * 
204         * @return the value of width
205         */
206        public int getWidth() {
207            return this.width;
208        }
209    
210        /**
211         * Sets the value of width
212         * 
213         * @param argWidth
214         *            Value to assign to this.width
215         */
216        public void setWidth(int argWidth) {
217            this.width = argWidth;
218        }
219    
220        /**
221         * Gets the value of height
222         * 
223         * @return the value of height
224         */
225        public int getHeight() {
226            return this.height;
227        }
228    
229        /**
230         * Sets the value of height
231         * 
232         * @param argHeight
233         *            Value to assign to this.height
234         */
235        public void setHeight(int argHeight) {
236            this.height = argHeight;
237        }
238    
239        /**
240         * Gets the value of ymax
241         * 
242         * @return ymax
243         */
244        public long getYMax() {
245            return yMax;
246        }
247    
248        /**
249         * Sets the value of ymax
250         * 
251         * @param ymax
252         */
253        public void setYMax(long ymax) {
254            this.yMax = ymax;
255        }
256    
257        /**
258         * Gets the value of shape
259         * 
260         * @return shape
261         */
262        public boolean getShape() {
263            return shape;
264        }
265    
266        public void setShape(boolean shape) {
267            this.shape = shape;
268        }
269    
270        public String[] getCategories() {
271            return categories;
272        }
273    
274        public void setCategories(String[] categories) {
275            this.categories = categories;
276        }
277    
278        public String getEndOfData() {
279            return endOfData;
280        }
281    
282        public void setEndOfData(String endOfData) {
283            this.endOfData = endOfData;
284        }
285    
286        public long getXMax() {
287            return xMax;
288        }
289    
290        public void setXMax(long max) {
291            xMax = max;
292        }
293    
294        public ChartType getChartType() {
295            return chartType;
296        }
297    
298        public void setChartType(ChartType chartType) {
299            this.chartType = chartType;
300        }
301    
302        public DataSetBuilder getDsBuilder() {
303            return dsBuilder;
304        }
305    
306        public void setDsBuilder(DataSetBuilder dsBuilder) {
307            this.dsBuilder = dsBuilder;
308        }
309    
310        public String getPostfix() {
311            return postfix;
312        }
313    
314        public void setPostfix(String postfix) {
315            this.postfix = postfix;
316        }
317    
318        public boolean isRenew() {
319            return renew;
320        }
321    
322        public void setRenew(boolean renew) {
323            this.renew = renew;
324        }
325    
326        public long getXMin() {
327            return xMin;
328        }
329    
330        public void setXMin(long min) {
331            xMin = min;
332        }
333    
334        public long getYMin() {
335            return yMin;
336        }
337    
338        public void setYMin(long min) {
339            yMin = min;
340        }
341    
342    }