]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.debug.ui/src/org/simantics/debug/ui/SessionDebuggerView.java
1426afa6dc08af020fbabbf9cf619c4987265b7c
[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.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;
51
52 @SuppressWarnings("deprecation")
53 public class SessionDebuggerView extends SimanticsView {
54
55     public static final String VIEW_ID = "org.simantics.debug.sessionDebugger"; //$NON-NLS-1$
56
57     private CTabFolder folder;
58     private Text commandLine;
59     private Browser console;
60
61     private final LinkedList<String> terminal = new LinkedList<String>();
62     private final LinkedList<String> history = new LinkedList<String>();
63     private int historyPosition = -1;
64
65     @Override
66     protected Set<String> getBrowseContexts() {
67         return Collections.singleton(""); //$NON-NLS-1$
68     }
69
70     private CTabItem createItem(int index, CTabFolder folder, Control control) {
71         CTabItem item = new CTabItem(folder, SWT.NONE, index);
72         item.setControl(control);
73         return item;
74     }
75
76     @Override
77     protected void createControls(Composite body, IWorkbenchSite site, ISessionContext context, WidgetSupport support) {
78
79         body.setLayout(LayoutUtils.createNoBorderGridLayout(1));
80
81         folder = new CTabFolder(body, SWT.BOTTOM | SWT.FLAT);
82         folder.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
83
84         Composite shell = new Composite(folder, SWT.NONE);
85         shell.setLayout(LayoutUtils.createNoBorderGridLayout(1));
86
87         commandLine = new Text(shell, SWT.BORDER);
88         commandLine.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
89
90         console = new Browser(shell, SWT.BORDER);
91         console.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
92
93         commandLine.addKeyListener(new KeyAdapter() {
94             @Override
95             public void keyReleased(KeyEvent e) {
96                 if (e.keyCode == SWT.ARROW_UP) {
97                     if (historyPosition > 10 || historyPosition >= (history.size() - 1)) {
98                         return;
99                     }
100                     commandLine.setText(history.get(++historyPosition));
101                 } else if (e.keyCode == SWT.ARROW_DOWN) {
102                     if (historyPosition < 0) {
103                         return;
104                     } else if (historyPosition == 0) {
105                         commandLine.setText(""); //$NON-NLS-1$
106                         historyPosition = -1;
107                     } else {
108                         commandLine.setText(history.get(--historyPosition));
109                     }
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());
115                 }
116             }
117         });
118
119         CTabItem shellItem = createItem(0, folder, shell);
120         shellItem.setText(Messages.SessionDebuggerView_Shell);
121
122 //        SessionDebugger deprecated = new SessionDebugger(folder, SWT.NONE, Simantics.getSession());
123 //        deprecated.initializeUI();
124 //        CTabItem debuggerItem = createItem(1, folder, deprecated);
125 //        debuggerItem.setText("Debugger");
126
127         folder.setSelection(shellItem);
128     }
129
130     private void applyCommand(final String command) {
131         try {
132             Simantics.getSession().syncRequest(new WriteRequest() {
133                 @Override
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);
138                 }
139             });
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));
145         }
146     }
147
148     private void addHistory(String command, Object output) {
149         if (output instanceof String) {
150             terminal.addFirst((String) output);
151         } else if (output instanceof Path) {
152             try {
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$
157                 }
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})));
163             }
164         } else {
165             throw new IllegalArgumentException("Unsupported output argument type " + output); //$NON-NLS-1$
166         }
167         if (terminal.size() > 10)
168             terminal.removeLast();
169
170         history.addFirst(command);
171         if (history.size() > 10)
172             history.removeLast();
173
174         historyPosition = -1;
175     }
176
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());
183             });
184         } else if (data instanceof ListenerReport) {
185             SWTUtils.asyncExec(commandLine, () -> {
186                 try {
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));
194                 }
195             });
196         }
197     }
198
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$
203             data.print(out);
204             out.print("</pre>"); //$NON-NLS-1$
205         }
206         return f.toPath();
207     }
208
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$
215         return b.toString();
216     }
217
218     @Override
219     public void setFocus() {
220         folder.setFocus();
221     }
222
223     @Override
224     protected IPropertyPage getPropertyPage() {
225         return null;
226     }
227
228 }