]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.issues.ui/src/org/simantics/issues/ui/handler/ExportIssuesAsCsv.java
Externalize strings
[simantics/platform.git] / bundles / org.simantics.issues.ui / src / org / simantics / issues / ui / handler / ExportIssuesAsCsv.java
index 20cf1b3358ee4f54d921e37515b90194b35b9278..dc7b26a24ad0f8c25f7d2e60a71d351492960ae2 100644 (file)
@@ -15,6 +15,9 @@ import java.io.File;
 import java.io.PrintStream;
 import java.lang.reflect.InvocationTargetException;
 import java.util.Collection;
+import java.util.Map;
+import java.util.Set;
+import java.util.TreeMap;
 
 import org.eclipse.core.commands.AbstractHandler;
 import org.eclipse.core.commands.ExecutionEvent;
@@ -22,7 +25,6 @@ import org.eclipse.core.commands.ExecutionException;
 import org.eclipse.core.runtime.IProgressMonitor;
 import org.eclipse.core.runtime.SubMonitor;
 import org.eclipse.core.runtime.preferences.InstanceScope;
-import org.eclipse.jface.operation.IRunnableWithProgress;
 import org.eclipse.swt.SWT;
 import org.eclipse.swt.widgets.FileDialog;
 import org.eclipse.swt.widgets.Shell;
@@ -42,6 +44,7 @@ import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.layer0.variable.Variable;
 import org.simantics.db.request.Read;
 import org.simantics.issues.common.AllVisibleIssues;
+import org.simantics.issues.common.DynamicIssueSources;
 import org.simantics.issues.ui.internal.Activator;
 import org.simantics.operation.Layer0X;
 import org.simantics.simulation.ontology.SimulationResource;
@@ -55,7 +58,7 @@ import org.simantics.utils.ui.ExceptionUtils;
  */
 public class ExportIssuesAsCsv extends AbstractHandler {
 
-    private static final String PROP_LAST_VALIDATION_REPORT_PATH= "validation.report.path";
+    private static final String PROP_LAST_VALIDATION_REPORT_PATH= "validation.report.path"; //$NON-NLS-1$
 
     @Override
     public Object execute(ExecutionEvent event) throws ExecutionException {
@@ -78,9 +81,9 @@ public class ExportIssuesAsCsv extends AbstractHandler {
                 Layer0X L0X = Layer0X.getInstance(graph);
                 SimulationResource SIMU = SimulationResource.getInstance(graph);
                 for (Resource model : graph.syncRequest(new ObjectsWithType(Simantics.getProjectResource(), L0X.Activates, SIMU.Model))) {
-                    return NameUtils.getSafeName(graph, model) + ".txt";
+                    return NameUtils.getSafeName(graph, model) + ".txt"; //$NON-NLS-1$
                 }
-                return "issues.txt";
+                return "issues.txt"; //$NON-NLS-1$
             }
         });
 
@@ -102,9 +105,9 @@ public class ExportIssuesAsCsv extends AbstractHandler {
         final DataContainer<PrintStream> externalOutput = new DataContainer<PrintStream>();
 
         FileDialog fd = new FileDialog(parentShell, SWT.SAVE);
-        fd.setText("Select Validation Output");
-        fd.setFilterExtensions(new String[] { "*.txt", "*.*" });
-        fd.setFilterNames(new String[] { "Comma-Separated Values (*.txt)", "All Files (*.*)" });
+        fd.setText(Messages.ExportIssuesAsCsv_SelectValidationOutput);
+        fd.setFilterExtensions(new String[] { "*.txt", "*.*" }); //$NON-NLS-1$ //$NON-NLS-2$
+        fd.setFilterNames(new String[] { Messages.ExportIssuesAsCsv_CommaSeparatedValues, Messages.ExportIssuesAsCsv_AllFiles });
         if (lastReportPath != null)
             fd.setFilterPath(lastReportPath);
         fd.setFileName(fileName);
@@ -117,46 +120,17 @@ public class ExportIssuesAsCsv extends AbstractHandler {
                 ExceptionUtils.logError(e);
             }
         } else {
-//            boolean result = MessageDialog.openQuestion(parentShell, "Print to stdout?", "Would you like to perform model validation and print the results into stdout?");
-//            if (!result)
-//                return;
-//            externalOutput.set(System.out);
             return;
         }
 
         try {
-            window.getWorkbench().getProgressService().busyCursorWhile(new IRunnableWithProgress() {
-                @Override
-                public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
-                    try {
-                        if (externalOutput.get() == null)
-                            externalOutput.set(new PrintStream(new File(path)));
-
-                        final SubMonitor progress = SubMonitor.convert(monitor, "Export issues", IProgressMonitor.UNKNOWN);
-                        Simantics.getSession().syncRequest(new ReadRequest() {
-                            @Override
-                            public void run(ReadGraph graph) throws DatabaseException {
-                                PrintStream out = externalOutput.get();
-
-                                Collection<Variable> activeIssues = graph.syncRequest(new AllVisibleIssues(Simantics.getProjectResource()));
-                                out.println("# Exported issues (" + activeIssues.size() + ")");
-                                for (Variable issue : activeIssues) {
-                                    String description = StringUtils.safeString( (String) issue.getPossiblePropertyValue(graph, "HasDescription") );
-                                    String severity = StringUtils.safeString( (String) issue.getPossiblePropertyValue(graph, "severity") );
-                                    String resource = StringUtils.safeString( (String) issue.getPossiblePropertyValue(graph, "resource") );
-                                    String path = StringUtils.safeString( (String) issue.getPossiblePropertyValue(graph, "path") );
-                                    out.println(description + ";" + severity + ";" + resource + ";" + path);
-                                    progress.worked(1);
-                                }
-                            }
-                        });
-                    } catch (Exception e) {
-                        throw new InvocationTargetException(e);
-                    } finally {
-                        monitor.done();
-                        if (externalOutput.get() != System.out)
-                            FileUtils.uncheckedClose(externalOutput.get());
-                    }
+            window.getWorkbench().getProgressService().busyCursorWhile(monitor -> {
+                try (PrintStream out = new PrintStream(new File(path))) {
+                    export(monitor, out);
+                } catch (Exception e) {
+                    throw new InvocationTargetException(e);
+                } finally {
+                    monitor.done();
                 }
             });
         } catch (InvocationTargetException e) {
@@ -166,4 +140,58 @@ public class ExportIssuesAsCsv extends AbstractHandler {
         }
     }
 
+    private void export(IProgressMonitor monitor, PrintStream out) throws DatabaseException {
+        SubMonitor progress = SubMonitor.convert(monitor, Messages.ExportIssuesAsCsv_ExportIssues, IProgressMonitor.UNKNOWN);
+        Simantics.getSession().syncRequest(new ReadRequest() {
+            @Override
+            public void run(ReadGraph graph) throws DatabaseException {
+                Collection<Variable> activeIssues = graph.syncRequest(new AllVisibleIssues(Simantics.getProjectResource()));
+                out.println("# Exported issues (" + activeIssues.size() + ")"); //$NON-NLS-1$ //$NON-NLS-2$
+                for (Variable issue : activeIssues) {
+                    exportIssue(graph, issue, out, 0);
+                    progress.worked(1);
+                }
+
+                Map<String, Variable> dynamicIssueSources = nameMap(graph,
+                        graph.syncRequest(new DynamicIssueSources(Simantics.getProjectResource())));
+                if (!dynamicIssueSources.isEmpty()) {
+                    out.println();
+                    out.println("# Dynamic Issues"); //$NON-NLS-1$
+                    for (Variable source : dynamicIssueSources.values()) {
+                        exportDynamicIssueSource(progress, graph, source, out, 0);
+                    }
+                }
+            }
+        });
+    }
+
+    private Map<String, Variable> nameMap(ReadGraph graph, Set<Variable> sources) throws DatabaseException {
+        TreeMap<String, Variable> sorted = new TreeMap<>();
+        for (Variable v : sources) {
+            String name = v.getPossiblePropertyValue(graph, "HasDescription", Bindings.STRING); //$NON-NLS-1$
+            if (name == null)
+                name = v.getName(graph);
+            sorted.put(name, v);
+        }
+        return sorted;
+    }
+
+    protected void exportDynamicIssueSource(IProgressMonitor monitor, ReadGraph graph, Variable issue, PrintStream out, int startColumn) throws DatabaseException {
+        exportIssue(graph, issue, out, startColumn);
+        for (Variable child : issue.getChildren(graph)) {
+            exportDynamicIssueSource(monitor, graph, child, out, startColumn+1);
+            monitor.worked(1);
+        }
+    }
+
+    private void exportIssue(ReadGraph graph, Variable issue, PrintStream out, int startColumn) throws DatabaseException {
+        String description = StringUtils.safeString( (String) issue.getPossiblePropertyValue(graph, "HasDescription") ); //$NON-NLS-1$
+        String severity = StringUtils.safeString( (String) issue.getPossiblePropertyValue(graph, "severity") ); //$NON-NLS-1$
+        String resource = StringUtils.safeString( (String) issue.getPossiblePropertyValue(graph, "resource") ); //$NON-NLS-1$
+        String path = StringUtils.safeString( (String) issue.getPossiblePropertyValue(graph, "path") ); //$NON-NLS-1$
+        for (int i = 0; i < startColumn; ++i)
+            out.print(";"); //$NON-NLS-1$
+        out.println(description + ";" + severity + ";" + resource + ";" + path); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
+    }
+
 }
\ No newline at end of file