414
|
1 package test.editortest;
|
|
2
|
|
3 import java.awt.BorderLayout;
|
|
4 import java.awt.Dimension;
|
|
5 import java.awt.Font;
|
|
6 import java.awt.event.ActionEvent;
|
|
7 import java.awt.event.ActionListener;
|
|
8
|
|
9 import javax.swing.JButton;
|
|
10 import javax.swing.JFrame;
|
|
11 import javax.swing.JScrollPane;
|
|
12 import javax.swing.JSplitPane;
|
|
13 import javax.swing.JTextArea;
|
|
14 import javax.swing.JToolBar;
|
|
15 import javax.swing.event.DocumentEvent;
|
|
16 import javax.swing.event.DocumentListener;
|
|
17 import javax.swing.text.BadLocationException;
|
|
18 import javax.swing.text.Document;
|
|
19
|
|
20 import rep.REP;
|
|
21 import rep.REPCommand;
|
|
22 import test.Text;
|
|
23
|
|
24 public class REPSimpleEditor extends JFrame implements DocumentListener, ActionListener, LogTarget{
|
|
25
|
|
26 /**
|
|
27 *
|
|
28 */
|
|
29 private static final long serialVersionUID = 1L;
|
|
30 private String BR = System.getProperty("line.separator");
|
|
31 private Document document;
|
|
32 private JTextArea textArea;
|
|
33 private int seq;
|
|
34 private int eid;
|
|
35 private int sid;
|
|
36 private JButton putButton;
|
|
37 private JButton joinButton;
|
|
38 private REPText repText;
|
|
39 private TestEditor2 testEditor;
|
|
40 private JSplitPane splitPane;
|
|
41 private JScrollPane scrollPane1;
|
|
42 private JTextArea console;
|
|
43 private JScrollPane scrollPane2;
|
|
44
|
|
45 public REPSimpleEditor(String title){
|
|
46 super(title);
|
|
47 setSize(new Dimension(640, 480));
|
|
48 this.setLayout(new BorderLayout());
|
|
49
|
|
50 setToolBar();
|
|
51 setEditor();
|
|
52 setConsole();
|
|
53 setSplitPane();
|
|
54 }
|
|
55
|
|
56 public REPSimpleEditor(){
|
|
57 this("Sample Editor");
|
|
58 }
|
|
59
|
|
60 private void setToolBar() {
|
|
61 JToolBar toolbar = new JToolBar();
|
|
62 putButton = new JButton("put");
|
|
63 joinButton = new JButton("join");
|
|
64
|
415
|
65 //putButton.addActionListener(this);
|
|
66 //joinButton.addActionListener(this);
|
414
|
67
|
|
68 toolbar.add(putButton);
|
|
69 toolbar.add(joinButton);
|
|
70 add(toolbar, BorderLayout.NORTH);
|
|
71 }
|
|
72
|
|
73 private void setEditor(){
|
|
74 textArea = new JTextArea();
|
|
75 textArea.setFont(new Font("Monaco", Font.PLAIN, textArea.getFont().getSize()));
|
|
76 document = textArea.getDocument();
|
|
77 document.addDocumentListener(this);
|
|
78
|
|
79 scrollPane1 = new JScrollPane(textArea);
|
|
80
|
|
81 repText = new REPTextImpl(textArea);
|
|
82 }
|
|
83
|
|
84 private void setConsole(){
|
|
85 console = new JTextArea();
|
|
86 console.setFont(new Font("Monaco", Font.PLAIN, console.getFont().getSize()));
|
|
87 console.setEditable(false);
|
|
88 scrollPane2 = new JScrollPane(console);
|
|
89 }
|
|
90
|
|
91 private void setSplitPane(){
|
|
92 splitPane = new JSplitPane();
|
|
93 splitPane.setOrientation(JSplitPane.VERTICAL_SPLIT);
|
|
94 splitPane.add(scrollPane1, JSplitPane.TOP);
|
|
95 splitPane.add(scrollPane2, JSplitPane.BOTTOM);
|
|
96 splitPane.setDividerLocation(300);
|
|
97 add(splitPane, BorderLayout.CENTER);
|
|
98 }
|
|
99
|
|
100 private REPCommand createREPCommand(int offset, int length) {
|
|
101 REPCommand command = null;
|
|
102 try {
|
|
103 int lineno = textArea.getLineOfOffset(offset);
|
|
104 int lineStart = textArea.getLineStartOffset(lineno);
|
|
105 int lineEnd = textArea.getLineEndOffset(lineno);
|
|
106 String text = textArea.getText(lineStart, lineEnd-lineStart);
|
|
107 command = new REPCommand(REP.REPCMD_INSERT_USER, sid, eid, seq++, lineno, text);
|
|
108 } catch (BadLocationException e1) {
|
|
109 e1.printStackTrace();
|
|
110 }
|
415
|
111 //Logger.printT(command);
|
414
|
112 return command;
|
|
113 }
|
|
114
|
|
115 public void changedUpdate(DocumentEvent e) {
|
|
116 Logger.print(e);
|
|
117 }
|
|
118
|
|
119 public void insertUpdate(DocumentEvent e) {
|
|
120 int offset = e.getOffset();
|
|
121 int length = e.getLength();
|
|
122 createREPCommand(offset, length);
|
|
123 }
|
|
124
|
|
125 public void removeUpdate(DocumentEvent e) {
|
|
126 Logger.print(e);
|
|
127 }
|
|
128
|
|
129 public void actionPerformed(ActionEvent e) {
|
|
130 if(e.getSource() == putButton){
|
|
131 repPut();
|
|
132 }else if(e.getSource() == joinButton){
|
|
133 repJoin();
|
|
134 }
|
|
135 }
|
|
136
|
|
137 private void repJoin() {
|
|
138 testEditor = new TestEditor2("TestEditor", "localhost", 8766, false);
|
|
139 testEditor.setREPText(repText);
|
|
140 testEditor.setLogTarget(this);
|
|
141 testEditor.start();
|
|
142 }
|
|
143
|
|
144 private void repPut() {
|
|
145 setMasterText();
|
|
146 testEditor = new TestEditor2("TestEditor", "localhost", 8766, true);
|
|
147 testEditor.setText(repText.list());
|
|
148 testEditor.setREPText(repText);
|
|
149 testEditor.setLogTarget(this);
|
|
150 testEditor.start();
|
|
151 }
|
|
152
|
|
153 private void setMasterText() {
|
|
154 textArea.append("AAAAA" + BR);
|
|
155 textArea.append("BBBBB" + BR);
|
|
156 textArea.append("CCCCC" + BR);
|
|
157 textArea.append("DDDDD" + BR);
|
|
158 }
|
|
159
|
|
160 public void printLog(Object obj) {
|
|
161 String log = obj.toString();
|
|
162 console.append(log + BR);
|
|
163 }
|
|
164
|
|
165 public REPText getREPText() {
|
|
166 return repText;
|
|
167 }
|
|
168
|
|
169 public void setText(Text text) {
|
|
170 for(String str : text){
|
|
171 textArea.append(str + BR);
|
|
172 }
|
|
173 }
|
|
174
|
|
175 }
|