forked from eclipse-platform/eclipse.platform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AnsiConsolePageParticipant.java
89 lines (76 loc) · 2.78 KB
/
AnsiConsolePageParticipant.java
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
/*******************************************************************************
* Copyright (c) 2012, 2024 Mihai Nita and others
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*******************************************************************************/
package org.eclipse.ui.internal.console.ansi.participants;
import java.util.EventListener;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.ITextViewer;
import org.eclipse.swt.custom.ST;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.ui.console.IConsole;
import org.eclipse.ui.console.IConsolePageParticipant;
import org.eclipse.ui.part.IPageBookViewPage;
public class AnsiConsolePageParticipant implements IConsolePageParticipant {
// Remember all the viewers we added, so that we can remove them.
private static final Map<StyledText, IConsolePageParticipant> viewers = new HashMap<>();
@Override
public <T> T getAdapter(Class<T> adapter) {
return null;
}
@Override
public void activated() {
// Nothing to do, but we are forced to implement it for IConsolePageParticipant
}
@Override
public void deactivated() {
// Nothing to do, but we are forced to implement it for IConsolePageParticipant
}
@Override
public void dispose() {
removeViewerWithPageParticipant(this);
}
@Override
public void init(IPageBookViewPage page, IConsole console) {
if (page.getControl() instanceof StyledText) {
StyledText viewer = (StyledText) page.getControl();
IDocument document = getDocument(viewer);
if (document == null) {
return;
}
AnsiConsoleStyleListener myListener = new AnsiConsoleStyleListener(document);
viewer.addLineStyleListener(myListener);
addViewer(viewer, this);
}
}
// Find the document associated with the viewer
static IDocument getDocument(StyledText viewer) {
return viewer.getTypedListeners(ST.LineGetStyle, EventListener.class).filter(ITextViewer.class::isInstance)
.map(v -> ((ITextViewer) v).getDocument()).findFirst().orElse(null);
}
private void addViewer(StyledText viewer, IConsolePageParticipant participant) {
viewers.put(viewer, participant);
}
private void removeViewerWithPageParticipant(IConsolePageParticipant participant) {
Set<StyledText> toRemove = new HashSet<>();
for (Entry<StyledText, IConsolePageParticipant> entry : viewers.entrySet()) {
if (entry.getValue() == participant) {
toRemove.add(entry.getKey());
}
}
for (StyledText viewer : toRemove) {
viewers.remove(viewer);
}
}
}