]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.debug.ui/src/org/simantics/debug/ui/SessionDebuggerView.java
0e0363fa87ec842b59a65d9119db15cc2a4a4135
[simantics/platform.git] / bundles / org.simantics.debug.ui / src / org / simantics / debug / ui / SessionDebuggerView.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.debug.ui;
13
14 import java.io.File;
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;
21 import java.util.Set;
22
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;
50
51 @SuppressWarnings("deprecation")
52 public class SessionDebuggerView extends SimanticsView {
53
54     public static final String VIEW_ID = "org.simantics.debug.sessionDebugger";
55
56     private CTabFolder folder;
57     private Text commandLine;
58     private Browser console;
59
60     private final LinkedList<String> terminal = new LinkedList<String>();
61     private final LinkedList<String> history = new LinkedList<String>();
62     private int historyPosition = -1;
63
64     @Override
65     protected Set<String> getBrowseContexts() {
66         return Collections.singleton("");
67     }
68
69     private CTabItem createItem(int index, CTabFolder folder, Control control) {
70         CTabItem item = new CTabItem(folder, SWT.NONE, index);
71         item.setControl(control);
72         return item;
73     }
74
75     @Override
76     protected void createControls(Composite body, IWorkbenchSite site, ISessionContext context, WidgetSupport support) {
77
78         body.setLayout(LayoutUtils.createNoBorderGridLayout(1));
79
80         folder = new CTabFolder(body, SWT.BOTTOM | SWT.FLAT);
81         folder.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
82
83         Composite shell = new Composite(folder, SWT.NONE);
84         shell.setLayout(LayoutUtils.createNoBorderGridLayout(1));
85
86         commandLine = new Text(shell, SWT.BORDER);
87         commandLine.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
88
89         console = new Browser(shell, SWT.BORDER);
90         console.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
91
92         commandLine.addKeyListener(new KeyAdapter() {
93             @Override
94             public void keyReleased(KeyEvent e) {
95                 if (e.keyCode == SWT.ARROW_UP) {
96                     if (historyPosition > 10 || historyPosition >= (history.size() - 1)) {
97                         return;
98                     }
99                     commandLine.setText(history.get(++historyPosition));
100                 } else if (e.keyCode == SWT.ARROW_DOWN) {
101                     if (historyPosition < 0) {
102                         return;
103                     } else if (historyPosition == 0) {
104                         commandLine.setText("");
105                         historyPosition = -1;
106                     } else {
107                         commandLine.setText(history.get(--historyPosition));
108                     }
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());
114                 }
115             }
116         });
117
118         CTabItem shellItem = createItem(0, folder, shell);
119         shellItem.setText("Shell");
120
121 //        SessionDebugger deprecated = new SessionDebugger(folder, SWT.NONE, Simantics.getSession());
122 //        deprecated.initializeUI();
123 //        CTabItem debuggerItem = createItem(1, folder, deprecated);
124 //        debuggerItem.setText("Debugger");
125
126         folder.setSelection(shellItem);
127     }
128
129     private void applyCommand(final String command) {
130         try {
131             Simantics.getSession().syncRequest(new WriteRequest() {
132                 @Override
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);
137                 }
138             });
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));
144         }
145     }
146
147     private void addHistory(String command, Object output) {
148         if (output instanceof String) {
149             terminal.addFirst((String) output);
150         } else if (output instanceof Path) {
151             try {
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"));
156                 }
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));
162             }
163         } else {
164             throw new IllegalArgumentException("Unsupported output argument type " + output);
165         }
166         if (terminal.size() > 10)
167             terminal.removeLast();
168
169         history.addFirst(command);
170         if (history.size() > 10)
171             history.removeLast();
172
173         historyPosition = -1;
174     }
175
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());
182             });
183         } else if (data instanceof ListenerReport) {
184             SWTUtils.asyncExec(commandLine, () -> {
185                 try {
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));
193                 }
194             });
195         }
196     }
197
198     private Path dumpListenerReport(ListenerReport data) throws IOException {
199         File f = Simantics.getTempfile("debug", "listenerReport");
200         try (PrintStream out = new PrintStream(f, "UTF-8")) {
201             out.print("<pre>");
202             data.print(out);
203             out.print("</pre>");
204         }
205         return f.toPath();
206     }
207
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>");
214         return b.toString();
215     }
216
217     @Override
218     public void setFocus() {
219         folder.setFocus();
220     }
221
222     @Override
223     protected IPropertyPage getPropertyPage() {
224         return null;
225     }
226
227 }