| 1 | package de.uka.ipd.sdq.sensorframework.visualisation.jfreechartvisualisation; |
| 2 | |
| 3 | import java.awt.Color; |
| 4 | import java.awt.Paint; |
| 5 | import java.awt.Rectangle; |
| 6 | import java.io.File; |
| 7 | import java.io.FileOutputStream; |
| 8 | import java.io.IOException; |
| 9 | import java.io.OutputStreamWriter; |
| 10 | import java.io.Writer; |
| 11 | import java.util.Collection; |
| 12 | |
| 13 | import org.apache.batik.dom.GenericDOMImplementation; |
| 14 | import org.apache.batik.svggen.SVGGraphics2D; |
| 15 | import org.eclipse.jface.action.Action; |
| 16 | import org.eclipse.jface.action.MenuManager; |
| 17 | import org.eclipse.swt.SWT; |
| 18 | import org.eclipse.swt.widgets.Composite; |
| 19 | import org.eclipse.swt.widgets.FileDialog; |
| 20 | import org.eclipse.swt.widgets.Menu; |
| 21 | import org.eclipse.ui.PlatformUI; |
| 22 | import org.jfree.chart.ChartUtilities; |
| 23 | import org.jfree.chart.JFreeChart; |
| 24 | import org.jfree.chart.title.LegendTitle; |
| 25 | import org.jfree.experimental.chart.swt.ChartComposite; |
| 26 | import org.w3c.dom.DOMImplementation; |
| 27 | import org.w3c.dom.Document; |
| 28 | |
| 29 | class SaveImageAsAction extends Action { |
| 30 | |
| 31 | |
| 32 | private AbstractJFreeChartChart<?> chartViewer; |
| 33 | |
| 34 | public SaveImageAsAction(AbstractJFreeChartChart<?> abstractJFreeChartChart) { |
| 35 | super(); |
| 36 | setText("Save Chart as PNG..."); |
| 37 | this.chartViewer = abstractJFreeChartChart; |
| 38 | } |
| 39 | |
| 40 | @Override |
| 41 | public void run() { |
| 42 | FileDialog dialog = new FileDialog(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell()); |
| 43 | dialog.setFilterExtensions(new String[]{"*.png"}); |
| 44 | dialog.setText("Enter the image file name"); |
| 45 | dialog.open(); |
| 46 | if (dialog.getFileName() != null){ |
| 47 | String filename = dialog.getFilterPath()+File.separator+dialog.getFileName(); |
| 48 | File f = new File(filename); |
| 49 | JFreeChart chart = chartViewer.getChart(); |
| 50 | try { |
| 51 | ChartUtilities.saveChartAsPNG(f, chart, chartViewer.getBounds().width, chartViewer.getBounds().height); |
| 52 | } catch (IOException e) { |
| 53 | // TODO Auto-generated catch block |
| 54 | e.printStackTrace(); |
| 55 | } |
| 56 | } |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | class ToggleLegendAction extends Action { |
| 61 | |
| 62 | |
| 63 | private AbstractJFreeChartChart<?> chartViewer; |
| 64 | private LegendTitle chartLegend; |
| 65 | |
| 66 | public ToggleLegendAction(AbstractJFreeChartChart<?> abstractJFreeChartChart) { |
| 67 | super(); |
| 68 | setText("Enable/Disable Legend"); |
| 69 | this.chartViewer = abstractJFreeChartChart; |
| 70 | this.chartLegend = null; |
| 71 | } |
| 72 | |
| 73 | @Override |
| 74 | public void run() { |
| 75 | if (chartLegend == null) { |
| 76 | chartLegend = this.chartViewer.getChart().getLegend(); |
| 77 | this.chartViewer.getChart().removeLegend(); |
| 78 | } else { |
| 79 | this.chartViewer.getChart().addLegend(chartLegend); |
| 80 | chartLegend = null; |
| 81 | } |
| 82 | this.chartViewer.forceRedraw(); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | class SaveSVGAsAction extends Action { |
| 87 | |
| 88 | |
| 89 | private AbstractJFreeChartChart<?> chartViewer; |
| 90 | |
| 91 | public SaveSVGAsAction(AbstractJFreeChartChart<?> abstractJFreeChartChart) { |
| 92 | super(); |
| 93 | setText("Save Chart as SVG..."); |
| 94 | this.chartViewer = abstractJFreeChartChart; |
| 95 | } |
| 96 | |
| 97 | @Override |
| 98 | public void run() { |
| 99 | FileDialog dialog = new FileDialog(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell()); |
| 100 | dialog.setFilterExtensions(new String[]{"*.svg"}); |
| 101 | dialog.setText("Enter the image file name"); |
| 102 | dialog.open(); |
| 103 | if (dialog.getFileName() != null){ |
| 104 | String filename = dialog.getFilterPath()+File.separator+dialog.getFileName(); |
| 105 | File f = new File(filename); |
| 106 | JFreeChart chart = chartViewer.getChart(); |
| 107 | try { |
| 108 | DOMImplementation domI = new GenericDOMImplementation(); |
| 109 | Document doc = domI.createDocument(null, "svg", null); |
| 110 | SVGGraphics2D svgRenderer = new SVGGraphics2D(doc); |
| 111 | Paint p = chart.getBackgroundPaint(); |
| 112 | chart.setBackgroundPaint(new Color(0, 0, 0, 0)); |
| 113 | chart.draw(svgRenderer, new Rectangle(0,0,640,480)); |
| 114 | chart.setBackgroundPaint(p); |
| 115 | Writer out = new OutputStreamWriter(new FileOutputStream(f),"UTF-8"); |
| 116 | svgRenderer.stream(out,true); |
| 117 | out.close(); |
| 118 | } catch (IOException e) { |
| 119 | e.printStackTrace(); |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | public abstract class AbstractJFreeChartChart<T> extends ChartComposite { |
| 126 | protected JFreeChart chart; |
| 127 | |
| 128 | @Override |
| 129 | protected Menu createPopupMenu(boolean arg0, boolean arg1, boolean arg2, |
| 130 | boolean arg3) { |
| 131 | Menu parentMenu = super.createPopupMenu(arg0, arg1, arg2, arg3); |
| 132 | MenuManager menu_manager = new MenuManager("Additional Functions"); |
| 133 | initializeContextMenu(menu_manager); |
| 134 | menu_manager.fill(parentMenu,SWT.NONE); |
| 135 | return parentMenu; |
| 136 | } |
| 137 | |
| 138 | public AbstractJFreeChartChart(Composite parent, int style) { |
| 139 | super(parent, style); |
| 140 | // final Graphics2DRenderer renderer = new Graphics2DRenderer(); |
| 141 | initChart(); |
| 142 | |
| 143 | // |
| 144 | // addPaintListener(new PaintListener() { |
| 145 | // |
| 146 | // public void paintControl(org.eclipse.swt.events.PaintEvent e) { |
| 147 | // Point controlSize = ((Control) e.getSource()).getSize(); |
| 148 | // |
| 149 | // GC gc = e.gc; // gets the SWT graphics context from the event |
| 150 | // |
| 151 | // renderer.prepareRendering(gc); // prepares the Graphics2D renderer |
| 152 | // |
| 153 | // // gets the Graphics2D context and switch on the antialiasing |
| 154 | // Graphics2D g2d = renderer.getGraphics2D(); |
| 155 | // |
| 156 | // if(myChart != null) |
| 157 | // myChart.draw(g2d, new Rectangle(0,0,controlSize.x,controlSize.y)); |
| 158 | // else |
| 159 | // g2d.drawString("No data yet", 5, 20); |
| 160 | // // now that we are done with Java 2D, renders Graphics2D operation |
| 161 | // // on the SWT graphics context |
| 162 | // renderer.render(gc); |
| 163 | // |
| 164 | // } |
| 165 | // }); |
| 166 | } |
| 167 | |
| 168 | protected void initializeContextMenu(MenuManager menu_manager) { |
| 169 | menu_manager.add(new ToggleLegendAction(this)); |
| 170 | menu_manager.add(new SaveImageAsAction(this)); |
| 171 | menu_manager.add(new SaveSVGAsAction(this)); |
| 172 | } |
| 173 | |
| 174 | protected abstract void initChart(); |
| 175 | |
| 176 | public abstract void setData(Collection<T> data); |
| 177 | |
| 178 | } |