1 | package com.framsticks.gui.console; |
---|
2 | |
---|
3 | import java.util.LinkedList; |
---|
4 | import java.util.List; |
---|
5 | |
---|
6 | import com.framsticks.communication.ClientSideManagedConnection; |
---|
7 | import com.framsticks.communication.ClientSideResponseFuture; |
---|
8 | import com.framsticks.communication.File; |
---|
9 | import com.framsticks.communication.Request; |
---|
10 | import com.framsticks.communication.Response; |
---|
11 | import com.framsticks.communication.queries.ApplicationRequest; |
---|
12 | |
---|
13 | import static com.framsticks.structure.TreeOperations.*; |
---|
14 | |
---|
15 | import com.framsticks.gui.SwingDispatcher; |
---|
16 | import com.framsticks.params.CompositeParam; |
---|
17 | import com.framsticks.params.annotations.AutoAppendAnnotation; |
---|
18 | import com.framsticks.params.annotations.FramsClassAnnotation; |
---|
19 | import com.framsticks.params.types.ListParam; |
---|
20 | import com.framsticks.remote.RemoteTree; |
---|
21 | import com.framsticks.structure.Path; |
---|
22 | import com.framsticks.util.FramsticksException; |
---|
23 | import com.framsticks.util.dispatching.Dispatching; |
---|
24 | import com.framsticks.util.dispatching.Future; |
---|
25 | import com.framsticks.util.dispatching.RunAt; |
---|
26 | import com.framsticks.util.lang.Casting; |
---|
27 | import com.framsticks.util.lang.Containers; |
---|
28 | import com.framsticks.util.lang.Pair; |
---|
29 | import com.framsticks.util.lang.Strings; |
---|
30 | |
---|
31 | @FramsClassAnnotation |
---|
32 | public class ManagedConsole extends InteractiveConsole { |
---|
33 | |
---|
34 | |
---|
35 | protected RemoteTree tree; |
---|
36 | |
---|
37 | /** |
---|
38 | * @param connection |
---|
39 | */ |
---|
40 | public ManagedConsole() { |
---|
41 | } |
---|
42 | |
---|
43 | /** |
---|
44 | * @return the connection |
---|
45 | */ |
---|
46 | @Override |
---|
47 | public ClientSideManagedConnection getConnection() { |
---|
48 | return (ClientSideManagedConnection) connection; |
---|
49 | } |
---|
50 | |
---|
51 | |
---|
52 | protected void sendImplementation(String line) { |
---|
53 | if (!connection.isConnected()) { |
---|
54 | throw new FramsticksException().msg("not connected").arg("console", this); |
---|
55 | } |
---|
56 | //Move that to managed connection |
---|
57 | ApplicationRequest request; |
---|
58 | try { |
---|
59 | Pair<String, String> command = Strings.splitIntoPair(line, ' ', ""); |
---|
60 | request = Casting.throwCast(ApplicationRequest.class, Request.createRequestByTypeString(command.first)); |
---|
61 | request.parseRest(command.second); |
---|
62 | } catch (FramsticksException e) { |
---|
63 | throw new FramsticksException().msg("invalid line").arg("line", line).cause(e); |
---|
64 | } |
---|
65 | |
---|
66 | paintLine(line); |
---|
67 | |
---|
68 | getConnection().send(request, SwingDispatcher.getInstance(), new ClientSideResponseFuture(this) { |
---|
69 | @Override |
---|
70 | protected void processOk(Response response) { |
---|
71 | for (File f : response.getFiles()) { |
---|
72 | consolePainter.paintMessage(f); |
---|
73 | } |
---|
74 | } |
---|
75 | }); |
---|
76 | } |
---|
77 | |
---|
78 | |
---|
79 | @Override |
---|
80 | protected void findCompletionPropositions(final String prefix) { |
---|
81 | Pair<CharSequence, CharSequence> command = Request.takeIdentifier(prefix); |
---|
82 | if (command == null) { |
---|
83 | return; |
---|
84 | } |
---|
85 | |
---|
86 | Casting.throwCast(ApplicationRequest.class, Request.createRequestByTypeString(command.first.toString())); |
---|
87 | // Pair<String, String> rest = Strings |
---|
88 | Pair<CharSequence, CharSequence> rest = Request.takeIdentifier(command.second); |
---|
89 | if (rest == null) { |
---|
90 | List<String> propositions = new LinkedList<String>(); |
---|
91 | propositions.add(command.first.toString() + " /"); |
---|
92 | processCompletionResult(prefix, propositions); |
---|
93 | return; |
---|
94 | } |
---|
95 | |
---|
96 | final String textual = rest.first.toString(); |
---|
97 | if (!textual.startsWith("/")) { |
---|
98 | throw new FramsticksException().msg("invalid line").arg("line", prefix); |
---|
99 | } |
---|
100 | // final Iterator<String> iterator = Path.splitPath(textual); |
---|
101 | |
---|
102 | tryGet(tree, textual, new Future<Path>(this) { |
---|
103 | |
---|
104 | @Override |
---|
105 | protected void result(final Path path) { |
---|
106 | if (!textual.startsWith(path.getTextual())) { |
---|
107 | throw new FramsticksException().msg("invalid state").arg("line", prefix).arg("path", path); |
---|
108 | } |
---|
109 | assert path.getTree().isActive(); |
---|
110 | |
---|
111 | final Runnable finalizeCompletion = new Runnable() { |
---|
112 | @Override |
---|
113 | public void run() { |
---|
114 | String remaining = textual.substring(path.getTextual().length()); |
---|
115 | |
---|
116 | String base = prefix.substring(0, prefix.length() - (textual.length() - path.getTextual().length())); |
---|
117 | if (path.size() > 1) { |
---|
118 | base = base + "/"; |
---|
119 | } |
---|
120 | |
---|
121 | if (remaining.startsWith("/")) { |
---|
122 | remaining = remaining.substring(1); |
---|
123 | } |
---|
124 | |
---|
125 | if (remaining.indexOf('/') != -1) { |
---|
126 | /** It is to long. */ |
---|
127 | return; |
---|
128 | } |
---|
129 | final List<String> propositions = new LinkedList<String>(); |
---|
130 | for (CompositeParam p : Containers.filterInstanceof(bindAccess(path).getParams(), CompositeParam.class)) { |
---|
131 | if (remaining.equals("") || p.getId().startsWith(remaining)) { |
---|
132 | propositions.add(base + p.getId()); |
---|
133 | } |
---|
134 | } |
---|
135 | |
---|
136 | dispatch(new RunAt<ManagedConsole>(ManagedConsole.this) { |
---|
137 | |
---|
138 | @Override |
---|
139 | protected void runAt() { |
---|
140 | processCompletionResult(prefix, propositions); |
---|
141 | } |
---|
142 | }); |
---|
143 | } |
---|
144 | }; |
---|
145 | |
---|
146 | if (path.getTop().getParam() instanceof ListParam) { |
---|
147 | tree.get(path, new Future<Path>(ManagedConsole.this) { |
---|
148 | @Override |
---|
149 | protected void result(Path result) { |
---|
150 | finalizeCompletion.run(); |
---|
151 | } |
---|
152 | }); |
---|
153 | return; |
---|
154 | } |
---|
155 | finalizeCompletion.run(); |
---|
156 | |
---|
157 | } |
---|
158 | }); |
---|
159 | |
---|
160 | |
---|
161 | } |
---|
162 | |
---|
163 | @AutoAppendAnnotation |
---|
164 | public ManagedConsole setTree(RemoteTree tree) { |
---|
165 | this.tree = tree; |
---|
166 | connection = tree.getConnection(); |
---|
167 | return this; |
---|
168 | } |
---|
169 | |
---|
170 | @Override |
---|
171 | protected void joinableStart() { |
---|
172 | super.joinableStart(); |
---|
173 | Dispatching.use(tree, this); |
---|
174 | } |
---|
175 | |
---|
176 | @Override |
---|
177 | protected void joinableInterrupt() { |
---|
178 | Dispatching.drop(tree, this); |
---|
179 | super.joinableInterrupt(); |
---|
180 | } |
---|
181 | |
---|
182 | @Override |
---|
183 | protected void joinableJoin() throws InterruptedException { |
---|
184 | Dispatching.join(tree); |
---|
185 | super.joinableJoin(); |
---|
186 | } |
---|
187 | } |
---|