1 | package de.uka.ipd.sdq.sensorframework.visualisation.jfreechartvisualisation; |
2 | |
3 | import java.io.BufferedWriter; |
4 | import java.io.File; |
5 | import java.io.FileWriter; |
6 | |
7 | import org.apache.log4j.Logger; |
8 | import org.eclipse.jface.action.Action; |
9 | import org.eclipse.swt.SWT; |
10 | import org.eclipse.swt.widgets.FileDialog; |
11 | import org.eclipse.ui.PlatformUI; |
12 | import org.jfree.data.xy.XYDataItem; |
13 | import org.jfree.data.xy.XYSeries; |
14 | |
15 | public class ExportCSV extends Action { |
16 | /** Logger for this class. */ |
17 | private static final Logger logger = Logger.getLogger(ExportCSV.class); |
18 | |
19 | private ISeriesExporter viewer; |
20 | |
21 | public ExportCSV(ISeriesExporter freeChartHistogramViewer) { |
22 | super(); |
23 | setText("Export CSV..."); |
24 | this.viewer = freeChartHistogramViewer; |
25 | } |
26 | |
27 | @Override |
28 | public void run() { |
29 | FileDialog dialog = new FileDialog(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), SWT.SAVE); |
30 | dialog.open(); |
31 | if (dialog.getFileName() != null){ |
32 | exportCSV(dialog.getFilterPath() + File.separatorChar + dialog.getFileName(), viewer.getSeries()); |
33 | } |
34 | } |
35 | |
36 | private void exportCSV(String filename, XYSeries series) { |
37 | FileWriter fileWriter; |
38 | BufferedWriter bufferedWriter; |
39 | try{ |
40 | fileWriter = new FileWriter(filename); |
41 | bufferedWriter = new BufferedWriter(fileWriter); |
42 | for(Object item : series.getItems()){ |
43 | XYDataItem data = (XYDataItem) item; |
44 | bufferedWriter.append((data.getX()+";"+data.getY()+"\n").replace(".", ",")); |
45 | } |
46 | bufferedWriter.close(); |
47 | fileWriter.close(); |
48 | } catch(Exception e) { |
49 | logger.error("Error writing CSV file.", e); |
50 | } |
51 | } |
52 | } |