]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.event/src/org/simantics/event/util/EventExporter.java
Merge "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.strings.EString;
30
31 /**
32  * @author Tuukka Lehtonen
33  */
34 public class EventExporter {
35
36         /**
37          * SCL API.
38          * 
39          * @param outputFile
40          * @throws DatabaseException
41          * @throws IOException
42          */
43         public static void exportCurrentEvents(String columnSeparator, String outputFile) throws DatabaseException, IOException {
44                 new EventExporter().exportCsv(null, new File(outputFile), columnSeparator);
45         }
46
47         /**
48          * SCL API.
49          * 
50          * @param outputFile
51          * @throws DatabaseException
52          * @throws IOException
53          */
54         public static void exportCurrentEventsG(ReadGraph graph, String columnSeparator, String outputFile) throws DatabaseException, IOException {
55                 new EventExporter().exportCsv(graph, null, new File(outputFile), columnSeparator);
56         }
57
58         public EventExporter() {
59         }
60
61         /**
62          * @param monitor
63          *            the progress monitor to use for reporting progress to the
64          *            user. It is the caller's responsibility to call done() on the
65          *            given monitor. Accepts <code>null</code>, indicating that no
66          *            progress should be reported and that the operation cannot be
67          *            cancelled.</pre>
68          * @param file
69          * @param columnSeparator
70          * @throws DatabaseException
71          * @throws IOException
72          */
73         public void exportCsv(IProgressMonitor monitor, File file, String columnSeparator) throws DatabaseException, IOException {
74                 try (PrintStream out = new PrintStream(file)) {
75                         Simantics.getSession().syncRequest(new ReadRequest() {
76                                 @Override
77                                 public void run(ReadGraph graph) throws DatabaseException {
78                                         exportCsv(graph, monitor, out, columnSeparator);
79                                 }
80                         });
81                 }
82         }
83
84         /**
85          * @param graph
86          * @param monitor
87          *            the progress monitor to use for reporting progress to the
88          *            user. It is the caller's responsibility to call done() on the
89          *            given monitor. Accepts <code>null</code>, indicating that no
90          *            progress should be reported and that the operation cannot be
91          *            cancelled.</pre>
92          * @param file
93          * @param columnSeparator
94          * @throws DatabaseException
95          * @throws IOException
96          */
97         public void exportCsv(ReadGraph graph, IProgressMonitor monitor, File file, String columnSeparator) throws DatabaseException, IOException {
98                 try (PrintStream out = new PrintStream(file)) {
99                         exportCsv(graph, monitor, out, columnSeparator);
100                 }
101         }
102
103         /**
104          * @param graph
105          * @param monitor
106          *            the progress monitor to use for reporting progress to the
107          *            user. It is the caller's responsibility to call done() on the
108          *            given monitor. Accepts <code>null</code>, indicating that no
109          *            progress should be reported and that the operation cannot be
110          *            cancelled.</pre>
111          * @param out
112          * @param columnSeparator
113          * @throws DatabaseException
114          */
115         public void exportCsv(ReadGraph graph, IProgressMonitor monitor, PrintStream out, String columnSeparator) throws DatabaseException {
116                 Collection<?> events = ProjectEventsRule.INSTANCE.getChildren(graph, Simantics.getProjectResource());
117                 if (events.isEmpty()) {
118                         out.println("No visible events recorded.");
119                         return;
120                 }
121
122                 final SubMonitor mon = SubMonitor.convert(monitor, "Export events", events.size());
123
124                 boolean first = true;
125                 for (Object obj : events) {
126                         if (mon.isCanceled())
127                                 return;
128
129                         Resource event = (Resource) obj;
130                         Map<String, String> labels = EventLabelRule.INSTANCE.getLabel(graph, event, true);
131                         if (first) {
132                                 out.println(EString.implode(labels.keySet(), columnSeparator));
133                                 first = false;
134                         }
135                         out.println(EString.implode(labels.values(), columnSeparator));
136                         mon.worked(1);
137                 }
138         }
139
140 }