1 7 package org.ejtools.graph.frame; 8 9 import java.awt.BorderLayout ; 10 import java.awt.image.BufferedImage ; 11 import java.io.File ; 12 import java.util.Hashtable ; 13 import java.util.Iterator ; 14 import java.util.ResourceBundle ; 15 import java.util.Timer ; 16 import java.util.TimerTask ; 17 18 import javax.swing.JFileChooser ; 19 import javax.swing.JMenuBar ; 20 import javax.swing.JPanel ; 21 import javax.swing.SwingUtilities ; 22 23 import org.apache.log4j.Logger; 24 import org.ejtools.adwt.SimpleCustomizer; 25 import org.ejtools.adwt.action.Command; 26 import org.ejtools.adwt.action.edit.DeleteAction; 27 import org.ejtools.adwt.service.BeanContextInternalFrame; 28 import org.ejtools.adwt.service.MenuBarServiceProvider; 29 import org.ejtools.adwt.service.ToolBarServiceProvider; 30 import org.ejtools.graph.DefaultGraphElement; 31 import org.ejtools.graph.Track; 32 import org.ejtools.graph.action.ExportAsImageAction; 33 import org.ejtools.graph.action.ExportAsTextAction; 34 import org.ejtools.graph.renderer.JPanelGraphRenderer; 35 import org.ejtools.graph.renderer.TriAxisLayoutRenderer; 36 import org.ejtools.graph.service.GraphConsumer; 37 import org.ejtools.graph.service.GraphProducer; 38 import org.ejtools.util.FileTools; 39 import org.ejtools.util.export.CSVFileTools; 40 import org.ejtools.util.export.PNGFileTools; 41 42 64 public class GraphInternalFrame extends BeanContextInternalFrame implements GraphConsumer 65 { 66 67 protected transient ControlCompositeTrack composite = null; 68 69 protected transient JPanel controls = new JPanel (); 70 71 protected long delay = 2500; 72 73 protected int index = 0; 74 75 protected MenuBarServiceProvider menubarProvider; 76 77 protected String name = "Untitled"; 78 79 protected transient JPanel printArea = new JPanel (new BorderLayout ()); 80 81 protected transient Hashtable producers = new Hashtable (); 82 83 protected transient JPanelGraphRenderer renderer = null; 84 85 protected boolean running = true; 86 87 protected double scale = 1.0d / 100; 88 89 protected ToolBarServiceProvider toolbarProvider; 90 91 private static Logger logger = Logger.getLogger(GraphInternalFrame.class); 92 93 private static ResourceBundle resources = ResourceBundle.getBundle("org.ejtools.graph.GraphService"); 94 95 96 97 public GraphInternalFrame() 98 { 99 super(); 100 101 this.menubarProvider = new MenuBarServiceProvider(); 102 this.toolbarProvider = new ToolBarServiceProvider(); 103 104 this.composite = new ControlCompositeTrack(this); 105 this.renderer = new TriAxisLayoutRenderer(); 106 this.renderer.setGraphElement(new DefaultGraphElement()); 107 this.renderer.setHorizontalScale(0.01); 108 109 this.add(this.menubarProvider); 110 this.add(this.toolbarProvider); 111 112 this.add(new DeleteAction( 113 new Command() 114 { 115 public void execute() 116 { 117 GraphInternalFrame.this.composite.clear(); 118 } 119 })); 120 121 this.add(new ExportAsTextAction( 122 new Command() 123 { 124 public void execute() 125 { 126 StringBuffer buffer = GraphInternalFrame.this.composite.getPointsAsText(); 127 File file = FileTools.selectFile(resources.getString("file.dialog.title.export.csv"), resources.getString("file.dialog.button.export.csv"), JFileChooser.SAVE_DIALOG, CSVFileTools.CSV_FILE_FILTER); 128 if (file != null) 129 { 130 CSVFileTools.exportAsCVS(buffer, file); 131 } 132 } 133 })); 134 135 this.add(new ExportAsImageAction( 136 new Command() 137 { 138 public void execute() 139 { 140 BufferedImage image = PNGFileTools.paintAsPNG(GraphInternalFrame.this.printArea); 141 File file = FileTools.selectFile(resources.getString("file.dialog.title.export.png"), resources.getString("file.dialog.button.export.png"), JFileChooser.SAVE_DIALOG, PNGFileTools.PNG_FILE_FILTER); 142 if (file != null) 143 { 144 PNGFileTools.exportAsPNG(image, file); 145 } 146 } 147 })); 148 149 this.frame.setJMenuBar((JMenuBar ) this.menubarProvider.getContainer()); 150 this.frame.getContentPane().add(this.toolbarProvider.getContainer(), BorderLayout.NORTH); 151 this.printArea.add(new SimpleCustomizer(this), BorderLayout.NORTH); 152 this.printArea.add(this.renderer, BorderLayout.CENTER); 153 this.frame.getContentPane().add(this.printArea, BorderLayout.CENTER); 154 155 Timer timer = new Timer (); 157 timer.schedule( 158 new TimerTask () 159 { 160 public void run() 161 { 162 logger.debug("Start graph polling"); 163 while (running) 164 { 165 consume(); 166 try 167 { 168 Thread.sleep(delay); 169 } 170 catch (InterruptedException ie) 171 { 172 } 174 } 175 logger.debug("Stop graph polling"); 176 } 177 }, 0); 178 } 179 180 181 184 public void addGraphProducer(GraphProducer producer) 185 { 186 if (this.producers.containsKey(producer)) 187 { 188 return; 189 } 190 if (this.producers.size() == 0) 191 { 192 this.renderer.setGraphElement(this.composite); 193 } 194 Track t = new Track(producer.getGraphProducerId()); 195 196 this.composite.addTrack(producer, t); 197 this.producers.put(producer, t); 198 199 this.refresh(); 200 this.activate(); 201 } 202 203 204 210 public boolean containsGraphProducer(GraphProducer producer) 211 { 212 return this.producers.containsKey(producer); 213 } 214 215 216 221 public long getDelay() 222 { 223 return delay; 224 } 225 226 227 232 public String getName() 233 { 234 return this.name; 235 } 236 237 238 243 public double getScale() 244 { 245 return scale; 246 } 247 248 249 252 public void removeGraphProducer(GraphProducer producer) 253 { 254 Track t = (Track) this.producers.get(producer); 255 256 this.composite.removeTrack(producer, t); 257 this.producers.remove(producer); 258 259 if (this.producers.size() == 0) 260 { 261 this.renderer.setGraphElement(new DefaultGraphElement()); 262 } 263 264 this.refresh(); 265 } 266 267 268 271 public void setDelay(long delay) 272 { 273 long oldDelay = this.delay; 274 this.delay = delay; 275 this.firePropertyChange("delay", new Long (oldDelay), new Long (this.delay)); 276 } 277 278 279 284 public void setName(String name) 285 { 286 String oldName = this.name; 287 this.name = name; 288 this.setTitle(resources.getString("graph.text.prefix") + " : " + name); 289 this.firePropertyChange("name", oldName, this.name); 290 } 291 292 293 298 public void setScale(double scale) 299 { 300 double oldScale = this.scale; 301 this.scale = scale; 302 this.firePropertyChange("scale", new Double (oldScale), new Double (this.scale)); 303 this.renderer.setHorizontalScale(scale); 304 SwingUtilities.invokeLater( 305 new Runnable () 306 { 307 public void run() 308 { 309 renderer.repaint(); 310 } 311 } 312 ); 313 } 314 315 316 319 public String toString() 320 { 321 return this.name; 322 } 323 324 325 326 protected void releaseBeanContextResources() 327 { 328 this.running = false; 329 super.releaseBeanContextResources(); 330 } 331 332 333 334 private void consume() 335 { 336 if (this.producers.size() > 0) 337 { 338 Hashtable copy = (Hashtable ) this.producers.clone(); 339 Iterator it = copy.keySet().iterator(); 340 while (it.hasNext()) 341 { 342 GraphProducer producer = (GraphProducer) it.next(); 343 Track t = (Track) copy.get(producer); 344 t.addValue(producer.produce()); 345 } 346 this.refresh(); 347 } 348 } 349 350 351 352 private void refresh() 353 { 354 SwingUtilities.invokeLater( 355 new Runnable () 356 { 357 public void run() 358 { 359 renderer.repaint(); 360 } 361 } 362 ); 363 } 364 } 365 | Popular Tags |