Skip to content

Commit cefa208

Browse files
committed
Simplify FixedLinePainter into FixedLineHighlighter
1 parent c67c1c4 commit cefa208

File tree

1 file changed

+55
-90
lines changed

1 file changed

+55
-90
lines changed

bundles/org.eclipse.text.quicksearch/src/org/eclipse/text/quicksearch/internal/ui/QuickSearchDialog.java

Lines changed: 55 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@
2121
package org.eclipse.text.quicksearch.internal.ui;
2222

2323
import static org.eclipse.jface.resource.JFaceResources.TEXT_FONT;
24+
import static org.eclipse.ui.texteditor.AbstractDecoratedTextEditorPreferenceConstants.EDITOR_CURRENT_LINE;
25+
import static org.eclipse.ui.texteditor.AbstractDecoratedTextEditorPreferenceConstants.EDITOR_CURRENT_LINE_COLOR;
26+
import static org.eclipse.ui.texteditor.AbstractDecoratedTextEditorPreferenceConstants.EDITOR_LINE_NUMBER_RULER_COLOR;
27+
import static org.eclipse.ui.texteditor.AbstractTextEditor.PREFERENCE_COLOR_BACKGROUND;
28+
import static org.eclipse.ui.texteditor.AbstractTextEditor.PREFERENCE_COLOR_BACKGROUND_SYSTEM_DEFAULT;
29+
import static org.eclipse.ui.texteditor.AbstractTextEditor.PREFERENCE_COLOR_FOREGROUND;
30+
import static org.eclipse.ui.texteditor.AbstractTextEditor.PREFERENCE_COLOR_FOREGROUND_SYSTEM_DEFAULT;
2431

2532
import java.util.ArrayList;
2633
import java.util.Arrays;
@@ -46,14 +53,11 @@
4653
import org.eclipse.jface.dialogs.IDialogConstants;
4754
import org.eclipse.jface.dialogs.IDialogSettings;
4855
import org.eclipse.jface.layout.GridDataFactory;
49-
import org.eclipse.jface.preference.IPreferenceStore;
5056
import org.eclipse.jface.preference.PreferenceConverter;
5157
import org.eclipse.jface.resource.JFaceResources;
5258
import org.eclipse.jface.text.BadLocationException;
5359
import org.eclipse.jface.text.CursorLinePainter;
5460
import org.eclipse.jface.text.IDocument;
55-
import org.eclipse.jface.text.IPaintPositionManager;
56-
import org.eclipse.jface.text.IPainter;
5761
import org.eclipse.jface.text.IRegion;
5862
import org.eclipse.jface.text.source.CompositeRuler;
5963
import org.eclipse.jface.text.source.ISharedTextColors;
@@ -80,7 +84,6 @@
8084
import org.eclipse.swt.accessibility.AccessibleEvent;
8185
import org.eclipse.swt.custom.LineBackgroundEvent;
8286
import org.eclipse.swt.custom.LineBackgroundListener;
83-
import org.eclipse.swt.custom.ST;
8487
import org.eclipse.swt.custom.SashForm;
8588
import org.eclipse.swt.custom.StyleRange;
8689
import org.eclipse.swt.custom.StyledText;
@@ -135,8 +138,6 @@
135138
import org.eclipse.ui.internal.WorkbenchImages;
136139
import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin;
137140
import org.eclipse.ui.progress.UIJob;
138-
import org.eclipse.ui.texteditor.AbstractDecoratedTextEditorPreferenceConstants;
139-
import org.eclipse.ui.texteditor.AbstractTextEditor;
140141
import org.eclipse.ui.texteditor.SourceViewerDecorationSupport;
141142
import org.osgi.framework.FrameworkUtil;
142143

@@ -382,7 +383,7 @@ public void update(ViewerCell cell) {
382383

383384
private SourceViewer viewer;
384385
private LineNumberRulerColumn lineNumberColumn;
385-
private FixedLinePainter targetLinePainter;
386+
private FixedLineHighlighter targetLineHighlighter;
386387
private final IPropertyChangeListener preferenceChangeListener = this::handlePropertyChangeEvent;
387388

388389
private DocumentFetcher documents;
@@ -976,11 +977,7 @@ private void createDetailsArea(Composite parent) {
976977

977978
viewer = new SourceViewer(viewerParent, new CompositeRuler(), SWT.H_SCROLL | SWT.V_SCROLL | SWT.READ_ONLY);
978979
viewer.getTextWidget().setFont(JFaceResources.getFont(TEXT_FONT));
979-
980-
lineNumberColumn = new LineNumberRulerColumn();
981-
viewer.addVerticalRulerColumn(lineNumberColumn);
982-
setColors(false);
983-
createLinesHighlightingDecorations();
980+
createViewerDecorations();
984981

985982
list.addSelectionChangedListener(event -> refreshDetails());
986983

@@ -992,16 +989,15 @@ public void controlResized(ControlEvent e) {
992989
});
993990
}
994991

995-
private void setColors(boolean refresh) {
992+
private void setColors() {
996993
RGB background = null;
997994
RGB foreground = null;
998995
var textWidget = viewer.getTextWidget();
999-
var preferenceStore = EditorsUI.getPreferenceStore();
1000996
ISharedTextColors sharedColors = EditorsUI.getSharedTextColors();
1001997

1002-
var isUsingSystemBackground = preferenceStore.getBoolean(AbstractTextEditor.PREFERENCE_COLOR_BACKGROUND_SYSTEM_DEFAULT);
998+
var isUsingSystemBackground = EditorsUI.getPreferenceStore().getBoolean(PREFERENCE_COLOR_BACKGROUND_SYSTEM_DEFAULT);
1003999
if (!isUsingSystemBackground) {
1004-
background = getColorFromStore(preferenceStore, AbstractTextEditor.PREFERENCE_COLOR_BACKGROUND);
1000+
background = getColorFromStore(PREFERENCE_COLOR_BACKGROUND);
10051001
}
10061002
if (background != null) {
10071003
var color = sharedColors.getColor(background);
@@ -1012,59 +1008,65 @@ private void setColors(boolean refresh) {
10121008
lineNumberColumn.setBackground(null);
10131009
}
10141010

1015-
var isUsingSystemForeground = preferenceStore.getBoolean(AbstractTextEditor.PREFERENCE_COLOR_FOREGROUND_SYSTEM_DEFAULT);
1011+
var isUsingSystemForeground = EditorsUI.getPreferenceStore().getBoolean(PREFERENCE_COLOR_FOREGROUND_SYSTEM_DEFAULT);
10161012
if (!isUsingSystemForeground) {
1017-
foreground = getColorFromStore(preferenceStore, AbstractTextEditor.PREFERENCE_COLOR_FOREGROUND);
1013+
foreground = getColorFromStore(PREFERENCE_COLOR_FOREGROUND);
10181014
}
10191015
if (foreground != null) {
10201016
textWidget.setForeground(sharedColors.getColor(foreground));
10211017
} else {
10221018
textWidget.setForeground(null);
10231019
}
1020+
}
10241021

1025-
var lineNumbersColor = getColorFromStore(EditorsUI.getPreferenceStore(), AbstractDecoratedTextEditorPreferenceConstants.EDITOR_LINE_NUMBER_RULER_COLOR);
1026-
if (lineNumbersColor == null) {
1027-
lineNumbersColor = new RGB(0, 0, 0);
1028-
}
1029-
lineNumberColumn.setForeground(sharedColors.getColor(lineNumbersColor));
1022+
private Color getLineNumbersColor() {
1023+
var lineNumbersColor = getColorFromStore(EDITOR_LINE_NUMBER_RULER_COLOR);
1024+
return EditorsUI.getSharedTextColors().getColor(lineNumbersColor == null ? new RGB(0, 0, 0) : lineNumbersColor);
1025+
}
10301026

1031-
if (refresh) {
1032-
textWidget.redraw();
1033-
lineNumberColumn.redraw();
1034-
}
1027+
private Color getTargetLineHighlightColor() {
1028+
RGB background = getColorFromStore(EDITOR_CURRENT_LINE_COLOR);
1029+
ISharedTextColors sharedColors = EditorsUI.getSharedTextColors();
1030+
return sharedColors.getColor(background);
10351031
}
10361032

1033+
private void createViewerDecorations() {
1034+
lineNumberColumn = new LineNumberRulerColumn();
1035+
lineNumberColumn.setForeground(getLineNumbersColor());
1036+
viewer.addVerticalRulerColumn(lineNumberColumn);
10371037

1038-
private void createLinesHighlightingDecorations() {
10391038
var sourceViewerDecorationSupport = new SourceViewerDecorationSupport(viewer, null, null, EditorsUI.getSharedTextColors());
1040-
sourceViewerDecorationSupport.setCursorLinePainterPreferenceKeys(AbstractDecoratedTextEditorPreferenceConstants.EDITOR_CURRENT_LINE, AbstractDecoratedTextEditorPreferenceConstants.EDITOR_CURRENT_LINE_COLOR);
1039+
sourceViewerDecorationSupport.setCursorLinePainterPreferenceKeys(EDITOR_CURRENT_LINE, EDITOR_CURRENT_LINE_COLOR);
10411040
sourceViewerDecorationSupport.install(EditorsUI.getPreferenceStore());
1042-
targetLinePainter = new FixedLinePainter();
1043-
viewer.addPainter(targetLinePainter);
1044-
viewer.getTextWidget().addLineBackgroundListener(targetLinePainter);
1041+
targetLineHighlighter = new FixedLineHighlighter();
1042+
targetLineHighlighter.highlightColor = getTargetLineHighlightColor();
1043+
viewer.getTextWidget().addLineBackgroundListener(targetLineHighlighter);
1044+
1045+
setColors();
10451046
}
10461047

10471048
private void handlePropertyChangeEvent(PropertyChangeEvent event) {
10481049
if (viewer == null) {
10491050
return;
10501051
}
10511052
var prop = event.getProperty();
1052-
if (prop.equals(AbstractDecoratedTextEditorPreferenceConstants.EDITOR_LINE_NUMBER_RULER_COLOR)
1053-
|| prop.equals(AbstractTextEditor.PREFERENCE_COLOR_BACKGROUND_SYSTEM_DEFAULT)
1054-
|| prop.equals(AbstractTextEditor.PREFERENCE_COLOR_BACKGROUND)
1055-
|| prop.equals(AbstractTextEditor.PREFERENCE_COLOR_FOREGROUND_SYSTEM_DEFAULT)
1056-
|| prop.equals(AbstractTextEditor.PREFERENCE_COLOR_FOREGROUND)) {
1057-
setColors(true);
1058-
}
1059-
if (prop.equals(AbstractDecoratedTextEditorPreferenceConstants.EDITOR_CURRENT_LINE)
1060-
|| prop.equals(AbstractDecoratedTextEditorPreferenceConstants.EDITOR_CURRENT_LINE_COLOR)) {
1061-
targetLinePainter.highlightColor = null;
1062-
targetLinePainter.cursorLinePainter = null;
1053+
if (PREFERENCE_COLOR_BACKGROUND_SYSTEM_DEFAULT.equals(prop)
1054+
|| PREFERENCE_COLOR_BACKGROUND.equals(prop)
1055+
|| PREFERENCE_COLOR_FOREGROUND_SYSTEM_DEFAULT.equals(prop)
1056+
|| PREFERENCE_COLOR_FOREGROUND.equals(prop)) {
1057+
setColors();
1058+
viewer.getTextWidget().redraw();
1059+
} else if (EDITOR_LINE_NUMBER_RULER_COLOR.equals(prop)) {
1060+
lineNumberColumn.setForeground(getLineNumbersColor());
1061+
lineNumberColumn.redraw();
1062+
} else if (EDITOR_CURRENT_LINE_COLOR.equals(prop)) {
1063+
targetLineHighlighter.highlightColor = getTargetLineHighlightColor();
10631064
viewer.getTextWidget().redraw();
10641065
}
10651066
}
10661067

1067-
private RGB getColorFromStore(IPreferenceStore store, String key) {
1068+
private RGB getColorFromStore(String key) {
1069+
var store = EditorsUI.getPreferenceStore();
10681070
RGB rgb = null;
10691071
if (store.contains(key)) {
10701072
if (store.isDefault(key)) {
@@ -1112,7 +1114,7 @@ private void refreshDetails() {
11121114
viewer.setDocument(document);
11131115
viewer.setVisibleRegion(start, contextLenght);
11141116

1115-
targetLinePainter.setTargetLineOffset(item.getOffset() - start);
1117+
targetLineHighlighter.setTargetLineOffset(item.getOffset() - start);
11161118

11171119
// center target line in the displayed area
11181120
IRegion rangeEndLineInfo = document.getLineInformation(Math.min(displayedEndLine, document.getNumberOfLines() - 1));
@@ -1122,11 +1124,12 @@ private void refreshDetails() {
11221124

11231125
var targetLineFirstMatch = getQuery().findFirst(document.get(item.getOffset(), contextLenght - (item.getOffset() - start)));
11241126
int targetLineFirstMatchStart = item.getOffset() + targetLineFirstMatch.getOffset();
1125-
// sets caret position (also highlights that line)
1127+
// sets caret position
11261128
viewer.setSelectedRange(targetLineFirstMatchStart, 0);
11271129
// does horizontal scrolling if necessary to reveal 1st occurrence in target line
11281130
viewer.revealRange(targetLineFirstMatchStart, targetLineFirstMatch.getLength());
11291131

1132+
// above setVisibleRegion() call makes these ranges to be aligned with content of text widget
11301133
StyledString styledString = highlightMatches(document.get(start, contextLenght));
11311134
viewer.getTextWidget().setStyleRanges(styledString.getStyleRanges());
11321135
return;
@@ -1565,66 +1568,28 @@ public QuickTextQuery getQuery() {
15651568
}
15661569

15671570
/**
1568-
* A painter that does 'current line' highlighting (background color) but for single fixed line.
1569-
* <br><br>
1570-
* This class piggybacks on {@link CursorLinePainter} instance already added to viewer's widget, which knows what color
1571-
* to use and does all the necessary repainting. This class only forces the background color to be used also for one
1572-
* fixed line in addition to current line = line where caret is currently located (done by <code>CursorLinePainter</code>).
1573-
* Background color for this fixed line never changes and so <code>CursorLinePainter</code>'s repainting code, although not
1574-
* knowing about this fixed line at all, produces correct visual results - target line always highlighted.
1571+
* A line background listener that provides the color that is used for current line highlighting (what
1572+
* {@link CursorLinePainter} does) but for single fixed line only and does so always regardless of show current
1573+
* line highlighting on/off preference.
15751574
*
15761575
* @see CursorLinePainter
15771576
*/
1578-
private class FixedLinePainter implements IPainter, LineBackgroundListener {
1577+
private static class FixedLineHighlighter implements LineBackgroundListener {
15791578

1580-
private CursorLinePainter cursorLinePainter;
15811579
private int lineOffset;
15821580
private Color highlightColor;
15831581

15841582
public void setTargetLineOffset(int lineOffset) {
15851583
this.lineOffset = lineOffset;
15861584
}
15871585

1588-
// CursorLinePainter adds itself as line LineBackgroundListener lazily
1589-
private boolean cursorLinePainterInstalled() {
1590-
if (cursorLinePainter == null) {
1591-
cursorLinePainter = viewer.getTextWidget().getTypedListeners(ST.LineGetBackground, CursorLinePainter.class).findFirst().orElse(null);
1592-
return cursorLinePainter != null;
1593-
}
1594-
return true;
1595-
}
1596-
15971586
@Override
15981587
public void lineGetBackground(LineBackgroundEvent event) {
1599-
if (cursorLinePainterInstalled()) {
1600-
cursorLinePainter.lineGetBackground(event);
1601-
if (event.lineBackground != null) {
1602-
// piggyback on CursorLinePainter knowing proper color
1603-
highlightColor = event.lineBackground;
1604-
} else if (lineOffset == event.lineOffset) { // target line
1605-
event.lineBackground = highlightColor;
1606-
}
1588+
if (lineOffset == event.lineOffset) {
1589+
event.lineBackground = highlightColor;
16071590
}
16081591
}
16091592

1610-
@Override
1611-
public void dispose() {
1612-
}
1613-
1614-
@Override
1615-
public void paint(int reason) {
1616-
// no custom painting here, cursorLinePainter (also being registered as IPainter) does all the work
1617-
// according to background color set in lineGetBackground()
1618-
}
1619-
1620-
@Override
1621-
public void deactivate(boolean redraw) {
1622-
}
1623-
1624-
@Override
1625-
public void setPositionManager(IPaintPositionManager manager) {
1626-
}
1627-
16281593
}
16291594

16301595
}

0 commit comments

Comments
 (0)