1 | package com.framsticks.gui.console; |
---|
2 | |
---|
3 | import java.util.LinkedList; |
---|
4 | import java.util.List; |
---|
5 | |
---|
6 | import com.framsticks.communication.Address; |
---|
7 | import com.framsticks.communication.ClientSideRawConnection; |
---|
8 | import com.framsticks.communication.ConnectionListener; |
---|
9 | import com.framsticks.params.annotations.FramsClassAnnotation; |
---|
10 | import com.framsticks.params.annotations.ParamAnnotation; |
---|
11 | import com.framsticks.util.dispatching.ThrowExceptionHandler; |
---|
12 | |
---|
13 | @FramsClassAnnotation |
---|
14 | public class DirectConsole extends InteractiveConsole implements ConnectionListener { |
---|
15 | |
---|
16 | /** |
---|
17 | * @param connection |
---|
18 | */ |
---|
19 | public DirectConsole() { |
---|
20 | } |
---|
21 | |
---|
22 | |
---|
23 | @ParamAnnotation |
---|
24 | public DirectConsole setAddress(String address) { |
---|
25 | return setAddress(new Address(address)); |
---|
26 | } |
---|
27 | |
---|
28 | public DirectConsole setAddress(Address address) { |
---|
29 | this.connection = new ClientSideRawConnection(); |
---|
30 | this.connection.setAddress(address); |
---|
31 | return this; |
---|
32 | } |
---|
33 | |
---|
34 | /** |
---|
35 | * @return the connection |
---|
36 | */ |
---|
37 | @Override |
---|
38 | public ClientSideRawConnection getConnection() { |
---|
39 | return (ClientSideRawConnection) connection; |
---|
40 | } |
---|
41 | |
---|
42 | |
---|
43 | protected void sendImplementation(String line) { |
---|
44 | getConnection().send(line, ThrowExceptionHandler.getInstance()); |
---|
45 | } |
---|
46 | |
---|
47 | /** |
---|
48 | * Creates commands tip list. |
---|
49 | * @param startText Text that user typed. |
---|
50 | */ |
---|
51 | protected void findCompletionPropositions(String prefix) { |
---|
52 | List<String> propositions = new LinkedList<String>(); |
---|
53 | for (String entry : history) { |
---|
54 | if (entry.startsWith(prefix)) { |
---|
55 | propositions.add(entry); |
---|
56 | } |
---|
57 | } |
---|
58 | processCompletionResult(prefix, propositions); |
---|
59 | } |
---|
60 | |
---|
61 | @Override |
---|
62 | protected void joinableStart() { |
---|
63 | super.joinableStart(); |
---|
64 | connection.getListeners().add(this); |
---|
65 | } |
---|
66 | |
---|
67 | @Override |
---|
68 | protected void joinableInterrupt() { |
---|
69 | connection.getListeners().remove(this); |
---|
70 | super.joinableInterrupt(); |
---|
71 | } |
---|
72 | |
---|
73 | @Override |
---|
74 | public void connectionOutgoing(String line) { |
---|
75 | dispatchWrite(line); |
---|
76 | } |
---|
77 | |
---|
78 | @Override |
---|
79 | public void connectionIncomming(String line) { |
---|
80 | dispatchWrite(line); |
---|
81 | } |
---|
82 | } |
---|