1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
\r
3 * in Industry THTH ry.
\r
4 * All rights reserved. This program and the accompanying materials
\r
5 * are made available under the terms of the Eclipse Public License v1.0
\r
6 * which accompanies this distribution, and is available at
\r
7 * http://www.eclipse.org/legal/epl-v10.html
\r
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.debug.ui;
\r
14 import java.io.File;
\r
15 import java.io.IOException;
\r
16 import java.io.PrintStream;
\r
17 import java.nio.file.Files;
\r
18 import java.nio.file.Path;
\r
19 import java.util.Collections;
\r
20 import java.util.LinkedList;
\r
21 import java.util.Set;
\r
23 import org.eclipse.core.runtime.IStatus;
\r
24 import org.eclipse.core.runtime.Status;
\r
25 import org.eclipse.swt.SWT;
\r
26 import org.eclipse.swt.browser.Browser;
\r
27 import org.eclipse.swt.custom.CTabFolder;
\r
28 import org.eclipse.swt.custom.CTabItem;
\r
29 import org.eclipse.swt.events.KeyAdapter;
\r
30 import org.eclipse.swt.events.KeyEvent;
\r
31 import org.eclipse.swt.layout.GridData;
\r
32 import org.eclipse.swt.widgets.Composite;
\r
33 import org.eclipse.swt.widgets.Control;
\r
34 import org.eclipse.swt.widgets.Text;
\r
35 import org.eclipse.ui.IWorkbenchSite;
\r
36 import org.simantics.Simantics;
\r
37 import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport;
\r
38 import org.simantics.db.WriteGraph;
\r
39 import org.simantics.db.common.request.WriteRequest;
\r
40 import org.simantics.db.debug.ListenerReport;
\r
41 import org.simantics.db.exception.CancelTransactionException;
\r
42 import org.simantics.db.exception.DatabaseException;
\r
43 import org.simantics.db.management.ISessionContext;
\r
44 import org.simantics.db.service.DebugSupport;
\r
45 import org.simantics.debug.ui.internal.Activator;
\r
46 import org.simantics.ui.workbench.IPropertyPage;
\r
47 import org.simantics.utils.ui.LayoutUtils;
\r
48 import org.simantics.utils.ui.SWTUtils;
\r
49 import org.simantics.views.swt.SimanticsView;
\r
51 @SuppressWarnings("deprecation")
\r
52 public class SessionDebuggerView extends SimanticsView {
\r
54 public static final String VIEW_ID = "org.simantics.debug.sessionDebugger";
\r
56 private CTabFolder folder;
\r
57 private Text commandLine;
\r
58 private Browser console;
\r
60 private final LinkedList<String> terminal = new LinkedList<String>();
\r
61 private final LinkedList<String> history = new LinkedList<String>();
\r
62 private int historyPosition = -1;
\r
65 protected Set<String> getBrowseContexts() {
\r
66 return Collections.singleton("");
\r
69 private CTabItem createItem(int index, CTabFolder folder, Control control) {
\r
70 CTabItem item = new CTabItem(folder, SWT.NONE, index);
\r
71 item.setControl(control);
\r
76 protected void createControls(Composite body, IWorkbenchSite site, ISessionContext context, WidgetSupport support) {
\r
78 body.setLayout(LayoutUtils.createNoBorderGridLayout(1));
\r
80 folder = new CTabFolder(body, SWT.BOTTOM | SWT.FLAT);
\r
81 folder.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
\r
83 Composite shell = new Composite(folder, SWT.NONE);
\r
84 shell.setLayout(LayoutUtils.createNoBorderGridLayout(1));
\r
86 commandLine = new Text(shell, SWT.BORDER);
\r
87 commandLine.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
\r
89 console = new Browser(shell, SWT.BORDER);
\r
90 console.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
\r
92 commandLine.addKeyListener(new KeyAdapter() {
\r
94 public void keyReleased(KeyEvent e) {
\r
95 if (e.keyCode == SWT.ARROW_UP) {
\r
96 if (historyPosition > 10 || historyPosition >= (history.size() - 1)) {
\r
99 commandLine.setText(history.get(++historyPosition));
\r
100 } else if (e.keyCode == SWT.ARROW_DOWN) {
\r
101 if (historyPosition < 0) {
\r
103 } else if (historyPosition == 0) {
\r
104 commandLine.setText("");
\r
105 historyPosition = -1;
\r
107 commandLine.setText(history.get(--historyPosition));
\r
109 } else if (e.keyCode == SWT.ESC) {
\r
110 historyPosition = -1;
\r
111 commandLine.setText("");
\r
112 } else if (e.keyCode == SWT.CR || e.keyCode == SWT.KEYPAD_CR) {
\r
113 applyCommand(commandLine.getText());
\r
118 CTabItem shellItem = createItem(0, folder, shell);
\r
119 shellItem.setText("Shell");
\r
121 // SessionDebugger deprecated = new SessionDebugger(folder, SWT.NONE, Simantics.getSession());
\r
122 // deprecated.initializeUI();
\r
123 // CTabItem debuggerItem = createItem(1, folder, deprecated);
\r
124 // debuggerItem.setText("Debugger");
\r
126 folder.setSelection(shellItem);
\r
129 private void applyCommand(final String command) {
\r
131 Simantics.getSession().syncRequest(new WriteRequest() {
\r
133 public void perform(WriteGraph graph) throws DatabaseException {
\r
134 DebugSupport support = graph.getService(DebugSupport.class);
\r
135 Object data = support.query(graph, command);
\r
136 apply(command, data);
\r
139 } catch (CancelTransactionException e) {
\r
140 } catch (DatabaseException e) {
\r
141 Activator.getDefault().getLog().log(
\r
142 new Status(IStatus.ERROR, Activator.PLUGIN_ID,
\r
143 "Unexpected exception while applying command " + command, e));
\r
147 private void addHistory(String command, Object output) {
\r
148 if (output instanceof String) {
\r
149 terminal.addFirst((String) output);
\r
150 } else if (output instanceof Path) {
\r
152 Path p = (Path) output;
\r
153 long size = Files.size(p);
\r
154 if (size < (1L << 16)) {
\r
155 terminal.addFirst(new String(Files.readAllBytes(p), "UTF-8"));
\r
157 terminal.addFirst("Wrote command '" + command + "' output to file " + p);
\r
158 } catch (IOException e) {
\r
159 Activator.getDefault().getLog().log(
\r
160 new Status(IStatus.ERROR, Activator.PLUGIN_ID,
\r
161 "Unexpected I/O exception while applying command " + command, e));
\r
164 throw new IllegalArgumentException("Unsupported output argument type " + output);
\r
166 if (terminal.size() > 10)
\r
167 terminal.removeLast();
\r
169 history.addFirst(command);
\r
170 if (history.size() > 10)
\r
171 history.removeLast();
\r
173 historyPosition = -1;
\r
176 private void apply(String command, Object data) {
\r
177 if (data instanceof String) {
\r
178 SWTUtils.asyncExec(commandLine, () -> {
\r
179 commandLine.setText("");
\r
180 addHistory(command, data);
\r
181 console.setText(formatTerminal());
\r
183 } else if (data instanceof ListenerReport) {
\r
184 SWTUtils.asyncExec(commandLine, () -> {
\r
186 addHistory(command, dumpListenerReport((ListenerReport) data));
\r
187 commandLine.setText("");
\r
188 console.setText(formatTerminal());
\r
189 } catch (IOException e) {
\r
190 Activator.getDefault().getLog().log(
\r
191 new Status(IStatus.ERROR, Activator.PLUGIN_ID,
\r
192 "Unexpected I/O exception while applying command " + command, e));
\r
198 private Path dumpListenerReport(ListenerReport data) throws IOException {
\r
199 File f = Simantics.getTempfile("debug", "listenerReport");
\r
200 try (PrintStream out = new PrintStream(f, "UTF-8")) {
\r
201 out.print("<pre>");
\r
203 out.print("</pre>");
\r
208 private String formatTerminal() {
\r
209 StringBuilder b = new StringBuilder();
\r
210 b.append("<html><head/><body>\n");
\r
211 for (String s : terminal)
\r
212 b.append(s).append("<br/>\n");
\r
213 b.append("</body></html>");
\r
214 return b.toString();
\r
218 public void setFocus() {
\r
223 protected IPropertyPage getPropertyPage() {
\r