5
5
import java .awt .Font ;
6
6
import java .awt .event .InputEvent ;
7
7
import java .awt .event .KeyEvent ;
8
+ import java .awt .event .MouseWheelEvent ;
8
9
import java .awt .event .MouseWheelListener ;
9
10
import javax .swing .JButton ;
10
11
import javax .swing .JCheckBox ;
13
14
import javax .swing .JTextField ;
14
15
import javax .swing .SwingUtilities ;
15
16
import javax .swing .text .BadLocationException ;
17
+
16
18
import org .fife .ui .rsyntaxtextarea .RSyntaxTextArea ;
17
19
import org .fife .ui .rtextarea .RTextScrollPane ;
18
20
import the .bytecode .club .bytecodeviewer .BytecodeViewer ;
50
52
* @author Konloch
51
53
* @since 6/25/2021
52
54
*/
53
- public class SearchableRSyntaxTextArea extends RSyntaxTextArea
54
- {
55
- private final RTextScrollPane scrollPane = new RTextScrollPane (this );
55
+ public class SearchableRSyntaxTextArea extends RSyntaxTextArea {
56
+
57
+ private RTextScrollPane scrollPane = new RTextScrollPane (this );
56
58
private final JPanel searchPanel = new JPanel (new BorderLayout ());
57
59
private final JTextField searchInput = new JTextField ();
58
60
private final JCheckBox caseSensitiveSearch = new TranslatedJCheckBox ("Match case" , TranslatedComponents .MATCH_CASE );
@@ -62,30 +64,26 @@ public class SearchableRSyntaxTextArea extends RSyntaxTextArea
62
64
private final Color blackScrollBackground = new Color (0x232323 );
63
65
private final Color blackScrollForeground = new Color (0x575859 );
64
66
private Runnable onCtrlS ;
65
-
66
- public SearchableRSyntaxTextArea ()
67
- {
68
- if (Configuration .lafTheme == LAFTheme .HIGH_CONTRAST_DARK )
69
- {
67
+
68
+ public SearchableRSyntaxTextArea () {
69
+ if (Configuration .lafTheme == LAFTheme .HIGH_CONTRAST_DARK ) {
70
70
//this fixes the white border on the jScrollBar panes
71
71
scrollPane .getHorizontalScrollBar ().setBackground (blackScrollBackground );
72
72
scrollPane .getHorizontalScrollBar ().setForeground (blackScrollForeground );
73
73
scrollPane .getVerticalScrollBar ().setBackground (blackScrollBackground );
74
74
scrollPane .getVerticalScrollBar ().setForeground (blackScrollForeground );
75
- }
76
- else if (Configuration .lafTheme .isDark ())
77
- {
75
+ } else if (Configuration .lafTheme .isDark ()) {
78
76
//this fixes the white border on the jScrollBar panes
79
77
scrollPane .getHorizontalScrollBar ().setBackground (darkScrollBackground );
80
78
scrollPane .getHorizontalScrollBar ().setForeground (darkScrollForeground );
81
79
scrollPane .getVerticalScrollBar ().setBackground (darkScrollBackground );
82
80
scrollPane .getVerticalScrollBar ().setForeground (darkScrollForeground );
83
81
}
84
-
82
+
85
83
setAntiAliasingEnabled (true );
86
-
84
+
87
85
scrollPane .setColumnHeaderView (searchPanel );
88
-
86
+
89
87
JButton searchNext = new JButton ();
90
88
JButton searchPrev = new JButton ();
91
89
JPanel buttonPane = new JPanel (new BorderLayout ());
@@ -96,133 +94,121 @@ else if(Configuration.lafTheme.isDark())
96
94
searchPanel .add (buttonPane , BorderLayout .WEST );
97
95
searchPanel .add (searchInput , BorderLayout .CENTER );
98
96
searchPanel .add (caseSensitiveSearch , BorderLayout .EAST );
99
-
97
+
100
98
searchNext .addActionListener (arg0 -> search (searchInput .getText (), true , caseSensitiveSearch .isSelected ()));
101
99
searchPrev .addActionListener (arg0 -> search (searchInput .getText (), false , caseSensitiveSearch .isSelected ()));
102
-
100
+
103
101
searchInput .addKeyListener (new ReleaseKeyListener (keyEvent ->
104
102
{
105
103
if (keyEvent .getKeyCode () == KeyEvent .VK_ENTER )
106
104
search (searchInput .getText (), true , caseSensitiveSearch .isSelected ());
107
105
}));
108
-
106
+
109
107
addKeyListener (new PressKeyListener (keyEvent ->
110
108
{
111
109
if ((keyEvent .getKeyCode () == KeyEvent .VK_F ) && ((keyEvent .getModifiersEx () & KeyEvent .CTRL_DOWN_MASK ) != 0 ))
112
110
searchInput .requestFocus ();
113
-
114
- if (onCtrlS != null && (keyEvent .getKeyCode () == KeyEvent .VK_S ) && ((keyEvent .getModifiersEx () & KeyEvent .CTRL_DOWN_MASK ) != 0 ))
115
- {
111
+
112
+ if (onCtrlS != null && (keyEvent .getKeyCode () == KeyEvent .VK_S ) && ((keyEvent .getModifiersEx () & KeyEvent .CTRL_DOWN_MASK ) != 0 )) {
116
113
onCtrlS .run ();
117
114
return ;
118
115
}
119
-
116
+
120
117
GlobalHotKeys .keyPressed (keyEvent );
121
118
}));
122
-
119
+
123
120
final Font newFont = getFont ().deriveFont ((float ) BytecodeViewer .viewer .getFontSize ());
124
-
121
+
125
122
//set number-bar font
126
123
setFont (newFont );
127
-
128
- SwingUtilities .invokeLater (()-> {
124
+
125
+ SwingUtilities .invokeLater (() -> {
129
126
//attach CTRL + Mouse Wheel Zoom
130
127
attachCtrlMouseWheelZoom ();
131
-
128
+
132
129
//set text font
133
130
setFont (newFont );
134
131
});
135
-
132
+
136
133
}
137
-
138
- public void search (String search , boolean forwardSearchDirection , boolean caseSensitiveSearch )
139
- {
134
+
135
+ public void search (String search , boolean forwardSearchDirection , boolean caseSensitiveSearch ) {
140
136
JTextAreaUtils .search (this , search , forwardSearchDirection , caseSensitiveSearch );
141
137
}
142
-
143
- public void highlight (String pattern , boolean caseSensitiveSearch )
144
- {
138
+
139
+ public void highlight (String pattern , boolean caseSensitiveSearch ) {
145
140
JTextAreaUtils .highlight (this , pattern , caseSensitiveSearch );
146
141
}
147
-
148
- public void attachCtrlMouseWheelZoom ()
149
- {
150
- //get the existing scroll event
151
- MouseWheelListener ogListener = scrollPane .getMouseWheelListeners ().length > 0 ?
152
- scrollPane .getMouseWheelListeners ()[0 ] : null ;
153
-
154
- //remove the existing event
155
- if (ogListener != null )
156
- scrollPane .removeMouseWheelListener (ogListener );
157
-
158
- //add a new event
159
- scrollPane .addMouseWheelListener (e ->
160
- {
161
- if (getText ().isEmpty ())
162
- return ;
163
-
164
- if ((e .getModifiersEx () & InputEvent .CTRL_DOWN_MASK ) != 0 )
165
- {
142
+
143
+ public void attachCtrlMouseWheelZoom () {
144
+ scrollPane .addMouseWheelListener (e -> {
145
+ if (getText ().isEmpty ()) return ;
146
+ if ((e .getModifiersEx () & InputEvent .CTRL_DOWN_MASK ) != 0 ) {
166
147
Font font = getFont ();
167
148
int size = font .getSize ();
168
-
169
- if (e .getWheelRotation () > 0 ) //Up
149
+ if (e .getWheelRotation () > 0 )
170
150
setFont (new Font (font .getName (), font .getStyle (), --size >= 2 ? --size : 2 ));
171
- else //Down
151
+ else
172
152
setFont (new Font (font .getName (), font .getStyle (), ++size ));
173
-
153
+
174
154
e .consume ();
175
155
}
176
- else if (ogListener != null )
177
- {
178
- ogListener .mouseWheelMoved (e );
179
- }
180
156
});
157
+
158
+ scrollPane = new RTextScrollPane () {
159
+ @ Override
160
+ protected void processMouseWheelEvent (MouseWheelEvent event ) {
161
+ if (!isWheelScrollingEnabled ()) {
162
+ if (getParent () != null ) {
163
+ getParent ().dispatchEvent (SwingUtilities .convertMouseEvent (this , event , getParent ()));
164
+ return ;
165
+ }
166
+ }
167
+
168
+ super .processMouseWheelEvent (event );
169
+ }
170
+ };
171
+
172
+ scrollPane .setWheelScrollingEnabled (false );
181
173
}
182
-
174
+
183
175
public String getLineText (int line ) {
184
176
try {
185
177
if (line < getLineCount ()) {
186
178
int start = getLineStartOffset (line );
187
179
int end = getLineEndOffset (line );
188
180
return getText (start , end - start ).trim ();
189
181
}
190
- } catch (BadLocationException ignored ) { }
182
+ } catch (BadLocationException ignored ) {
183
+ }
191
184
return "" ;
192
185
}
193
-
194
- public void setOnCtrlS (Runnable onCtrlS )
195
- {
186
+
187
+ public void setOnCtrlS (Runnable onCtrlS ) {
196
188
this .onCtrlS = onCtrlS ;
197
189
}
198
-
199
- public RTextScrollPane getScrollPane ()
200
- {
190
+
191
+ public RTextScrollPane getScrollPane () {
201
192
return scrollPane ;
202
193
}
203
-
204
- public JPanel getSearchPanel ()
205
- {
194
+
195
+ public JPanel getSearchPanel () {
206
196
return searchPanel ;
207
197
}
208
-
209
- public JTextField getSearchInput ()
210
- {
198
+
199
+ public JTextField getSearchInput () {
211
200
return searchInput ;
212
201
}
213
-
214
- public JCheckBox getCaseSensitiveSearch ()
215
- {
202
+
203
+ public JCheckBox getCaseSensitiveSearch () {
216
204
return caseSensitiveSearch ;
217
205
}
218
-
219
- public JLabel getTitleHeader ()
220
- {
206
+
207
+ public JLabel getTitleHeader () {
221
208
return titleHeader ;
222
209
}
223
-
224
- public Runnable getOnCtrlS ()
225
- {
210
+
211
+ public Runnable getOnCtrlS () {
226
212
return onCtrlS ;
227
213
}
228
214
}
0 commit comments