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)

!include once #1015780 // RSyntaxTextArea

/*
 * original author: Robert Futrell
 */
static class EditorFrame extends JFrame implements SearchListener {
	CollapsibleSectionPanel csp;
	RSyntaxTextArea textArea;
	FindDialog findDialog;
	ReplaceDialog replaceDialog;
	FindToolBar findToolBar;
	ReplaceToolBar replaceToolBar;
	StatusBar statusBar;

	*() {
	  rsyntaxTextArea_fixNumPad();
	  _initFrame(this);
		initSearchDialogs();

		JPanel contentPane = new JPanel(new BorderLayout());
		setContentPane(contentPane);
		csp = new CollapsibleSectionPanel();
		contentPane.add(csp);

		setJMenuBar(createMenuBar());
		textArea = javaxSyntaxTextArea();
		RTextScrollPane sp = new RTextScrollPane(textArea);
		csp.add(sp);

		ErrorStrip errorStrip = new ErrorStrip(textArea);
		contentPane.add(errorStrip, BorderLayout.LINE_END);
//org.fife.rsta.ui.DocumentMap docMap = new org.fife.rsta.ui.DocumentMap(textArea);
//contentPane.add(docMap, BorderLayout.LINE_END);

		statusBar = new StatusBar();
		contentPane.add(statusBar, BorderLayout.SOUTH);

		setTitle(autoFrameTitle());
		centerFrame(this);
		
		bindBotToFrame(this, makeBot("Editor.", this));
	}
	
	S answer(S s) {
	  new Matches m;
	  if "get text" ret quote(textArea.getText());
	  if "set text to * " {
	    final S text = m.unq(0);
	    setText(text);
	    ret "OK";
	  }
	  if "get frame title" ret quote(getTitle());
	  null;
	}

	void addItem(Action a, ButtonGroup bg, JMenu menu) {
		JRadioButtonMenuItem item = new JRadioButtonMenuItem(a);
		bg.add(item);
		menu.add(item);
	}

	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);
		return mb;
	}

	@Override
	public String getSelectedText() {
		return textArea.getSelectedText();
	}

	/**
	 * Creates our Find and Replace dialogs.
	 */
	public void initSearchDialogs() {

		findDialog = new FindDialog(this, this);
		replaceDialog = new ReplaceDialog(this, 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()) {
					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";
		}
		statusBar.setText(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(EditorFrame.this);
			dialog.setMaxLineNumberAllowed(textArea.getLineCount());
			dialog.setVisible(true);
			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);
			findDialog.setVisible(true);
		}
	}

	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);
			replaceDialog.setVisible(true);
		}
	}

	static class StatusBar extends JPanel {
		JLabel label;

		*() {
			label = new JLabel("Ready");
			setLayout(new BorderLayout());
			add(label, BorderLayout.LINE_START);
			add(new JLabel(new SizeGripIcon()), BorderLayout.LINE_END);
		}

		public void setText(String label) {
			main.setText(this.label, label);
		}
	}
	
	void setStatus(S text) {
	  statusBar.setText(text);
	}
	
	void setText(S text) {
	  main.setText(textArea, text);
	}
	
	S getText() {
	  ret main.getText(textArea);
	}
	 
	void _setFont(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 _getFont() {
    ret swing(func -> Font { textArea.getFont() });
  }
  
  int _getFontSize() {
    ret _getFont().getSize();
  }
  
  void _setFontSize(int size) {
    _setFont(deriveFont(_getFont(), size));
  }
  
  RSyntaxTextArea getTextArea() {
    ret textArea;
  }
  
  RSyntaxDocument getDocument() {
    ret (RSyntaxDocument) textArea.getDocument();
  }
}

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: 676 / 1295
Version history: 19 change(s)
Referenced in: [show references]