1 /*******************************************************************************
2 * Copyright (c) 2011 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.editor.e4;
14 import java.awt.Toolkit;
15 import java.awt.datatransfer.Clipboard;
16 import java.awt.datatransfer.StringSelection;
17 import java.io.BufferedWriter;
19 import java.io.FileOutputStream;
20 import java.io.IOException;
21 import java.io.OutputStreamWriter;
22 import java.lang.reflect.InvocationTargetException;
23 import java.nio.charset.Charset;
24 import java.util.Collections;
25 import java.util.List;
26 import java.util.concurrent.atomic.AtomicBoolean;
28 import javax.inject.Named;
30 import org.eclipse.core.commands.ExecutionException;
31 import org.eclipse.core.runtime.IProgressMonitor;
32 import org.eclipse.core.runtime.preferences.InstanceScope;
33 import org.eclipse.e4.core.di.annotations.CanExecute;
34 import org.eclipse.e4.core.di.annotations.Execute;
35 import org.eclipse.e4.ui.model.application.ui.basic.MPart;
36 import org.eclipse.e4.ui.services.IServiceConstants;
37 import org.eclipse.jface.action.IStatusLineManager;
38 import org.eclipse.jface.operation.IRunnableWithProgress;
39 import org.eclipse.jface.preference.IPreferenceStore;
40 import org.eclipse.swt.SWT;
41 import org.eclipse.swt.widgets.FileDialog;
42 import org.eclipse.swt.widgets.Shell;
43 import org.eclipse.ui.IWorkbenchPart;
44 import org.eclipse.ui.PlatformUI;
45 import org.eclipse.ui.preferences.ScopedPreferenceStore;
46 import org.simantics.charts.ui.CSVProgressMonitor;
47 import org.simantics.databoard.binding.error.BindingException;
48 import org.simantics.databoard.parser.StringEscapeUtils;
49 import org.simantics.databoard.util.Bean;
50 import org.simantics.databoard.util.StreamUtil;
51 import org.simantics.history.HistoryException;
52 import org.simantics.history.ItemManager;
53 import org.simantics.history.csv.CSVFormatter;
54 import org.simantics.history.csv.ColumnSeparator;
55 import org.simantics.history.csv.DecimalSeparator;
56 import org.simantics.history.csv.ExportInterpolation;
57 import org.simantics.history.util.subscription.SamplingFormat;
58 import org.simantics.modeling.preferences.CSVPreferences;
59 import org.simantics.trend.configuration.TrendItem;
60 import org.simantics.trend.impl.TrendNode;
61 import org.simantics.utils.format.FormattingUtils;
62 import org.simantics.utils.ui.ErrorLogger;
63 import org.simantics.utils.ui.workbench.WorkbenchUtils;
66 * @author Tuukka Lehtonen
68 public class ChartCopyHandler {
73 public boolean canExecute(@Named(IServiceConstants.ACTIVE_PART) MPart part) {
76 if (part.getObject() instanceof TimeSeriesEditor == false)
82 public void execute(@Named(IServiceConstants.ACTIVE_PART) MPart part, @Named(IServiceConstants.ACTIVE_SHELL) Shell shell) throws ExecutionException {
83 if (part.getObject() instanceof TimeSeriesEditor == false)
85 TimeSeriesEditor editor = (TimeSeriesEditor) part.getObject();
86 final TrendNode trendNode = editor.trendNode;
87 IWorkbenchPart wbpart = WorkbenchUtils.getActiveWorkbenchPart();
88 IStatusLineManager status = WorkbenchUtils.getStatusLine(wbpart);
90 final AtomicBoolean result = new AtomicBoolean(false);
92 PlatformUI.getWorkbench().getProgressService().busyCursorWhile(new IRunnableWithProgress() {
94 public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
95 result.set( copyDataToClipboard(monitor, trendNode, Format.JOINED_TIME, shell) );
99 status.setMessage("No data to copy");
101 status.setMessage("Copied chart data to clipboard");
103 status.setErrorMessage(null);
104 } catch (InvocationTargetException e) {
105 ErrorLogger.defaultLogError(e.getCause());
106 } catch (InterruptedException e) {
107 ErrorLogger.defaultLogError(e);
116 public boolean copyDataToClipboard(IProgressMonitor monitor, TrendNode t, Format format, final Shell shell) {
117 Charset UTF8 = Charset.forName("UTF-8");
119 // String builder can be really slow when it is extended many times over.
120 // Instead stream to file with buffering
121 // StringBuilder sb = new StringBuilder();
122 IPreferenceStore csvnode = new ScopedPreferenceStore( InstanceScope.INSTANCE, CSVPreferences.P_NODE );
123 String ext = csvnode.getString(CSVPreferences.P_CSV_FILE_EXTENSION);
124 File tmpFile = File.createTempFile("clipboard", ext);
125 tmpFile.deleteOnExit();
126 FileOutputStream fos = new FileOutputStream(tmpFile);
127 BufferedWriter w = new BufferedWriter(new OutputStreamWriter(fos, UTF8));
129 ItemManager im = new ItemManager( t.historian.getItems() );
130 CSVFormatter formatter = new CSVFormatter();
131 formatter.setTimeRange(t.horizRuler.from, t.horizRuler.end);
134 formatter.setStartTime( csvnode.getDouble(CSVPreferences.P_CSV_START_TIME) );
135 formatter.setTimeStep( csvnode.getDouble(CSVPreferences.P_CSV_TIME_STEP) );
136 formatter.setDecimalSeparator( DecimalSeparator.fromPreference(csvnode.getString(CSVPreferences.P_CSV_DECIMAL_SEPARATOR) ) );
137 formatter.setColumnSeparator( ColumnSeparator.fromPreference(StringEscapeUtils.unescape( csvnode.getString(CSVPreferences.P_CSV_COLUMN_SEPARATOR) ) ) );
138 formatter.setResample( csvnode.getBoolean(CSVPreferences.P_CSV_RESAMPLE) );
139 formatter.setNumberInterpolation( ExportInterpolation.fromPreference (csvnode.getString(CSVPreferences.P_CSV_SAMPLING_MODE) ) );
140 formatter.setTimeFormat( FormattingUtils.significantDigitFormat( csvnode.getInt(CSVPreferences.P_CSV_TIME_DIGITS) ) );
141 formatter.setFloatFormat( FormattingUtils.significantDigitFormat( csvnode.getInt(CSVPreferences.P_CSV_FLOAT_DIGITS) ) );
142 formatter.setNumberFormat( FormattingUtils.significantDigitFormat( csvnode.getInt(CSVPreferences.P_CSV_DOUBLE_DIGITS) ) );
144 for (TrendItem i : t.spec.items) {
145 if (i.hidden) continue;
146 List<Bean> items = im.search("variableId", i.variableId);
147 Collections.sort(items, SamplingFormat.INTERVAL_COMPARATOR);
148 if (items.isEmpty()) continue;
149 Bean config = items.get(0);
150 String historyId = (String) config.getFieldUnchecked("id");
151 formatter.addItem( t.historian, historyId, i.simpleLabel, i.variableReference, i.unit);
155 case TIME_VALUE_PAIRS:
156 // formatter.formulate1(new CSVProgressMonitor(monitor), w);
159 formatter.formulate2(new CSVProgressMonitor(monitor), w);
162 throw new UnsupportedOperationException("unsupported format " + format);
166 if (tmpFile.length()==0) return false;
168 Toolkit toolkit = Toolkit.getDefaultToolkit();
169 Clipboard clipboard = toolkit.getSystemClipboard();
174 System.out.println("Exported to "+tmpFile+" size: "+tmpFile.length());
175 if ( tmpFile.length() > 10*1024*1024 ) {
176 // String msg = "The data has been written to temporary file:\n"+tmpFile.getCanonicalPath();
177 // ShowMessage.showInformation( shell.getDisplay(), "Too much data for clipboard.", msg);
178 final File csvFile = tmpFile;
180 shell.getDisplay().asyncExec( new Runnable() {
183 FileDialog fd = new FileDialog(shell, SWT.SAVE);
184 fd.setText("Write CSV to File");
185 fd.setFileName( lastFile!=null ? lastFile : csvFile.getAbsolutePath() );
186 String newFile = fd.open();
187 if ( newFile != null ) {
189 File ff = new File( newFile );
191 csvFile.renameTo( ff );
197 String str = StreamUtil.readString(tmpFile, UTF8);
199 StringSelection strSel = new StringSelection(str);
200 clipboard.setContents(strSel, null);
203 } catch (BindingException e1) {
204 ErrorLogger.defaultLogError(e1);
206 } catch (IOException e) {
207 ErrorLogger.defaultLogError(e);
209 if ( fos != null ) try { fos.close(); } catch (IOException e) { ErrorLogger.defaultLogError(e); }
210 if ( tmpFile != null ) tmpFile.delete();
214 } catch (HistoryException e) {
215 ErrorLogger.defaultLogError(e);
216 } catch (IOException e) {
217 ErrorLogger.defaultLogError(e);