1 package org.catacomb.dataview;
2
3
4 import org.catacomb.dataview.build.DataSource;
5 import org.catacomb.dataview.build.Dataview;
6 import org.catacomb.dataview.read.ContentReader;
7 import org.catacomb.dataview.read.Exporter;
8 import org.catacomb.dataview.read.FUImportContext;
9 import org.catacomb.druid.gui.base.DruFrame;
10 import org.catacomb.druid.gui.base.DruPanel;
11 import org.catacomb.interlish.service.Env;
12 import org.catacomb.interlish.structure.*;
13 import org.catacomb.numeric.data.DataExtractor;
14 import org.catacomb.numeric.data.NumDataSet;
15 import org.catacomb.report.E;
16 import org.catacomb.serial.jar.CustomJarWriter;
17 import org.catacomb.util.AWTUtil;
18 import org.catacomb.util.ImageUtil;
19
20
21 import java.awt.image.BufferedImage;
22 import java.io.File;
23 import java.util.ArrayList;
24
25
26 public class DataviewController implements Controller {
27
28 Dataview dataview;
29
30 int npainter;
31 DataPlotPainter[] painters;
32
33 int ndisplay;
34 GraphicsView[] displays;
35
36 FrameController frameController;
37
38 DruFrame mainFrame;
39 DruPanel mainPanel;
40
41
42 DataExtractor dataExtractor;
43
44 ContentReader contentReader;
45
46 String datsrc = "";
47
48
49 public DataviewController(Dataview dv) {
50 super();
51 dataview = dv;
52 }
53
54
55
56 public void initData(ContentReader dsr) {
57 contentReader = dsr;
58
59 dataExtractor = new DataExtractor();
60 initDataSource();
61
62 exportData();
63
64 if (Env.isApplication()) {
65 cacheBinary();
66 }
67 }
68
69
70 public void setMainFrame(DruFrame df) {
71 mainFrame = df;
72 }
73
74
75 public void exit() {
76 mainFrame.dispose();
77 }
78
79
80
81 public void attached() {
82 }
83
84
85
86 public void cacheBinary() {
87
88 FUImportContext ctxt = contentReader.getContext();
89
90 File fsrc = ctxt.getSourceFile();
91
92 String cbin = ctxt.getExtensionRelativeName("-cache.bnd");
93 File fbin = ctxt.getRelativeFile(cbin);
94
95 String cjar = ctxt.getExtensionRelativeName("sdj");
96 File fjar = ctxt.getRelativeFile(cjar);
97
98
99
100 if (fbin != null && !(fbin.exists())) {
101 DataSource ds = dataview.getDataSource();
102 String datname = ds.name;
103 NumDataSet omin = getMinimalDataSet(datname);
104 Exporter.export(omin, fbin);
105 }
106
107
108 if (fsrc != null && fsrc.getName().endsWith("xml")) {
109 CustomJarWriter jarw = new CustomJarWriter();
110 jarw.addMain(fsrc);
111 jarw.add(fbin);
112 jarw.write(fjar);
113 }
114
115 }
116
117 public void cacheData(File fcache, String cnm) {
118
119 }
120
121
122
123 private void initDataSource() {
124 DataSource ds = dataview.getDataSource();
125
126 String fnm = ds.file;
127
128 Object dataobj = null;
129
130 FUImportContext ctxt = contentReader.getContext();
131
132 String cca = ctxt.getExtensionRelativeName("-cache.bnd");
133 if (cca != null && ctxt.hasRelative(cca)) {
134 dataobj = ctxt.getRelative(cca);
135
136 } else if (fnm != null && ctxt.hasRelative(fnm)) {
137 dataobj = ctxt.getRelative(fnm);
138 }
139
140 if (dataobj instanceof NumDataSet) {
141 NumDataSet dataSet = (NumDataSet)dataobj;
142 dataSet.setName(ds.name);
143 dataExtractor.addDataSet(dataSet);
144
145 } else {
146 E.error("Dataview controller - wrong data type " + dataobj);
147 }
148
149 }
150
151
152
153
154 public void setDisplays(ArrayList<Object> arl) {
155 displays = new GraphicsView[10];
156 ndisplay = 0;
157 painters = new DataPlotPainter[10];
158 npainter = 0;
159
160
161 for (Object obj : arl) {
162 if (obj instanceof GraphicsView) {
163 displays[ndisplay++] = (GraphicsView)obj;
164
165 } else if (obj instanceof DataPlotPainter) {
166 painters[npainter++] = (DataPlotPainter)obj;
167
168 } else if (obj instanceof FrameController) {
169 frameController = (FrameController)obj;
170 frameController.setDataviewController(this);
171
172
173 } else if (obj instanceof ModeSetter) {
174 E.missing();
175
176
177 } else if (obj instanceof DruPanel && ((DruPanel)obj).getID().equals("main")) {
178
179
180
181 } else {
182 E.error("dataview controller cant handle display item " + obj);
183 }
184
185 if (obj instanceof DruPanel && ((DruPanel)obj).getID().equals("main")) {
186 mainPanel = (DruPanel)obj;
187 }
188
189 }
190 }
191
192
193
194 private void setPaintWidthFactor(int ithick) {
195 for (int i = 0; i < npainter; i++) {
196 painters[i].setPaintWidthFactor(ithick);
197 }
198 }
199
200
201 private void exportData() {
202
203 for (int i = 0; i < npainter; i++) {
204 painters[i].setDataSource(dataExtractor);
205 }
206
207 if (frameController != null) {
208 frameController.setDataSource(dataExtractor);
209 }
210
211 updateDisplays();
212 }
213
214
215
216 private void markNeeded() {
217
218 for (int i = 0; i < npainter; i++) {
219 painters[i].markNeeded();
220 }
221
222 if (frameController != null) {
223 frameController.markNeeded();
224 }
225
226 }
227
228
229 public void setMode(String dom, String mod) {
230 E.missing("cant set mode from here");
231 for (int i = 0; i < ndisplay; i++) {
232
233 }
234 }
235
236
237 public void showFrame(int iframe) {
238 for (int i = 0; i < npainter; i++) {
239 painters[i].showFrame(iframe);
240 }
241 updateDisplays();
242 }
243
244
245
246 private void updateDisplays() {
247 for (int i = 0; i < ndisplay; i++) {
248 displays[i].viewChanged();
249 }
250 }
251
252
253
254 public void requestClose() {
255 E.missing();
256 requestExit();
257 }
258
259
260 public void requestExit() {
261 E.missing();
262 System.exit(0);
263 }
264
265
266
267 public BufferedImage getBufferedImage(int ithick) {
268
269 setPaintWidthFactor(ithick);
270
271 BufferedImage ret = AWTUtil.getBufferedImage(mainPanel);
272
273 setPaintWidthFactor(1);
274
275 return ret;
276 }
277
278
279
280 private NumDataSet getMinimalDataSet(String datname) {
281 markNeeded();
282
283 NumDataSet nds = dataExtractor.getDataSet(datname);
284
285 NumDataSet ret = nds.copyMarked();
286
287 return ret;
288
289 }
290
291
292
293
294
295
296 public void saveImage(File file, int ifr) {
297 showFrame(ifr);
298 BufferedImage bim = getBufferedImage(1);
299 ImageUtil.writePNG(bim, file);
300 }
301
302
303 public void saveThumbnailImage(File file, int ifr) {
304 showFrame(ifr);
305
306 BufferedImage bim = getBufferedImage(1);
307
308 int wscl = 140;
309 int hscl = 140;
310
311 int wf = bim.getWidth();
312 int hf = bim.getHeight();
313 double fo = ((float)wscl) / hscl;
314 double ff = ((float)wf) / hf;
315 if (ff > fo) {
316 hscl = (int)(wscl / ff);
317 } else {
318 wscl = (int)(hscl * ff);
319 }
320 int ithick = ((wf + wscl / 2) / wscl);
321 bim = getBufferedImage(ithick);
322 BufferedImage bufim = AWTUtil.getScaledBufferedImage(bim, wscl, hscl);
323 ImageUtil.writePNG(bufim, file);
324 }
325
326
327
328 public void makeMovie(File f) {
329 if (frameController != null) {
330 frameController.makeMovie(f);
331 } else {
332 E.error("no frame controller - cant make movie");
333 }
334 }
335
336
337 public void makeThumbnailMovie(File f) {
338 if (frameController != null) {
339 frameController.makeThumbnailMovie(f);
340 } else {
341 E.error("no frame controller - cant make movie");
342 }
343 }
344
345 }