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

345
LINES

< > BotCompany Repo | #1013049 // Test RSTAUI (RSyntaxTextArea + dialogs) - new version (OK)

JavaX source code [tags: use-pretranspiled] - run with: x30.jar

Uses 6678K of libraries. Click here for Pure Java version (6085L/40K/146K).

1  
!7
2  
3  
lib 1013041 // RSyntaxTextArea
4  
lib 1013047 // AutoComplete
5  
lib 1013048 // RSTAUI
6  
7  
import javax.swing.UIManager.LookAndFeelInfo;
8  
9  
import org.fife.rsta.ui.CollapsibleSectionPanel;
10  
//import org.fife.rsta.ui.DocumentMap;
11  
import org.fife.rsta.ui.GoToDialog;
12  
import org.fife.rsta.ui.SizeGripIcon;
13  
import org.fife.rsta.ui.search.FindDialog;
14  
import org.fife.rsta.ui.search.ReplaceDialog;
15  
import org.fife.rsta.ui.search.ReplaceToolBar;
16  
import org.fife.rsta.ui.search.SearchEvent;
17  
import org.fife.rsta.ui.search.SearchListener;
18  
import org.fife.rsta.ui.search.FindToolBar;
19  
import org.fife.ui.rsyntaxtextarea.ErrorStrip;
20  
import org.fife.ui.rsyntaxtextarea.RSyntaxTextArea;
21  
import org.fife.ui.rsyntaxtextarea.SyntaxConstants;
22  
import org.fife.ui.rtextarea.RTextScrollPane;
23  
import org.fife.ui.rtextarea.SearchContext;
24  
import org.fife.ui.rtextarea.SearchEngine;
25  
import org.fife.ui.rtextarea.SearchResult;
26  
27  
p-substance {
28  
  rsyntaxTextArea_fixNumPad();
29  
  new RSTAUIDemoApp().setVisible(true);
30  
}
31  
32  
33  
/**
34  
 * An application that demonstrates use of the RSTAUI project.  Please don't
35  
 * take this as good application design; it's just a simple example.<p>
36  
 *
37  
 * Unlike the library itself, this class is public domain.
38  
 *
39  
 * @author Robert Futrell
40  
 * @version 1.0
41  
 */
42  
sclass RSTAUIDemoApp extends JFrame implements SearchListener {
43  
44  
	private CollapsibleSectionPanel csp;
45  
	private RSyntaxTextArea textArea;
46  
	private FindDialog findDialog;
47  
	private ReplaceDialog replaceDialog;
48  
	private FindToolBar findToolBar;
49  
	private ReplaceToolBar replaceToolBar;
50  
	private StatusBar statusBar;
51  
52  
53  
	public RSTAUIDemoApp() {
54  
55  
		initSearchDialogs();
56  
57  
		JPanel contentPane = new JPanel(new BorderLayout());
58  
		setContentPane(contentPane);
59  
		csp = new CollapsibleSectionPanel();
60  
		contentPane.add(csp);
61  
62  
		setJMenuBar(createMenuBar());
63  
		
64  
		textArea = new RSyntaxTextArea(25, 80);
65  
		textArea.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_JAVA);
66  
		textArea.setCodeFoldingEnabled(true);
67  
		textArea.setMarkOccurrences(true);
68  
		RTextScrollPane sp = new RTextScrollPane(textArea);
69  
		csp.add(sp);
70  
71  
		ErrorStrip errorStrip = new ErrorStrip(textArea);
72  
		contentPane.add(errorStrip, BorderLayout.LINE_END);
73  
//org.fife.rsta.ui.DocumentMap docMap = new org.fife.rsta.ui.DocumentMap(textArea);
74  
//contentPane.add(docMap, BorderLayout.LINE_END);
75  
76  
		statusBar = new StatusBar();
77  
		contentPane.add(statusBar, BorderLayout.SOUTH);
78  
79  
		setTitle("RSTAUI Demo Application");
80  
		setDefaultCloseOperation(EXIT_ON_CLOSE);
81  
		pack();
82  
		setLocationRelativeTo(null);
83  
84  
	}
85  
86  
87  
	private void addItem(Action a, ButtonGroup bg, JMenu menu) {
88  
		JRadioButtonMenuItem item = new JRadioButtonMenuItem(a);
89  
		bg.add(item);
90  
		menu.add(item);
91  
	}
92  
93  
94  
	private JMenuBar createMenuBar() {
95  
96  
		JMenuBar mb = new JMenuBar();
97  
		JMenu menu = new JMenu("Search");
98  
		menu.add(new JMenuItem(new ShowFindDialogAction()));
99  
		menu.add(new JMenuItem(new ShowReplaceDialogAction()));
100  
		menu.add(new JMenuItem(new GoToLineAction()));
101  
		menu.addSeparator();
102  
103  
		int ctrl = getToolkit().getMenuShortcutKeyMask();
104  
		int shift = InputEvent.SHIFT_MASK;
105  
		KeyStroke ks = KeyStroke.getKeyStroke(KeyEvent.VK_F, ctrl|shift);
106  
		Action a = csp.addBottomComponent(ks, findToolBar);
107  
		a.putValue(Action.NAME, "Show Find Search Bar");
108  
		menu.add(new JMenuItem(a));
109  
		ks = KeyStroke.getKeyStroke(KeyEvent.VK_H, ctrl|shift);
110  
		a = csp.addBottomComponent(ks, replaceToolBar);
111  
		a.putValue(Action.NAME, "Show Replace Search Bar");
112  
		menu.add(new JMenuItem(a));
113  
114  
		mb.add(menu);
115  
116  
		menu = new JMenu("LookAndFeel");
117  
		ButtonGroup bg = new ButtonGroup();
118  
		LookAndFeelInfo[] infos = UIManager.getInstalledLookAndFeels();
119  
		for (int i=0; i<infos.length; i++) {
120  
			addItem(new LookAndFeelAction(infos[i]), bg, menu);
121  
		}
122  
		mb.add(menu);
123  
124  
		return mb;
125  
126  
	}
127  
128  
129  
	@Override
130  
	public String getSelectedText() {
131  
		return textArea.getSelectedText();
132  
	}
133  
134  
135  
	/**
136  
	 * Creates our Find and Replace dialogs.
137  
	 */
138  
	public void initSearchDialogs() {
139  
140  
		findDialog = new FindDialog(this, this);
141  
		replaceDialog = new ReplaceDialog(this, this);
142  
143  
		// This ties the properties of the two dialogs together (match case,
144  
		// regex, etc.).
145  
		SearchContext context = findDialog.getSearchContext();
146  
		replaceDialog.setSearchContext(context);
147  
148  
		// Create tool bars and tie their search contexts together also.
149  
		findToolBar = new FindToolBar(this);
150  
		findToolBar.setSearchContext(context);
151  
		replaceToolBar = new ReplaceToolBar(this);
152  
		replaceToolBar.setSearchContext(context);
153  
154  
	}
155  
156  
157  
	/**
158  
	 * Listens for events from our search dialogs and actually does the dirty
159  
	 * work.
160  
	 */
161  
	@Override
162  
	public void searchEvent(SearchEvent e) {
163  
164  
		SearchEvent.Type type = e.getType();
165  
		SearchContext context = e.getSearchContext();
166  
		SearchResult result = null;
167  
168  
		switch (type) {
169  
			default: // Prevent FindBugs warning later
170  
			case MARK_ALL:
171  
				result = SearchEngine.markAll(textArea, context);
172  
				break;
173  
			case FIND:
174  
				result = SearchEngine.find(textArea, context);
175  
				if (!result.wasFound()) {
176  
					UIManager.getLookAndFeel().provideErrorFeedback(textArea);
177  
				}
178  
				break;
179  
			case REPLACE:
180  
				result = SearchEngine.replace(textArea, context);
181  
				if (!result.wasFound()) {
182  
					UIManager.getLookAndFeel().provideErrorFeedback(textArea);
183  
				}
184  
				break;
185  
			case REPLACE_ALL:
186  
				result = SearchEngine.replaceAll(textArea, context);
187  
				JOptionPane.showMessageDialog(null, result.getCount() +
188  
						" occurrences replaced.");
189  
				break;
190  
		}
191  
192  
		String text = null;
193  
		if (result.wasFound()) {
194  
			text = "Text found; occurrences marked: " + result.getMarkedCount();
195  
		}
196  
		else if (type==SearchEvent.Type.MARK_ALL) {
197  
			if (result.getMarkedCount()>0) {
198  
				text = "Occurrences marked: " + result.getMarkedCount();
199  
			}
200  
			else {
201  
				text = "";
202  
			}
203  
		}
204  
		else {
205  
			text = "Text not found";
206  
		}
207  
		statusBar.setLabel(text);
208  
209  
	}
210  
211  
212  
	public static void main(String[] args) {
213  
		SwingUtilities.invokeLater(new Runnable() {
214  
			@Override
215  
			public void run() {
216  
				try {
217  
					UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
218  
//					UIManager.setLookAndFeel("org.pushingpixels.substance.api.skin.SubstanceGraphiteAquaLookAndFeel");
219  
				} catch (Exception e) {
220  
					e.printStackTrace();
221  
				}
222  
				new RSTAUIDemoApp().setVisible(true);
223  
			}
224  
		});
225  
	}
226  
227  
228  
	private class GoToLineAction extends AbstractAction {
229  
230  
		public GoToLineAction() {
231  
			super("Go To Line...");
232  
			int c = getToolkit().getMenuShortcutKeyMask();
233  
			putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_L, c));
234  
		}
235  
236  
		@Override
237  
		public void actionPerformed(ActionEvent e) {
238  
			if (findDialog.isVisible()) {
239  
				findDialog.setVisible(false);
240  
			}
241  
			if (replaceDialog.isVisible()) {
242  
				replaceDialog.setVisible(false);
243  
			}
244  
			GoToDialog dialog = new GoToDialog(RSTAUIDemoApp.this);
245  
			dialog.setMaxLineNumberAllowed(textArea.getLineCount());
246  
			dialog.setVisible(true);
247  
			int line = dialog.getLineNumber();
248  
			if (line>0) {
249  
				try {
250  
					textArea.setCaretPosition(textArea.getLineStartOffset(line-1));
251  
				} catch (BadLocationException ble) { // Never happens
252  
					UIManager.getLookAndFeel().provideErrorFeedback(textArea);
253  
					ble.printStackTrace();
254  
				}
255  
			}
256  
		}
257  
258  
	}
259  
260  
261  
	private class LookAndFeelAction extends AbstractAction {
262  
263  
		private LookAndFeelInfo info;
264  
265  
		public LookAndFeelAction(LookAndFeelInfo info) {
266  
			putValue(NAME, info.getName());
267  
			this.info = info;
268  
		}
269  
270  
		@Override
271  
		public void actionPerformed(ActionEvent e) {
272  
			try {
273  
				UIManager.setLookAndFeel(info.getClassName());
274  
				SwingUtilities.updateComponentTreeUI(RSTAUIDemoApp.this);
275  
				if (findDialog!=null) {
276  
					findDialog.updateUI();
277  
					replaceDialog.updateUI();
278  
				}
279  
				pack();
280  
			} catch (RuntimeException re) {
281  
				throw re; // FindBugs
282  
			} catch (Exception ex) {
283  
				ex.printStackTrace();
284  
			}
285  
		}
286  
	}
287  
288  
289  
	private class ShowFindDialogAction extends AbstractAction {
290  
		
291  
		public ShowFindDialogAction() {
292  
			super("Find...");
293  
			int c = getToolkit().getMenuShortcutKeyMask();
294  
			putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_F, c));
295  
		}
296  
297  
		@Override
298  
		public void actionPerformed(ActionEvent e) {
299  
			if (replaceDialog.isVisible()) {
300  
				replaceDialog.setVisible(false);
301  
			}
302  
			findDialog.setVisible(true);
303  
		}
304  
305  
	}
306  
307  
308  
	private class ShowReplaceDialogAction extends AbstractAction {
309  
		
310  
		public ShowReplaceDialogAction() {
311  
			super("Replace...");
312  
			int c = getToolkit().getMenuShortcutKeyMask();
313  
			putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_H, c));
314  
		}
315  
316  
		@Override
317  
		public void actionPerformed(ActionEvent e) {
318  
			if (findDialog.isVisible()) {
319  
				findDialog.setVisible(false);
320  
			}
321  
			replaceDialog.setVisible(true);
322  
		}
323  
324  
	}
325  
326  
327  
	private static class StatusBar extends JPanel {
328  
329  
		private JLabel label;
330  
331  
		public StatusBar() {
332  
			label = new JLabel("Ready");
333  
			setLayout(new BorderLayout());
334  
			add(label, BorderLayout.LINE_START);
335  
			add(new JLabel(new SizeGripIcon()), BorderLayout.LINE_END);
336  
		}
337  
338  
		public void setLabel(String label) {
339  
			this.label.setText(label);
340  
		}
341  
342  
	}
343  
344  
345  
}

Author comment

Began life as a copy of #1003942

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1013049
Snippet name: Test RSTAUI (RSyntaxTextArea + dialogs) - new version (OK)
Eternal ID of this version: #1013049/5
Text MD5: 3011eb82f021666b7499788cee4ca781
Transpilation MD5: 41f17b1b6f520a692f226f50fbffa30b
Author: stefan
Category: javax / gui
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2017-12-22 16:54:50
Source code size: 9278 bytes / 345 lines
Pitched / IR pitched: No / No
Views / Downloads: 467 / 549
Version history: 4 change(s)
Referenced in: [show references]