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