1 /*******************************************************************************
2 * Copyright (c) 2019 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 * Semantum Oy - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.platform.ui;
14 import java.io.IOException;
15 import java.util.concurrent.ExecutorService;
16 import java.util.concurrent.Executors;
17 import java.util.regex.Matcher;
18 import java.util.regex.Pattern;
20 import org.eclipse.swt.SWT;
21 import org.eclipse.swt.graphics.Color;
22 import org.eclipse.swt.widgets.Display;
23 import org.eclipse.ui.console.ConsolePlugin;
24 import org.eclipse.ui.console.IConsole;
25 import org.eclipse.ui.console.MessageConsole;
26 import org.eclipse.ui.console.MessageConsoleStream;
28 public class SimanticsConsole extends MessageConsole {
30 // We don't want to constantly lose data, but this is still only half MB
31 private static int HIGH_WATERMARK = 2<<18;
33 private static int LOW_WATERMARK = 3 * (HIGH_WATERMARK>>2);
35 private ExecutorService executor = Executors.newSingleThreadExecutor();
42 private Color magenta;
46 private MessageConsoleStream stream;
48 public SimanticsConsole() {
49 super("Simantics Console", "Simantics Console", null, true);
53 protected void init() {
54 Display d = Display.getDefault();
56 black = tuneColor(d, SWT.COLOR_BLACK);
57 red = tuneColor(d, SWT.COLOR_RED);
58 green = tuneColor(d, SWT.COLOR_GREEN);
59 yellow = tuneColor(d, SWT.COLOR_YELLOW);
60 blue = tuneColor(d, SWT.COLOR_BLUE);
61 magenta = tuneColor(d, SWT.COLOR_MAGENTA);
62 cyan = tuneColor(d, SWT.COLOR_CYAN);
63 white = tuneColor(d, SWT.COLOR_WHITE);
66 stream = newMessageStream();
67 stream.setFontStyle(SWT.NORMAL);
68 stream.setColor(white);
70 setWaterMarks(LOW_WATERMARK, HIGH_WATERMARK);
75 protected void dispose() {
80 } catch (IOException e) {
95 private Color tuneColor(Display d, int color) {
96 Color c = d.getSystemColor(color);
97 return new Color(d, (int)(c.getRed() * 0.75), (int)(c.getGreen() * 0.75), (int)(c.getBlue() * 0.75));
100 public static SimanticsConsole show() {
102 if(!Thread.currentThread().equals(Display.getDefault().getThread())) {
103 Display.getDefault().asyncExec(() -> show());
107 for (IConsole c : ConsolePlugin.getDefault().getConsoleManager().getConsoles()) {
108 if (c instanceof SimanticsConsole) {
109 SimanticsConsole sc = (SimanticsConsole) c;
115 SimanticsConsole sc = new SimanticsConsole();
116 ConsolePlugin.getDefault().getConsoleManager().addConsoles(new SimanticsConsole[] {sc});
122 public static SimanticsConsole findConsole() {
124 for (IConsole c : ConsolePlugin.getDefault().getConsoleManager().getConsoles()) {
125 if (c instanceof SimanticsConsole) {
126 return (SimanticsConsole) c;
130 SimanticsConsole sc = new SimanticsConsole();
131 ConsolePlugin.getDefault().getConsoleManager().addConsoles(new SimanticsConsole[] {sc});
136 public void write(String data) {
137 executor.submit(() -> {
138 Pattern p = Pattern.compile("(\\x1b\\[(?<cmd>.*?(m)))");
139 Matcher m = p.matcher(data);
142 if (m.start() > pos) {
143 stream.print(data.substring(pos, m.start()));
147 String cmd = m.group("cmd");
148 if (cmd.endsWith("m")) {
149 // Stream style can be set only in UI thread
150 Display.getDefault().syncExec(() -> {
151 int fontStyle = stream.getFontStyle();
152 Color color = stream.getColor();
155 } catch (IOException e) {
158 stream = newMessageStream();
160 String attributes[] = cmd.substring(0, cmd.length() - 1).split(";");
161 for (String attribute : attributes) {
162 switch (Integer.parseInt(attribute)) {
163 case 0: fontStyle = SWT.NORMAL; break;
164 case 1: fontStyle = SWT.BOLD; break;
165 case 4: break; // underline
166 case 5: break; // blink
167 case 7: break; // reverse video
168 case 8: break; // nondisplayed
169 case 30: color = black; break;
170 case 31: color = red; break;
171 case 32: color = green; break;
172 case 33: color = yellow; break;
173 case 34: color = blue; break;
174 case 35: color = magenta; break;
175 case 36: color = cyan; break;
176 case 37: color = white; break;
177 // background colors not supported
181 stream.setColor(color);
182 stream.setFontStyle(fontStyle);
186 if (pos < data.length()) {
187 stream.print(data.substring(pos));