]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.charts/src/org/simantics/charts/editor/ChartCopyHandler.java
Improved Copy Visible Data usability in time series chart editor
[simantics/platform.git] / bundles / org.simantics.charts / src / org / simantics / charts / editor / ChartCopyHandler.java
index 65108d6f7a4ab4e3dde7d664d2e147ffdbff2584..183000045aed17cd3f2f6b44ed60fe834e1f8292 100644 (file)
@@ -1,6 +1,6 @@
 /*******************************************************************************
- * Copyright (c) 2011 Association for Decentralized Information Management in
- * Industry THTH ry.
+ * Copyright (c) 2011,2020 Association for Decentralized Information Management
+ * in Industry THTH ry.
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
  * which accompanies this distribution, and is available at
@@ -8,6 +8,7 @@
  *
  * Contributors:
  *     VTT Technical Research Centre of Finland - initial API and implementation
+ *     Semantum Oy - #501
  *******************************************************************************/
 package org.simantics.charts.editor;
 
@@ -33,11 +34,14 @@ import org.eclipse.core.runtime.preferences.InstanceScope;
 import org.eclipse.jface.action.IStatusLineManager;
 import org.eclipse.jface.operation.IRunnableWithProgress;
 import org.eclipse.jface.preference.IPreferenceStore;
+import org.eclipse.jface.preference.PreferenceDialog;
+import org.eclipse.jface.window.Window;
 import org.eclipse.swt.SWT;
 import org.eclipse.swt.widgets.FileDialog;
 import org.eclipse.swt.widgets.Shell;
 import org.eclipse.ui.IEditorPart;
 import org.eclipse.ui.PlatformUI;
+import org.eclipse.ui.dialogs.PreferencesUtil;
 import org.eclipse.ui.handlers.HandlerUtil;
 import org.eclipse.ui.preferences.ScopedPreferenceStore;
 import org.simantics.charts.ui.CSVProgressMonitor;
@@ -74,8 +78,28 @@ public class ChartCopyHandler extends AbstractHandler {
         IStatusLineManager status = editor.getEditorSite().getActionBars().getStatusLineManager();
         final Shell shell = HandlerUtil.getActiveShell(event);
 
-        final AtomicBoolean result = new AtomicBoolean(false);
+        // Find a good value to use for START_TIME based on the current horizontal ruler
+        // time range and the data start time.
+        IPreferenceStore csvnode = new ScopedPreferenceStore( InstanceScope.INSTANCE, CSVPreferences.P_NODE );
+        double oldStartTime = csvnode.getDouble(CSVPreferences.P_CSV_START_TIME);
+        double timeStep = csvnode.getDouble(CSVPreferences.P_CSV_TIME_STEP);
+        double visibleChartMinTime = trendNode.horizRuler.from;
+        double dataStartTime = trendNode.horizRuler.getItemFromTime();
+        // Find the first sample time that contains data if startTime < _from 
+        double n = Math.max(0, Math.ceil((visibleChartMinTime-dataStartTime) / timeStep));
+        double temporaryStartTime = dataStartTime + n*timeStep;
+        csvnode.setValue(CSVPreferences.P_CSV_START_TIME, temporaryStartTime);
+
         try {
+            PreferenceDialog dialog = PreferencesUtil.createPreferenceDialogOn(
+                    shell, "org.simantics.modeling.csv.preferences",
+                    new String[] { "org.simantics.modeling.csv.preferences" },
+                    null);
+            dialog.setMessage("Select Used CSV Export Settings");
+            if (dialog.open() != Window.OK)
+                return null;
+
+            AtomicBoolean result = new AtomicBoolean(false);
             PlatformUI.getWorkbench().getProgressService().busyCursorWhile(new IRunnableWithProgress() {
                 @Override
                 public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
@@ -92,6 +116,8 @@ public class ChartCopyHandler extends AbstractHandler {
             ErrorLogger.defaultLogError(e.getCause());
         } catch (InterruptedException e) {
             ErrorLogger.defaultLogError(e);
+        } finally {
+            csvnode.setValue(CSVPreferences.P_CSV_START_TIME, oldStartTime);
         }
 
         return null;