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

273
LINES

< > BotCompany Repo | #1004356 // EditorFrame (syntax-enhanced swing editor)

JavaX fragment (include)

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

Author comment

Began life as a copy of #1004353

download  show line numbers  debug dex  old transpilations   

Travelled to 17 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, ddnzoavkxhuk, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, sawdedvomwva, tslmcundralx, tvejysmllsmz, vouqrxazstgt, wtqryiryparv

No comments. add comment

Snippet ID: #1004356
Snippet name: EditorFrame (syntax-enhanced swing editor)
Eternal ID of this version: #1004356/20
Text MD5: 66fbe262e03a7cc276b4b8e736377858
Author: stefan
Category: javax / gui
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2018-05-27 18:21:18
Source code size: 7277 bytes / 273 lines
Pitched / IR pitched: No / No
Views / Downloads: 737 / 1367
Version history: 19 change(s)
Referenced in: [show references]