Bug 573707: [SourceEditor] Add ExtStyledText with workaround for
background color of console input
Change-Id: I99e611a5d36e16a330a90682499a2d8d940a8938
diff --git a/ltk/org.eclipse.statet.ltk.ui/src/org/eclipse/statet/ltk/ui/sourceediting/ExtStyledText.java b/ltk/org.eclipse.statet.ltk.ui/src/org/eclipse/statet/ltk/ui/sourceediting/ExtStyledText.java
new file mode 100644
index 0000000..04e841a
--- /dev/null
+++ b/ltk/org.eclipse.statet.ltk.ui/src/org/eclipse/statet/ltk/ui/sourceediting/ExtStyledText.java
@@ -0,0 +1,100 @@
+/*=============================================================================#
+ # Copyright (c) 2021 Stephan Wahlbrink and others.
+ #
+ # This program and the accompanying materials are made available under the
+ # terms of the Eclipse Public License 2.0 which is available at
+ # https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
+ # which is available at https://www.apache.org/licenses/LICENSE-2.0.
+ #
+ # SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
+ #
+ # Contributors:
+ # Stephan Wahlbrink <sw@wahlbrink.eu> - initial API and implementation
+ #=============================================================================*/
+
+package org.eclipse.statet.ltk.ui.sourceediting;
+
+import org.eclipse.swt.custom.StyledText;
+import org.eclipse.swt.graphics.Color;
+import org.eclipse.swt.widgets.Composite;
+
+import org.eclipse.statet.jcommons.lang.NonNullByDefault;
+import org.eclipse.statet.jcommons.lang.Nullable;
+
+
+@NonNullByDefault
+public class ExtStyledText extends StyledText {
+
+
+ private @Nullable Color customBackgroundColor;
+ private @Nullable Color customForegroundColor;
+
+ private boolean inSetEnabled;
+
+
+ public ExtStyledText(final Composite parent, final int style) {
+ super(parent, style);
+ }
+
+
+ @Override
+ public void setEnabled(final boolean enabled) {
+ this.inSetEnabled= true;
+ try {
+ if (enabled) {
+ super.setEnabled(true);
+ applyCustomBackgroundColor();
+ applyCustomForegroundColor();
+ }
+ else {
+ super.setBackground(null);
+ super.setForeground(null);
+ super.setEnabled(false);
+ }
+ }
+ finally {
+ this.inSetEnabled= false;
+ }
+ }
+
+ @Override
+ public void setBackground(final @Nullable Color color) {
+ if (this.inSetEnabled) {
+ super.setBackground(color);
+ }
+ else {
+ this.customBackgroundColor= color;
+ if (isEnabled()) {
+ applyCustomBackgroundColor();
+ }
+ }
+ }
+
+ private void applyCustomBackgroundColor() {
+ super.setBackground(this.customBackgroundColor);
+ if (this.customBackgroundColor == null) { // workaround for bug 573707
+ super.setBackground(getBackground());
+ }
+ }
+
+ @Override
+ public void setForeground(final @Nullable Color color) {
+ if (this.inSetEnabled) {
+ super.setForeground(color);
+ }
+ else {
+ this.customForegroundColor= color;
+ if (isEnabled()) {
+ applyCustomForegroundColor();
+ }
+ }
+ }
+
+ private void applyCustomForegroundColor() {
+ super.setForeground(this.customForegroundColor);
+ if (this.customForegroundColor == null) { // workaround for bug 573707
+ super.setForeground(getForeground());
+ }
+ }
+
+}