]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.debug.ui/src/org/simantics/debug/ui/SessionDebuggerView.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.debug.ui / src / org / simantics / debug / ui / SessionDebuggerView.java
diff --git a/bundles/org.simantics.debug.ui/src/org/simantics/debug/ui/SessionDebuggerView.java b/bundles/org.simantics.debug.ui/src/org/simantics/debug/ui/SessionDebuggerView.java
new file mode 100644 (file)
index 0000000..77ff498
--- /dev/null
@@ -0,0 +1,227 @@
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 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
+ *     VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.debug.ui;\r
+\r
+import java.io.File;\r
+import java.io.IOException;\r
+import java.io.PrintStream;\r
+import java.nio.file.Files;\r
+import java.nio.file.Path;\r
+import java.util.Collections;\r
+import java.util.LinkedList;\r
+import java.util.Set;\r
+\r
+import org.eclipse.core.runtime.IStatus;\r
+import org.eclipse.core.runtime.Status;\r
+import org.eclipse.swt.SWT;\r
+import org.eclipse.swt.browser.Browser;\r
+import org.eclipse.swt.custom.CTabFolder;\r
+import org.eclipse.swt.custom.CTabItem;\r
+import org.eclipse.swt.events.KeyAdapter;\r
+import org.eclipse.swt.events.KeyEvent;\r
+import org.eclipse.swt.layout.GridData;\r
+import org.eclipse.swt.widgets.Composite;\r
+import org.eclipse.swt.widgets.Control;\r
+import org.eclipse.swt.widgets.Text;\r
+import org.eclipse.ui.IWorkbenchSite;\r
+import org.simantics.Simantics;\r
+import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport;\r
+import org.simantics.db.WriteGraph;\r
+import org.simantics.db.common.request.WriteRequest;\r
+import org.simantics.db.debug.ListenerReport;\r
+import org.simantics.db.exception.CancelTransactionException;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.management.ISessionContext;\r
+import org.simantics.db.service.DebugSupport;\r
+import org.simantics.debug.ui.internal.Activator;\r
+import org.simantics.ui.workbench.IPropertyPage;\r
+import org.simantics.utils.ui.LayoutUtils;\r
+import org.simantics.utils.ui.SWTUtils;\r
+import org.simantics.views.swt.SimanticsView;\r
+\r
+@SuppressWarnings("deprecation")\r
+public class SessionDebuggerView extends SimanticsView {\r
+\r
+    public static final String VIEW_ID = "org.simantics.debug.sessionDebugger";\r
+\r
+    private CTabFolder folder;\r
+    private Text commandLine;\r
+    private Browser console;\r
+\r
+    private final LinkedList<String> terminal = new LinkedList<String>();\r
+    private final LinkedList<String> history = new LinkedList<String>();\r
+    private int historyPosition = -1;\r
+\r
+    @Override\r
+    protected Set<String> getBrowseContexts() {\r
+        return Collections.singleton("");\r
+    }\r
+\r
+    private CTabItem createItem(int index, CTabFolder folder, Control control) {\r
+        CTabItem item = new CTabItem(folder, SWT.NONE, index);\r
+        item.setControl(control);\r
+        return item;\r
+    }\r
+\r
+    @Override\r
+    protected void createControls(Composite body, IWorkbenchSite site, ISessionContext context, WidgetSupport support) {\r
+\r
+        body.setLayout(LayoutUtils.createNoBorderGridLayout(1));\r
+\r
+        folder = new CTabFolder(body, SWT.BOTTOM | SWT.FLAT);\r
+        folder.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));\r
+\r
+        Composite shell = new Composite(folder, SWT.NONE);\r
+        shell.setLayout(LayoutUtils.createNoBorderGridLayout(1));\r
+\r
+        commandLine = new Text(shell, SWT.BORDER);\r
+        commandLine.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));\r
+\r
+        console = new Browser(shell, SWT.BORDER);\r
+        console.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));\r
+\r
+        commandLine.addKeyListener(new KeyAdapter() {\r
+            @Override\r
+            public void keyReleased(KeyEvent e) {\r
+                if (e.keyCode == SWT.ARROW_UP) {\r
+                    if (historyPosition > 10 || historyPosition >= (history.size() - 1)) {\r
+                        return;\r
+                    }\r
+                    commandLine.setText(history.get(++historyPosition));\r
+                } else if (e.keyCode == SWT.ARROW_DOWN) {\r
+                    if (historyPosition < 0) {\r
+                        return;\r
+                    } else if (historyPosition == 0) {\r
+                        commandLine.setText("");\r
+                        historyPosition = -1;\r
+                    } else {\r
+                        commandLine.setText(history.get(--historyPosition));\r
+                    }\r
+                } else if (e.keyCode == SWT.ESC) {\r
+                    historyPosition = -1;\r
+                    commandLine.setText("");\r
+                } else if (e.keyCode == SWT.CR || e.keyCode == SWT.KEYPAD_CR) {\r
+                    applyCommand(commandLine.getText());\r
+                }\r
+            }\r
+        });\r
+\r
+        CTabItem shellItem = createItem(0, folder, shell);\r
+        shellItem.setText("Shell");\r
+\r
+//        SessionDebugger deprecated = new SessionDebugger(folder, SWT.NONE, Simantics.getSession());\r
+//        deprecated.initializeUI();\r
+//        CTabItem debuggerItem = createItem(1, folder, deprecated);\r
+//        debuggerItem.setText("Debugger");\r
+\r
+        folder.setSelection(shellItem);\r
+    }\r
+\r
+    private void applyCommand(final String command) {\r
+        try {\r
+            Simantics.getSession().syncRequest(new WriteRequest() {\r
+                @Override\r
+                public void perform(WriteGraph graph) throws DatabaseException {\r
+                    DebugSupport support = graph.getService(DebugSupport.class);\r
+                    Object data = support.query(graph, command);\r
+                    apply(command, data);\r
+                }\r
+            });\r
+        } catch (CancelTransactionException e) {\r
+        } catch (DatabaseException e) {\r
+            Activator.getDefault().getLog().log(\r
+                    new Status(IStatus.ERROR, Activator.PLUGIN_ID,\r
+                            "Unexpected exception while applying command " + command, e));\r
+        }\r
+    }\r
+\r
+    private void addHistory(String command, Object output) {\r
+        if (output instanceof String) {\r
+            terminal.addFirst((String) output);\r
+        } else if (output instanceof Path) {\r
+            try {\r
+                Path p = (Path) output;\r
+                long size = Files.size(p);\r
+                if (size < (1L << 16)) {\r
+                    terminal.addFirst(new String(Files.readAllBytes(p), "UTF-8"));\r
+                }\r
+                terminal.addFirst("Wrote command '" + command + "' output to file " + p);\r
+            } catch (IOException e) {\r
+                Activator.getDefault().getLog().log(\r
+                        new Status(IStatus.ERROR, Activator.PLUGIN_ID,\r
+                                "Unexpected I/O exception while applying command " + command, e));\r
+            }\r
+        } else {\r
+            throw new IllegalArgumentException("Unsupported output argument type " + output);\r
+        }\r
+        if (terminal.size() > 10)\r
+            terminal.removeLast();\r
+\r
+        history.addFirst(command);\r
+        if (history.size() > 10)\r
+            history.removeLast();\r
+\r
+        historyPosition = -1;\r
+    }\r
+\r
+    private void apply(String command, Object data) {\r
+        if (data instanceof String) {\r
+            SWTUtils.asyncExec(commandLine, () -> {\r
+                commandLine.setText("");\r
+                addHistory(command, data);\r
+                console.setText(formatTerminal());\r
+            });\r
+        } else if (data instanceof ListenerReport) {\r
+            SWTUtils.asyncExec(commandLine, () -> {\r
+                try {\r
+                    addHistory(command, dumpListenerReport((ListenerReport) data));\r
+                    commandLine.setText("");\r
+                    console.setText(formatTerminal());\r
+                } catch (IOException e) {\r
+                    Activator.getDefault().getLog().log(\r
+                            new Status(IStatus.ERROR, Activator.PLUGIN_ID,\r
+                                    "Unexpected I/O exception while applying command " + command, e));\r
+                }\r
+            });\r
+        }\r
+    }\r
+\r
+    private Path dumpListenerReport(ListenerReport data) throws IOException {\r
+        File f = Simantics.getTempfile("debug", "listenerReport");\r
+        try (PrintStream out = new PrintStream(f, "UTF-8")) {\r
+            out.print("<pre>");\r
+            data.print(out);\r
+            out.print("</pre>");\r
+        }\r
+        return f.toPath();\r
+    }\r
+\r
+    private String formatTerminal() {\r
+        StringBuilder b = new StringBuilder();\r
+        b.append("<html><head/><body>\n");\r
+        for (String s : terminal)\r
+            b.append(s).append("<br/>\n");\r
+        b.append("</body></html>");\r
+        return b.toString();\r
+    }\r
+\r
+    @Override\r
+    public void setFocus() {\r
+        folder.setFocus();\r
+    }\r
+\r
+    @Override\r
+    protected IPropertyPage getPropertyPage() {\r
+        return null;\r
+    }\r
+\r
+}\r