417
|
1 package test.editortest;
|
|
2
|
|
3 import java.awt.event.ActionEvent;
|
|
4 import java.awt.event.ActionListener;
|
|
5 import java.util.LinkedList;
|
|
6 import java.util.List;
|
|
7
|
|
8 import javax.swing.JButton;
|
|
9 import javax.swing.JTextArea;
|
|
10 import javax.swing.JTextField;
|
|
11 import javax.swing.event.DocumentEvent;
|
|
12 import javax.swing.event.DocumentListener;
|
|
13 import javax.swing.text.BadLocationException;
|
|
14
|
|
15 public class REPTextWithJTextArea implements REPText, DocumentListener, ActionListener {
|
|
16
|
|
17 private JTextArea textArea;
|
|
18 private String BR = System.getProperty("line.separator");
|
|
19 private LinkedList<REPTextListener> textListenerList = new LinkedList<REPTextListener>();
|
|
20 private JTextField lineField;
|
|
21 private JTextField textField;
|
|
22 private JButton deleteButton;
|
|
23 private JButton insertButton;
|
|
24
|
|
25 public REPTextWithJTextArea(JTextArea textArea, JTextField lineField, JTextField textField, JButton deleteButton, JButton insertButton) {
|
|
26 this.textArea = textArea;
|
|
27 this.lineField = lineField;
|
|
28 this.textField = textField;
|
|
29 this.deleteButton = deleteButton;
|
|
30 this.insertButton = insertButton;
|
|
31 textArea.getDocument().addDocumentListener(this);
|
|
32 textField.addActionListener(this);
|
|
33 deleteButton.addActionListener(this);
|
|
34 insertButton.addActionListener(this);
|
|
35 }
|
|
36
|
|
37 public String delete(int lineno) {
|
|
38 for(int i = size(); i <= lineno; i++){
|
|
39 increaseLine();
|
|
40 }
|
|
41
|
|
42 String del = getLineText(lineno);
|
|
43 try {
|
|
44 int start = textArea.getLineStartOffset(lineno);
|
|
45 int end = textArea.getLineEndOffset(lineno);
|
|
46 textArea.replaceRange("", start, end);
|
|
47 } catch (BadLocationException e) {
|
|
48 e.printStackTrace();
|
|
49 }
|
|
50 return del;
|
|
51 }
|
|
52
|
|
53 public void insert(int lineno, String text) {
|
|
54 for(int i = size(); i <= lineno; i++){
|
|
55 increaseLine();
|
|
56 }
|
|
57
|
|
58 String text2 = text + BR;
|
|
59 try {
|
|
60 int offset = textArea.getLineStartOffset(lineno);
|
|
61 textArea.insert(text2, offset);
|
|
62 } catch (BadLocationException e) {
|
|
63 e.printStackTrace();
|
|
64 }
|
|
65 }
|
|
66
|
|
67 private void increaseLine(){
|
|
68 textArea.append(BR);
|
|
69 }
|
|
70
|
|
71 private String getLineText(int lineno){
|
|
72 String text = null;
|
|
73 try {
|
|
74 int start = textArea.getLineStartOffset(lineno);
|
|
75 int end = textArea.getLineEndOffset(lineno);
|
|
76 text = textArea.getText(start, end-start);
|
|
77 String text2 = text.replace(BR, "");
|
|
78 text = text2;
|
|
79 } catch (BadLocationException e) {
|
|
80 e.printStackTrace();
|
|
81 }
|
|
82 return text;
|
|
83 }
|
|
84
|
|
85 public int size() {
|
|
86 return textArea.getLineCount();
|
|
87 }
|
|
88
|
|
89 public String get(int i) {
|
|
90 return getLineText(i);
|
|
91 }
|
|
92
|
|
93 public List<String> list() {
|
|
94 LinkedList<String> list = new LinkedList<String>();
|
|
95 for(int i = 0; i < size(); i++){
|
|
96 list.add(getLineText(i));
|
|
97 }
|
|
98 return list;
|
|
99 }
|
|
100
|
|
101 public void addTextListener(REPTextListener listener) {
|
|
102 textListenerList.add(listener);
|
|
103 }
|
|
104
|
|
105 public void changedUpdate(DocumentEvent e) {
|
|
106 Logger.print(e);
|
|
107 }
|
|
108
|
|
109 public void insertUpdate(DocumentEvent e) {
|
|
110 Logger.print(e);
|
|
111
|
|
112 }
|
|
113
|
|
114 public void removeUpdate(DocumentEvent e) {
|
|
115 Logger.print(e);
|
|
116 }
|
|
117
|
|
118 public void actionPerformed(ActionEvent e) {
|
|
119 if(e.getSource() == textField){
|
|
120 userInsert();
|
|
121 textField.setText("");
|
|
122 }else if(e.getSource() == deleteButton){
|
|
123 userDelete();
|
|
124 }else if(e.getSource() == insertButton){
|
|
125 userInsert();
|
|
126 }
|
|
127 }
|
|
128
|
|
129 private void userDelete() {
|
|
130 int lineno = 0;
|
|
131 try {
|
|
132 lineno = Integer.parseInt((lineField.getText()));
|
|
133 if(lineno < 0) lineno = 0;
|
|
134 }catch(NumberFormatException e){}
|
|
135
|
|
136 String del = delete(lineno);
|
|
137 for(REPTextListener listener : textListenerList){
|
|
138 listener.textDeleted(new REPTextEvent(lineno, del));
|
|
139 }
|
|
140 }
|
|
141
|
|
142 private void userInsert() {
|
|
143 int lineno;
|
|
144 try {
|
|
145 lineno = Integer.parseInt((lineField.getText()));
|
|
146 }catch(NumberFormatException e){
|
|
147 lineno = 0;
|
|
148 }
|
|
149 String text = textField.getText();
|
|
150 insert(lineno, text);
|
|
151 for(REPTextListener listener : textListenerList){
|
|
152 listener.textInserted(new REPTextEvent(lineno, text));
|
|
153 }
|
|
154 }
|
|
155
|
|
156 }
|