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.osgi.util.NLS;
26 import org.eclipse.swt.SWT;
27 import org.eclipse.swt.browser.Browser;
28 import org.eclipse.swt.custom.CTabFolder;
29 import org.eclipse.swt.custom.CTabItem;
30 import org.eclipse.swt.events.KeyAdapter;
31 import org.eclipse.swt.events.KeyEvent;
32 import org.eclipse.swt.layout.GridData;
33 import org.eclipse.swt.widgets.Composite;
34 import org.eclipse.swt.widgets.Control;
35 import org.eclipse.swt.widgets.Text;
36 import org.eclipse.ui.IWorkbenchSite;
37 import org.simantics.Simantics;
38 import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport;
39 import org.simantics.db.WriteGraph;
40 import org.simantics.db.common.request.WriteRequest;
41 import org.simantics.db.debug.ListenerReport;
42 import org.simantics.db.exception.CancelTransactionException;
43 import org.simantics.db.exception.DatabaseException;
44 import org.simantics.db.management.ISessionContext;
45 import org.simantics.db.service.DebugSupport;
46 import org.simantics.debug.ui.internal.Activator;
47 import org.simantics.ui.workbench.IPropertyPage;
48 import org.simantics.utils.ui.LayoutUtils;
49 import org.simantics.utils.ui.SWTUtils;
50 import org.simantics.views.swt.SimanticsView;
52 @SuppressWarnings("deprecation")
53 public class SessionDebuggerView extends SimanticsView {
55 public static final String VIEW_ID = "org.simantics.debug.sessionDebugger"; //$NON-NLS-1$
57 private CTabFolder folder;
58 private Text commandLine;
59 private Browser console;
61 private final LinkedList<String> terminal = new LinkedList<String>();
62 private final LinkedList<String> history = new LinkedList<String>();
63 private int historyPosition = -1;
66 protected Set<String> getBrowseContexts() {
67 return Collections.singleton(""); //$NON-NLS-1$
70 private CTabItem createItem(int index, CTabFolder folder, Control control) {
71 CTabItem item = new CTabItem(folder, SWT.NONE, index);
72 item.setControl(control);
77 protected void createControls(Composite body, IWorkbenchSite site, ISessionContext context, WidgetSupport support) {
79 body.setLayout(LayoutUtils.createNoBorderGridLayout(1));
81 folder = new CTabFolder(body, SWT.BOTTOM | SWT.FLAT);
82 folder.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
84 Composite shell = new Composite(folder, SWT.NONE);
85 shell.setLayout(LayoutUtils.createNoBorderGridLayout(1));
87 commandLine = new Text(shell, SWT.BORDER);
88 commandLine.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
90 console = new Browser(shell, SWT.BORDER);
91 console.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
93 commandLine.addKeyListener(new KeyAdapter() {
95 public void keyReleased(KeyEvent e) {
96 if (e.keyCode == SWT.ARROW_UP) {
97 if (historyPosition > 10 || historyPosition >= (history.size() - 1)) {
100 commandLine.setText(history.get(++historyPosition));
101 } else if (e.keyCode == SWT.ARROW_DOWN) {
102 if (historyPosition < 0) {
104 } else if (historyPosition == 0) {
105 commandLine.setText(""); //$NON-NLS-1$
106 historyPosition = -1;
108 commandLine.setText(history.get(--historyPosition));
110 } else if (e.keyCode == SWT.ESC) {
111 historyPosition = -1;
112 commandLine.setText(""); //$NON-NLS-1$
113 } else if (e.keyCode == SWT.CR || e.keyCode == SWT.KEYPAD_CR) {
114 applyCommand(commandLine.getText());
119 CTabItem shellItem = createItem(0, folder, shell);
120 shellItem.setText(Messages.SessionDebuggerView_Shell);
122 // SessionDebugger deprecated = new SessionDebugger(folder, SWT.NONE, Simantics.getSession());
123 // deprecated.initializeUI();
124 // CTabItem debuggerItem = createItem(1, folder, deprecated);
125 // debuggerItem.setText("Debugger");
127 folder.setSelection(shellItem);
130 private void applyCommand(final String command) {
132 Simantics.getSession().syncRequest(new WriteRequest() {
134 public void perform(WriteGraph graph) throws DatabaseException {
135 DebugSupport support = graph.getService(DebugSupport.class);
136 Object data = support.query(graph, command);
137 apply(command, data);
140 } catch (CancelTransactionException e) {
141 } catch (DatabaseException e) {
142 Activator.getDefault().getLog().log(
143 new Status(IStatus.ERROR, Activator.PLUGIN_ID,
144 Messages.SessionDebuggerView_ActivatorUnexpectedException + command, e));
148 private void addHistory(String command, Object output) {
149 if (output instanceof String) {
150 terminal.addFirst((String) output);
151 } else if (output instanceof Path) {
153 Path p = (Path) output;
154 long size = Files.size(p);
155 if (size < (1L << 16)) {
156 terminal.addFirst(new String(Files.readAllBytes(p), "UTF-8")); //$NON-NLS-1$
158 terminal.addFirst(NLS.bind(Messages.SessionDebuggerView_WroteCommand ,new Object[] { command , p}));
159 } catch (IOException e) {
160 Activator.getDefault().getLog().log(
161 new Status(IStatus.ERROR, Activator.PLUGIN_ID,
162 NLS.bind(Messages.SessionDebuggerView_ActivatorUnexpectedIOException , new Object[]{ command, e})));
165 throw new IllegalArgumentException("Unsupported output argument type " + output); //$NON-NLS-1$
167 if (terminal.size() > 10)
168 terminal.removeLast();
170 history.addFirst(command);
171 if (history.size() > 10)
172 history.removeLast();
174 historyPosition = -1;
177 private void apply(String command, Object data) {
178 if (data instanceof String) {
179 SWTUtils.asyncExec(commandLine, () -> {
180 commandLine.setText(""); //$NON-NLS-1$
181 addHistory(command, data);
182 console.setText(formatTerminal());
184 } else if (data instanceof ListenerReport) {
185 SWTUtils.asyncExec(commandLine, () -> {
187 addHistory(command, dumpListenerReport((ListenerReport) data));
188 commandLine.setText(""); //$NON-NLS-1$
189 console.setText(formatTerminal());
190 } catch (IOException e) {
191 Activator.getDefault().getLog().log(
192 new Status(IStatus.ERROR, Activator.PLUGIN_ID,
193 Messages.SessionDebuggerView_ActivatorUnexpectedIOException + command, e));
199 private Path dumpListenerReport(ListenerReport data) throws IOException {
200 File f = Simantics.getTempfile("debug", "listenerReport"); //$NON-NLS-1$ //$NON-NLS-2$
201 try (PrintStream out = new PrintStream(f, "UTF-8")) { //$NON-NLS-1$
202 out.print("<pre>"); //$NON-NLS-1$
204 out.print("</pre>"); //$NON-NLS-1$
209 private String formatTerminal() {
210 StringBuilder b = new StringBuilder();
211 b.append("<html><head/><body>\n"); //$NON-NLS-1$
212 for (String s : terminal)
213 b.append(s).append("<br/>\n"); //$NON-NLS-1$
214 b.append("</body></html>"); //$NON-NLS-1$
219 public void setFocus() {
224 protected IPropertyPage getPropertyPage() {