Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

277
LINES

< > BotCompany Repo | #1016116 // EditorFrame2 (not actually a frame itself, can be put in JInternalFrame or JFrame)

JavaX fragment (include)

1  
!include once #1015780 // RSyntaxTextArea
2  
3  
import java.awt.Dialog;
4  
5  
/*
6  
 * original author: Robert Futrell
7  
 */
8  
static class EditorFrame2 implements SearchListener {
9  
  CollapsibleSectionPanel csp;
10  
  RSyntaxTextArea textArea;
11  
  FindDialog findDialog;
12  
  ReplaceDialog replaceDialog;
13  
  FindToolBar findToolBar;
14  
  ReplaceToolBar replaceToolBar;
15  
  StatusBar statusBar;
16  
  JPanel panel;
17  
  
18  
  *() { swing {
19  
    rsyntaxTextArea_fixNumPad();
20  
    initSearchDialogs();
21  
  
22  
    panel = new JPanel(new BorderLayout);
23  
    csp = new CollapsibleSectionPanel;
24  
    panel.add(csp);
25  
  
26  
    textArea = javaxSyntaxTextArea();
27  
    RTextScrollPane sp = new RTextScrollPane(textArea);
28  
    csp.add(sp);
29  
  
30  
    ErrorStrip errorStrip = new ErrorStrip(textArea);
31  
    panel.add(errorStrip, BorderLayout.LINE_END);
32  
  
33  
    statusBar = new StatusBar();
34  
    panel.add(statusBar, BorderLayout.SOUTH);
35  
  }}
36  
  
37  
  void prepareFrame(RootPaneContainer f) {
38  
    callOpt(f, 'setJMenuBar, createMenuBar());
39  
  }
40  
  
41  
  void addItem(Action a, ButtonGroup bg, JMenu menu) {
42  
    JRadioButtonMenuItem item = new JRadioButtonMenuItem(a);
43  
    bg.add(item);
44  
    menu.add(item);
45  
  }
46  
  
47  
  JMenuBar createMenuBar() {
48  
    JMenuBar mb = new JMenuBar();
49  
    JMenu menu = new JMenu("Search");
50  
    menu.add(new JMenuItem(new ShowFindDialogAction()));
51  
    menu.add(new JMenuItem(new ShowReplaceDialogAction()));
52  
    menu.add(new JMenuItem(new GoToLineAction()));
53  
    menu.addSeparator();
54  
  
55  
    int ctrl = getToolkit().getMenuShortcutKeyMask();
56  
    int shift = InputEvent.SHIFT_MASK;
57  
    KeyStroke ks = KeyStroke.getKeyStroke(KeyEvent.VK_F, ctrl|shift);
58  
    Action a = csp.addBottomComponent(ks, findToolBar);
59  
    a.putValue(Action.NAME, "Show Find Search Bar");
60  
    menu.add(new JMenuItem(a));
61  
    ks = KeyStroke.getKeyStroke(KeyEvent.VK_H, ctrl|shift);
62  
    a = csp.addBottomComponent(ks, replaceToolBar);
63  
    a.putValue(Action.NAME, "Show Replace Search Bar");
64  
    menu.add(new JMenuItem(a));
65  
  
66  
    mb.add(menu);
67  
    return mb;
68  
  }
69  
  
70  
  public String getSelectedText() {
71  
    return textArea.getSelectedText();
72  
  }
73  
  
74  
  /**
75  
   * Creates our Find and Replace dialogs.
76  
   */
77  
  public void initSearchDialogs() {
78  
  
79  
    findDialog = new FindDialog((Dialog) null, this);
80  
    replaceDialog = new ReplaceDialog((Dialog) null, this);
81  
  
82  
    // This ties the properties of the two dialogs together (match case,
83  
    // regex, etc.).
84  
    SearchContext context = findDialog.getSearchContext();
85  
    replaceDialog.setSearchContext(context);
86  
  
87  
    // Create tool bars and tie their search contexts together also.
88  
    findToolBar = new FindToolBar(this);
89  
    findToolBar.setSearchContext(context);
90  
    replaceToolBar = new ReplaceToolBar(this);
91  
    replaceToolBar.setSearchContext(context);
92  
  
93  
  }
94  
  
95  
  
96  
  /**
97  
   * Listens for events from our search dialogs and actually does the dirty
98  
   * work.
99  
   */
100  
  @Override
101  
  public void searchEvent(SearchEvent e) {
102  
  
103  
    SearchEvent.Type type = e.getType();
104  
    SearchContext context = e.getSearchContext();
105  
    SearchResult result = null;
106  
  
107  
    switch (type) {
108  
      default: // Prevent FindBugs warning later
109  
      case MARK_ALL:
110  
        result = SearchEngine.markAll(textArea, context);
111  
        break;
112  
       case FIND:
113  
            result = SearchEngine.find(textArea, context);
114  
            
115  
            if (!result.wasFound())
116  
            {
117  
                //Try to wrap the result...
118  
                Caret c = textArea.getCaret();
119  
                int pos = c.getDot();
120  
                if (context.getSearchForward())
121  
                {
122  
                    c.setDot(0);
123  
                }
124  
                else
125  
                {
126  
                    c.setDot(textArea.getDocument().getLength());
127  
                }
128  
                result = SearchEngine.find(textArea, context);
129  
                if (!result.wasFound())
130  
                {
131  
                    c.setDot(pos);
132  
                    UIManager.getLookAndFeel().provideErrorFeedback(textArea);
133  
                }
134  
            }
135  
            break;
136  
      case REPLACE:
137  
        result = SearchEngine.replace(textArea, context);
138  
        if (!result.wasFound()) {
139  
          UIManager.getLookAndFeel().provideErrorFeedback(textArea);
140  
        }
141  
        break;
142  
      case REPLACE_ALL:
143  
        result = SearchEngine.replaceAll(textArea, context);
144  
        JOptionPane.showMessageDialog(null, result.getCount() +
145  
            " occurrences replaced.");
146  
        break;
147  
    }
148  
  
149  
    String text = null;
150  
    if (result.wasFound()) {
151  
      text = "Text found; occurrences marked: " + result.getMarkedCount();
152  
    }
153  
    else if (type==SearchEvent.Type.MARK_ALL) {
154  
      if (result.getMarkedCount()>0) {
155  
        text = "Occurrences marked: " + result.getMarkedCount();
156  
      }
157  
      else {
158  
        text = "";
159  
      }
160  
    }
161  
    else {
162  
      text = "Text not found";
163  
    }
164  
    statusBar.setText(text);
165  
  }
166  
  
167  
  class GoToLineAction extends AbstractAction {
168  
    *() {
169  
      super("Go To Line...");
170  
      int c = getToolkit().getMenuShortcutKeyMask();
171  
      putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_L, c));
172  
    }
173  
  
174  
    public void actionPerformed(ActionEvent e) {
175  
      findDialog.setVisible(false);
176  
      replaceDialog.setVisible(false);
177  
      GoToDialog dialog = new GoToDialog((Dialog) null);
178  
      dialog.setMaxLineNumberAllowed(textArea.getLineCount());
179  
      showDialogOnSameScreen(dialog, panel);
180  
      int line = dialog.getLineNumber();
181  
      if (line>0) {
182  
        try {
183  
          textArea.setCaretPosition(textArea.getLineStartOffset(line-1));
184  
        } catch (BadLocationException ble) { // Never happens
185  
          UIManager.getLookAndFeel().provideErrorFeedback(textArea);
186  
          ble.printStackTrace();
187  
        }
188  
      }
189  
    }
190  
  }
191  
  
192  
  class ShowFindDialogAction extends AbstractAction {
193  
    
194  
    *() {
195  
      super("Find...");
196  
      int c = getToolkit().getMenuShortcutKeyMask();
197  
      putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_F, c));
198  
    }
199  
  
200  
    public void actionPerformed(ActionEvent e) {
201  
      replaceDialog.setVisible(false);
202  
      showDialogOnSameScreen(findDialog, panel);
203  
    }
204  
  }
205  
  
206  
  class ShowReplaceDialogAction extends AbstractAction {
207  
    *() {
208  
      super("Replace...");
209  
      int c = getToolkit().getMenuShortcutKeyMask();
210  
      putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_H, c));
211  
    }
212  
  
213  
    public void actionPerformed(ActionEvent e) {
214  
      findDialog.setVisible(false);
215  
      showDialogOnSameScreen(replaceDialog, panel);
216  
    }
217  
  }
218  
  
219  
  static class StatusBar extends JPanel {
220  
    JLabel label;
221  
  
222  
    *() {
223  
      label = new JLabel("Ready");
224  
      setLayout(new BorderLayout());
225  
      add(label, BorderLayout.LINE_START);
226  
      add(new JLabel(new SizeGripIcon()), BorderLayout.LINE_END);
227  
    }
228  
  
229  
    public void setText(String label) {
230  
      main.setText(this.label, label);
231  
    }
232  
  }
233  
  
234  
  void setStatus(S text) {
235  
    statusBar.setText(text);
236  
  }
237  
  
238  
  void setText(S text) {
239  
    main.setText(textArea, text);
240  
  }
241  
  
242  
  S getText() {
243  
    ret main.getText(textArea);
244  
  }
245  
   
246  
  void _setFont(final Font font) swing {
247  
    SyntaxScheme ss = textArea.getSyntaxScheme();
248  
    ss = (SyntaxScheme) ss.clone();
249  
    for (int i = 0; i < ss.getStyleCount(); i++)
250  
      if (ss.getStyle(i) != null)
251  
        ss.getStyle(i).font = font;
252  
     textArea.setSyntaxScheme(ss);
253  
     textArea.setFont(font);
254  
  }
255  
  
256  
  Font _getFont() {
257  
    ret swing(func -> Font { textArea.getFont() });
258  
  }
259  
  
260  
  int _getFontSize() {
261  
    ret _getFont().getSize();
262  
  }
263  
  
264  
  void _setFontSize(int size) {
265  
    _setFont(deriveFont(_getFont(), size));
266  
  }
267  
  
268  
  RSyntaxTextArea getTextArea() {
269  
    ret textArea;
270  
  }
271  
  
272  
  RSyntaxDocument getDocument() {
273  
    ret (RSyntaxDocument) textArea.getDocument();
274  
  }
275  
  
276  
  Toolkit getToolkit() { ret panel.getToolkit(); }
277  
}

Author comment

Began life as a copy of #1004356

download  show line numbers  debug dex  old transpilations   

Travelled to 14 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, irmadwmeruwu, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1016116
Snippet name: EditorFrame2 (not actually a frame itself, can be put in JInternalFrame or JFrame)
Eternal ID of this version: #1016116/16
Text MD5: 11505b9f5403da8a7398c641cea95c6b
Author: stefan
Category: javax / gui
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-01-23 21:05:25
Source code size: 7984 bytes / 277 lines
Pitched / IR pitched: No / No
Views / Downloads: 442 / 1021
Version history: 15 change(s)
Referenced in: [show references]