]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.event/src/org/simantics/event/util/EventExporter.java
Fix column width issues on HiDPI displays. KeyTiSelection fixes.
[simantics/platform.git] / bundles / org.simantics.event / src / org / simantics / event / util / EventExporter.java
1 /*******************************************************************************
2  * Copyright (c) 2014 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     Semantum Oy - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.event.util;
13
14 import java.io.File;
15 import java.io.IOException;
16 import java.io.PrintStream;
17 import java.util.Collection;
18 import java.util.Map;
19
20 import org.eclipse.core.runtime.IProgressMonitor;
21 import org.eclipse.core.runtime.SubMonitor;
22 import org.simantics.Simantics;
23 import org.simantics.db.ReadGraph;
24 import org.simantics.db.Resource;
25 import org.simantics.db.common.request.ReadRequest;
26 import org.simantics.db.exception.DatabaseException;
27 import org.simantics.event.view.contribution.EventLabelRule;
28 import org.simantics.event.view.contribution.ProjectEventsRule;
29 import org.simantics.utils.FileUtils;
30 import org.simantics.utils.strings.EString;
31
32 /**
33  * @author Tuukka Lehtonen
34  */
35 public class EventExporter {
36
37         public EventExporter() {
38         }
39
40         /**
41          * @param monitor
42          *            the progress monitor to use for reporting progress to the
43          *            user. It is the caller's responsibility to call done() on the
44          *            given monitor. Accepts <code>null</code>, indicating that no
45          *            progress should be reported and that the operation cannot be
46          *            cancelled.</pre>
47          * @param file
48          * @param columnSeparator
49          * @throws DatabaseException
50          * @throws IOException
51          */
52         public void exportCsv(final IProgressMonitor monitor, File file, final String columnSeparator) throws DatabaseException, IOException {
53                 final PrintStream out = new PrintStream(file);
54                 try {
55                         Simantics.getSession().syncRequest(new ReadRequest() {
56                                 @Override
57                                 public void run(ReadGraph graph) throws DatabaseException {
58                                         exportCsv(graph, monitor, out, columnSeparator);
59                                 }
60                         });
61                 } finally {
62                         FileUtils.uncheckedClose(out);
63                 }
64         }
65
66         /**
67          * @param graph
68          * @param monitor
69          *            the progress monitor to use for reporting progress to the
70          *            user. It is the caller's responsibility to call done() on the
71          *            given monitor. Accepts <code>null</code>, indicating that no
72          *            progress should be reported and that the operation cannot be
73          *            cancelled.</pre>
74          * @param out
75          * @param columnSeparator
76          * @throws DatabaseException
77          */
78         public void exportCsv(ReadGraph graph, IProgressMonitor monitor, PrintStream out, String columnSeparator) throws DatabaseException {
79                 Collection<?> events = ProjectEventsRule.INSTANCE.getChildren(graph, Simantics.getProjectResource());
80                 if (events.isEmpty()) {
81                         out.println("No visible events recorded.");
82                         return;
83                 }
84
85                 final SubMonitor mon = SubMonitor.convert(monitor, "Export events", events.size());
86
87                 boolean first = true;
88                 for (Object obj : events) {
89                         if (mon.isCanceled())
90                                 return;
91
92                         Resource event = (Resource) obj;
93                         Map<String, String> labels = EventLabelRule.INSTANCE.getLabel(graph, event, true);
94                         if (first) {
95                                 out.println(EString.implode(labels.keySet(), columnSeparator));
96                                 first = false;
97                         }
98                         out.println(EString.implode(labels.values(), columnSeparator));
99                         mon.worked(1);
100                 }
101         }
102
103 }