1 | package de.uka.ipd.sdq.sensorframework.visualisation.jfreechartvisualisation; |
2 | |
3 | import java.io.BufferedReader; |
4 | import java.io.File; |
5 | import java.io.FileReader; |
6 | |
7 | import org.eclipse.jface.action.Action; |
8 | import org.eclipse.swt.widgets.FileDialog; |
9 | import org.eclipse.ui.PlatformUI; |
10 | |
11 | import de.uka.ipd.sdq.codegen.simudatavisualisation.datatypes.Histogram; |
12 | import de.uka.ipd.sdq.codegen.simudatavisualisation.datatypes.HistogramBucketInformation; |
13 | |
14 | public class LoadCSVHistogram extends Action { |
15 | private Histogram histogram = null; |
16 | private IHistogramAccepter viewer; |
17 | |
18 | public Histogram getHistogram() { |
19 | return histogram; |
20 | } |
21 | |
22 | public LoadCSVHistogram(IHistogramAccepter freeChartHistogramViewer) { |
23 | super(); |
24 | setText("Load CSV..."); |
25 | this.viewer = freeChartHistogramViewer; |
26 | } |
27 | |
28 | @Override |
29 | public void run() { |
30 | FileDialog dialog = new FileDialog(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell()); |
31 | dialog.open(); |
32 | if (dialog.getFileName() != null){ |
33 | this.histogram = importHistogram(dialog.getFilterPath()+File.separator+dialog.getFileName()); |
34 | } |
35 | viewer.addHistogram(histogram); |
36 | } |
37 | |
38 | private Histogram importHistogram(String fileName) { |
39 | Histogram h = new Histogram("Measurements"); |
40 | FileReader fileReader; |
41 | BufferedReader bufferedReader; |
42 | try { |
43 | String row; |
44 | fileReader = new FileReader(fileName); |
45 | bufferedReader = new BufferedReader(fileReader); |
46 | while((row=bufferedReader.readLine()) != null) |
47 | { |
48 | String[] parts = row.split(";"); |
49 | double x = Double.parseDouble(parts[0].replace(',','.')); |
50 | double y = Double.parseDouble(parts[1].replace(',','.')); |
51 | h.addEntity(new HistogramBucketInformation(y, x)); |
52 | } |
53 | } catch(Exception e) { |
54 | return null; |
55 | // TODO: Error Message Box |
56 | } |
57 | for (HistogramBucketInformation e:h.getBucketInformation()) |
58 | System.out.print("runif("+Math.round(e.getProbability()*100000)+","+(e.getValue()-5)+","+(e.getValue()+5)+"),"); |
59 | return h; |
60 | } |
61 | } |