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