1 package com.wikihouse.wildcats0201.jdbcchart.dto;
2
3 import java.io.File;
4 import java.io.IOException;
5
6 import org.jfree.chart.ChartUtilities;
7 import org.jfree.chart.JFreeChart;
8
9 /***
10 * <B>FileTypeFactory </B>. FileType生成のFactoryです。
11 *
12 * @author $Author: wildcats $
13 * @version $Revision: 5 $
14 */
15 public final class FileTypeFactory {
16
17 private FileTypeFactory() {
18 }
19
20 /***
21 * 生成.
22 *
23 * @param ext
24 * 拡張子 (jpgまたはpng)
25 * @param width
26 * グラフの横幅
27 * @param height
28 * グラフの縦幅
29 * @return FileType
30 */
31 public static FileType create(String ext, int width, int height) {
32 if (FileType.JPG.equalsIgnoreCase(ext)) {
33 return new JpgFileType(width, height);
34 } else if (FileType.PNG.equalsIgnoreCase(ext)) {
35 return new PngFileType(width, height);
36 } else {
37 throw new NoSuchFileTypeException(ext);
38 }
39 }
40
41 private static class NoSuchFileTypeException extends RuntimeException {
42
43 private static final long serialVersionUID = 8757718354820250136L;
44
45 public NoSuchFileTypeException(String message) {
46 super(message);
47 }
48 }
49
50 private static abstract class AbstractFileType {
51
52 private ChartSize chart;
53
54 protected AbstractFileType(ChartSize chart) {
55 this.chart = chart;
56 }
57
58 protected File createFile(String name, String ext) {
59 return new File(name + "." + ext);
60 }
61
62 public void saveChart(File file, JFreeChart myChart) {
63 chart.saveChart(file, myChart);
64 }
65 }
66
67 private static class JpgFileType extends AbstractFileType implements
68 FileType {
69
70 public JpgFileType(int width, int height) {
71 super(new JpgChartSize(width, height));
72 }
73
74 /***
75 * @see com.wikihouse.wildcats0201.jdbcchart.dto.FileType#saveChart(java.lang.String,
76 * org.jfree.chart.JFreeChart,
77 * com.wikihouse.wildcats0201.jdbcchart.dto.ChartFile)
78 */
79 public void saveChart(String fileName, JFreeChart myChart) {
80 super.saveChart(super.createFile(fileName, FileType.JPG), myChart);
81 }
82
83 }
84
85 private static class PngFileType extends AbstractFileType implements
86 FileType {
87
88 protected PngFileType(int width, int height) {
89 super(new PngChartSize(width, height));
90 }
91
92 /***
93 * @see com.wikihouse.wildcats0201.jdbcchart.dto.FileType#saveChart(java.lang.String,
94 * org.jfree.chart.JFreeChart)
95 */
96 public void saveChart(String fileName, JFreeChart chart) {
97 super.saveChart(super.createFile(fileName, FileType.PNG), chart);
98
99 }
100 }
101
102 private static class PngChartSize extends AbstractChartSize {
103
104 public PngChartSize(int width, int height) {
105 super(width, height);
106 }
107
108 /***
109 * @see com.wikihouse.wildcats0201.jdbcchart.dto.FileTypeFactory.ChartSize#saveChart(java.io.File,
110 * org.jfree.chart.JFreeChart)
111 */
112 public void saveChart(File file, JFreeChart chart) {
113 try {
114 ChartUtilities.saveChartAsPNG(file, chart, super.getWidth(),
115 super.getHeight());
116 } catch (IOException ignore) {
117 }
118 }
119
120 }
121
122 private static class JpgChartSize extends AbstractChartSize {
123
124 public JpgChartSize(int width, int height) {
125 super(width, height);
126 }
127
128 /***
129 * @see com.wikihouse.wildcats0201.jdbcchart.dto.FileTypeFactory.ChartSize#saveChart(java.io.File,
130 * org.jfree.chart.JFreeChart)
131 */
132 public void saveChart(File file, JFreeChart chart) {
133 try {
134 ChartUtilities.saveChartAsJPEG(file, chart, super.getWidth(),
135 super.getHeight());
136 } catch (IOException ignore) {
137 }
138 }
139
140 }
141
142 private static abstract class AbstractChartSize implements ChartSize {
143
144 private int width;
145
146 private int height;
147
148 public AbstractChartSize(int width, int height) {
149 this.width = width;
150 this.height = height;
151 }
152
153 protected int getWidth() {
154 return this.width;
155 }
156
157 protected int getHeight() {
158 return this.height;
159 }
160
161 /***
162 * @see com.wikihouse.wildcats0201.jdbcchart.dto.FileTypeFactory.ChartSize#saveChart(java.io.File,
163 * org.jfree.chart.JFreeChart)
164 */
165 public abstract void saveChart(File file, JFreeChart chart);
166
167 }
168
169 private interface ChartSize {
170
171 public abstract void saveChart(File file, JFreeChart chart);
172
173 }
174
175 }