summaryrefslogtreecommitdiffstats
path: root/rba.tool.core/src/rba/tool/core/console/ConsoleManager.java
blob: 823d3dda99bd22c54deac9b756dfcfb75ebef078 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package rba.tool.core.console;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.ui.console.ConsolePlugin;
import org.eclipse.ui.console.MessageConsole;
import org.eclipse.ui.console.MessageConsoleStream;

public class ConsoleManager implements IConsoleManager {

    public static ConsoleManager INSTANCE = new ConsoleManager();

    org.eclipse.ui.console.IConsoleManager consoleManager;

    MessageConsole console;

    MessageConsoleStream consoleStream;

    MessageConsoleStream consoleWarnStream;

    String title = "RBA Tool Console";

    Map<String, List<IConsoleHook>> hook_notifierIdMap = new HashMap<String, List<IConsoleHook>>();

    private ConsoleManager() {
        consoleManager = ConsolePlugin.getDefault().getConsoleManager();
        console = new MessageConsole(title, null);
        consoleManager.addConsoles(new MessageConsole[] { console });

        consoleStream = console.newMessageStream();

        consoleWarnStream = console.newMessageStream();
        consoleWarnStream.setColor(new Color(null, 255, 0, 0)); // red
        consoleWarnStream.setFontStyle(SWT.BOLD);
    }

    public void clearConsole() {
        console.clearConsole();
    }

    @Override
    public void output(String message, String notifierId) {
        preNotify(message, notifierId);
        // original output message
        consoleManager.showConsoleView(console);
        consoleStream.println(message);
        postNotify(message, notifierId);
    }

    @Override
    public void warning(String message, String notifierId) {
        preNotify(message, notifierId);
        // original warning message
        consoleManager.showConsoleView(console);
        consoleWarnStream.println(message);
        postNotify(message, notifierId);
    }

    @Override
    public void addHook(IConsoleHook hook, String targetNotifierId) {
        List<IConsoleHook> hookList = hook_notifierIdMap.get(targetNotifierId);
        if (hookList == null) {
            hookList = new ArrayList<IConsoleHook>();
            hook_notifierIdMap.put(targetNotifierId, hookList);
        }
        hookList.add(hook);
    }

    @Override
    public void removeHook(IConsoleHook hook, String targetNotifierId) {
        List<IConsoleHook> hookList = hook_notifierIdMap.get(targetNotifierId);
        if (hookList == null) {
            return;
        }
        hookList.remove(hook);
    }

    public void preNotify(String message, String notifierId) {
        List<IConsoleHook> hookList = hook_notifierIdMap.get(notifierId);
        if (hookList != null && !hookList.isEmpty()) {
            hookList.forEach(hook -> hook.preNotify(message, notifierId));
        }
    }

    public void postNotify(String message, String notifierId) {
        List<IConsoleHook> hookList = hook_notifierIdMap.get(notifierId);
        if (hookList != null && !hookList.isEmpty()) {
            hookList.forEach(hook -> hook.postNotify(message, notifierId));
        }
    }
}