+/*******************************************************************************\r
+ * Copyright (c) 2014 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ * Semantum Oy - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.event.view.handler;\r
+\r
+import java.io.File;\r
+import java.io.FileNotFoundException;\r
+import java.lang.reflect.InvocationTargetException;\r
+import java.util.Calendar;\r
+\r
+import org.eclipse.core.commands.AbstractHandler;\r
+import org.eclipse.core.commands.ExecutionEvent;\r
+import org.eclipse.core.commands.ExecutionException;\r
+import org.eclipse.core.runtime.IProgressMonitor;\r
+import org.eclipse.core.runtime.preferences.InstanceScope;\r
+import org.eclipse.jface.operation.IRunnableWithProgress;\r
+import org.eclipse.swt.SWT;\r
+import org.eclipse.swt.widgets.FileDialog;\r
+import org.eclipse.swt.widgets.Shell;\r
+import org.eclipse.ui.IWorkbenchWindow;\r
+import org.eclipse.ui.handlers.HandlerUtil;\r
+import org.osgi.service.prefs.BackingStoreException;\r
+import org.osgi.service.prefs.Preferences;\r
+import org.simantics.Simantics;\r
+import org.simantics.browsing.ui.common.ErrorLogger;\r
+import org.simantics.databoard.Bindings;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.common.request.ObjectsWithType;\r
+import org.simantics.db.common.utils.NameUtils;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.request.Read;\r
+import org.simantics.event.Activator;\r
+import org.simantics.event.util.EventExporter;\r
+import org.simantics.operation.Layer0X;\r
+import org.simantics.simulation.ontology.SimulationResource;\r
+import org.simantics.utils.FileUtils;\r
+import org.simantics.utils.ui.ExceptionUtils;\r
+import org.simantics.utils.ui.dialogs.ShowError;\r
+\r
+/**\r
+ * @author Tuukka Lehtonen\r
+ */\r
+public class ExportEventsAsCsv extends AbstractHandler {\r
+\r
+ private static final String PROP_LAST_PATH= "event.csv.export.path";\r
+\r
+ @Override\r
+ public Object execute(ExecutionEvent event) throws ExecutionException {\r
+ IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindow(event);\r
+\r
+ try {\r
+ String fileName = generateFileName();\r
+ validate(window, fileName);\r
+ } catch (DatabaseException e) {\r
+ ErrorLogger.defaultLogError(e);\r
+ }\r
+\r
+ return null;\r
+ }\r
+\r
+ private String generateFileName() throws DatabaseException {\r
+ String generatedName = Simantics.getSession().syncRequest(new Read<String>() {\r
+ @Override\r
+ public String perform(ReadGraph graph) throws DatabaseException {\r
+ Layer0X L0X = Layer0X.getInstance(graph);\r
+ SimulationResource SIMU = SimulationResource.getInstance(graph);\r
+ for (Resource model : graph.syncRequest(new ObjectsWithType(Simantics.getProjectResource(), L0X.Activates, SIMU.Model))) {\r
+ return NameUtils.getSafeName(graph, model) + "-events-" + getTimestamp() + ".txt";\r
+ }\r
+ return "events-" + getTimestamp() + ".txt";\r
+ }\r
+\r
+ private String getTimestamp() {\r
+ Calendar c = Calendar.getInstance();\r
+ return c.get(Calendar.YEAR) + "-" + (1 + c.get(Calendar.MONTH)) + "-" + c.get(Calendar.DAY_OF_MONTH)\r
+ + "_" + c.get(Calendar.HOUR_OF_DAY) \r
+ + "-" + c.get(Calendar.MINUTE) \r
+ + "-" + c.get(Calendar.SECOND); \r
+ }\r
+ });\r
+\r
+ if (!FileUtils.isValidFileName(generatedName))\r
+ generatedName = (String) Bindings.STR_VARIANT.createUnchecked(Bindings.STRING, generatedName);\r
+\r
+ return generatedName;\r
+ }\r
+\r
+ public void validate(IWorkbenchWindow window, String fileName) {\r
+ Preferences prefs = InstanceScope.INSTANCE.getNode(Activator.BUNDLE_ID);\r
+ String lastReportPath = prefs.get(PROP_LAST_PATH, null);\r
+\r
+ // Query for output path\r
+ Shell parentShell = null;\r
+ if (window != null)\r
+ parentShell = window.getShell();\r
+\r
+ FileDialog fd = new FileDialog(parentShell, SWT.SAVE);\r
+ fd.setText("Select Output");\r
+ fd.setFilterExtensions(new String[] { "*.txt", "*.csv", "*.*" });\r
+ fd.setFilterNames(new String[] { "Tab-Separated Values (*.txt)", "Comma-Separated Values (*.csv)", "All Files (*.*)" });\r
+ if (lastReportPath != null)\r
+ fd.setFilterPath(lastReportPath);\r
+ fd.setFileName(fileName);\r
+ String path = fd.open();\r
+ if (path != null) {\r
+ prefs.put(PROP_LAST_PATH, path);\r
+ try {\r
+ prefs.flush();\r
+ } catch (BackingStoreException e) {\r
+ ExceptionUtils.logError(e);\r
+ }\r
+ } else {\r
+ return;\r
+ }\r
+\r
+ String _cs = null;\r
+ switch (fd.getFilterIndex()) {\r
+ case 1:\r
+ _cs = ";";\r
+ if (!path.endsWith(".csv"))\r
+ path += ".csv";\r
+ break;\r
+ case 0:\r
+ if (!path.endsWith(".txt"))\r
+ path += ".txt";\r
+ // Intentional fallthrough\r
+ default:\r
+ _cs = "\t";\r
+ break;\r
+ }\r
+ final String columnSeparator = _cs;\r
+ final File out = new File(path);\r
+ System.out.println("out: " + out);\r
+\r
+ try {\r
+ window.getWorkbench().getProgressService().busyCursorWhile(new IRunnableWithProgress() {\r
+ @Override\r
+ public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {\r
+ try {\r
+ new EventExporter().exportCsv(monitor, out, columnSeparator);\r
+ } catch (Exception e) {\r
+ throw new InvocationTargetException(e);\r
+ } finally {\r
+ monitor.done();\r
+ }\r
+ }\r
+ });\r
+ } catch (InvocationTargetException e) {\r
+ Throwable t = e.getTargetException();\r
+ if (t instanceof FileNotFoundException) {\r
+ ShowError.showError("Export Failed", "Failed to write " + t.getMessage(), (Exception) null);\r
+ } else {\r
+ ExceptionUtils.logAndShowError(t);\r
+ }\r
+ } catch (InterruptedException e) {\r
+ // Operation cancelled, ignore.\r
+ }\r
+ }\r
+\r
+}
\ No newline at end of file