21
21
package org .eclipse .text .quicksearch .internal .ui ;
22
22
23
23
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 ;
24
31
25
32
import java .util .ArrayList ;
26
33
import java .util .Arrays ;
46
53
import org .eclipse .jface .dialogs .IDialogConstants ;
47
54
import org .eclipse .jface .dialogs .IDialogSettings ;
48
55
import org .eclipse .jface .layout .GridDataFactory ;
49
- import org .eclipse .jface .preference .IPreferenceStore ;
50
56
import org .eclipse .jface .preference .PreferenceConverter ;
51
57
import org .eclipse .jface .resource .JFaceResources ;
52
58
import org .eclipse .jface .text .BadLocationException ;
53
59
import org .eclipse .jface .text .CursorLinePainter ;
54
60
import org .eclipse .jface .text .IDocument ;
55
- import org .eclipse .jface .text .IPaintPositionManager ;
56
- import org .eclipse .jface .text .IPainter ;
57
61
import org .eclipse .jface .text .IRegion ;
58
62
import org .eclipse .jface .text .source .CompositeRuler ;
59
63
import org .eclipse .jface .text .source .ISharedTextColors ;
80
84
import org .eclipse .swt .accessibility .AccessibleEvent ;
81
85
import org .eclipse .swt .custom .LineBackgroundEvent ;
82
86
import org .eclipse .swt .custom .LineBackgroundListener ;
83
- import org .eclipse .swt .custom .ST ;
84
87
import org .eclipse .swt .custom .SashForm ;
85
88
import org .eclipse .swt .custom .StyleRange ;
86
89
import org .eclipse .swt .custom .StyledText ;
135
138
import org .eclipse .ui .internal .WorkbenchImages ;
136
139
import org .eclipse .ui .internal .ide .IDEWorkbenchPlugin ;
137
140
import org .eclipse .ui .progress .UIJob ;
138
- import org .eclipse .ui .texteditor .AbstractDecoratedTextEditorPreferenceConstants ;
139
- import org .eclipse .ui .texteditor .AbstractTextEditor ;
140
141
import org .eclipse .ui .texteditor .SourceViewerDecorationSupport ;
141
142
import org .osgi .framework .FrameworkUtil ;
142
143
@@ -382,7 +383,7 @@ public void update(ViewerCell cell) {
382
383
383
384
private SourceViewer viewer ;
384
385
private LineNumberRulerColumn lineNumberColumn ;
385
- private FixedLinePainter targetLinePainter ;
386
+ private FixedLineHighlighter targetLineHighlighter ;
386
387
private final IPropertyChangeListener preferenceChangeListener = this ::handlePropertyChangeEvent ;
387
388
388
389
private DocumentFetcher documents ;
@@ -976,11 +977,7 @@ private void createDetailsArea(Composite parent) {
976
977
977
978
viewer = new SourceViewer (viewerParent , new CompositeRuler (), SWT .H_SCROLL | SWT .V_SCROLL | SWT .READ_ONLY );
978
979
viewer .getTextWidget ().setFont (JFaceResources .getFont (TEXT_FONT ));
979
-
980
- lineNumberColumn = new LineNumberRulerColumn ();
981
- viewer .addVerticalRulerColumn (lineNumberColumn );
982
- setColors (false );
983
- createLinesHighlightingDecorations ();
980
+ createViewerDecorations ();
984
981
985
982
list .addSelectionChangedListener (event -> refreshDetails ());
986
983
@@ -992,16 +989,15 @@ public void controlResized(ControlEvent e) {
992
989
});
993
990
}
994
991
995
- private void setColors (boolean refresh ) {
992
+ private void setColors () {
996
993
RGB background = null ;
997
994
RGB foreground = null ;
998
995
var textWidget = viewer .getTextWidget ();
999
- var preferenceStore = EditorsUI .getPreferenceStore ();
1000
996
ISharedTextColors sharedColors = EditorsUI .getSharedTextColors ();
1001
997
1002
- var isUsingSystemBackground = preferenceStore . getBoolean ( AbstractTextEditor . PREFERENCE_COLOR_BACKGROUND_SYSTEM_DEFAULT );
998
+ var isUsingSystemBackground = EditorsUI . getPreferenceStore (). getBoolean ( PREFERENCE_COLOR_BACKGROUND_SYSTEM_DEFAULT );
1003
999
if (!isUsingSystemBackground ) {
1004
- background = getColorFromStore (preferenceStore , AbstractTextEditor . PREFERENCE_COLOR_BACKGROUND );
1000
+ background = getColorFromStore (PREFERENCE_COLOR_BACKGROUND );
1005
1001
}
1006
1002
if (background != null ) {
1007
1003
var color = sharedColors .getColor (background );
@@ -1012,59 +1008,65 @@ private void setColors(boolean refresh) {
1012
1008
lineNumberColumn .setBackground (null );
1013
1009
}
1014
1010
1015
- var isUsingSystemForeground = preferenceStore . getBoolean ( AbstractTextEditor . PREFERENCE_COLOR_FOREGROUND_SYSTEM_DEFAULT );
1011
+ var isUsingSystemForeground = EditorsUI . getPreferenceStore (). getBoolean ( PREFERENCE_COLOR_FOREGROUND_SYSTEM_DEFAULT );
1016
1012
if (!isUsingSystemForeground ) {
1017
- foreground = getColorFromStore (preferenceStore , AbstractTextEditor . PREFERENCE_COLOR_FOREGROUND );
1013
+ foreground = getColorFromStore (PREFERENCE_COLOR_FOREGROUND );
1018
1014
}
1019
1015
if (foreground != null ) {
1020
1016
textWidget .setForeground (sharedColors .getColor (foreground ));
1021
1017
} else {
1022
1018
textWidget .setForeground (null );
1023
1019
}
1020
+ }
1024
1021
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
+ }
1030
1026
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 );
1035
1031
}
1036
1032
1033
+ private void createViewerDecorations () {
1034
+ lineNumberColumn = new LineNumberRulerColumn ();
1035
+ lineNumberColumn .setForeground (getLineNumbersColor ());
1036
+ viewer .addVerticalRulerColumn (lineNumberColumn );
1037
1037
1038
- private void createLinesHighlightingDecorations () {
1039
1038
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 );
1041
1040
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 ();
1045
1046
}
1046
1047
1047
1048
private void handlePropertyChangeEvent (PropertyChangeEvent event ) {
1048
1049
if (viewer == null ) {
1049
1050
return ;
1050
1051
}
1051
1052
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 () ;
1063
1064
viewer .getTextWidget ().redraw ();
1064
1065
}
1065
1066
}
1066
1067
1067
- private RGB getColorFromStore (IPreferenceStore store , String key ) {
1068
+ private RGB getColorFromStore (String key ) {
1069
+ var store = EditorsUI .getPreferenceStore ();
1068
1070
RGB rgb = null ;
1069
1071
if (store .contains (key )) {
1070
1072
if (store .isDefault (key )) {
@@ -1112,7 +1114,7 @@ private void refreshDetails() {
1112
1114
viewer .setDocument (document );
1113
1115
viewer .setVisibleRegion (start , contextLenght );
1114
1116
1115
- targetLinePainter .setTargetLineOffset (item .getOffset () - start );
1117
+ targetLineHighlighter .setTargetLineOffset (item .getOffset () - start );
1116
1118
1117
1119
// center target line in the displayed area
1118
1120
IRegion rangeEndLineInfo = document .getLineInformation (Math .min (displayedEndLine , document .getNumberOfLines () - 1 ));
@@ -1122,11 +1124,12 @@ private void refreshDetails() {
1122
1124
1123
1125
var targetLineFirstMatch = getQuery ().findFirst (document .get (item .getOffset (), contextLenght - (item .getOffset () - start )));
1124
1126
int targetLineFirstMatchStart = item .getOffset () + targetLineFirstMatch .getOffset ();
1125
- // sets caret position (also highlights that line)
1127
+ // sets caret position
1126
1128
viewer .setSelectedRange (targetLineFirstMatchStart , 0 );
1127
1129
// does horizontal scrolling if necessary to reveal 1st occurrence in target line
1128
1130
viewer .revealRange (targetLineFirstMatchStart , targetLineFirstMatch .getLength ());
1129
1131
1132
+ // above setVisibleRegion() call makes these ranges to be aligned with content of text widget
1130
1133
StyledString styledString = highlightMatches (document .get (start , contextLenght ));
1131
1134
viewer .getTextWidget ().setStyleRanges (styledString .getStyleRanges ());
1132
1135
return ;
@@ -1565,66 +1568,28 @@ public QuickTextQuery getQuery() {
1565
1568
}
1566
1569
1567
1570
/**
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.
1575
1574
*
1576
1575
* @see CursorLinePainter
1577
1576
*/
1578
- private class FixedLinePainter implements IPainter , LineBackgroundListener {
1577
+ private static class FixedLineHighlighter implements LineBackgroundListener {
1579
1578
1580
- private CursorLinePainter cursorLinePainter ;
1581
1579
private int lineOffset ;
1582
1580
private Color highlightColor ;
1583
1581
1584
1582
public void setTargetLineOffset (int lineOffset ) {
1585
1583
this .lineOffset = lineOffset ;
1586
1584
}
1587
1585
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
-
1597
1586
@ Override
1598
1587
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 ;
1607
1590
}
1608
1591
}
1609
1592
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
-
1628
1593
}
1629
1594
1630
1595
}
0 commit comments