Mercurial > hg > RemoteEditor > REPSessionManager
view test/editortest/REPTextWithJTextArea.java @ 419:7ff127c8ad64
(no commit message)
author | one |
---|---|
date | Tue, 20 Jan 2009 18:39:02 +0900 |
parents | 267f9748e826 |
children |
line wrap: on
line source
package test.editortest; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.LinkedList; import java.util.List; import javax.swing.JButton; import javax.swing.JTextArea; import javax.swing.JTextField; import javax.swing.event.DocumentEvent; import javax.swing.event.DocumentListener; import javax.swing.text.BadLocationException; import rep.REPCommand; public class REPTextWithJTextArea implements REPText, DocumentListener, ActionListener { private JTextArea textArea; private String BR = System.getProperty("line.separator"); private LinkedList<REPTextListener> textListenerList = new LinkedList<REPTextListener>(); private JTextField lineField; private JTextField textField; private JButton deleteButton; private JButton insertButton; public REPTextWithJTextArea(JTextArea textArea, JTextField lineField, JTextField textField, JButton deleteButton, JButton insertButton) { this.textArea = textArea; this.lineField = lineField; this.textField = textField; this.deleteButton = deleteButton; this.insertButton = insertButton; textArea.getDocument().addDocumentListener(this); textField.addActionListener(this); deleteButton.addActionListener(this); insertButton.addActionListener(this); } public String delete(int lineno) { for(int i = size(); i <= lineno; i++){ increaseLine(); } String del = getLineText(lineno); try { int start = textArea.getLineStartOffset(lineno); int end = textArea.getLineEndOffset(lineno); textArea.replaceRange("", start, end); } catch (BadLocationException e) { e.printStackTrace(); } return del; } public void insert(int lineno, String text) { for(int i = size(); i <= lineno; i++){ increaseLine(); } String text2 = text + BR; try { int offset = textArea.getLineStartOffset(lineno); textArea.insert(text2, offset); } catch (BadLocationException e) { e.printStackTrace(); } } private void increaseLine(){ textArea.append(BR); } private String getLineText(int lineno){ String text = null; try { int start = textArea.getLineStartOffset(lineno); int end = textArea.getLineEndOffset(lineno); text = textArea.getText(start, end-start); String text2 = text.replace(BR, ""); text = text2; } catch (BadLocationException e) { e.printStackTrace(); } return text; } public int size() { return textArea.getLineCount(); } public String get(int i) { return getLineText(i); } public List<String> list() { LinkedList<String> list = new LinkedList<String>(); for(int i = 0; i < size(); i++){ list.add(getLineText(i)); } return list; } public void addTextListener(REPTextListener listener) { textListenerList.add(listener); } public void changedUpdate(DocumentEvent e) { Logger.print(e); } public void insertUpdate(DocumentEvent e) { Logger.print(e); } public void removeUpdate(DocumentEvent e) { Logger.print(e); } public void actionPerformed(ActionEvent e) { if(e.getSource() == textField){ userInsert(); textField.setText(""); }else if(e.getSource() == deleteButton){ userDelete(); }else if(e.getSource() == insertButton){ userInsert(); } } private void userDelete() { int lineno = 0; try { lineno = Integer.parseInt((lineField.getText())); if(lineno < 0) lineno = 0; }catch(NumberFormatException e){} String del = delete(lineno); for(REPTextListener listener : textListenerList){ listener.textDeleted(new REPTextEvent(lineno, del)); } } private void userInsert() { int lineno; try { lineno = Integer.parseInt((lineField.getText())); }catch(NumberFormatException e){ lineno = 0; } String text = textField.getText(); insert(lineno, text); for(REPTextListener listener : textListenerList){ listener.textInserted(new REPTextEvent(lineno, text)); } } public void userInsert(REPCommand command) { int lineno = command.lineno; String text = command.string; insert(lineno, text); for(REPTextListener listener : textListenerList){ listener.textInserted(new REPTextEvent(lineno, text)); } } }