sclass RSyntaxTextAreaWithSearch is SearchListener, Swingable { JPanel panel; gettable volatile RSyntaxTextArea textArea; CollapsibleSectionPanel csp; FindDialog findDialog; ReplaceDialog replaceDialog; FindToolBar findToolBar; ReplaceToolBar replaceToolBar; StatusBar statusBar; *() { init(); } *(IF1 *wrapStatusLabel) { init(); } *(S text) { init(); setText(text); } *(RSyntaxTextArea *textArea) { init(); } visual panel; void init() swing { rsyntaxTextArea_fixNumPad(); initSearchDialogs(); panel = new JPanel(new BorderLayout); csp = new CollapsibleSectionPanel; panel.add(csp); textArea if null = javaxSyntaxTextArea(); RTextScrollPane sp = new RTextScrollPane(textArea); csp.add(sp); ErrorStrip errorStrip = new(textArea); panel.add(errorStrip, BorderLayout.LINE_END); statusBar = new StatusBar; panel.add(statusBar, BorderLayout.SOUTH); } swappable JComponent wrapStatusLabel(JComponent label) { ret label; } void addItem(Action a, ButtonGroup bg, JMenu menu) { JRadioButtonMenuItem item = new JRadioButtonMenuItem(a); bg.add(item); menu.add(item); } // register keystrokes if we don't have a menu bar void menuLessOperation swing { //print("menuLessOperation"); var mb = createMenuBar(); for (menu : getMenus(mb)) for (menuItem : getMenuItems(menu)) { var ks = menuItem.getAccelerator(); //printVars(+ks, +menuItem); if (ks != null) { var action = menuItem.getAction(); //print("Registering keystroke: " + ks + " => " + action); textArea.getInputMap().put(ks, action); } } } JMenuBar createMenuBar() { JMenuBar mb = new JMenuBar(); JMenu menu = new JMenu("Search"); menu.add(new JMenuItem(new ShowFindDialogAction())); menu.add(new JMenuItem(new ShowReplaceDialogAction())); menu.add(new JMenuItem(new GoToLineAction())); menu.addSeparator(); int ctrl = getToolkit().getMenuShortcutKeyMask(); int shift = InputEvent.SHIFT_MASK; KeyStroke ks = KeyStroke.getKeyStroke(KeyEvent.VK_F, ctrl|shift); Action a = csp.addBottomComponent(ks, findToolBar); a.putValue(Action.NAME, "Show Find Search Bar"); menu.add(new JMenuItem(a)); ks = KeyStroke.getKeyStroke(KeyEvent.VK_H, ctrl|shift); a = csp.addBottomComponent(ks, replaceToolBar); a.putValue(Action.NAME, "Show Replace Search Bar"); menu.add(new JMenuItem(a)); mb.add(menu); ret mb; } public String getSelectedText() { return textArea.getSelectedText(); } /** * Creates our Find and Replace dialogs. */ public void initSearchDialogs() { findDialog = new FindDialog((java.awt.Dialog) null, this); replaceDialog = new ReplaceDialog((java.awt.Dialog) null, this); // This ties the properties of the two dialogs together (match case, // regex, etc.). SearchContext context = findDialog.getSearchContext(); replaceDialog.setSearchContext(context); // Create tool bars and tie their search contexts together also. findToolBar = new FindToolBar(this); findToolBar.setSearchContext(context); replaceToolBar = new ReplaceToolBar(this); replaceToolBar.setSearchContext(context); } /** * Listens for events from our search dialogs and actually does the dirty * work. */ @Override public void searchEvent(SearchEvent e) { SearchEvent.Type type = e.getType(); SearchContext context = e.getSearchContext(); SearchResult result = null; switch (type) { default: // Prevent FindBugs warning later case MARK_ALL: result = SearchEngine.markAll(textArea, context); break; case FIND: result = SearchEngine.find(textArea, context); if (!result.wasFound()) { //Try to wrap the result... Caret c = textArea.getCaret(); int pos = c.getDot(); if (context.getSearchForward()) { c.setDot(0); } else { c.setDot(textArea.getDocument().getLength()); } result = SearchEngine.find(textArea, context); if (!result.wasFound()) { c.setDot(pos); UIManager.getLookAndFeel().provideErrorFeedback(textArea); } } break; case REPLACE: result = SearchEngine.replace(textArea, context); if (!result.wasFound()) { UIManager.getLookAndFeel().provideErrorFeedback(textArea); } break; case REPLACE_ALL: result = SearchEngine.replaceAll(textArea, context); JOptionPane.showMessageDialog(null, result.getCount() + " occurrences replaced."); break; } String text = null; if (result.wasFound()) { text = "Text found; occurrences marked: " + result.getMarkedCount(); } else if (type==SearchEvent.Type.MARK_ALL) { if (result.getMarkedCount()>0) { text = "Occurrences marked: " + result.getMarkedCount(); } else { text = ""; } } else { text = "Text not found"; } setStatus(text); } class GoToLineAction extends AbstractAction { *() { super("Go To Line..."); int c = getToolkit().getMenuShortcutKeyMask(); putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_L, c)); } public void actionPerformed(ActionEvent e) { findDialog.setVisible(false); replaceDialog.setVisible(false); GoToDialog dialog = new GoToDialog((java.awt.Dialog) null); dialog.setMaxLineNumberAllowed(textArea.getLineCount()); showDialogOnSameScreen(dialog, panel); int line = dialog.getLineNumber(); if (line>0) { try { textArea.setCaretPosition(textArea.getLineStartOffset(line-1)); } catch (BadLocationException ble) { // Never happens UIManager.getLookAndFeel().provideErrorFeedback(textArea); ble.printStackTrace(); } } } } class ShowFindDialogAction extends AbstractAction { *() { super("Find..."); int c = getToolkit().getMenuShortcutKeyMask(); putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_F, c)); } public void actionPerformed(ActionEvent e) { replaceDialog.setVisible(false); showDialogOnSameScreen(findDialog, panel); } } class ShowReplaceDialogAction extends AbstractAction { *() { super("Replace..."); int c = getToolkit().getMenuShortcutKeyMask(); putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_H, c)); } public void actionPerformed(ActionEvent e) { findDialog.setVisible(false); showDialogOnSameScreen(replaceDialog, panel); } } class StatusBar extends JPanel { JLabel label; *() { label = new JLabel(" "); setLayout(new BorderLayout()); add(wrapStatusLabel(label), BorderLayout.CENTER); add(new JLabel(new SizeGripIcon()), BorderLayout.EAST); } public void setText(String label) { main setText(this.label, shorten(1000, label)); } } void setStatus(S text) { statusBar.setText(text); } void setText(S text) { main setText(textArea, text); } S getText() { ret main getText(textArea); } void setEditorFont(final Font font) swing { SyntaxScheme ss = textArea.getSyntaxScheme(); ss = (SyntaxScheme) ss.clone(); for (int i = 0; i < ss.getStyleCount(); i++) if (ss.getStyle(i) != null) ss.getStyle(i).font = font; textArea.setSyntaxScheme(ss); textArea.setFont(font); } Font getEditorFont() { ret swing(func -> Font { textArea.getFont() }); } int getEditorFontSize() { ret getEditorFont().getSize(); } void setEditorFontSize(int size) { setEditorFont(deriveFont(getEditorFont(), size)); } RSyntaxDocument getDocument() { ret (RSyntaxDocument) textArea.getDocument(); } Toolkit getToolkit() { ret panel.getToolkit(); } JLabel statusLabel() { ret statusBar.label; } }