blob: de5dcf85c4d7d1cdc739882ade73cb713f3ac4b1 [file] [log] [blame]
/*=============================================================================#
# Copyright (c) 2011, 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.ecommons.text.ui;
import static org.eclipse.ui.texteditor.AbstractTextEditor.PREFERENCE_COLOR_BACKGROUND;
import static org.eclipse.ui.texteditor.AbstractTextEditor.PREFERENCE_COLOR_BACKGROUND_SYSTEM_DEFAULT;
import static org.eclipse.ui.texteditor.AbstractTextEditor.PREFERENCE_COLOR_FOREGROUND;
import static org.eclipse.ui.texteditor.AbstractTextEditor.PREFERENCE_COLOR_FOREGROUND_SYSTEM_DEFAULT;
import static org.eclipse.ui.texteditor.AbstractTextEditor.PREFERENCE_COLOR_SELECTION_BACKGROUND;
import static org.eclipse.ui.texteditor.AbstractTextEditor.PREFERENCE_COLOR_SELECTION_BACKGROUND_SYSTEM_DEFAULT;
import static org.eclipse.ui.texteditor.AbstractTextEditor.PREFERENCE_COLOR_SELECTION_FOREGROUND;
import static org.eclipse.ui.texteditor.AbstractTextEditor.PREFERENCE_COLOR_SELECTION_FOREGROUND_SYSTEM_DEFAULT;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.preference.PreferenceConverter;
import org.eclipse.jface.text.source.ISourceViewer;
import org.eclipse.jface.text.source.SourceViewer;
import org.eclipse.jface.util.IPropertyChangeListener;
import org.eclipse.jface.util.PropertyChangeEvent;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.statet.jcommons.lang.NonNullByDefault;
import org.eclipse.statet.jcommons.lang.Nullable;
import org.eclipse.statet.ecommons.ui.util.UIAccess;
@NonNullByDefault
public class TextViewerEditorColorUpdater {
protected final ISourceViewer viewer;
protected final IPreferenceStore preferenceStore;
private @Nullable IPropertyChangeListener listener;
public TextViewerEditorColorUpdater(final SourceViewer viewer, final IPreferenceStore preferenceStore) {
assert (viewer != null);
assert (preferenceStore != null);
this.viewer= viewer;
this.preferenceStore= preferenceStore;
viewer.getTextWidget().addDisposeListener(new DisposeListener() {
@Override
public void widgetDisposed(final DisposeEvent e) {
dispose();
}
});
this.listener= new IPropertyChangeListener() {
@Override
public void propertyChange(final PropertyChangeEvent event) {
if (PREFERENCE_COLOR_FOREGROUND.equals(event.getProperty())
|| PREFERENCE_COLOR_FOREGROUND_SYSTEM_DEFAULT.equals(event.getProperty())
|| PREFERENCE_COLOR_BACKGROUND.equals(event.getProperty())
|| PREFERENCE_COLOR_BACKGROUND_SYSTEM_DEFAULT.equals(event.getProperty())
|| PREFERENCE_COLOR_SELECTION_FOREGROUND.equals(event.getProperty())
|| PREFERENCE_COLOR_SELECTION_FOREGROUND_SYSTEM_DEFAULT.equals(event.getProperty())
|| PREFERENCE_COLOR_SELECTION_BACKGROUND.equals(event.getProperty())
|| PREFERENCE_COLOR_SELECTION_BACKGROUND_SYSTEM_DEFAULT.equals(event.getProperty()) ) {
updateColors();
}
}
};
this.preferenceStore.addPropertyChangeListener(this.listener);
updateColors();
}
protected void updateColors() {
final StyledText styledText= this.viewer.getTextWidget();
if (UIAccess.isOkToUse(styledText)) {
{ // foreground color
final Color color= this.preferenceStore.getBoolean(
PREFERENCE_COLOR_FOREGROUND_SYSTEM_DEFAULT) ? null :
createColor(this.preferenceStore, PREFERENCE_COLOR_FOREGROUND);
styledText.setForeground(color);
}
{ // background color
final Color color= this.preferenceStore.getBoolean(
PREFERENCE_COLOR_BACKGROUND_SYSTEM_DEFAULT) ? null :
createColor(this.preferenceStore, PREFERENCE_COLOR_BACKGROUND);
styledText.setBackground(color);
}
{ // selection foreground color
final Color color= this.preferenceStore.getBoolean(
PREFERENCE_COLOR_SELECTION_FOREGROUND_SYSTEM_DEFAULT) ? null :
createColor(this.preferenceStore, PREFERENCE_COLOR_SELECTION_FOREGROUND);
styledText.setSelectionForeground(color);
}
{ // selection background color
final Color color= this.preferenceStore.getBoolean(
PREFERENCE_COLOR_SELECTION_BACKGROUND_SYSTEM_DEFAULT) ? null :
createColor(this.preferenceStore, PREFERENCE_COLOR_SELECTION_BACKGROUND);
styledText.setSelectionBackground(color);
}
}
}
protected @Nullable Color createColor(final IPreferenceStore store, final String key) {
final RGB rgb= PreferenceConverter.getColor(store, key);
return (rgb != null) ? new Color(rgb) : null;
}
protected void dispose() {
{ final var listener= this.listener;
if (listener != null) {
this.listener= null;
this.preferenceStore.removePropertyChangeListener(listener);
}
}
final StyledText styledText= this.viewer.getTextWidget();
if (UIAccess.isOkToUse(styledText)) {
styledText.setForeground(null);
styledText.setBackground(null);
styledText.setSelectionForeground(null);
styledText.setSelectionBackground(null);
}
}
}