1 /*******************************************************************************
2 * Copyright (c) 2012 Association for Decentralized Information Management in
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.charts.ui;
15 import java.util.ArrayList;
16 import java.util.Collection;
17 import java.util.Collections;
18 import java.util.HashSet;
19 import java.util.List;
23 import org.eclipse.core.runtime.preferences.InstanceScope;
24 import org.eclipse.jface.layout.GridDataFactory;
25 import org.eclipse.jface.layout.GridLayoutFactory;
26 import org.eclipse.jface.preference.IPreferenceStore;
27 import org.eclipse.jface.wizard.WizardPage;
28 import org.eclipse.swt.SWT;
29 import org.eclipse.swt.custom.CCombo;
30 import org.eclipse.swt.custom.ScrolledComposite;
31 import org.eclipse.swt.events.ModifyListener;
32 import org.eclipse.swt.events.SelectionAdapter;
33 import org.eclipse.swt.events.SelectionEvent;
34 import org.eclipse.swt.events.SelectionListener;
35 import org.eclipse.swt.layout.GridData;
36 import org.eclipse.swt.widgets.Button;
37 import org.eclipse.swt.widgets.Composite;
38 import org.eclipse.swt.widgets.FileDialog;
39 import org.eclipse.swt.widgets.Group;
40 import org.eclipse.swt.widgets.Label;
41 import org.eclipse.swt.widgets.Table;
42 import org.eclipse.swt.widgets.TableItem;
43 import org.eclipse.swt.widgets.Text;
44 import org.eclipse.ui.preferences.ScopedPreferenceStore;
45 import org.simantics.NameLabelUtil;
46 import org.simantics.Simantics;
47 import org.simantics.browsing.ui.common.ColumnKeys;
48 import org.simantics.databoard.Bindings;
49 import org.simantics.databoard.parser.StringEscapeUtils;
50 import org.simantics.db.ReadGraph;
51 import org.simantics.db.Resource;
52 import org.simantics.db.common.NamedResource;
53 import org.simantics.db.common.request.IsParent;
54 import org.simantics.db.common.request.UniqueRead;
55 import org.simantics.db.exception.DatabaseException;
56 import org.simantics.db.layer0.SelectionHints;
57 import org.simantics.history.csv.ColumnSeparator;
58 import org.simantics.history.csv.DecimalSeparator;
59 import org.simantics.history.csv.ExportInterpolation;
60 import org.simantics.layer0.Layer0;
61 import org.simantics.modeling.ModelingResources;
62 import org.simantics.modeling.ModelingUtils;
63 import org.simantics.modeling.preferences.CSVPreferences;
64 import org.simantics.modeling.ui.modelBrowser2.label.SubscriptionItemLabelRule;
65 import org.simantics.utils.datastructures.Arrays;
66 import org.simantics.utils.datastructures.Pair;
67 import org.simantics.utils.strings.AlphanumComparator;
68 import org.simantics.utils.ui.ISelectionUtils;
72 * @author Antti Villberg
74 public class CSVExportPage extends WizardPage {
76 CSVExportPlan exportModel;
79 Button selectAllItems;
80 SelectionAdapter selectAllItemsListener;
81 CCombo exportLocation;
83 CCombo decimalSeparator;
84 CCombo columnSeparator;
95 Collection<Resource> initialSelection;
97 List<Pair<NamedResource,List<NamedResource>>> models = new ArrayList<>();
99 private Button overwrite;
101 ModifyListener m = (e) -> validatePage();
103 SelectionListener s = new SelectionAdapter() {
105 public void widgetSelected(SelectionEvent e) {
110 protected CSVExportPage(CSVExportPlan model) {
111 super("Export CSV Data", "Define Export Properties", null);
112 this.exportModel = model;
116 public void createControl(Composite parent) {
117 ScrolledComposite scroller = new ScrolledComposite(parent, SWT.V_SCROLL);
118 scroller.setExpandHorizontal(true);
119 scroller.setExpandVertical(true);
121 Composite container = new Composite(scroller, SWT.NONE);
122 scroller.setContent(container);
123 GridLayoutFactory.swtDefaults().spacing(20, 10).numColumns(3).applyTo(container);
124 new Label(container, SWT.NONE).setText("Select a model:");
125 model = new CCombo(container, SWT.BORDER);
127 model.setEditable(false);
129 model.setToolTipText("Selects the Model To Export From");
130 GridDataFactory.fillDefaults().grab(true, false).span(2, 1).applyTo(model);
133 new Label(container, SWT.NONE).setText("Exported items:");
134 item = new Table(container, SWT.BORDER | SWT.MULTI | SWT.FULL_SELECTION | SWT.CHECK);
136 item.setToolTipText("Selects the Subscription Items");
137 GridDataFactory.fillDefaults().grab(true, true).span(2, 1).hint(SWT.DEFAULT, 105).applyTo(item);
139 item.addSelectionListener(new SelectionAdapter() {
141 public void widgetSelected(SelectionEvent e) {
142 if (e.detail == SWT.CHECK) {
143 TableItem[] selected = item.getSelection();
144 TableItem it = (TableItem) e.item;
145 boolean checkedWasSelected = Arrays.contains(selected, it);
146 if (checkedWasSelected) {
147 boolean check = it.getChecked();
148 for (TableItem i : selected)
151 int checked = countCheckedItems(item);
152 int totalItems = item.getItemCount();
153 updateSelectAll(checked > 0, checked < totalItems, false);
158 new Label(container, 0);
159 selectAllItems = new Button(container, SWT.CHECK);
161 selectAllItems.setText("&Select All");
162 selectAllItems.setToolTipText("Select/Deselect All Listed Subscription Items");
163 GridDataFactory.fillDefaults().grab(true, false).span(2, 1).applyTo(selectAllItems);
165 selectAllItemsListener = new SelectionAdapter() {
167 public void widgetSelected(SelectionEvent e) {
168 boolean select = selectAllItems.getSelection();
169 updateSelectAll(select, false, false);
170 item.setRedraw(false);
171 for (TableItem it : item.getItems())
172 it.setChecked(select);
173 item.setRedraw(true);
178 selectAllItems.addSelectionListener(selectAllItemsListener);
180 new Label(container, SWT.NONE).setText("&Output file:");
181 exportLocation = new CCombo(container, SWT.BORDER);
183 exportLocation.setText("");
184 GridDataFactory.fillDefaults().grab(true, false).span(1, 1).applyTo(exportLocation);
185 exportLocation.addModifyListener(m);
187 Button browseFileButton = new Button(container, SWT.PUSH);
189 browseFileButton.setText("Browse...");
190 browseFileButton.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
191 browseFileButton.addSelectionListener(new SelectionAdapter() {
193 public void widgetSelected(SelectionEvent e) {
194 FileDialog dialog = new FileDialog(getShell(), SWT.SAVE);
195 dialog.setText("Choose Output File");
196 dialog.setFilterPath(new File(exportLocation.getText()).getParent());
197 dialog.setFilterExtensions(new String[] { "*.csv" });
198 dialog.setFilterNames(new String[] { "Comma separated values (*.csv)" });
199 dialog.setOverwrite(false);
200 String file = dialog.open();
203 exportLocation.setText(file);
210 Label horizRule = new Label(container, SWT.BORDER);
211 GridDataFactory.fillDefaults().hint(SWT.DEFAULT, 0).grab(true, false).span(3, 1).applyTo(horizRule);
213 new Label(container, SWT.NONE).setText("&Decimal separator:");
214 decimalSeparator = new CCombo(container, SWT.READ_ONLY | SWT.BORDER);
215 for(DecimalSeparator s : DecimalSeparator.values())
216 decimalSeparator.add(s.label);
217 decimalSeparator.select(0);
218 decimalSeparator.addSelectionListener(s);
219 GridDataFactory.fillDefaults().grab(true, false).span(2, 1).applyTo(decimalSeparator);
221 new Label(container, SWT.NONE).setText("&Column separator:");
222 columnSeparator = new CCombo(container, SWT.READ_ONLY | SWT.BORDER);
223 for(ColumnSeparator s : ColumnSeparator.values())
224 columnSeparator.add(s.label);
225 columnSeparator.select(0);
226 columnSeparator.addSelectionListener(s);
227 GridDataFactory.fillDefaults().grab(true, false).span(2, 1).applyTo(columnSeparator);
229 new Label(container, SWT.NONE).setText("Sampling:");
230 sampling = new CCombo(container, SWT.READ_ONLY | SWT.BORDER);
231 sampling.add("Recorded samples");
232 sampling.add("Resampled");
234 sampling.addSelectionListener(s);
235 GridDataFactory.fillDefaults().grab(true, false).span(2, 1).applyTo(sampling);
237 resampling = new Group(container, SWT.NONE);
238 resampling.setText("Resampling settings (not used with recorded samples)");
239 GridDataFactory.fillDefaults().grab(true, false).span(3, 1).applyTo(resampling);
240 GridLayoutFactory.swtDefaults().numColumns(3).applyTo(resampling);
242 new Label(resampling, SWT.NONE).setText("&Start time:");
243 startTime = new Text(resampling, SWT.BORDER);
244 startTime.addModifyListener(m);
245 GridDataFactory.fillDefaults().grab(true, false).applyTo(startTime);
246 new Label(resampling, SWT.NONE).setText(" seconds");
248 new Label(resampling, SWT.NONE).setText("&Time step:");
249 timeStep = new Text(resampling, SWT.BORDER);
250 timeStep.addModifyListener(m);
251 GridDataFactory.fillDefaults().grab(true, false).applyTo(timeStep);
252 new Label(resampling, SWT.NONE).setText(" seconds");
254 new Label(resampling, SWT.NONE).setText("Sampling mode:");
255 samplingMode = new CCombo(resampling, SWT.READ_ONLY | SWT.BORDER);
256 samplingMode.add("Linear interpolation");
257 samplingMode.add("Previous sample");
258 samplingMode.select(0);
259 samplingMode.addSelectionListener(s);
260 GridDataFactory.fillDefaults().grab(true, false).span(2,1).applyTo(samplingMode);
262 Group digits = new Group(container, SWT.NONE);
263 digits.setText("Significant digits");
264 GridDataFactory.fillDefaults().grab(true, false).span(3, 1).applyTo(digits);
265 GridLayoutFactory.swtDefaults().numColumns(2).applyTo(digits);
267 new Label(digits, SWT.NONE).setText("&Time stamps:");
268 timeStamps = new Text(digits, SWT.BORDER);
269 timeStamps.addModifyListener(m);
270 GridDataFactory.fillDefaults().grab(true, false).applyTo(timeStamps);
272 new Label(digits, SWT.NONE).setText("&Single precision floating point:");
273 singlePrecision = new Text(digits, SWT.BORDER);
274 singlePrecision.addModifyListener(m);
275 GridDataFactory.fillDefaults().grab(true, false).applyTo(singlePrecision);
277 new Label(digits, SWT.NONE).setText("&Double precision floating point:");
278 doublePrecision = new Text(digits, SWT.BORDER);
279 doublePrecision.addModifyListener(m);
280 GridDataFactory.fillDefaults().grab(true, false).applyTo(doublePrecision);
282 overwrite = new Button(container, SWT.CHECK);
283 overwrite.setText("&Overwrite existing files without warning");
284 overwrite.setSelection(exportModel.overwrite);
285 GridDataFactory.fillDefaults().grab(true, false).span(3, 1).applyTo(overwrite);
286 overwrite.addSelectionListener(new SelectionAdapter() {
288 public void widgetSelected(SelectionEvent e) {
295 } catch (DatabaseException e) {
299 model.addSelectionListener(s);
301 scroller.setMinSize(container.computeSize(SWT.DEFAULT, SWT.DEFAULT));
303 setControl(scroller);
308 void updateSelectAll(boolean checked, boolean gray, boolean notify) {
310 selectAllItems.setText("Select None");
312 selectAllItems.setText("Select All");
315 selectAllItems.removeSelectionListener(selectAllItemsListener);
316 selectAllItems.setGrayed(checked && gray);
317 selectAllItems.setSelection(checked);
319 selectAllItems.addSelectionListener(selectAllItemsListener);
322 protected int countCheckedItems(Table table) {
324 for (TableItem item : table.getItems())
325 ret += item.getChecked() ? 1 : 0;
329 private void initializeData() throws DatabaseException {
331 // Write preferences to formatter
332 IPreferenceStore csvnode = new ScopedPreferenceStore( InstanceScope.INSTANCE, CSVPreferences.P_NODE );
334 Double startTime = csvnode.getDouble(CSVPreferences.P_CSV_START_TIME);
335 Double timeStep = csvnode.getDouble(CSVPreferences.P_CSV_TIME_STEP);
336 String decimalSeparator = csvnode.getString(CSVPreferences.P_CSV_DECIMAL_SEPARATOR);
337 String columnSeparator = StringEscapeUtils.unescape( csvnode.getString(CSVPreferences.P_CSV_COLUMN_SEPARATOR) );
338 Boolean resample = csvnode.getBoolean(CSVPreferences.P_CSV_RESAMPLE);
339 String samplingModePreference = csvnode.getString(CSVPreferences.P_CSV_SAMPLING_MODE);
340 int timeDigits = csvnode.getInt(CSVPreferences.P_CSV_TIME_DIGITS);
341 int floatDigits = csvnode.getInt(CSVPreferences.P_CSV_FLOAT_DIGITS);
342 int doubleDigits = csvnode.getInt(CSVPreferences.P_CSV_DOUBLE_DIGITS);
344 initialSelection = ISelectionUtils.getPossibleKeys(exportModel.selection, SelectionHints.KEY_MAIN, Resource.class);
346 models = exportModel.sessionContext.getSession().syncRequest(new UniqueRead<List<Pair<NamedResource,List<NamedResource>>>>() {
349 public List<Pair<NamedResource,List<NamedResource>>> perform(ReadGraph graph) throws DatabaseException {
350 Layer0 L0 = Layer0.getInstance(graph);
351 ModelingResources MOD = ModelingResources.getInstance(graph);
352 List<Pair<NamedResource,List<NamedResource>>> result = new ArrayList<>();
353 for(Resource model : graph.syncRequest(new org.simantics.db.layer0.request.ProjectModels(Simantics.getProjectResource()))) {
354 String name = graph.getPossibleRelatedValue(model, L0.HasName, Bindings.STRING);
355 if(name == null) continue;
356 name = NameLabelUtil.modalName(graph, model);
357 List<NamedResource> subs = new ArrayList<>();
358 for(Resource item : ModelingUtils.searchByType(graph, model, MOD.Subscription_Item)) {
359 String subscriptionLabel = null;
360 Resource subscription = graph.getPossibleObject(item, L0.PartOf);
361 if(subscription != null) {
362 subscriptionLabel = graph.getPossibleRelatedValue(subscription, L0.HasLabel, Bindings.STRING);
364 SubscriptionItemLabelRule rule = new SubscriptionItemLabelRule();
365 Map<String,String> map = rule.getLabel(graph, item);
366 String label = map.get(ColumnKeys.SINGLE);
367 if(label == null) continue;
368 if(subscriptionLabel != null) label = subscriptionLabel + "/" + label;
369 subs.add(new NamedResource(label, item));
371 subs.sort((o1,o2) -> AlphanumComparator.CASE_INSENSITIVE_COMPARATOR.compare(o1.getName(), o2.getName()));
372 result.add(new Pair<>(new NamedResource(name, model), subs));
379 Set<NamedResource> selected = exportModel.sessionContext.getSession().syncRequest(new UniqueRead<Set<NamedResource>>() {
382 public Set<NamedResource> perform(ReadGraph graph) throws DatabaseException {
384 if(initialSelection == null) return Collections.emptySet();
386 HashSet<NamedResource> result = new HashSet<>();
388 for(Pair<NamedResource,List<NamedResource>> p : models) {
389 for(NamedResource nr : p.second) {
390 for(Resource i : initialSelection) {
391 if(graph.syncRequest(new IsParent(i, nr.getResource()))) result.add(nr);
401 for(int i=0;i<models.size();i++) {
403 Pair<NamedResource,List<NamedResource>> p = models.get(i);
404 model.add(p.first.getName());
406 boolean hasInitial = false;
407 for(NamedResource nr : p.second) {
408 if(selected.contains(nr)) hasInitial = true;
413 exportModel.items.clear();
416 for(NamedResource nr : p.second) {
417 TableItem ti = new TableItem(item, SWT.NONE);
418 ti.setText(nr.getName());
420 if(selected.contains(nr)) {
421 exportModel.items.add(nr.getResource());
423 if(firstIndex == 0) firstIndex = index;
427 item.setTopIndex(firstIndex);
428 item.setData(p.first);
433 this.decimalSeparator.select(DecimalSeparator.fromPreference(decimalSeparator).ordinal());
434 this.columnSeparator.select(ColumnSeparator.fromPreference(columnSeparator).ordinal());
437 this.sampling.select(1);
439 this.sampling.select(0);
442 this.samplingMode.select(ExportInterpolation.fromPreference(samplingModePreference).index());
444 this.startTime.setText("" + startTime);
445 this.timeStep.setText("" + timeStep);
446 this.timeStamps.setText("" + timeDigits);
447 this.singlePrecision.setText("" + floatDigits);
448 this.doublePrecision.setText("" + doubleDigits);
450 for (String path : exportModel.recentLocations) {
451 exportLocation.add(path);
453 if (exportLocation.getItemCount() > 0)
454 exportLocation.select(0);
458 Integer validInteger(String s) {
460 return Integer.parseInt(s);
461 } catch (NumberFormatException e) {
466 Double validDouble(String s) {
468 return Double.parseDouble(s);
469 } catch (NumberFormatException e) {
474 Pair<NamedResource,List<NamedResource>> getModel(String name) {
475 for(Pair<NamedResource,List<NamedResource>> data : models) {
476 if(data.first.getName().equals(name)) return data;
481 void validatePage() {
483 boolean resample = sampling.getText().equals("Resampled");
486 resampling.setText("Resampling settings");
487 timeStep.setEnabled(true);
488 startTime.setEnabled(true);
489 samplingMode.setEnabled(true);
491 resampling.setText("Resampling settings (not used with recorded samples)");
492 timeStep.setEnabled(false);
493 startTime.setEnabled(false);
494 samplingMode.setEnabled(false);
497 String selectedModel = model.getText();
498 Pair<NamedResource,List<NamedResource>> p = getModel(selectedModel);
501 Set<Resource> checked = new HashSet<>();
503 NamedResource existing = (NamedResource)item.getData();
504 if(!p.first.equals(existing)) {
507 for(NamedResource nr : p.second) {
508 TableItem ti = new TableItem(item, SWT.NONE);
509 ti.setText(nr.getName());
512 item.setData(p.first);
516 for(TableItem ti : item.getItems()) {
517 if(ti.getChecked()) checked.add(((NamedResource)ti.getData()).getResource());
520 exportModel.items = checked;
524 Double validStartTime = validDouble(startTime.getText());
525 Double validStepSize = validDouble(timeStep.getText());
529 if(validStartTime == null) {
530 setErrorMessage("Start time must be a number.");
531 setPageComplete(false);
535 if(validStepSize == null) {
536 setErrorMessage("Step size must be a number.");
537 setPageComplete(false);
540 if(validStepSize <= 0) {
541 setErrorMessage("Step size must be greater than 0.");
542 setPageComplete(false);
548 if(exportModel.items.size() > 1) {
549 setErrorMessage("Recorded samples can only be exported for a single subscription item.");
550 setPageComplete(false);
556 if(item.getItemCount() == 0) {
557 setErrorMessage("No subscription items in selected model.");
558 setPageComplete(false);
562 if (exportModel.items.isEmpty()) {
563 setErrorMessage("No items selected for export.");
564 setPageComplete(false);
568 String exportLoc = exportLocation.getText();
569 if (exportLoc.isEmpty()) {
570 setErrorMessage("Select output file.");
571 setPageComplete(false);
574 File file = new File(exportLoc);
575 if (file.isDirectory()) {
576 setErrorMessage("The output file is a directory.");
577 setPageComplete(false);
580 File parent = file.getParentFile();
581 if (parent == null || !parent.isDirectory()) {
582 setErrorMessage("The output directory does not exist.");
583 setPageComplete(false);
587 exportModel.columnSeparator = ColumnSeparator.fromIndex(columnSeparator.getSelectionIndex());
588 exportModel.decimalSeparator = DecimalSeparator.fromIndex(decimalSeparator.getSelectionIndex());
589 if (exportModel.columnSeparator.preference.equals(exportModel.decimalSeparator.preference)) {
590 setErrorMessage("Decimal and column separator cannot be the same character.");
591 setPageComplete(false);
595 Integer validTimeDigits = validInteger(timeStamps.getText());
597 if(validTimeDigits == null) {
598 setErrorMessage("Time stamps needs to be an integer number.");
599 setPageComplete(false);
603 Integer validSinglePrecision = validInteger(singlePrecision.getText());
604 if(validSinglePrecision == null) {
605 setErrorMessage("Single precision needs to be an integer number.");
606 setPageComplete(false);
610 Integer validDoublePrecision = validInteger(doublePrecision.getText());
611 if(validDoublePrecision == null) {
612 setErrorMessage("Double precision needs to be an integer number.");
613 setPageComplete(false);
617 exportModel.exportLocation = file;
618 exportModel.overwrite = overwrite.getSelection();
620 exportModel.startTime = validStartTime;
621 exportModel.timeStep = validStepSize;
623 exportModel.resample = sampling.getSelectionIndex() == 1;
624 exportModel.samplingMode = ExportInterpolation.fromIndex(samplingMode.getSelectionIndex());
626 exportModel.timeDigits = validTimeDigits;
627 exportModel.floatDigits = validSinglePrecision;
628 exportModel.doubleDigits = validDoublePrecision;
630 setErrorMessage(null);
631 setMessage("Press Finish to export subscription data.");
632 setPageComplete(true);