Mercurial > hg > RemoteEditor > REPSessionManager
comparison test/editortest/REPSimpleEditor.java @ 416:b7f42fc75a36
(no commit message)
author | one |
---|---|
date | Wed, 31 Dec 2008 14:47:39 +0900 |
parents | 648c676bf9df |
children |
comparison
equal
deleted
inserted
replaced
415:648c676bf9df | 416:b7f42fc75a36 |
---|---|
3 import java.awt.BorderLayout; | 3 import java.awt.BorderLayout; |
4 import java.awt.Dimension; | 4 import java.awt.Dimension; |
5 import java.awt.Font; | 5 import java.awt.Font; |
6 import java.awt.event.ActionEvent; | 6 import java.awt.event.ActionEvent; |
7 import java.awt.event.ActionListener; | 7 import java.awt.event.ActionListener; |
8 import java.io.IOException; | |
9 import java.net.InetSocketAddress; | |
8 | 10 |
9 import javax.swing.JButton; | 11 import javax.swing.JButton; |
10 import javax.swing.JFrame; | 12 import javax.swing.JFrame; |
13 import javax.swing.JLabel; | |
11 import javax.swing.JScrollPane; | 14 import javax.swing.JScrollPane; |
12 import javax.swing.JSplitPane; | 15 import javax.swing.JSplitPane; |
13 import javax.swing.JTextArea; | 16 import javax.swing.JTextArea; |
17 import javax.swing.JTextField; | |
14 import javax.swing.JToolBar; | 18 import javax.swing.JToolBar; |
15 import javax.swing.event.DocumentEvent; | 19 import javax.swing.event.DocumentEvent; |
16 import javax.swing.event.DocumentListener; | 20 import javax.swing.event.DocumentListener; |
17 import javax.swing.text.BadLocationException; | 21 import javax.swing.text.BadLocationException; |
18 import javax.swing.text.Document; | 22 import javax.swing.text.Document; |
19 | 23 |
20 import rep.REP; | 24 import rep.REP; |
21 import rep.REPCommand; | 25 import rep.REPCommand; |
26 import rep.REPCommandPacker; | |
27 import rep.channel.REPSocketChannel; | |
22 import test.Text; | 28 import test.Text; |
23 | 29 |
24 public class REPSimpleEditor extends JFrame implements DocumentListener, ActionListener, LogTarget{ | 30 public class REPSimpleEditor extends JFrame implements DocumentListener, ActionListener, LogTarget{ |
25 | 31 |
26 /** | 32 /** |
39 private TestEditor2 testEditor; | 45 private TestEditor2 testEditor; |
40 private JSplitPane splitPane; | 46 private JSplitPane splitPane; |
41 private JScrollPane scrollPane1; | 47 private JScrollPane scrollPane1; |
42 private JTextArea console; | 48 private JTextArea console; |
43 private JScrollPane scrollPane2; | 49 private JScrollPane scrollPane2; |
50 private JTextField lineField; | |
51 private JTextField textField; | |
52 private REPSocketChannel<REPCommand> channel; | |
44 | 53 |
45 public REPSimpleEditor(String title){ | 54 public REPSimpleEditor(String title){ |
46 super(title); | 55 super(title); |
47 setSize(new Dimension(640, 480)); | 56 setSize(new Dimension(640, 480)); |
48 this.setLayout(new BorderLayout()); | 57 setLayout(new BorderLayout()); |
49 | 58 |
50 setToolBar(); | 59 setToolBar(); |
51 setEditor(); | 60 setEditor(); |
52 setConsole(); | 61 setConsole(); |
53 setSplitPane(); | 62 setSplitPane(); |
60 private void setToolBar() { | 69 private void setToolBar() { |
61 JToolBar toolbar = new JToolBar(); | 70 JToolBar toolbar = new JToolBar(); |
62 putButton = new JButton("put"); | 71 putButton = new JButton("put"); |
63 joinButton = new JButton("join"); | 72 joinButton = new JButton("join"); |
64 | 73 |
65 //putButton.addActionListener(this); | 74 putButton.addActionListener(this); |
66 //joinButton.addActionListener(this); | 75 joinButton.addActionListener(this); |
76 | |
77 JLabel label1 = new JLabel("line"); | |
78 JLabel label2 = new JLabel("text"); | |
79 lineField = new JTextField(); | |
80 textField = new JTextField(); | |
81 | |
82 textField.addActionListener(this); | |
67 | 83 |
68 toolbar.add(putButton); | 84 toolbar.add(putButton); |
69 toolbar.add(joinButton); | 85 toolbar.add(joinButton); |
86 toolbar.addSeparator(); | |
87 toolbar.add(label1); | |
88 toolbar.add(lineField); | |
89 toolbar.add(label2); | |
90 toolbar.add(textField); | |
91 | |
70 add(toolbar, BorderLayout.NORTH); | 92 add(toolbar, BorderLayout.NORTH); |
71 } | 93 } |
72 | 94 |
73 private void setEditor(){ | 95 private void setEditor(){ |
74 textArea = new JTextArea(); | 96 textArea = new JTextArea(); |
97 textArea.setEditable(false); | |
75 textArea.setFont(new Font("Monaco", Font.PLAIN, textArea.getFont().getSize())); | 98 textArea.setFont(new Font("Monaco", Font.PLAIN, textArea.getFont().getSize())); |
76 document = textArea.getDocument(); | 99 document = textArea.getDocument(); |
77 document.addDocumentListener(this); | 100 document.addDocumentListener(this); |
78 | 101 |
79 scrollPane1 = new JScrollPane(textArea); | 102 scrollPane1 = new JScrollPane(textArea); |
93 splitPane.setOrientation(JSplitPane.VERTICAL_SPLIT); | 116 splitPane.setOrientation(JSplitPane.VERTICAL_SPLIT); |
94 splitPane.add(scrollPane1, JSplitPane.TOP); | 117 splitPane.add(scrollPane1, JSplitPane.TOP); |
95 splitPane.add(scrollPane2, JSplitPane.BOTTOM); | 118 splitPane.add(scrollPane2, JSplitPane.BOTTOM); |
96 splitPane.setDividerLocation(300); | 119 splitPane.setDividerLocation(300); |
97 add(splitPane, BorderLayout.CENTER); | 120 add(splitPane, BorderLayout.CENTER); |
121 } | |
122 | |
123 public void setButtonEnabled(boolean b) { | |
124 putButton.setEnabled(b); | |
125 joinButton.setEnabled(b); | |
98 } | 126 } |
99 | 127 |
100 private REPCommand createREPCommand(int offset, int length) { | 128 private REPCommand createREPCommand(int offset, int length) { |
101 REPCommand command = null; | 129 REPCommand command = null; |
102 try { | 130 try { |
106 String text = textArea.getText(lineStart, lineEnd-lineStart); | 134 String text = textArea.getText(lineStart, lineEnd-lineStart); |
107 command = new REPCommand(REP.REPCMD_INSERT_USER, sid, eid, seq++, lineno, text); | 135 command = new REPCommand(REP.REPCMD_INSERT_USER, sid, eid, seq++, lineno, text); |
108 } catch (BadLocationException e1) { | 136 } catch (BadLocationException e1) { |
109 e1.printStackTrace(); | 137 e1.printStackTrace(); |
110 } | 138 } |
111 //Logger.printT(command); | |
112 return command; | 139 return command; |
113 } | 140 } |
114 | 141 |
115 public void changedUpdate(DocumentEvent e) { | 142 public void changedUpdate(DocumentEvent e) { |
116 Logger.print(e); | 143 Logger.print(e); |
129 public void actionPerformed(ActionEvent e) { | 156 public void actionPerformed(ActionEvent e) { |
130 if(e.getSource() == putButton){ | 157 if(e.getSource() == putButton){ |
131 repPut(); | 158 repPut(); |
132 }else if(e.getSource() == joinButton){ | 159 }else if(e.getSource() == joinButton){ |
133 repJoin(); | 160 repJoin(); |
161 }else if(e.getSource() == textField){ | |
162 testEditor.addUserInput(new REPCommand(REP.REPCMD_INSERT_USER, 0, 0, 0, 0, textField.getText())); | |
163 lineField.setText(""); | |
164 textField.setText(""); | |
134 } | 165 } |
135 } | 166 } |
136 | 167 |
137 private void repJoin() { | 168 private void repJoin() { |
138 testEditor = new TestEditor2("TestEditor", "localhost", 8766, false); | 169 // testEditor = new TestEditor2("TestEditor", "localhost", 8766, false); |
139 testEditor.setREPText(repText); | 170 // testEditor.setREPText(repText); |
140 testEditor.setLogTarget(this); | 171 // testEditor.setLogTarget(this); |
141 testEditor.start(); | 172 // testEditor.start(); |
142 } | 173 } |
143 | 174 |
144 private void repPut() { | 175 private void repPut() { |
145 setMasterText(); | 176 setMasterText(); |
146 testEditor = new TestEditor2("TestEditor", "localhost", 8766, true); | 177 connectToSessionManager(); |
147 testEditor.setText(repText.list()); | 178 // testEditor = new TestEditor2("TestEditor", "localhost", 8766, true); |
148 testEditor.setREPText(repText); | 179 // testEditor.setText(repText.list()); |
149 testEditor.setLogTarget(this); | 180 // testEditor.setREPText(repText); |
150 testEditor.start(); | 181 // testEditor.setLogTarget(this); |
182 // testEditor.start(); | |
151 } | 183 } |
152 | 184 |
153 private void setMasterText() { | 185 private void setMasterText() { |
154 textArea.append("AAAAA" + BR); | 186 textArea.append("AAAAA" + BR); |
155 textArea.append("BBBBB" + BR); | 187 textArea.append("BBBBB" + BR); |
156 textArea.append("CCCCC" + BR); | 188 textArea.append("CCCCC" + BR); |
157 textArea.append("DDDDD" + BR); | 189 textArea.append("DDDDD" + BR); |
158 } | 190 } |
159 | 191 |
160 public void printLog(Object obj) { | 192 private void connectToSessionManager() { |
161 String log = obj.toString(); | 193 InetSocketAddress semaIP = new InetSocketAddress("localhost", 8766); |
162 console.append(log + BR); | 194 try { |
195 channel = REPSocketChannel.<REPCommand>create(new REPCommandPacker()); | |
196 } catch (IOException e) { | |
197 e.printStackTrace(); return; | |
198 } | |
199 try { | |
200 while (!channel.connect(semaIP)){ | |
201 Logger.print("SeMa not listen to socket yet, wait"); | |
202 } | |
203 } catch (IOException e) { | |
204 e.printStackTrace(); return; | |
205 } | |
206 | |
207 } | |
208 | |
209 public void printLog(String msg) { | |
210 console.append(msg + BR); | |
163 } | 211 } |
164 | 212 |
165 public REPText getREPText() { | 213 public REPText getREPText() { |
166 return repText; | 214 return repText; |
167 } | 215 } |
170 for(String str : text){ | 218 for(String str : text){ |
171 textArea.append(str + BR); | 219 textArea.append(str + BR); |
172 } | 220 } |
173 } | 221 } |
174 | 222 |
223 public void setTestEditor(TestEditor2 testEditor2) { | |
224 testEditor = testEditor2; | |
225 } | |
226 | |
175 } | 227 } |