| /*=============================================================================# |
| # 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.swt.widgets.Display; |
| |
| import org.eclipse.statet.ecommons.ui.util.UIAccess; |
| |
| |
| public class TextViewerEditorColorUpdater { |
| |
| |
| protected final ISourceViewer fViewer; |
| protected final IPreferenceStore fPreferenceStore; |
| |
| private IPropertyChangeListener fListener; |
| |
| private Color fForegroundColor; |
| private Color fBackgroundColor; |
| private Color fSelectionForegroundColor; |
| private Color fSelectionBackgroundColor; |
| |
| |
| public TextViewerEditorColorUpdater(final SourceViewer viewer, final IPreferenceStore preferenceStore) { |
| assert (viewer != null); |
| assert (preferenceStore != null); |
| |
| this.fViewer= viewer; |
| this.fPreferenceStore= preferenceStore; |
| |
| viewer.getTextWidget().addDisposeListener(new DisposeListener() { |
| @Override |
| public void widgetDisposed(final DisposeEvent e) { |
| dispose(); |
| } |
| }); |
| |
| this.fListener= 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.fPreferenceStore.addPropertyChangeListener(this.fListener); |
| |
| updateColors(); |
| } |
| |
| |
| protected void updateColors() { |
| final StyledText styledText= this.fViewer.getTextWidget(); |
| if (UIAccess.isOkToUse(styledText)) { |
| { // foreground color |
| final Color color= this.fPreferenceStore.getBoolean( |
| PREFERENCE_COLOR_FOREGROUND_SYSTEM_DEFAULT) ? null : |
| createColor(this.fPreferenceStore, PREFERENCE_COLOR_FOREGROUND, |
| styledText.getDisplay() ); |
| styledText.setForeground(color); |
| if (this.fForegroundColor != null) { |
| this.fForegroundColor.dispose(); |
| } |
| this.fForegroundColor= color; |
| } |
| { // background color |
| final Color color= this.fPreferenceStore.getBoolean( |
| PREFERENCE_COLOR_BACKGROUND_SYSTEM_DEFAULT) ? null : |
| createColor(this.fPreferenceStore, PREFERENCE_COLOR_BACKGROUND, |
| styledText.getDisplay() ); |
| styledText.setBackground(color); |
| if (this.fBackgroundColor != null) { |
| this.fBackgroundColor.dispose(); |
| } |
| this.fBackgroundColor= color; |
| } |
| { // selection foreground color |
| final Color color= this.fPreferenceStore.getBoolean( |
| PREFERENCE_COLOR_SELECTION_FOREGROUND_SYSTEM_DEFAULT) ? null : |
| createColor(this.fPreferenceStore, PREFERENCE_COLOR_SELECTION_FOREGROUND, |
| styledText.getDisplay() ); |
| styledText.setSelectionForeground(color); |
| if (this.fSelectionForegroundColor != null) { |
| this.fSelectionForegroundColor.dispose(); |
| } |
| this.fSelectionForegroundColor= color; |
| } |
| { // selection background color |
| final Color color= this.fPreferenceStore.getBoolean( |
| PREFERENCE_COLOR_SELECTION_BACKGROUND_SYSTEM_DEFAULT) ? null : |
| createColor(this.fPreferenceStore, PREFERENCE_COLOR_SELECTION_BACKGROUND, |
| styledText.getDisplay() ); |
| styledText.setSelectionBackground(color); |
| if (this.fSelectionBackgroundColor != null) { |
| this.fSelectionBackgroundColor.dispose(); |
| } |
| this.fSelectionBackgroundColor= color; |
| } |
| } |
| } |
| |
| protected Color createColor(final IPreferenceStore store, final String key, |
| final Display display) { |
| final RGB rgb= PreferenceConverter.getColor(store, key); |
| return (rgb != null) ? new Color(display, rgb) : null; |
| } |
| |
| |
| protected void dispose() { |
| if (this.fListener != null) { |
| this.fPreferenceStore.removePropertyChangeListener(this.fListener); |
| this.fListener= null; |
| } |
| |
| final StyledText styledText= this.fViewer.getTextWidget(); |
| if (UIAccess.isOkToUse(styledText)) { |
| styledText.setForeground(null); |
| styledText.setBackground(null); |
| styledText.setSelectionForeground(null); |
| styledText.setSelectionBackground(null); |
| } |
| if (this.fForegroundColor != null) { |
| this.fForegroundColor.dispose(); |
| this.fForegroundColor= null; |
| } |
| if (this.fBackgroundColor != null) { |
| this.fBackgroundColor.dispose(); |
| this.fBackgroundColor= null; |
| } |
| if (this.fSelectionForegroundColor != null) { |
| this.fSelectionForegroundColor.dispose(); |
| this.fSelectionForegroundColor= null; |
| } |
| if (this.fSelectionBackgroundColor != null) { |
| this.fSelectionBackgroundColor.dispose(); |
| this.fSelectionBackgroundColor= null; |
| } |
| } |
| |
| } |