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