diff options
author | 2020-06-25 19:55:10 +0200 | |
---|---|---|
committer | 2020-06-25 19:55:10 +0200 | |
commit | c3a6d4b9cf3657070d180aa65ddbf0459e880329 (patch) | |
tree | 780c4fc61578dcb309af53fb0c164c7627e51676 /Application/hu.bme.mit.inf.dslreasoner.application.ui/src/hu/bme/mit/inf/dslreasoner/application/ui/execute/RuntimeConsoleBasedScriptConsole.xtend | |
parent | New configuration language parser WIP (diff) | |
parent | Scope unsat benchmarks (diff) | |
download | VIATRA-Generator-c3a6d4b9cf3657070d180aa65ddbf0459e880329.tar.gz VIATRA-Generator-c3a6d4b9cf3657070d180aa65ddbf0459e880329.tar.zst VIATRA-Generator-c3a6d4b9cf3657070d180aa65ddbf0459e880329.zip |
Merge branch 'kris'
Diffstat (limited to 'Application/hu.bme.mit.inf.dslreasoner.application.ui/src/hu/bme/mit/inf/dslreasoner/application/ui/execute/RuntimeConsoleBasedScriptConsole.xtend')
-rw-r--r-- | Application/hu.bme.mit.inf.dslreasoner.application.ui/src/hu/bme/mit/inf/dslreasoner/application/ui/execute/RuntimeConsoleBasedScriptConsole.xtend | 172 |
1 files changed, 172 insertions, 0 deletions
diff --git a/Application/hu.bme.mit.inf.dslreasoner.application.ui/src/hu/bme/mit/inf/dslreasoner/application/ui/execute/RuntimeConsoleBasedScriptConsole.xtend b/Application/hu.bme.mit.inf.dslreasoner.application.ui/src/hu/bme/mit/inf/dslreasoner/application/ui/execute/RuntimeConsoleBasedScriptConsole.xtend new file mode 100644 index 00000000..d344ffba --- /dev/null +++ b/Application/hu.bme.mit.inf.dslreasoner.application.ui/src/hu/bme/mit/inf/dslreasoner/application/ui/execute/RuntimeConsoleBasedScriptConsole.xtend | |||
@@ -0,0 +1,172 @@ | |||
1 | package hu.bme.mit.inf.dslreasoner.application.ui.execute | ||
2 | |||
3 | import hu.bme.mit.inf.dslreasoner.application.execution.ScriptConsole | ||
4 | import hu.bme.mit.inf.dslreasoner.application.execution.ScriptConsoleDecorator | ||
5 | import java.util.concurrent.CompletableFuture | ||
6 | import org.eclipse.emf.common.util.URI | ||
7 | import org.eclipse.jface.text.BadLocationException | ||
8 | import org.eclipse.jface.text.DocumentEvent | ||
9 | import org.eclipse.jface.text.IDocumentListener | ||
10 | import org.eclipse.swt.graphics.Color | ||
11 | import org.eclipse.ui.console.ConsolePlugin | ||
12 | import org.eclipse.ui.console.MessageConsole | ||
13 | import org.eclipse.ui.console.MessageConsoleStream | ||
14 | |||
15 | class RuntimeConsoleBasedScriptConsole extends ScriptConsole { | ||
16 | /** | ||
17 | * Console is identified with the name of this class. | ||
18 | */ | ||
19 | static val consoleID = ScriptConsole.name | ||
20 | |||
21 | val MessageConsole runtimeConsole | ||
22 | |||
23 | new(boolean cleanFiles, URI messageConsoleURI, URI errorConsoleURI, URI statisticsConsoleURI) { | ||
24 | super(!hasConsolePlugin, cleanFiles, messageConsoleURI, errorConsoleURI, statisticsConsoleURI) | ||
25 | runtimeConsole = prepareRuntimeConsole | ||
26 | writeErrorMessagesDuringInitialisation() | ||
27 | } | ||
28 | |||
29 | override writeMessage(CharSequence message, String separator, ScriptConsoleDecorator[] decorators) { | ||
30 | super.writeMessage(message, separator, decorators) | ||
31 | if (runtimeConsole !== null) { | ||
32 | writeToRuntimeConsole(message, separator, decorators) | ||
33 | } | ||
34 | } | ||
35 | |||
36 | override writeMessage(String message) { | ||
37 | super.writeMessage(message) | ||
38 | if (runtimeConsole !== null) { | ||
39 | writeToRuntimeConsole(message) | ||
40 | } | ||
41 | } | ||
42 | |||
43 | override writeError(CharSequence message, String separator, ScriptConsoleDecorator[] decorators) { | ||
44 | super.writeError(message, separator, decorators) | ||
45 | if (runtimeConsole !== null) { | ||
46 | writeToRuntimeConsole(message, separator, decorators) | ||
47 | } | ||
48 | } | ||
49 | |||
50 | override writeError(String message) { | ||
51 | super.writeError(message) | ||
52 | if (runtimeConsole !== null) { | ||
53 | writeToRuntimeConsole(message) | ||
54 | } | ||
55 | } | ||
56 | |||
57 | private def MessageConsole prepareRuntimeConsole() { | ||
58 | val plugin = ConsolePlugin.getDefault(); | ||
59 | if (plugin === null) { | ||
60 | return null | ||
61 | } else { | ||
62 | val conMan = plugin.getConsoleManager(); | ||
63 | val existingConsoles = conMan.getConsoles(); | ||
64 | val existingConsolesWithID = existingConsoles.filter[it.name.equals(consoleID)] | ||
65 | if (existingConsolesWithID.empty) { | ||
66 | val MessageConsole res = new MessageConsole(consoleID, null) | ||
67 | conMan.addConsoles(#[res]); | ||
68 | |||
69 | return res | ||
70 | } else { | ||
71 | return existingConsolesWithID.head as MessageConsole | ||
72 | } | ||
73 | } | ||
74 | } | ||
75 | |||
76 | private def writeToRuntimeConsole(CharSequence message) { | ||
77 | // 1. reveal the console view | ||
78 | ConsolePlugin.getDefault().getConsoleManager().showConsoleView(this.runtimeConsole); | ||
79 | val stream = this.runtimeConsole.newMessageStream | ||
80 | stream.println(message.toString) | ||
81 | stream.close | ||
82 | } | ||
83 | |||
84 | private def writeToRuntimeConsole(CharSequence message, String separator, ScriptConsoleDecorator[] decorators) { | ||
85 | val messageString = message.toString | ||
86 | // 0. split the message | ||
87 | val separatedMessage = if (messageString.startsWith(separator)) { | ||
88 | #[""] + messageString.split(separator, -1) | ||
89 | } else { | ||
90 | messageString.split(separator, -1).toList | ||
91 | } | ||
92 | if (separatedMessage.size - 1 !== decorators.size) { | ||
93 | throw new IllegalArgumentException | ||
94 | } | ||
95 | |||
96 | // 1. reveal the console view | ||
97 | ConsolePlugin.getDefault().getConsoleManager().showConsoleView(this.runtimeConsole); | ||
98 | val stream = this.runtimeConsole.newMessageStream | ||
99 | |||
100 | // 2. print the segments of the view | ||
101 | for (i : 0 ..< decorators.size) { | ||
102 | stream.print(separatedMessage.get(i)) | ||
103 | writeDecoratedTextToRuntimeConsole(decorators.get(i), stream) | ||
104 | |||
105 | } | ||
106 | // 2.1 write the last segment of | ||
107 | stream.println(separatedMessage.last) | ||
108 | |||
109 | // stream.println(message.toString) | ||
110 | stream.close | ||
111 | } | ||
112 | |||
113 | private def writeDecoratedTextToRuntimeConsole(ScriptConsoleDecorator message, MessageConsoleStream stream) { | ||
114 | val originalBackgroundColor = this.runtimeConsole.background | ||
115 | var Color newColor = null; | ||
116 | |||
117 | val text = '''[«message.text»]''' | ||
118 | if (message.red >= 0 && message.green >= 0 && message.blue >= 0) { | ||
119 | newColor = new Color(originalBackgroundColor.device, message.red, message.green, message.blue) | ||
120 | this.runtimeConsole.setBackground(newColor) | ||
121 | } | ||
122 | stream.flush | ||
123 | val CompletableFuture<Boolean> finished = new CompletableFuture<Boolean> | ||
124 | val listener = new IDocumentListener() { | ||
125 | override documentAboutToBeChanged(DocumentEvent event) {} | ||
126 | |||
127 | override documentChanged(DocumentEvent event) { | ||
128 | // println('''ftext="«event.fText»", message="«message.text»" endswith=«event.fText.endsWith(message.text)»''') | ||
129 | if (event.fText.endsWith(text)) { | ||
130 | val from = event.fDocument.length - text.length + 1 | ||
131 | val length = message.text.length | ||
132 | // println('''from: «from» length «length»''') | ||
133 | try { | ||
134 | runtimeConsole.addHyperlink( | ||
135 | new ScriptConsoleFileHiperlink(message.hyperlink), | ||
136 | from, | ||
137 | length | ||
138 | ) | ||
139 | // println("link added") | ||
140 | } catch (BadLocationException e) { | ||
141 | } finally { | ||
142 | runtimeConsole.document.removeDocumentListener(this) | ||
143 | finished.complete(true) | ||
144 | } | ||
145 | } | ||
146 | |||
147 | } | ||
148 | } | ||
149 | runtimeConsole.document.addDocumentListener(listener) | ||
150 | stream.print(text) | ||
151 | stream.flush | ||
152 | finished.get | ||
153 | // stream.console.new | ||
154 | if (message.red >= 0 && message.green >= 0 && message.blue >= 0) { | ||
155 | newColor.dispose | ||
156 | this.runtimeConsole.setBackground(originalBackgroundColor) | ||
157 | } | ||
158 | } | ||
159 | |||
160 | private static def hasConsolePlugin() { | ||
161 | ConsolePlugin.getDefault() !== null | ||
162 | } | ||
163 | |||
164 | public static val FACTORY = new ScriptConsole.Factory { | ||
165 | |||
166 | override createScriptConsole(boolean cleanFiles, URI messageConsoleURI, URI errorConsoleURI, | ||
167 | URI statisticsConsoleURI) { | ||
168 | new RuntimeConsoleBasedScriptConsole(cleanFiles, messageConsoleURI, errorConsoleURI, statisticsConsoleURI) | ||
169 | } | ||
170 | |||
171 | } | ||
172 | } | ||