blob: 04e841a7643c62649331e36a31de1cc93e887c07 [file] [log] [blame]
/*=============================================================================#
# 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());
}
}
}