Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: dba5fec11c8cdd2d4af72567d3bcc14be4655a0d (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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*******************************************************************************
 * Copyright (c) 2000, 2018 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
 *     vogella GmbH - Bug 287303 - [patch] Add Word Wrap action to Console View
 *******************************************************************************/
package org.eclipse.ui.internal.console;

import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.DocumentEvent;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IDocumentListener;
import org.eclipse.jface.text.IRegion;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.events.VerifyEvent;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.console.ConsolePlugin;
import org.eclipse.ui.console.IConsoleDocumentPartitioner;
import org.eclipse.ui.console.IScrollLockStateProvider;
import org.eclipse.ui.console.TextConsole;
import org.eclipse.ui.console.TextConsoleViewer;

/**
 * Viewer used to display an IOConsole
 *
 * @since 3.1
 */
public class IOConsoleViewer extends TextConsoleViewer {
	/**
	 * will always scroll with output if value is true.
	 */
	private boolean fAutoScroll = true;

	private boolean fWordWrap = false;

	private IDocumentListener fDocumentListener;

	public IOConsoleViewer(Composite parent, TextConsole console) {
		super(parent, console);
	}

	/**
	 * Constructs a new viewer in the given parent for the specified console.
	 *
	 * @param parent the containing composite
	 * @param console the IO console
	 * @param scrollLockStateProvider the scroll lock state provider
	 * @since 3.6
	 */
	public IOConsoleViewer(Composite parent, TextConsole console, IScrollLockStateProvider scrollLockStateProvider) {
		super(parent, console, scrollLockStateProvider);
	}

	public boolean isAutoScroll() {
		return fAutoScroll;
	}

	public void setAutoScroll(boolean scroll) {
		fAutoScroll = scroll;
	}

	public boolean isWordWrap() {
		return fWordWrap;
	}

	public void setWordWrap(boolean wordwrap) {
		fWordWrap = wordwrap;
		getTextWidget().setWordWrap(wordwrap);
	}

	@Override
	protected void handleVerifyEvent(VerifyEvent e) {
		IDocument doc = getDocument();
		String[] legalLineDelimiters = doc.getLegalLineDelimiters();
		String eventString = e.text;
		try {
			IConsoleDocumentPartitioner partitioner = (IConsoleDocumentPartitioner) doc.getDocumentPartitioner();
			if (!partitioner.isReadOnly(e.start)) {
				boolean isCarriageReturn = false;
				for (int i = 0; i < legalLineDelimiters.length; i++) {
					if (e.text.equals(legalLineDelimiters[i])) {
						isCarriageReturn = true;
						break;
					}
				}

				if (!isCarriageReturn) {
					super.handleVerifyEvent(e);
					return;
				}
			}

			int length = doc.getLength();
			if (e.start == length) {
				super.handleVerifyEvent(e);
			} else {
				try {
					doc.replace(length, 0, eventString);
					updateWidgetCaretLocation(length);
				} catch (BadLocationException e1) {
				}
				e.doit = false;
			}
		} finally {
			StyledText text = (StyledText) e.widget;
			text.setCaretOffset(text.getCharCount());
		}
	}

	/*
	 * Update the Text widget location to new location
	 */
	private void updateWidgetCaretLocation(int documentCaret) {
		int widgetCaret = modelOffset2WidgetOffset(documentCaret);
		if (widgetCaret == -1) {
			// try to move it to the closest spot
			IRegion region = getModelCoverage();
			if (region != null) {
				if (documentCaret <= region.getOffset()) {
					widgetCaret = 0;
				} else if (documentCaret >= region.getOffset() + region.getLength()) {
					widgetCaret = getVisibleRegion().getLength();
				}
			}
		}
		if (widgetCaret != -1) {
			// there is a valid widget caret
			getTextWidget().setCaretOffset(widgetCaret);
			getTextWidget().showSelection();
		}
	}

	/**
	 * makes the associated text widget uneditable.
	 */
	public void setReadOnly() {
		ConsolePlugin.getStandardDisplay().asyncExec(() -> {
			StyledText text = getTextWidget();
			if (text != null && !text.isDisposed()) {
				text.setEditable(false);
			}
		});
	}

	/**
	 * @return <code>false</code> if text is editable
	 */
	public boolean isReadOnly() {
		return !getTextWidget().getEditable();
	}

	@Override
	public void setDocument(IDocument document) {
		IDocument oldDocument= getDocument();

		super.setDocument(document);

		if (oldDocument != null) {
			oldDocument.removeDocumentListener(getDocumentListener());
		}
		if (document != null) {
			document.addDocumentListener(getDocumentListener());
		}
	}

	private IDocumentListener getDocumentListener() {
		if (fDocumentListener == null) {
			fDocumentListener= new IDocumentListener() {
				@Override
				public void documentAboutToBeChanged(DocumentEvent event) {
				}

				@Override
				public void documentChanged(DocumentEvent event) {
					if (fAutoScroll) {
						revealEndOfDocument();
					}
				}
			};
		}
		return fDocumentListener;
	}
}

Back to the top