View Javadoc

1   package org.catacomb.dataview.gui;
2   
3   import java.awt.image.BufferedImage;
4   import java.io.File;
5   import java.util.ArrayList;
6   
7   import javax.imageio.ImageIO;
8   
9   import org.catacomb.dataview.display.ViewConfig;
10  import org.catacomb.dataview.formats.DataHandler;
11  import org.catacomb.dataview.formats.PolyLineHandler;
12  import org.catacomb.dataview.model.LineGraph;
13  import org.catacomb.dataview.model.Plottable;
14  import org.catacomb.dataview.model.View;
15  import org.catacomb.druid.dialog.Dialoguer;
16  import org.catacomb.druid.gui.edit.DruMenu;
17  import org.catacomb.druid.load.DruidResourceLoader;
18  import org.catacomb.interlish.annotation.ControlPoint;
19  import org.catacomb.interlish.annotation.IOPoint;
20  import org.catacomb.interlish.reflect.ReflectionConstructor;
21  import org.catacomb.interlish.service.ResourceAccess;
22  import org.catacomb.interlish.structure.Controller;
23  import org.catacomb.report.E;
24  import org.catacomb.serial.Deserializer;
25  import org.catacomb.util.FileUtil;
26  
27  public class DViewController implements Controller {
28  
29  
30      @IOPoint(xid="ViewMenu")
31      public DruMenu viewMenu;
32  
33      @ControlPoint(xid="plotController")
34      public DViewPlotController basicController;
35  
36  
37      private DataHandler dataHandler;
38  
39      ArrayList<ViewConfig> viewConfigs;
40  
41      public DViewController() {
42  
43      }
44  
45  
46      public void open() {
47          File f = Dialoguer.getFile("CCViz");
48          if (f != null) {
49              open(f);
50          }
51      }
52  
53      public void open(File fin) {
54          File f = fin;
55          if (f.isDirectory()) {
56              f = findConfig(f);
57          }
58  
59  
60          DruidResourceLoader drl = new DruidResourceLoader();
61          drl.addPath("org.catacomb.dataview.model");
62          ResourceAccess.setResourceLoader(drl);
63  
64          ReflectionConstructor.addPath("org.catacomb.dataview.model");
65  
66          String s = FileUtil.readStringFromFile(f);
67          Object obj = Deserializer.deserialize(s);
68  
69          if (obj instanceof LineGraph) {
70              LineGraph lineGraph = (LineGraph)obj;
71  
72              PolyLineHandler plh = new PolyLineHandler();
73              dataHandler = plh;
74  
75              for (Plottable pl : lineGraph.getPlottables()) {
76                  plh.addItems(pl.getDisplayables(f.getParentFile()));
77              }
78  
79              plh.setXAxis(lineGraph.getXAxis());
80              plh.setYAxis(lineGraph.getYAxis());
81  
82              viewConfigs = new ArrayList<ViewConfig>();
83              for (View v : lineGraph.getViews()) {
84                  ViewConfig vc = new ViewConfig(v.getID(), v.getXYXY());
85                  viewConfigs.add(vc);
86                  plh.addView(vc);
87              }
88  
89              int w = lineGraph.getWidth();
90              int h = lineGraph.getHeight();
91              if (w > 99 && h > 99) {
92                  basicController.setViewSize(w, h);
93              }
94  
95  
96              basicController.setDataHandler(plh);
97          }
98  
99  
100 
101 
102 
103 
104         /*
105            String s = FileUtil.readFirstLine(f);
106 
107            dataHandler = DataHandlerFactory.getHandler(s);
108            if (dataHandler == null) {
109              int[] xy = {500, 500};
110              Dialoguer.message(xy, "unrecognized file - cant plot " + s);
111            } else {
112               dataHandler.read(f);
113 
114               syncOptions();
115 
116               int ic = dataHandler.getContentStyle();
117               if (ic == DataHandler.STATIC) {
118                  basicController.setDataHandler(dataHandler);
119 
120               } else {
121                  E.missing();
122               }
123 
124            }
125 
126            */
127     }
128 
129     private File findConfig(File fdir) {
130         File ret = null;
131         for (File f : fdir.listFiles()) {
132             String s = FileUtil.readFirstLine(f);
133             s = s.trim();
134             if (s.startsWith("<") && s.indexOf("LineGraph") > 0) {
135                 ret = f;
136                 break;
137             }
138         }
139         return ret;
140     }
141 
142 
143     public void requestClose() {
144         exit();
145     }
146 
147 
148     public void reload() {
149         E.info("time to reload...");
150     }
151 
152 
153     public void requestExit() {
154         exit();
155     }
156 
157 
158     public void exit() {
159         System.exit(0);
160     }
161 
162 
163     public void syncOptions() {
164         String[] sa = new String[0];
165         if (dataHandler != null) {
166             sa = dataHandler.getViewOptions();
167         }
168         viewMenu.setOptions(sa);
169     }
170 
171 
172     public void setViewStyle(String s) {
173 
174         if (dataHandler != null) {
175             dataHandler.setViewStyle(s);
176         }
177 
178         basicController.repaint();
179     }
180 
181 
182     public void attached() {
183         // TODO Auto-generated method stub
184 
185     }
186 
187 
188     public void makeImages(File fdir) {
189         for (ViewConfig vc : viewConfigs) {
190 
191             basicController.showPlot(vc.getID());
192 
193             File fout = new File(fdir, vc.getID().replace(" ", "_") + ".png");
194             try {
195                 Thread.sleep(1000);
196             } catch (Exception ex) {
197 
198             }
199             BufferedImage bim = basicController.getSnapshot();
200             try {
201                 ImageIO.write(bim, "png", fout);
202             } catch (Exception ex) {
203                 ex.printStackTrace();
204             }
205         }
206 
207     }
208 
209 }