]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.simulation/src/org/simantics/simulation/export/CSVFormat.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.simulation / src / org / simantics / simulation / export / CSVFormat.java
1 package org.simantics.simulation.export;
2
3 import java.io.File;
4 import java.io.IOException;
5 import java.text.DecimalFormatSymbols;
6 import java.util.Collections;
7 import java.util.List;
8 import java.util.Locale;
9
10 import org.eclipse.core.runtime.preferences.InstanceScope;
11 import org.eclipse.jface.preference.IPreferenceStore;
12 import org.eclipse.ui.preferences.ScopedPreferenceStore;
13 import org.osgi.service.prefs.Preferences;
14 import org.simantics.databoard.Accessors;
15 import org.simantics.databoard.Bindings;
16 import org.simantics.databoard.Datatypes;
17 import org.simantics.databoard.accessor.RecordAccessor;
18 import org.simantics.databoard.accessor.error.AccessorConstructionException;
19 import org.simantics.databoard.accessor.error.AccessorException;
20 import org.simantics.databoard.accessor.reference.ChildReference;
21 import org.simantics.databoard.binding.mutable.Variant;
22 import org.simantics.databoard.type.Datatype;
23 import org.simantics.databoard.type.DoubleType;
24 import org.simantics.databoard.type.RecordType;
25 import org.simantics.export.core.ExportContext;
26 import org.simantics.export.core.error.ExportException;
27 import org.simantics.export.core.intf.FormatClass;
28 import org.simantics.export.core.manager.Content;
29 import org.simantics.history.HistoryException;
30 import org.simantics.history.csv.ColumnSeparator;
31 import org.simantics.history.csv.DecimalSeparator;
32
33 /**
34  * This class represents the CSV (Comma Separated Value) file format.
35  *
36  * @author toni.kalajainen@semantum.fi
37  */
38 public class CSVFormat implements FormatClass {
39
40         // Accessor paths
41         public static ChildReference P_CSV_COLUMN_SEPARATOR = ChildReference.parsePath("Comma Separated Value (CSV)/Column Separator");
42         public static ChildReference P_CSV_DECIMAL_SEPARATOR = ChildReference.parsePath("Comma Separated Value (CSV)/Decimal Separator");
43         public static ChildReference P_CSV_TIME_STEP = ChildReference.parsePath("Comma Separated Value (CSV)/Time Step");       
44         
45         static RecordType options;
46     static RecordType csvOptions;
47     
48     static {
49             Datatype second = new DoubleType("s");
50         
51         csvOptions = new RecordType();
52         csvOptions.addComponent("Time Step", second);
53         csvOptions.addComponent("Column Separator", Datatypes.STRING);
54         csvOptions.addComponent("Decimal Separator", Datatypes.STRING);
55
56         options = new RecordType();
57         options.addComponent("Comma Separated Value (CSV)", csvOptions);
58     }
59         
60         @Override
61         public RecordType options(ExportContext context)
62         throws ExportException {
63                 return options;
64         }
65
66         @Override
67         public List<String> validate(ExportContext context, Variant options) throws ExportException {
68                 return Collections.emptyList();
69         }
70         
71
72         @Override
73         public void fillDefaultPrefs( ExportContext ctx, Variant options ) throws ExportException {
74                 // 1. Figure out suitable default values
75         IPreferenceStore csvnode = new ScopedPreferenceStore( InstanceScope.INSTANCE, CSVPreferences.P_NODE );
76         
77         Double timeStep = CSVPreferences.DEFAULT_CSV_TIME_STEP;
78         String decimalSeparator = ".";
79         String columnSeparator = "\t";
80         
81         Locale locale = Locale.getDefault();
82         DecimalFormatSymbols symbols = DecimalFormatSymbols.getInstance( locale );
83         decimalSeparator = symbols.getDecimalSeparator()+""; 
84         columnSeparator = decimalSeparator.equals(",")?"\\t":",";
85
86         if (csvnode != null) {
87                 if ( csvnode.contains(CSVPreferences.P_CSV_TIME_STEP) ) timeStep = csvnode.getDouble(CSVPreferences.P_CSV_TIME_STEP);
88                 if ( csvnode.contains(CSVPreferences.P_CSV_DECIMAL_SEPARATOR) ) decimalSeparator = csvnode.getString(CSVPreferences.P_CSV_DECIMAL_SEPARATOR);
89                 if ( csvnode.contains(CSVPreferences.P_CSV_COLUMN_SEPARATOR) ) columnSeparator = csvnode.getString(CSVPreferences.P_CSV_COLUMN_SEPARATOR);
90         }
91                 
92         // 2. Write default values
93         try {
94                         RecordAccessor ra = Accessors.getAccessor(options);
95                         ra.setValue(P_CSV_COLUMN_SEPARATOR, Bindings.STRING, columnSeparator);
96                         ra.setValue(P_CSV_DECIMAL_SEPARATOR, Bindings.STRING, decimalSeparator);
97                         ra.setValue(P_CSV_TIME_STEP, Bindings.DOUBLE, timeStep);
98                         
99                 } catch (AccessorConstructionException e) {
100                         throw new ExportException(e);
101                 } catch (AccessorException e) {
102                         throw new ExportException(e);
103                 }
104         }
105
106         @Override
107         public void savePref(Variant options, Preferences contentScopeNode, Preferences workbenchScopeNode) throws ExportException {
108                 try {
109                         RecordAccessor ra = Accessors.getAccessor(options);
110                         
111                         String columnSeparator = (String) ra.getValue( P_CSV_COLUMN_SEPARATOR, Bindings.STRING );
112                         if ( columnSeparator != null ) workbenchScopeNode.put(CSVPreferences.P_CSV_COLUMN_SEPARATOR, columnSeparator);                  
113                         
114                         String decimalSeparator = (String) ra.getValue(P_CSV_DECIMAL_SEPARATOR, Bindings.STRING );
115                         if ( decimalSeparator != null ) workbenchScopeNode.put(CSVPreferences.P_CSV_DECIMAL_SEPARATOR, decimalSeparator);                       
116                         
117                         Double timeStep = (Double) ra.getValue(P_CSV_TIME_STEP, Bindings.DOUBLE );
118                         if ( timeStep != null ) contentScopeNode.putDouble(CSVPreferences.P_CSV_TIME_STEP, timeStep);                   
119
120                 } catch (AccessorConstructionException e) {
121                         throw new ExportException(e);
122                 } catch (AccessorException e) {
123                         throw new ExportException(e);
124                 }               
125         }
126
127         @Override
128         public void loadPref(Variant options, Preferences contentScopeNode, Preferences workbenchScopeNode) throws ExportException {
129                 try {
130                         RecordAccessor ra = Accessors.getAccessor(options);
131                         
132                         String columnSeparator = workbenchScopeNode.get(CSVPreferences.P_CSV_COLUMN_SEPARATOR, null);
133                         if ( columnSeparator != null ) ra.setValue(P_CSV_COLUMN_SEPARATOR, Bindings.STRING, columnSeparator );                  
134                         
135                         String decimalSeparator = workbenchScopeNode.get(CSVPreferences.P_CSV_DECIMAL_SEPARATOR, null);
136                         if ( decimalSeparator != null ) ra.setValue(P_CSV_DECIMAL_SEPARATOR, Bindings.STRING, decimalSeparator );
137                         
138                         Double timeStep = contentScopeNode.getDouble(CSVPreferences.P_CSV_TIME_STEP, 0);
139                         if ( timeStep != null ) ra.setValue(P_CSV_TIME_STEP, Bindings.DOUBLE, timeStep );                       
140
141                 } catch (AccessorConstructionException e) {
142                         throw new ExportException(e);
143                 } catch (AccessorException e) {
144                         throw new ExportException(e);
145                 }               
146         }
147         
148         @Override
149         public Object createFile(ExportContext context, File outputFile, Variant options) throws ExportException {
150                 CSVWriter writer = new CSVWriter();
151                 writer.file = outputFile;
152
153                 try {
154                         /// Read configurations 
155                         RecordAccessor ra = Accessors.getAccessor(options);
156                                 
157                         // Start, End time
158                         Double startTime = (Double) ra.getValue( ExperimentExportClass.P_EXPERIMENT_START, Bindings.DOUBLE );
159                         Double endTime = (Double) ra.getValue( ExperimentExportClass.P_EXPERIMENT_END, Bindings.DOUBLE );
160                         writer.setTimeRange(startTime, endTime);
161                 
162                 // Separators
163                         String columnSeparator = (String) ra.getValue( CSVFormat.P_CSV_COLUMN_SEPARATOR, Bindings.STRING );
164                         writer.setColumnSeparator(ColumnSeparator.fromPreference(columnSeparator));
165                         
166                         String decimalSeparator = (String) ra.getValue( CSVFormat.P_CSV_DECIMAL_SEPARATOR, Bindings.STRING );
167                         writer.setDecimalSeparator(DecimalSeparator.fromPreference(decimalSeparator));
168                         
169                         Double timeStep = (Double) ra.getValue( CSVFormat.P_CSV_TIME_STEP, Bindings.DOUBLE );
170                         writer.setTimeStep(timeStep);
171                         
172                 } catch (AccessorConstructionException e) {
173                         throw new ExportException(e);
174                 } catch (AccessorException e) {
175                         throw new ExportException(e);
176                 }               
177                 
178                 return writer;
179         }
180
181         @Override
182         public Object openFile(ExportContext context, File inputFile, Variant options) throws ExportException {
183                 throw new ExportException("Not implemented");
184         }
185
186         @Override
187         public void closeFile(ExportContext context, Object handle_) throws ExportException {
188                 CSVWriter handle = (CSVWriter) handle_;
189                 try {
190                         handle.write();
191                 } catch (IOException e) {
192                         throw new ExportException(e);
193                 } catch (HistoryException e) {
194                         throw new ExportException(e);
195                 }
196         }
197         
198         @Override
199         public void addAttachment(ExportContext context, Object handle, List<Content> attachments) throws ExportException {
200                 throw new ExportException( "Cannot add attachments to a CSV file." );
201         }
202
203 }