Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 02170dd2a82807bfc52cacbb2dfddb3cf17890aa (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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
/*******************************************************************************
 * 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
 *******************************************************************************/

package org.eclipse.ui.console;

import java.util.HashMap;

import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.jobs.ISchedulingRule;
import org.eclipse.core.runtime.preferences.InstanceScope;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.resource.JFaceResources;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.BadPositionCategoryException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IRegion;
import org.eclipse.jface.text.Position;
import org.eclipse.jface.text.Region;
import org.eclipse.jface.util.IPropertyChangeListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.ui.internal.console.ConsoleDocument;
import org.eclipse.ui.internal.console.ConsoleHyperlinkPosition;
import org.eclipse.ui.internal.console.ConsolePatternMatcher;
import org.eclipse.ui.part.IPageBookViewPage;
import org.eclipse.ui.preferences.ScopedPreferenceStore;

/**
 * An abstract text console that supports regular expression matching and
 * hyperlinks.
 * <p>
 * Pattern match listeners can be registered with a console programmatically
 * or via the <code>org.eclipse.ui.console.consolePatternMatchListeners</code>
 * extension point.
 * </p>
 * <p>
 * Clients may subclass this class. Subclasses must provide a document partitioner.
 * </p>
 * @since 3.1
 */
public abstract class TextConsole extends AbstractConsole {

	/**
	 * The current width of the console. Used for fixed width consoles.
	 * A value of <=0 means does not have a fixed width.
	 */
	private int fConsoleWidth;
	/**
	 * The current tab width
	 */
	private int fTabWidth;
	/**
	 * The font used by this console
	 */
	private Font fFont;

	/**
	 * The background color used by this console or <code>null</code> if default
	 */
	private Color fBackground;

	/**
	 * The Console's regular expression pattern matcher
	 */
	private ConsolePatternMatcher fPatternMatcher;

	/**
	 * The Console's document
	 */
	private ConsoleDocument fDocument;

	/**
	 * indication that the console's partitioner is not expecting more input
	 */
	private boolean fPartitionerFinished = false;

	/**
	 * Indication that the console's pattern matcher has finished.
	 * (all matches have been found and all listeners notified)
	 */
	private boolean fMatcherFinished = false;

	/**
	 * indication that the console output complete property has been fired
	 */
	private boolean fCompleteFired = false;

	private boolean fConsoleAutoScrollLock = true;


	/**
	 * Map of client defined attributes
	 */
	private HashMap<String, Object> fAttributes = new HashMap<>();

	private IConsoleManager fConsoleManager = ConsolePlugin.getDefault().getConsoleManager();
	private ScopedPreferenceStore store;
	private IPropertyChangeListener propListener;

	@Override
	protected void dispose() {
		super.dispose();
		fFont = null;
		synchronized(fAttributes) {
			fAttributes.clear();
		}
		if (store != null) {
			store.removePropertyChangeListener(propListener);
		}
	}
	/**
	 * Constructs a console with the given name, image descriptor, and lifecycle
	 *
	 * @param name name to display for this console
	 * @param consoleType console type identifier or <code>null</code>
	 * @param imageDescriptor image to display for this console or <code>null</code>
	 * @param autoLifecycle whether lifecycle methods should be called automatically
	 *  when this console is added/removed from the console manager
	 */
	public TextConsole(String name, String consoleType, ImageDescriptor imageDescriptor, boolean autoLifecycle) {
		super(name, consoleType, imageDescriptor, autoLifecycle);
		fDocument = new ConsoleDocument();
		fDocument.addPositionCategory(ConsoleHyperlinkPosition.HYPER_LINK_CATEGORY);
		fPatternMatcher = new ConsolePatternMatcher(this);
		fDocument.addDocumentListener(fPatternMatcher);
		fTabWidth = IConsoleConstants.DEFAULT_TAB_SIZE;
	}

	@Override
	public IPageBookViewPage createPage(IConsoleView view) {
		return new TextConsolePage(this, view);
	}

	/**
	 * Returns this console's document.
	 * <p>
	 * Note that a console may or may not support direct manipulation of its document.
	 * For example, an I/O console document and its partitions are produced from the
	 * streams connected to it, and clients are not intended to modify the document's
	 * contents.
	 * </p>
	 *
	 * @return this console's document
	 */
	public IDocument getDocument() {
		return fDocument;
	}

	/**
	 * Returns the current width of this console. A value of zero of less
	 * indicates this console has no fixed width.
	 *
	 * @return the current width of this console
	 */
	public int getConsoleWidth() {
		return fConsoleWidth;
	}

	/**
	 * Returns the user preference for enabling auto scroll lock feature.
	 *
	 * @return auto scroll lock
	 * @since 3.8
	 */
	public boolean isConsoleAutoScrollLock() {
		return fConsoleAutoScrollLock;
	}

	/**
	 * Sets the auto scroll lock preference.
	 *
	 * @param autoScrollLockPref enable auto scroll lock preference.
	 * @since 3.8
	 */
	public void setConsoleAutoScrollLock(boolean autoScrollLockPref) {
		if (fConsoleAutoScrollLock != autoScrollLockPref) {
			boolean old = fConsoleAutoScrollLock;
			fConsoleAutoScrollLock = autoScrollLockPref;

			firePropertyChange(this, IConsoleConstants.P_CONSOLE_AUTO_SCROLL_LOCK, Boolean.valueOf(old), Boolean.valueOf(fConsoleAutoScrollLock));
		}
	}

	@Override
	protected void init() {
		super.init();
		String qualifier = ConsolePlugin.getUniqueIdentifier();
		String key = IConsoleConstants.P_CONSOLE_AUTO_SCROLL_LOCK;
		setConsoleAutoScrollLock(Platform.getPreferencesService().getBoolean(qualifier, key, true, null));
		store = new ScopedPreferenceStore(InstanceScope.INSTANCE, qualifier);

		propListener = event -> {
			String property = event.getProperty();
			if (key.equals(property)) {
				setConsoleAutoScrollLock(store.getBoolean(key));
			}
		};
		store.addPropertyChangeListener(propListener);
	}

	/**
	 * Sets the width of this console in characters. Any value greater than zero
	 * will cause this console to have a fixed width.
	 *
	 * @param width the width to make this console. Values of 0 or less imply
	 *            the console does not have any fixed width.
	 */
	public void setConsoleWidth(int width) {
		if (fConsoleWidth != width) {
			int old = fConsoleWidth;
			fConsoleWidth = width;

			firePropertyChange(this, IConsoleConstants.P_CONSOLE_WIDTH, Integer.valueOf(old), Integer.valueOf(fConsoleWidth));
		}
	}

	/**
	 * Sets the tab width used in this console.
	 *
	 * @param newTabWidth the tab width
	 */
	public void setTabWidth(final int newTabWidth) {
		if (fTabWidth != newTabWidth) {
			final int oldTabWidth = fTabWidth;
			fTabWidth = newTabWidth;
			ConsolePlugin.getStandardDisplay().asyncExec(() -> firePropertyChange(TextConsole.this, IConsoleConstants.P_TAB_SIZE, Integer.valueOf(oldTabWidth), Integer.valueOf(fTabWidth)));
		}
	}

	/**
	 * Returns the tab width used in this console.
	 *
	 * @return tab width used in this console
	 */
	public int getTabWidth() {
		return fTabWidth;
	}

	/**
	 * Returns the font used by this console. Must be called in the UI thread.
	 *
	 * @return font used by this console
	 */
	public Font getFont() {
		if (fFont == null) {
			fFont = getDefaultFont();
		}
		return fFont;
	}

	/**
	 * Returns the default text font.
	 *
	 * @return the default text font
	 */
	private Font getDefaultFont() {
		return JFaceResources.getFont(JFaceResources.TEXT_FONT);
	}

	/**
	 * Sets the font used by this console. Specify <code>null</code> to use
	 * the default text font.
	 *
	 * @param newFont font, or <code>null</code> to indicate the default font
	 */
	public void setFont(Font newFont) {
		// ensure font is initialized
		getFont();
		// translate null to default font
		Font font = newFont;
		if (font == null) {
			font = getDefaultFont();
		}
		// fire property change if required
		if (!fFont.equals(font)) {
			Font old = fFont;
			fFont = font;
			firePropertyChange(this, IConsoleConstants.P_FONT, old, fFont);
		}
	}

	/**
	 * Sets the background color used by this console. Specify <code>null</code> to use
	 * the default background color.
	 *
	 * @param background background color or <code>null</code> for default
	 * @since 3.3
	 * @deprecated use setBackground(Color) instead
	 */
	@Deprecated
	public void setBackgrond(Color background) {
		setBackground(background);
	}

	/**
	 * Sets the background color used by this console. Specify <code>null</code> to use
	 * the default background color.
	 *
	 * @param background background color or <code>null</code> for default
	 * @since 3.3
	 */
	public void setBackground(Color background) {
		if (fBackground == null) {
			if (background == null) {
				return;
			}
		} else if (fBackground.equals(background)){
			return;
		}
		Color old = fBackground;
		fBackground = background;
		firePropertyChange(this, IConsoleConstants.P_BACKGROUND_COLOR, old, fBackground);
	}

	/**
	 * Returns the background color to use for this console or <code>null</code> for the
	 * default background color.
	 *
	 * @return background color or <code>null</code> for default
	 * @since 3.3
	 */
	public Color getBackground() {
		return fBackground;
	}

	/**
	 * Clears the console.
	 * <p>
	 * Since a console may or may not support direct manipulation
	 * of its document's contents, this method should be called to clear a text console's
	 * document. The default implementation sets this console's document content
	 * to the empty string directly. Subclasses should override as required.
	 * </p>
	 */
	public void clearConsole() {
		IDocument document = getDocument();
		if (document != null) {
			document.set(""); //$NON-NLS-1$
		}
	}

	/**
	 * Returns the console's document partitioner.
	 * @return The console's document partitioner
	 */
	protected abstract IConsoleDocumentPartitioner getPartitioner();

	/**
	 * Returns all hyperlinks in this console.
	 *
	 * @return all hyperlinks in this console
	 */
	public IHyperlink[] getHyperlinks() {
		try {
			Position[] positions = getDocument().getPositions(ConsoleHyperlinkPosition.HYPER_LINK_CATEGORY);
			IHyperlink[] hyperlinks = new IHyperlink[positions.length];
			for (int i = 0; i < positions.length; i++) {
				ConsoleHyperlinkPosition position = (ConsoleHyperlinkPosition) positions[i];
				hyperlinks[i] = position.getHyperLink();
			}
			return hyperlinks;
		} catch (BadPositionCategoryException e) {
			return new IHyperlink[0];
		}
	}

	/**
	 * Returns the hyperlink at the given offset of <code>null</code> if none.
	 *
	 * @param offset offset for which a hyperlink is requested
	 * @return the hyperlink at the given offset of <code>null</code> if none
	 */
	public IHyperlink getHyperlink(int offset) {
		try {
			IDocument document = getDocument();
			if (document != null) {
				Position[] positions = document.getPositions(ConsoleHyperlinkPosition.HYPER_LINK_CATEGORY);
				Position position = findPosition(offset, positions);
				if (position instanceof ConsoleHyperlinkPosition) {
					return ((ConsoleHyperlinkPosition) position).getHyperLink();
				}
			}
		} catch (BadPositionCategoryException e) {
		}
		return null;
	}

	/**
	 * Binary search for the position at a given offset.
	 *
	 * @param offset the offset whose position should be found
	 * @return the position containing the offset, or <code>null</code>
	 */
	private Position findPosition(int offset, Position[] positions) {

		if (positions.length == 0) {
			return null;
		}

		int left= 0;
		int right= positions.length -1;
		int mid= 0;
		Position position= null;

		while (left < right) {

			mid= (left + right) / 2;

			position= positions[mid];
			if (offset < position.getOffset()) {
				if (left == mid) {
					right= left;
				} else {
					right= mid -1;
				}
			} else if (offset > (position.getOffset() + position.getLength() - 1)) {
				if (right == mid) {
					left= right;
				} else {
					left= mid  +1;
				}
			} else {
				left= right= mid;
			}
		}

		position= positions[left];
		if (offset >= position.getOffset() && (offset < (position.getOffset() + position.getLength()))) {
			return position;
		}
		return null;
	}

	/**
	 * Adds the given pattern match listener to this console. The listener will
	 * be connected and receive match notifications. Has no effect if an identical
	 * listener has already been added.
	 *
	 * @param listener the listener to add
	 */
	public void addPatternMatchListener(IPatternMatchListener listener) {
		fPatternMatcher.addPatternMatchListener(listener);
	}

	/**
	 * Removes the given pattern match listener from this console. The listener will be
	 * disconnected and will no longer receive match notifications. Has no effect
	 * if the listener was not previously added.
	 *
	 * @param listener the pattern match listener to remove
	 */
	public void removePatternMatchListener(IPatternMatchListener listener) {
		fPatternMatcher.removePatternMatchListener(listener);
	}


	/**
	 * Job scheduling rule that prevent the job from running if the console's PatternMatcher
	 * is active.
	 */
	private class MatcherSchedulingRule implements ISchedulingRule {
		@Override
		public boolean contains(ISchedulingRule rule) {
			return rule == this;
		}

		@Override
		public boolean isConflicting(ISchedulingRule rule) {
			if (contains(rule)) {
				return true;
			}
			if (rule != this && rule instanceof MatcherSchedulingRule) {
				return (((MatcherSchedulingRule)rule).getConsole() == TextConsole.this);
			}
			return false;
		}

		public TextConsole getConsole() {
			return TextConsole.this;
		}
	}

	/**
	 * Returns a scheduling rule which can be used to prevent jobs from running
	 * while this console's pattern matcher is active.
	 * <p>
	 * Although this scheduling rule prevents jobs from running at the same time as
	 * pattern matching jobs for this console, it does not enforce any ordering of jobs.
	 * Since 3.2, pattern matching jobs belong to the job family identified by the console
	 * object that matching is occurring on. To ensure a job runs after all scheduled pattern
	 * matching is complete, clients must join on this console's job family.
	 * </p>
	 * @return a scheduling rule which can be used to prevent jobs from running
	 * while this console's pattern matcher is active
	 */
	public ISchedulingRule getSchedulingRule() {
		return new MatcherSchedulingRule();
	}

	/**
	 * This console's partitioner should call this method when it is not expecting any new data
	 * to be appended to the document.
	 */
	public void partitionerFinished() {
		fPatternMatcher.forceFinalMatching();
		fPartitionerFinished  = true;
		checkFinished();
	}

	/**
	 * Called by this console's pattern matcher when matching is complete.
	 * <p>
	 * Clients should not call this method.
	 * <p>
	 */
	public void matcherFinished() {
		fMatcherFinished = true;
		fDocument.removeDocumentListener(fPatternMatcher);
		checkFinished();
	}

	/**
	 * Fires the console output complete property change event.
	 */
	private synchronized void checkFinished() {
		if (!fCompleteFired && fPartitionerFinished && fMatcherFinished ) {
			fCompleteFired = true;
			firePropertyChange(this, IConsoleConstants.P_CONSOLE_OUTPUT_COMPLETE, null, null);
		}
	}

	/**
	 * Adds a hyperlink to this console.
	 *
	 * @param hyperlink the hyperlink to add
	 * @param offset the offset in the console document at which the hyperlink should be added
	 * @param length the length of the text which should be hyperlinked
	 * @throws BadLocationException if the specified location is not valid.
	 */
	public void addHyperlink(IHyperlink hyperlink, int offset, int length) throws BadLocationException {
		IDocument document = getDocument();
		ConsoleHyperlinkPosition hyperlinkPosition = new ConsoleHyperlinkPosition(hyperlink, offset, length);
		try {
			document.addPosition(ConsoleHyperlinkPosition.HYPER_LINK_CATEGORY, hyperlinkPosition);
			fConsoleManager.refresh(this);
		} catch (BadPositionCategoryException e) {
			ConsolePlugin.log(e);
		}
	}

	/**
	 * Returns the region associated with the given hyperlink.
	 *
	 * @param link hyperlink
	 * @return the region associated with the hyperlink or null if the hyperlink is not found.
	 */
	public IRegion getRegion(IHyperlink link) {
		try {
			IDocument doc = getDocument();
			if (doc != null) {
				Position[] positions = doc.getPositions(ConsoleHyperlinkPosition.HYPER_LINK_CATEGORY);
				for (int i = 0; i < positions.length; i++) {
					ConsoleHyperlinkPosition position = (ConsoleHyperlinkPosition)positions[i];
					if (position.getHyperLink().equals(link)) {
						return new Region(position.getOffset(), position.getLength());
					}
				}
			}
		} catch (BadPositionCategoryException e) {
		}
		return null;
	}

	/**
	 * Returns the attribute associated with the specified key.
	 *
	 * @param key attribute key
	 * @return the attribute associated with the specified key
	 */
	public Object getAttribute(String key) {
		synchronized (fAttributes) {
			return fAttributes.get(key);
		}
	}

	/**
	 * Sets an attribute value. Intended for client data.
	 *
	 * @param key attribute key
	 * @param value attribute value
	 */
	public void setAttribute(String key, Object value) {
		synchronized(fAttributes) {
			fAttributes.put(key, value);
		}
	}
}

Back to the top