Mercurial > hg > RemoteEditor > REPSessionManager
diff test/editortest/REPTextWithJTextArea.java @ 417:267f9748e826
(no commit message)
author | one |
---|---|
date | Wed, 31 Dec 2008 14:52:45 +0900 |
parents | |
children | 7ff127c8ad64 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/editortest/REPTextWithJTextArea.java Wed Dec 31 14:52:45 2008 +0900 @@ -0,0 +1,156 @@ +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; + +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)); + } + } + +}