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 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
public final class FileTypeFactory { |
16 |
|
|
17 |
|
private FileTypeFactory() { |
18 |
|
} |
19 |
|
|
20 |
|
|
21 |
|
|
22 |
|
|
23 |
|
|
24 |
|
|
25 |
|
|
26 |
|
|
27 |
|
|
28 |
|
|
29 |
|
|
30 |
|
|
31 |
|
public static FileType create(String ext, int width, class="keyword">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 |
3 |
protected AbstractFileType(ChartSize chart) { |
55 |
3 |
this.chart = chart; |
56 |
3 |
} |
57 |
|
|
58 |
|
protected File createFile(String name, String ext) { |
59 |
0 |
return new File(name + "." + ext); |
60 |
|
} |
61 |
|
|
62 |
|
public void saveChart(File file, JFreeChart myChart) { |
63 |
0 |
chart.saveChart(file, myChart); |
64 |
0 |
} |
65 |
|
} |
66 |
|
|
67 |
|
private static class JpgFileType extends AbstractFileType implements |
68 |
|
FileType { |
69 |
|
|
70 |
|
public JpgFileType(int width, class="keyword">int height) { |
71 |
|
super(new JpgChartSize(width, height)); |
72 |
|
} |
73 |
|
|
74 |
|
|
75 |
|
|
76 |
|
|
77 |
|
|
78 |
|
|
79 |
|
public void saveChart(String fileName, JFreeChart myChart) { |
80 |
|
super.saveChart(class="keyword">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, class="keyword">int height) { |
89 |
|
super(new PngChartSize(width, height)); |
90 |
|
} |
91 |
|
|
92 |
|
|
93 |
|
|
94 |
|
|
95 |
|
|
96 |
|
public void saveChart(String fileName, JFreeChart chart) { |
97 |
|
super.saveChart(class="keyword">super.createFile(fileName, FileType.PNG), chart); |
98 |
|
|
99 |
|
} |
100 |
|
} |
101 |
|
|
102 |
|
private static class PngChartSize extends AbstractChartSize { |
103 |
|
|
104 |
|
public PngChartSize(int width, class="keyword">int height) { |
105 |
|
super(width, height); |
106 |
|
} |
107 |
|
|
108 |
|
|
109 |
|
|
110 |
|
|
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, class="keyword">int height) { |
125 |
|
super(width, height); |
126 |
|
} |
127 |
|
|
128 |
|
|
129 |
|
|
130 |
|
|
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, class="keyword">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 |
|
|
163 |
|
|
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 |
|
} |