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

348
LINES

< > BotCompany Repo | #1003942 // Test RSTAUI (RSyntaxTextArea + dialogs!) - WORKS

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

Uses 1314K of libraries. Click here for Pure Java version (369L/3K/11K).

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

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1003942
Snippet name: Test RSTAUI (RSyntaxTextArea + dialogs!) - WORKS
Eternal ID of this version: #1003942/1
Text MD5: c5aad58995f6141fe26eacb1bb18f0b2
Transpilation MD5: d0425874a158803c14557a80905a514f
Author: stefan
Category: javax / talking robots
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2016-07-31 16:06:49
Source code size: 9350 bytes / 348 lines
Pitched / IR pitched: No / No
Views / Downloads: 754 / 1026
Referenced in: [show references]