Bug 495658 - Auto scroll lock should be disableable

Change-Id: Ie5b39dad6843a62c95914fa685ef3f25f97a1ec7
diff --git a/org.eclipse.debug.ui/ui/org/eclipse/debug/internal/ui/DebugUIPreferenceInitializer.java b/org.eclipse.debug.ui/ui/org/eclipse/debug/internal/ui/DebugUIPreferenceInitializer.java
index f9f0a43..087fc9f 100644
--- a/org.eclipse.debug.ui/ui/org/eclipse/debug/internal/ui/DebugUIPreferenceInitializer.java
+++ b/org.eclipse.debug.ui/ui/org/eclipse/debug/internal/ui/DebugUIPreferenceInitializer.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- *  Copyright (c) 2004, 2016 IBM Corporation and others.
+ *  Copyright (c) 2004, 2018 IBM Corporation and others.
  *  All rights reserved. This program and the accompanying materials
  *  are made available under the terms of the Eclipse Public License v1.0
  *  which accompanies this distribution, and is available at
@@ -74,6 +74,7 @@
 		//ConsolePreferencePage
 		prefs.setDefault(IDebugPreferenceConstants.CONSOLE_WRAP, false);
 		prefs.setDefault(IDebugPreferenceConstants.CONSOLE_WIDTH, 80);
+		prefs.setDefault(IDebugPreferenceConstants.CONSOLE_AUTO_SCROLL_LOCK, true);
 		prefs.setDefault(IDebugPreferenceConstants.CONSOLE_OPEN_ON_OUT, true);
 		prefs.setDefault(IDebugPreferenceConstants.CONSOLE_OPEN_ON_ERR, true);
 		prefs.setDefault(IDebugPreferenceConstants.CONSOLE_LIMIT_CONSOLE_OUTPUT, true);
diff --git a/org.eclipse.debug.ui/ui/org/eclipse/debug/internal/ui/preferences/ConsolePreferencePage.java b/org.eclipse.debug.ui/ui/org/eclipse/debug/internal/ui/preferences/ConsolePreferencePage.java
index 9c67df1..14e0bd8 100644
--- a/org.eclipse.debug.ui/ui/org/eclipse/debug/internal/ui/preferences/ConsolePreferencePage.java
+++ b/org.eclipse.debug.ui/ui/org/eclipse/debug/internal/ui/preferences/ConsolePreferencePage.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2000, 2015 IBM Corporation and others.
+ * Copyright (c) 2000, 2018 IBM Corporation and others.
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
  * which accompanies this distribution, and is available at
@@ -140,6 +140,7 @@
 		fTabSizeEditor.setValidRange(1,100);
 		fTabSizeEditor.setErrorMessage(DebugPreferencesMessages.ConsolePreferencePage_13);
 
+		addField(new BooleanFieldEditor(IDebugPreferenceConstants.CONSOLE_AUTO_SCROLL_LOCK, DebugPreferencesMessages.ConsolePreferencePage_Show__Console_View_enable_auto_scroll_lock, SWT.NONE, getFieldEditorParent()));
 		addField(new BooleanFieldEditor(IDebugPreferenceConstants.CONSOLE_OPEN_ON_OUT, DebugPreferencesMessages.ConsolePreferencePage_Show__Console_View_when_there_is_program_output_3, SWT.NONE, getFieldEditorParent()));
 		addField(new BooleanFieldEditor(IDebugPreferenceConstants.CONSOLE_OPEN_ON_ERR, DebugPreferencesMessages.ConsolePreferencePage_Show__Console_View_when_there_is_program_error_3, SWT.NONE, getFieldEditorParent()));
 
diff --git a/org.eclipse.debug.ui/ui/org/eclipse/debug/internal/ui/preferences/DebugPreferencesMessages.java b/org.eclipse.debug.ui/ui/org/eclipse/debug/internal/ui/preferences/DebugPreferencesMessages.java
index 80e32a2..c45eb77 100644
--- a/org.eclipse.debug.ui/ui/org/eclipse/debug/internal/ui/preferences/DebugPreferencesMessages.java
+++ b/org.eclipse.debug.ui/ui/org/eclipse/debug/internal/ui/preferences/DebugPreferencesMessages.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- *  Copyright (c) 2000, 2016 IBM Corporation and others.
+ *  Copyright (c) 2000, 2018 IBM Corporation and others.
  *  All rights reserved. This program and the accompanying materials
  *  are made available under the terms of the Eclipse Public License v1.0
  *  which accompanies this distribution, and is available at
@@ -20,6 +20,7 @@
 	public static String ConsolePreferencePage_Console_settings;
 	public static String ConsolePreferencePage_Show__Console_View_when_there_is_program_error_3;
 	public static String ConsolePreferencePage_Show__Console_View_when_there_is_program_output_3;
+	public static String ConsolePreferencePage_Show__Console_View_enable_auto_scroll_lock;
 	public static String ConsolePreferencePage_Standard_Error__3;
 	public static String ConsolePreferencePage_Standard_In__4;
 	public static String ConsolePreferencePage_Standard_Out__2;
diff --git a/org.eclipse.debug.ui/ui/org/eclipse/debug/internal/ui/preferences/DebugPreferencesMessages.properties b/org.eclipse.debug.ui/ui/org/eclipse/debug/internal/ui/preferences/DebugPreferencesMessages.properties
index b4a6265..94f5252 100644
--- a/org.eclipse.debug.ui/ui/org/eclipse/debug/internal/ui/preferences/DebugPreferencesMessages.properties
+++ b/org.eclipse.debug.ui/ui/org/eclipse/debug/internal/ui/preferences/DebugPreferencesMessages.properties
@@ -1,5 +1,5 @@
 ###############################################################################
-# Copyright (c) 2000, 2016 IBM Corporation and others.
+# Copyright (c) 2000, 2018 IBM Corporation and others.
 # All rights reserved. This program and the accompanying materials
 # are made available under the terms of the Eclipse Public License v1.0
 # which accompanies this distribution, and is available at
@@ -12,6 +12,7 @@
 ConsolePreferencePage_Console_settings=Debug Console Settings.
 ConsolePreferencePage_Show__Console_View_when_there_is_program_error_3=Show when &program writes to standard error
 ConsolePreferencePage_Show__Console_View_when_there_is_program_output_3=&Show when program writes to standard out
+ConsolePreferencePage_Show__Console_View_enable_auto_scroll_lock=Enable &auto scroll lock
 ConsolePreferencePage_Standard_Error__3=Standard &Error text color:
 ConsolePreferencePage_Standard_In__4=Standard &In text color:
 ConsolePreferencePage_Standard_Out__2=Standard &Out text color:
diff --git a/org.eclipse.debug.ui/ui/org/eclipse/debug/internal/ui/preferences/IDebugPreferenceConstants.java b/org.eclipse.debug.ui/ui/org/eclipse/debug/internal/ui/preferences/IDebugPreferenceConstants.java
index 7188985..45fd9cd 100644
--- a/org.eclipse.debug.ui/ui/org/eclipse/debug/internal/ui/preferences/IDebugPreferenceConstants.java
+++ b/org.eclipse.debug.ui/ui/org/eclipse/debug/internal/ui/preferences/IDebugPreferenceConstants.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2000, 2013 IBM Corporation and others.
+ * Copyright (c) 2000, 2018 IBM Corporation and others.
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
  * which accompanies this distribution, and is available at
@@ -60,6 +60,11 @@
 	public static final String CONSOLE_WIDTH = "Console.width"; //$NON-NLS-1$
 
 	/**
+	 * (boolean) Whether or not the console view will enable auto scroll lock
+	 */
+	public static final String CONSOLE_AUTO_SCROLL_LOCK = "DEBUG.autoScrollLock"; //$NON-NLS-1$
+
+	/**
 	 * (boolean) Whether or not the console view is shown
 	 * when there is program output.
   	 */
diff --git a/org.eclipse.debug.ui/ui/org/eclipse/debug/internal/ui/views/console/ProcessConsole.java b/org.eclipse.debug.ui/ui/org/eclipse/debug/internal/ui/views/console/ProcessConsole.java
index 1619261..2ca9e09 100644
--- a/org.eclipse.debug.ui/ui/org/eclipse/debug/internal/ui/views/console/ProcessConsole.java
+++ b/org.eclipse.debug.ui/ui/org/eclipse/debug/internal/ui/views/console/ProcessConsole.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- *  Copyright (c) 2000, 2015 IBM Corporation and others.
+ *  Copyright (c) 2000, 2018 IBM Corporation and others.
  *  All rights reserved. This program and the accompanying materials
  *  are made available under the terms of the Eclipse Public License v1.0
  *  which accompanies this distribution, and is available at
@@ -320,6 +320,9 @@
         } else if (property.equals(IDebugPreferenceConstants.CONSOLE_TAB_WIDTH)) {
             int tabWidth = store.getInt(IDebugPreferenceConstants.CONSOLE_TAB_WIDTH);
             setTabWidth(tabWidth);
+		} else if (property.equals(IDebugPreferenceConstants.CONSOLE_AUTO_SCROLL_LOCK)) {
+			boolean autoScrollLock = store.getBoolean(IDebugPreferenceConstants.CONSOLE_AUTO_SCROLL_LOCK);
+			setConsoleAutoScrollLock(autoScrollLock);
         } else if (property.equals(IDebugPreferenceConstants.CONSOLE_OPEN_ON_OUT)) {
             boolean activateOnOut = store.getBoolean(IDebugPreferenceConstants.CONSOLE_OPEN_ON_OUT);
 			@SuppressWarnings("resource")
@@ -449,6 +452,7 @@
             setConsoleWidth(store.getInt(IDebugPreferenceConstants.CONSOLE_WIDTH));
         }
         setTabWidth(store.getInt(IDebugPreferenceConstants.CONSOLE_TAB_WIDTH));
+		setConsoleAutoScrollLock(store.getBoolean(IDebugPreferenceConstants.CONSOLE_AUTO_SCROLL_LOCK));
 
         if (store.getBoolean(IDebugPreferenceConstants.CONSOLE_LIMIT_CONSOLE_OUTPUT)) {
             int highWater = store.getInt(IDebugPreferenceConstants.CONSOLE_HIGH_WATER_MARK);
diff --git a/org.eclipse.ui.console/META-INF/MANIFEST.MF b/org.eclipse.ui.console/META-INF/MANIFEST.MF
index a3cd1cb..a581616 100644
--- a/org.eclipse.ui.console/META-INF/MANIFEST.MF
+++ b/org.eclipse.ui.console/META-INF/MANIFEST.MF
@@ -2,7 +2,7 @@
 Bundle-ManifestVersion: 2
 Bundle-Name: %pluginName
 Bundle-SymbolicName: org.eclipse.ui.console; singleton:=true
-Bundle-Version: 3.7.100.qualifier
+Bundle-Version: 3.8.0.qualifier
 Bundle-Activator: org.eclipse.ui.console.ConsolePlugin
 Bundle-Vendor: %providerName
 Bundle-Localization: plugin
diff --git a/org.eclipse.ui.console/pom.xml b/org.eclipse.ui.console/pom.xml
index df2058e..9d4f5d6 100644
--- a/org.eclipse.ui.console/pom.xml
+++ b/org.eclipse.ui.console/pom.xml
@@ -1,6 +1,6 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <!--
-  Copyright (c) 2012, 2017 Eclipse Foundation and others.
+  Copyright (c) 2012, 2018 Eclipse Foundation and others.
   All rights reserved. This program and the accompanying materials
   are made available under the terms of the Eclipse Distribution License v1.0
   which accompanies this distribution, and is available at
@@ -18,6 +18,6 @@
   </parent>
   <groupId>org.eclipse.ui</groupId>
   <artifactId>org.eclipse.ui.console</artifactId>
-  <version>3.7.100-SNAPSHOT</version>
+  <version>3.8.0-SNAPSHOT</version>
   <packaging>eclipse-plugin</packaging>
 </project>
diff --git a/org.eclipse.ui.console/src/org/eclipse/ui/console/IConsoleConstants.java b/org.eclipse.ui.console/src/org/eclipse/ui/console/IConsoleConstants.java
index 27e8b09..7df3fa2 100644
--- a/org.eclipse.ui.console/src/org/eclipse/ui/console/IConsoleConstants.java
+++ b/org.eclipse.ui.console/src/org/eclipse/ui/console/IConsoleConstants.java
@@ -126,6 +126,13 @@
 	public static final String P_TAB_SIZE = ConsolePlugin.getUniqueIdentifier()  + ".P_TAB_SIZE";	 //$NON-NLS-1$
 
 	/**
+	 * Property constant indicating the user preference for auto scroll lock enabling.
+	 *
+	 * @since 3.8
+	 */
+	public static final String P_CONSOLE_AUTO_SCROLL_LOCK = ConsolePlugin.getUniqueIdentifier() + ".P_CONSOLE_AUTO_SCROLL_LOCK"; //$NON-NLS-1$
+
+	/**
 	 * Property constant indicating the width of a fixed width console has changed.
 	 *
 	 * @since 3.1
diff --git a/org.eclipse.ui.console/src/org/eclipse/ui/console/TextConsole.java b/org.eclipse.ui.console/src/org/eclipse/ui/console/TextConsole.java
index 02c1715..eedaaf5 100644
--- a/org.eclipse.ui.console/src/org/eclipse/ui/console/TextConsole.java
+++ b/org.eclipse.ui.console/src/org/eclipse/ui/console/TextConsole.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2000, 2015 IBM Corporation and others.
+ * Copyright (c) 2000, 2018 IBM Corporation and others.
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
  * which accompanies this distribution, and is available at
@@ -89,6 +89,8 @@
 	 */
 	private boolean fCompleteFired = false;
 
+	private boolean fConsoleAutoScrollLock = true;
+
 
 	/**
 	 * Map of client defined attributes
@@ -161,11 +163,36 @@
 	}
 
 	/**
+	 * 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));
+		}
+	}
+
+	/**
 	 * 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.
+	 *            the console does not have any fixed width.
 	 */
 	public void setConsoleWidth(int width) {
 		if (fConsoleWidth != width) {
diff --git a/org.eclipse.ui.console/src/org/eclipse/ui/console/TextConsolePage.java b/org.eclipse.ui.console/src/org/eclipse/ui/console/TextConsolePage.java
index a0fefa9..ef372d1 100644
--- a/org.eclipse.ui.console/src/org/eclipse/ui/console/TextConsolePage.java
+++ b/org.eclipse.ui.console/src/org/eclipse/ui/console/TextConsolePage.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2000, 2015 IBM Corporation and others.
+ * Copyright (c) 2000, 2018 IBM Corporation and others.
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
  * which accompanies this distribution, and is available at
@@ -258,6 +258,8 @@
 			} else if (source.equals(fConsole) && property.equals(IConsoleConstants.P_TAB_SIZE)) {
 				Integer tabSize = (Integer)event.getNewValue();
 				fViewer.setTabWidth(tabSize.intValue());
+			} else if (source.equals(fConsole) && property.equals(IConsoleConstants.P_CONSOLE_AUTO_SCROLL_LOCK)) {
+				fViewer.setConsoleAutoScrollLock(fConsole.isConsoleAutoScrollLock());
 			} else if (source.equals(fConsole) && property.equals(IConsoleConstants.P_CONSOLE_WIDTH)) {
 				fViewer.setConsoleWidth(fConsole.getConsoleWidth());
 			} else if (IConsoleConstants.P_BACKGROUND_COLOR.equals(property)) {
diff --git a/org.eclipse.ui.console/src/org/eclipse/ui/console/TextConsoleViewer.java b/org.eclipse.ui.console/src/org/eclipse/ui/console/TextConsoleViewer.java
index f1fbae3..16ffa80 100644
--- a/org.eclipse.ui.console/src/org/eclipse/ui/console/TextConsoleViewer.java
+++ b/org.eclipse.ui.console/src/org/eclipse/ui/console/TextConsoleViewer.java
@@ -89,6 +89,9 @@
 
 	private TextConsole console;
 
+	private boolean consoleAutoScrollLock = true;
+
+
 	private IPropertyChangeListener propertyChangeListener;
 
 	private IScrollLockStateProvider scrollLockStateProvider;
@@ -166,10 +169,13 @@
 	}
 
 	/*
-	 * Check if the document is empty or the line count is smaller than each
+	 * Check if user preference is enabled for auto scroll lock and the document is empty or the line count is smaller than each
 	 * vertical scroll
 	 */
-	private boolean isEmptyDocument() {
+	private boolean isAutoScrollLockNotApplicable() {
+		if (!consoleAutoScrollLock) {
+			return true;
+		}
 		StyledText textWidget = getTextWidget();
 		if (textWidget != null && !textWidget.isDisposed()) {
 			return (textWidget.getLineCount() <= textWidget.getVerticalBar().getIncrement());
@@ -237,6 +243,7 @@
 	public TextConsoleViewer(Composite parent, TextConsole console) {
 		super(parent, null, SWT.V_SCROLL | SWT.H_SCROLL);
 		this.console = console;
+		this.consoleAutoScrollLock = console.isConsoleAutoScrollLock();
 
 		IDocument document = console.getDocument();
 		setDocument(document);
@@ -254,7 +261,7 @@
 		styledText.getVerticalBar().addSelectionListener(new SelectionAdapter() {
 			@Override
 			public void widgetSelected(SelectionEvent e) {
-				if (isEmptyDocument()) {
+				if (isAutoScrollLockNotApplicable()) {
 					return;
 				}
 				// scroll lock if vertical scroll bar dragged, OR selection on
@@ -284,7 +291,7 @@
 		styledText.addKeyListener(new KeyAdapter() {
 			@Override
 			public void keyPressed(KeyEvent e) {
-				if (isEmptyDocument()) {
+				if (isAutoScrollLockNotApplicable()) {
 					return;
 				}
 				// lock the scroll if PAGE_UP ,HOME or TOP selected
@@ -304,7 +311,7 @@
 		styledText.addMouseWheelListener(new MouseWheelListener() {
 			@Override
 			public void mouseScrolled(MouseEvent e) {
-				if (isEmptyDocument()) {
+				if (isAutoScrollLockNotApplicable()) {
 					return;
 				}
 				if (e.count < 0) { // Mouse dragged down
@@ -724,6 +731,18 @@
 	}
 
 	/**
+	 * Sets the user preference for console auto scroll lock.
+	 *
+	 * @param autoScrollLock user preference for console auto scroll lock
+	 * @since 3.8
+	 */
+	public void setConsoleAutoScrollLock(boolean autoScrollLock) {
+		if (consoleAutoScrollLock != autoScrollLock) {
+			consoleAutoScrollLock = autoScrollLock;
+		}
+	}
+
+	/**
 	 * Sets the console to have a fixed character width. Use -1 to indicate that
 	 * a fixed width should not be used.
 	 *