Transpiled version (9358L) is out of date.
1 | sclass RSyntaxTextAreaWithSearch is SearchListener, Swingable { |
2 | JPanel panel; |
3 | gettable volatile RSyntaxTextArea textArea; |
4 | CollapsibleSectionPanel csp; |
5 | |
6 | FindDialog findDialog; |
7 | ReplaceDialog replaceDialog; |
8 | |
9 | FindToolBar findToolBar; |
10 | ReplaceToolBar replaceToolBar; |
11 | |
12 | StatusBar statusBar; |
13 | |
14 | *() { init(); } |
15 | *(IF1<JComponent> *wrapStatusLabel) { init(); } |
16 | *(S text) { init(); setText(text); } |
17 | *(RSyntaxTextArea *textArea) { init(); } |
18 | |
19 | visual panel; |
20 | |
21 | void init() swing { |
22 | rsyntaxTextArea_fixNumPad(); |
23 | initSearchDialogs(); |
24 | |
25 | panel = new JPanel(new BorderLayout); |
26 | csp = new CollapsibleSectionPanel; |
27 | panel.add(csp); |
28 | |
29 | textArea if null = javaxSyntaxTextArea(); |
30 | RTextScrollPane sp = new RTextScrollPane(textArea); |
31 | csp.add(sp); |
32 | |
33 | ErrorStrip errorStrip = new(textArea); |
34 | panel.add(errorStrip, BorderLayout.LINE_END); |
35 | |
36 | statusBar = new StatusBar; |
37 | panel.add(statusBar, BorderLayout.SOUTH); |
38 | } |
39 | |
40 | swappable JComponent wrapStatusLabel(JComponent label) { ret label; } |
41 | |
42 | void addItem(Action a, ButtonGroup bg, JMenu menu) { |
43 | JRadioButtonMenuItem item = new JRadioButtonMenuItem(a); |
44 | bg.add(item); |
45 | menu.add(item); |
46 | } |
47 | |
48 | // register keystrokes if we don't have a menu bar |
49 | void menuLessOperation swing { |
50 | //print("menuLessOperation"); |
51 | var mb = createMenuBar(); |
52 | for (menu : getMenus(mb)) |
53 | for (menuItem : getMenuItems(menu)) { |
54 | var ks = menuItem.getAccelerator(); |
55 | //printVars(+ks, +menuItem); |
56 | if (ks != null) { |
57 | var action = menuItem.getAction(); |
58 | //print("Registering keystroke: " + ks + " => " + action); |
59 | textArea.getInputMap().put(ks, action); |
60 | } |
61 | } |
62 | } |
63 | |
64 | JMenuBar createMenuBar() { |
65 | JMenuBar mb = new JMenuBar(); |
66 | JMenu menu = new JMenu("Search"); |
67 | menu.add(new JMenuItem(new ShowFindDialogAction())); |
68 | menu.add(new JMenuItem(new ShowReplaceDialogAction())); |
69 | menu.add(new JMenuItem(new GoToLineAction())); |
70 | menu.addSeparator(); |
71 | |
72 | int ctrl = getToolkit().getMenuShortcutKeyMask(); |
73 | int shift = InputEvent.SHIFT_MASK; |
74 | KeyStroke ks = KeyStroke.getKeyStroke(KeyEvent.VK_F, ctrl|shift); |
75 | Action a = csp.addBottomComponent(ks, findToolBar); |
76 | a.putValue(Action.NAME, "Show Find Search Bar"); |
77 | menu.add(new JMenuItem(a)); |
78 | ks = KeyStroke.getKeyStroke(KeyEvent.VK_H, ctrl|shift); |
79 | a = csp.addBottomComponent(ks, replaceToolBar); |
80 | a.putValue(Action.NAME, "Show Replace Search Bar"); |
81 | menu.add(new JMenuItem(a)); |
82 | |
83 | mb.add(menu); |
84 | ret mb; |
85 | } |
86 | |
87 | public String getSelectedText() { |
88 | return textArea.getSelectedText(); |
89 | } |
90 | |
91 | /** |
92 | * Creates our Find and Replace dialogs. |
93 | */ |
94 | public void initSearchDialogs() { |
95 | |
96 | findDialog = new FindDialog((java.awt.Dialog) null, this); |
97 | replaceDialog = new ReplaceDialog((java.awt.Dialog) null, this); |
98 | |
99 | // This ties the properties of the two dialogs together (match case, |
100 | // regex, etc.). |
101 | SearchContext context = findDialog.getSearchContext(); |
102 | replaceDialog.setSearchContext(context); |
103 | |
104 | // Create tool bars and tie their search contexts together also. |
105 | findToolBar = new FindToolBar(this); |
106 | findToolBar.setSearchContext(context); |
107 | replaceToolBar = new ReplaceToolBar(this); |
108 | replaceToolBar.setSearchContext(context); |
109 | |
110 | } |
111 | |
112 | |
113 | /** |
114 | * Listens for events from our search dialogs and actually does the dirty |
115 | * work. |
116 | */ |
117 | @Override |
118 | public void searchEvent(SearchEvent e) { |
119 | |
120 | SearchEvent.Type type = e.getType(); |
121 | SearchContext context = e.getSearchContext(); |
122 | SearchResult result = null; |
123 | |
124 | switch (type) { |
125 | default: // Prevent FindBugs warning later |
126 | case MARK_ALL: |
127 | result = SearchEngine.markAll(textArea, context); |
128 | break; |
129 | case FIND: |
130 | result = SearchEngine.find(textArea, context); |
131 | |
132 | if (!result.wasFound()) |
133 | { |
134 | //Try to wrap the result... |
135 | Caret c = textArea.getCaret(); |
136 | int pos = c.getDot(); |
137 | if (context.getSearchForward()) |
138 | { |
139 | c.setDot(0); |
140 | } |
141 | else |
142 | { |
143 | c.setDot(textArea.getDocument().getLength()); |
144 | } |
145 | result = SearchEngine.find(textArea, context); |
146 | if (!result.wasFound()) |
147 | { |
148 | c.setDot(pos); |
149 | UIManager.getLookAndFeel().provideErrorFeedback(textArea); |
150 | } |
151 | } |
152 | break; |
153 | case REPLACE: |
154 | result = SearchEngine.replace(textArea, context); |
155 | if (!result.wasFound()) { |
156 | UIManager.getLookAndFeel().provideErrorFeedback(textArea); |
157 | } |
158 | break; |
159 | case REPLACE_ALL: |
160 | result = SearchEngine.replaceAll(textArea, context); |
161 | JOptionPane.showMessageDialog(null, result.getCount() + |
162 | " occurrences replaced."); |
163 | break; |
164 | } |
165 | |
166 | String text = null; |
167 | if (result.wasFound()) { |
168 | text = "Text found; occurrences marked: " + result.getMarkedCount(); |
169 | } |
170 | else if (type==SearchEvent.Type.MARK_ALL) { |
171 | if (result.getMarkedCount()>0) { |
172 | text = "Occurrences marked: " + result.getMarkedCount(); |
173 | } |
174 | else { |
175 | text = ""; |
176 | } |
177 | } |
178 | else { |
179 | text = "Text not found"; |
180 | } |
181 | setStatus(text); |
182 | } |
183 | |
184 | class GoToLineAction extends AbstractAction { |
185 | *() { |
186 | super("Go To Line..."); |
187 | int c = getToolkit().getMenuShortcutKeyMask(); |
188 | putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_L, c)); |
189 | } |
190 | |
191 | public void actionPerformed(ActionEvent e) { |
192 | findDialog.setVisible(false); |
193 | replaceDialog.setVisible(false); |
194 | GoToDialog dialog = new GoToDialog((java.awt.Dialog) null); |
195 | dialog.setMaxLineNumberAllowed(textArea.getLineCount()); |
196 | showDialogOnSameScreen(dialog, panel); |
197 | int line = dialog.getLineNumber(); |
198 | if (line>0) { |
199 | try { |
200 | textArea.setCaretPosition(textArea.getLineStartOffset(line-1)); |
201 | } catch (BadLocationException ble) { // Never happens |
202 | UIManager.getLookAndFeel().provideErrorFeedback(textArea); |
203 | ble.printStackTrace(); |
204 | } |
205 | } |
206 | } |
207 | } |
208 | |
209 | class ShowFindDialogAction extends AbstractAction { |
210 | |
211 | *() { |
212 | super("Find..."); |
213 | int c = getToolkit().getMenuShortcutKeyMask(); |
214 | putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_F, c)); |
215 | } |
216 | |
217 | public void actionPerformed(ActionEvent e) { |
218 | replaceDialog.setVisible(false); |
219 | showDialogOnSameScreen(findDialog, panel); |
220 | } |
221 | } |
222 | |
223 | class ShowReplaceDialogAction extends AbstractAction { |
224 | *() { |
225 | super("Replace..."); |
226 | int c = getToolkit().getMenuShortcutKeyMask(); |
227 | putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_H, c)); |
228 | } |
229 | |
230 | public void actionPerformed(ActionEvent e) { |
231 | findDialog.setVisible(false); |
232 | showDialogOnSameScreen(replaceDialog, panel); |
233 | } |
234 | } |
235 | |
236 | class StatusBar extends JPanel { |
237 | JLabel label; |
238 | |
239 | *() { |
240 | label = new JLabel(" "); |
241 | setLayout(new BorderLayout()); |
242 | add(wrapStatusLabel(label), BorderLayout.CENTER); |
243 | add(new JLabel(new SizeGripIcon()), BorderLayout.EAST); |
244 | } |
245 | |
246 | public void setText(String label) { |
247 | main setText(this.label, shorten(1000, label)); |
248 | } |
249 | } |
250 | |
251 | void setStatus(S text) { |
252 | statusBar.setText(text); |
253 | } |
254 | |
255 | void setText(S text) { |
256 | main setText(textArea, text); |
257 | } |
258 | |
259 | S getText() { |
260 | ret main getText(textArea); |
261 | } |
262 | |
263 | void setEditorFont(final Font font) swing { |
264 | SyntaxScheme ss = textArea.getSyntaxScheme(); |
265 | ss = (SyntaxScheme) ss.clone(); |
266 | for (int i = 0; i < ss.getStyleCount(); i++) |
267 | if (ss.getStyle(i) != null) |
268 | ss.getStyle(i).font = font; |
269 | textArea.setSyntaxScheme(ss); |
270 | textArea.setFont(font); |
271 | } |
272 | |
273 | Font getEditorFont() { |
274 | ret swing(func -> Font { textArea.getFont() }); |
275 | } |
276 | |
277 | int getEditorFontSize() { |
278 | ret getEditorFont().getSize(); |
279 | } |
280 | |
281 | void setEditorFontSize(int size) { |
282 | setEditorFont(deriveFont(getEditorFont(), size)); |
283 | } |
284 | |
285 | RSyntaxDocument getDocument() { |
286 | ret (RSyntaxDocument) textArea.getDocument(); |
287 | } |
288 | |
289 | Toolkit getToolkit() { ret panel.getToolkit(); } |
290 | |
291 | JLabel statusLabel() { ret statusBar.label; } |
292 | } |
Began life as a copy of #1016116
download show line numbers debug dex old transpilations
Travelled to 4 computer(s): bhatertpkbcr, ekrmjmnbrukm, mowyntqkapby, mqqgnosmbjvj
No comments. add comment
Snippet ID: | #1033997 |
Snippet name: | RSyntaxTextAreaWithSearch |
Eternal ID of this version: | #1033997/31 |
Text MD5: | efdc971b4d3fd2644ca4982a274abc03 |
Author: | stefan |
Category: | javax / gui |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2022-12-11 17:54:41 |
Source code size: | 8652 bytes / 292 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 255 / 493 |
Version history: | 30 change(s) |
Referenced in: | [show references] |