|
|||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| GraphInternalFrame.java | 0% | 0% | 0% | 0% |
|
||||||||||||||
| 1 |
/*
|
|
| 2 |
* EJTools, the Enterprise Java Tools
|
|
| 3 |
*
|
|
| 4 |
* Distributable under LGPL license.
|
|
| 5 |
* See terms of license at www.gnu.org.
|
|
| 6 |
*/
|
|
| 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 |
/**
|
|
| 43 |
* Description of the Class
|
|
| 44 |
*
|
|
| 45 |
* @author Laurent Etiemble
|
|
| 46 |
* @version $Revision: 1.6 $
|
|
| 47 |
* @javabean:class displayName="Graph Frame"
|
|
| 48 |
* shortDescription="Graph Internal Frame"
|
|
| 49 |
* @javabean:property name="name"
|
|
| 50 |
* class="java.lang.String"
|
|
| 51 |
* displayName="Name"
|
|
| 52 |
* shortDescription="Graph name"
|
|
| 53 |
* @javabean:property name="delay"
|
|
| 54 |
* class="long"
|
|
| 55 |
* displayName="Refresh Delay"
|
|
| 56 |
* shortDescription="Time between two refresh"
|
|
| 57 |
* propertyEditor="org.ejtools.graph.editor.GraphDelayEditor"
|
|
| 58 |
* @javabean:property name="scale"
|
|
| 59 |
* class="double"
|
|
| 60 |
* displayName="Horizontal Scale"
|
|
| 61 |
* shortDescription="Horizontal Scale"
|
|
| 62 |
* propertyEditor="org.ejtools.graph.editor.GraphScaleEditor"
|
|
| 63 |
*/
|
|
| 64 |
public class GraphInternalFrame extends BeanContextInternalFrame implements GraphConsumer |
|
| 65 |
{
|
|
| 66 |
/** Description of the Field */
|
|
| 67 |
protected transient ControlCompositeTrack composite = null; |
|
| 68 |
/** Description of the Field */
|
|
| 69 |
protected transient JPanel controls = new JPanel(); |
|
| 70 |
/** Description of the Field */
|
|
| 71 |
protected long delay = 2500; |
|
| 72 |
/** Description of the Field */
|
|
| 73 |
protected int index = 0; |
|
| 74 |
/** Description of the Field */
|
|
| 75 |
protected MenuBarServiceProvider menubarProvider;
|
|
| 76 |
/** Description of the Field */
|
|
| 77 |
protected String name = "Untitled"; |
|
| 78 |
/** Description of the Field */
|
|
| 79 |
protected transient JPanel printArea = new JPanel(new BorderLayout()); |
|
| 80 |
/** Description of the Field */
|
|
| 81 |
protected transient Hashtable producers = new Hashtable(); |
|
| 82 |
/** Description of the Field */
|
|
| 83 |
protected transient JPanelGraphRenderer renderer = null; |
|
| 84 |
/** Description of the Field */
|
|
| 85 |
protected boolean running = true; |
|
| 86 |
/** Description of the Field */
|
|
| 87 |
protected double scale = 1.0d / 100; |
|
| 88 |
/** Description of the Field */
|
|
| 89 |
protected ToolBarServiceProvider toolbarProvider;
|
|
| 90 |
/** Description of the Field */
|
|
| 91 |
private static Logger logger = Logger.getLogger(GraphInternalFrame.class); |
|
| 92 |
/** Description of the Field */
|
|
| 93 |
private static ResourceBundle resources = ResourceBundle.getBundle("org.ejtools.graph.GraphService"); |
|
| 94 |
|
|
| 95 |
|
|
| 96 |
/** Constructor for GraphInternalFrame. */
|
|
| 97 | 0 |
public GraphInternalFrame()
|
| 98 |
{
|
|
| 99 | 0 |
super();
|
| 100 |
|
|
| 101 | 0 |
this.menubarProvider = new MenuBarServiceProvider(); |
| 102 | 0 |
this.toolbarProvider = new ToolBarServiceProvider(); |
| 103 |
|
|
| 104 | 0 |
this.composite = new ControlCompositeTrack(this); |
| 105 | 0 |
this.renderer = new TriAxisLayoutRenderer(); |
| 106 | 0 |
this.renderer.setGraphElement(new DefaultGraphElement()); |
| 107 | 0 |
this.renderer.setHorizontalScale(0.01);
|
| 108 |
|
|
| 109 | 0 |
this.add(this.menubarProvider); |
| 110 | 0 |
this.add(this.toolbarProvider); |
| 111 |
|
|
| 112 | 0 |
this.add(new DeleteAction( |
| 113 |
new Command()
|
|
| 114 |
{
|
|
| 115 | 0 |
public void execute() |
| 116 |
{
|
|
| 117 | 0 |
GraphInternalFrame.this.composite.clear();
|
| 118 |
} |
|
| 119 |
})); |
|
| 120 |
|
|
| 121 | 0 |
this.add(new ExportAsTextAction( |
| 122 |
new Command()
|
|
| 123 |
{
|
|
| 124 | 0 |
public void execute() |
| 125 |
{
|
|
| 126 | 0 |
StringBuffer buffer = GraphInternalFrame.this.composite.getPointsAsText();
|
| 127 | 0 |
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 | 0 |
if (file != null) |
| 129 |
{
|
|
| 130 | 0 |
CSVFileTools.exportAsCVS(buffer, file); |
| 131 |
} |
|
| 132 |
} |
|
| 133 |
})); |
|
| 134 |
|
|
| 135 | 0 |
this.add(new ExportAsImageAction( |
| 136 |
new Command()
|
|
| 137 |
{
|
|
| 138 | 0 |
public void execute() |
| 139 |
{
|
|
| 140 | 0 |
BufferedImage image = PNGFileTools.paintAsPNG(GraphInternalFrame.this.printArea);
|
| 141 | 0 |
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 | 0 |
if (file != null) |
| 143 |
{
|
|
| 144 | 0 |
PNGFileTools.exportAsPNG(image, file); |
| 145 |
} |
|
| 146 |
} |
|
| 147 |
})); |
|
| 148 |
|
|
| 149 | 0 |
this.frame.setJMenuBar((JMenuBar) this.menubarProvider.getContainer()); |
| 150 | 0 |
this.frame.getContentPane().add(this.toolbarProvider.getContainer(), BorderLayout.NORTH); |
| 151 | 0 |
this.printArea.add(new SimpleCustomizer(this), BorderLayout.NORTH); |
| 152 | 0 |
this.printArea.add(this.renderer, BorderLayout.CENTER); |
| 153 | 0 |
this.frame.getContentPane().add(this.printArea, BorderLayout.CENTER); |
| 154 |
|
|
| 155 |
// The timer to consume graph producers
|
|
| 156 | 0 |
Timer timer = new Timer();
|
| 157 | 0 |
timer.schedule( |
| 158 |
new TimerTask()
|
|
| 159 |
{
|
|
| 160 | 0 |
public void run() |
| 161 |
{
|
|
| 162 | 0 |
logger.debug("Start graph polling");
|
| 163 | 0 |
while (running)
|
| 164 |
{
|
|
| 165 | 0 |
consume(); |
| 166 | 0 |
try
|
| 167 |
{
|
|
| 168 | 0 |
Thread.sleep(delay); |
| 169 |
} |
|
| 170 |
catch (InterruptedException ie)
|
|
| 171 |
{
|
|
| 172 |
// Do nothing
|
|
| 173 |
} |
|
| 174 |
} |
|
| 175 | 0 |
logger.debug("Stop graph polling");
|
| 176 |
} |
|
| 177 |
}, 0); |
|
| 178 |
} |
|
| 179 |
|
|
| 180 |
|
|
| 181 |
/**
|
|
| 182 |
* @param producer The feature to be added to the GraphProducer attribute
|
|
| 183 |
*/
|
|
| 184 | 0 |
public void addGraphProducer(GraphProducer producer) |
| 185 |
{
|
|
| 186 | 0 |
if (this.producers.containsKey(producer)) |
| 187 |
{
|
|
| 188 | 0 |
return;
|
| 189 |
} |
|
| 190 | 0 |
if (this.producers.size() == 0) |
| 191 |
{
|
|
| 192 | 0 |
this.renderer.setGraphElement(this.composite); |
| 193 |
} |
|
| 194 | 0 |
Track t = new Track(producer.getGraphProducerId());
|
| 195 |
|
|
| 196 | 0 |
this.composite.addTrack(producer, t);
|
| 197 | 0 |
this.producers.put(producer, t);
|
| 198 |
|
|
| 199 | 0 |
this.refresh();
|
| 200 | 0 |
this.activate();
|
| 201 |
} |
|
| 202 |
|
|
| 203 |
|
|
| 204 |
/**
|
|
| 205 |
* Description of the Method
|
|
| 206 |
*
|
|
| 207 |
* @param producer Description of the Parameter
|
|
| 208 |
* @return Description of the Return Value
|
|
| 209 |
*/
|
|
| 210 | 0 |
public boolean containsGraphProducer(GraphProducer producer) |
| 211 |
{
|
|
| 212 | 0 |
return this.producers.containsKey(producer); |
| 213 |
} |
|
| 214 |
|
|
| 215 |
|
|
| 216 |
/**
|
|
| 217 |
* Returns the delay.
|
|
| 218 |
*
|
|
| 219 |
* @return long
|
|
| 220 |
*/
|
|
| 221 | 0 |
public long getDelay() |
| 222 |
{
|
|
| 223 | 0 |
return delay;
|
| 224 |
} |
|
| 225 |
|
|
| 226 |
|
|
| 227 |
/**
|
|
| 228 |
* Returns the name.
|
|
| 229 |
*
|
|
| 230 |
* @return String
|
|
| 231 |
*/
|
|
| 232 | 0 |
public String getName()
|
| 233 |
{
|
|
| 234 | 0 |
return this.name; |
| 235 |
} |
|
| 236 |
|
|
| 237 |
|
|
| 238 |
/**
|
|
| 239 |
* Returns the scale.
|
|
| 240 |
*
|
|
| 241 |
* @return double
|
|
| 242 |
*/
|
|
| 243 | 0 |
public double getScale() |
| 244 |
{
|
|
| 245 | 0 |
return scale;
|
| 246 |
} |
|
| 247 |
|
|
| 248 |
|
|
| 249 |
/**
|
|
| 250 |
* @param producer Description of the Parameter
|
|
| 251 |
*/
|
|
| 252 | 0 |
public void removeGraphProducer(GraphProducer producer) |
| 253 |
{
|
|
| 254 | 0 |
Track t = (Track) this.producers.get(producer);
|
| 255 |
|
|
| 256 | 0 |
this.composite.removeTrack(producer, t);
|
| 257 | 0 |
this.producers.remove(producer);
|
| 258 |
|
|
| 259 | 0 |
if (this.producers.size() == 0) |
| 260 |
{
|
|
| 261 | 0 |
this.renderer.setGraphElement(new DefaultGraphElement()); |
| 262 |
} |
|
| 263 |
|
|
| 264 | 0 |
this.refresh();
|
| 265 |
} |
|
| 266 |
|
|
| 267 |
|
|
| 268 |
/**
|
|
| 269 |
* @param delay The new delay value
|
|
| 270 |
*/
|
|
| 271 | 0 |
public void setDelay(long delay) |
| 272 |
{
|
|
| 273 | 0 |
long oldDelay = this.delay; |
| 274 | 0 |
this.delay = delay;
|
| 275 | 0 |
this.firePropertyChange("delay", new Long(oldDelay), new Long(this.delay)); |
| 276 |
} |
|
| 277 |
|
|
| 278 |
|
|
| 279 |
/**
|
|
| 280 |
* Sets the name.
|
|
| 281 |
*
|
|
| 282 |
* @param name The name to set
|
|
| 283 |
*/
|
|
| 284 | 0 |
public void setName(String name) |
| 285 |
{
|
|
| 286 | 0 |
String oldName = this.name;
|
| 287 | 0 |
this.name = name;
|
| 288 | 0 |
this.setTitle(resources.getString("graph.text.prefix") + " : " + name); |
| 289 | 0 |
this.firePropertyChange("name", oldName, this.name); |
| 290 |
} |
|
| 291 |
|
|
| 292 |
|
|
| 293 |
/**
|
|
| 294 |
* Sets the scale attribute of the GraphInternalFrame object
|
|
| 295 |
*
|
|
| 296 |
* @param scale The new scale value
|
|
| 297 |
*/
|
|
| 298 | 0 |
public void setScale(double scale) |
| 299 |
{
|
|
| 300 | 0 |
double oldScale = this.scale; |
| 301 | 0 |
this.scale = scale;
|
| 302 | 0 |
this.firePropertyChange("scale", new Double(oldScale), new Double(this.scale)); |
| 303 | 0 |
this.renderer.setHorizontalScale(scale);
|
| 304 | 0 |
SwingUtilities.invokeLater( |
| 305 |
new Runnable()
|
|
| 306 |
{
|
|
| 307 | 0 |
public void run() |
| 308 |
{
|
|
| 309 | 0 |
renderer.repaint(); |
| 310 |
} |
|
| 311 |
} |
|
| 312 |
); |
|
| 313 |
} |
|
| 314 |
|
|
| 315 |
|
|
| 316 |
/**
|
|
| 317 |
* @return Description of the Return Value
|
|
| 318 |
*/
|
|
| 319 | 0 |
public String toString()
|
| 320 |
{
|
|
| 321 | 0 |
return this.name; |
| 322 |
} |
|
| 323 |
|
|
| 324 |
|
|
| 325 |
/** Description of the Method */
|
|
| 326 | 0 |
protected void releaseBeanContextResources() |
| 327 |
{
|
|
| 328 | 0 |
this.running = false; |
| 329 | 0 |
super.releaseBeanContextResources();
|
| 330 |
} |
|
| 331 |
|
|
| 332 |
|
|
| 333 |
/** Description of the Method */
|
|
| 334 | 0 |
private void consume() |
| 335 |
{
|
|
| 336 | 0 |
if (this.producers.size() > 0) |
| 337 |
{
|
|
| 338 | 0 |
Hashtable copy = (Hashtable) this.producers.clone();
|
| 339 | 0 |
Iterator it = copy.keySet().iterator(); |
| 340 | 0 |
while (it.hasNext())
|
| 341 |
{
|
|
| 342 | 0 |
GraphProducer producer = (GraphProducer) it.next(); |
| 343 | 0 |
Track t = (Track) copy.get(producer); |
| 344 | 0 |
t.addValue(producer.produce()); |
| 345 |
} |
|
| 346 | 0 |
this.refresh();
|
| 347 |
} |
|
| 348 |
} |
|
| 349 |
|
|
| 350 |
|
|
| 351 |
/** Description of the Method */
|
|
| 352 | 0 |
private void refresh() |
| 353 |
{
|
|
| 354 | 0 |
SwingUtilities.invokeLater( |
| 355 |
new Runnable()
|
|
| 356 |
{
|
|
| 357 | 0 |
public void run() |
| 358 |
{
|
|
| 359 | 0 |
renderer.repaint(); |
| 360 |
} |
|
| 361 |
} |
|
| 362 |
); |
|
| 363 |
} |
|
| 364 |
} |
|
| 365 |
|
|
||||||||||