Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: e9961aab6d659f2e6c24b9bf6745ea97f31aea80 (plain) (blame)
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*******************************************************************************
 * Copyright (c) 2000, 2013 IBM Corporation 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
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.debug.internal.ui.views.console;


import java.util.ArrayList;
import java.util.List;

import org.eclipse.debug.internal.ui.DebugUIPlugin;
import org.eclipse.debug.ui.console.IConsoleLineTracker;
import org.eclipse.debug.ui.console.IConsoleLineTrackerExtension;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IRegion;
import org.eclipse.jface.text.Region;
import org.eclipse.jface.util.IPropertyChangeListener;
import org.eclipse.jface.util.PropertyChangeEvent;
import org.eclipse.ui.console.IConsoleConstants;
import org.eclipse.ui.console.IPatternMatchListener;
import org.eclipse.ui.console.PatternMatchEvent;
import org.eclipse.ui.console.TextConsole;

/**
 * Tracks text appended to the console and notifies listeners in terms of whole
 * lines.
 */
public class ConsoleLineNotifier implements IPatternMatchListener, IPropertyChangeListener {
	/**
	 * Console listeners
	 */
	private List<IConsoleLineTracker> fListeners = new ArrayList<>(2);

	/**
	 * The console this notifier is tracking
	 */
	private ProcessConsole fConsole = null;

	@Override
	public void connect(TextConsole console) {
		if (console instanceof ProcessConsole) {
			fConsole = (ProcessConsole)console;

			IConsoleLineTracker[] lineTrackers = DebugUIPlugin.getDefault().getProcessConsoleManager().getLineTrackers(fConsole.getProcess());
			for (int i = 0; i < lineTrackers.length; i++) {
				lineTrackers[i].init(fConsole);
				addConsoleListener(lineTrackers[i]);
			}

			fConsole.addPropertyChangeListener(this);
		}
	}

	@Override
	public synchronized void disconnect() {
		try {
			IDocument document = fConsole.getDocument();
			if (document != null) {
				int lastLine = document.getNumberOfLines() - 1;
				if (document.getLineDelimiter(lastLine) == null) {
					IRegion lineInformation = document.getLineInformation(lastLine);
					lineAppended(lineInformation);
				}
			}
		} catch (BadLocationException e) {
		}
	}

	/**
	 * Notification the console's streams have been closed
	 */
	public synchronized void consoleClosed() {
		int size = fListeners.size();
		for (int i = 0; i < size; i++) {
			IConsoleLineTracker tracker = fListeners.get(i);
			if (tracker instanceof IConsoleLineTrackerExtension) {
				((IConsoleLineTrackerExtension) tracker).consoleClosed();
			}
			tracker.dispose();
		}

		fConsole = null;
		fListeners = null;
	}

	/**
	 * Adds the given listener to the list of listeners notified when a line of
	 * text is appended to the console.
	 *
	 * @param listener the listener to add
	 */
	public void addConsoleListener(IConsoleLineTracker listener) {
		if (!fListeners.contains(listener)) {
			fListeners.add(listener);
		}
	}

	@Override
	public void matchFound(PatternMatchEvent event) {
		try  {
			IDocument document = fConsole.getDocument();
			int lineOfOffset = document.getLineOfOffset(event.getOffset());
			String delimiter = document.getLineDelimiter(lineOfOffset);
			int strip = delimiter==null ? 0 : delimiter.length();
			Region region = new Region(event.getOffset(), event.getLength()-strip);
			lineAppended(region);
		} catch (BadLocationException e) {}
	}

	public void lineAppended(IRegion region) {
		int size = fListeners.size();
		for (int i=0; i<size; i++) {
			IConsoleLineTracker tracker = fListeners.get(i);
			tracker.lineAppended(region);
		}
	}

	@Override
	public void propertyChange(PropertyChangeEvent event) {
		if(event.getProperty().equals(IConsoleConstants.P_CONSOLE_OUTPUT_COMPLETE)) {
			fConsole.removePropertyChangeListener(this);
			consoleClosed();
		}
	}

	@Override
	public String getPattern() {
		return ".*\\r(\\n?)|.*\\n"; //$NON-NLS-1$
	}

	@Override
	public int getCompilerFlags() {
		return 0;
	}

	@Override
	public String getLineQualifier() {
		return "\\n|\\r"; //$NON-NLS-1$
	}

}

Back to the top