[97] | 1 | package com.framsticks.gui.console; |
---|
| 2 | |
---|
| 3 | import java.awt.AWTKeyStroke; |
---|
| 4 | import java.awt.BorderLayout; |
---|
| 5 | import java.awt.KeyboardFocusManager; |
---|
| 6 | import java.awt.event.ActionEvent; |
---|
| 7 | import java.awt.event.ActionListener; |
---|
| 8 | import java.awt.event.KeyEvent; |
---|
| 9 | import java.awt.event.KeyListener; |
---|
| 10 | import java.awt.event.WindowAdapter; |
---|
| 11 | import java.awt.event.WindowEvent; |
---|
| 12 | import java.awt.font.FontRenderContext; |
---|
| 13 | import java.util.Collections; |
---|
| 14 | import java.util.Iterator; |
---|
| 15 | import java.util.LinkedList; |
---|
| 16 | import java.util.List; |
---|
| 17 | import java.util.ListIterator; |
---|
| 18 | |
---|
| 19 | import javax.swing.AbstractAction; |
---|
| 20 | import javax.swing.BorderFactory; |
---|
| 21 | import javax.swing.Box; |
---|
| 22 | import javax.swing.BoxLayout; |
---|
| 23 | import javax.swing.JButton; |
---|
| 24 | import javax.swing.JMenuItem; |
---|
| 25 | import javax.swing.JPopupMenu; |
---|
| 26 | import javax.swing.JTextField; |
---|
| 27 | |
---|
| 28 | import com.framsticks.params.annotations.FramsClassAnnotation; |
---|
| 29 | import com.framsticks.util.FramsticksException; |
---|
| 30 | import com.framsticks.util.lang.Strings; |
---|
| 31 | |
---|
| 32 | @FramsClassAnnotation |
---|
| 33 | public abstract class InteractiveConsole extends Console { |
---|
| 34 | |
---|
| 35 | /** |
---|
| 36 | * Text field for typing commands. |
---|
| 37 | */ |
---|
| 38 | protected JTextField commandLine; |
---|
| 39 | protected JButton sendButton; |
---|
| 40 | |
---|
| 41 | protected final LinkedList<String> history = new LinkedList<>(); |
---|
| 42 | protected ListIterator<String> historyCursor; |
---|
| 43 | |
---|
| 44 | public void setCommandLine(String command) { |
---|
| 45 | commandLine.setText(command); |
---|
| 46 | } |
---|
| 47 | |
---|
| 48 | /** |
---|
| 49 | * @param connection |
---|
| 50 | */ |
---|
| 51 | public InteractiveConsole() { |
---|
| 52 | } |
---|
| 53 | |
---|
| 54 | @Override |
---|
| 55 | protected void initializeGui() { |
---|
| 56 | // TODO Auto-generated method stub |
---|
| 57 | super.initializeGui(); |
---|
| 58 | |
---|
| 59 | commandLine = new JTextField(); |
---|
| 60 | |
---|
| 61 | commandLine.addKeyListener(new KeyListener() { |
---|
| 62 | @Override |
---|
| 63 | public void keyTyped(KeyEvent e) { |
---|
| 64 | |
---|
| 65 | } |
---|
| 66 | |
---|
| 67 | @Override |
---|
| 68 | public void keyPressed(KeyEvent e) { |
---|
| 69 | onKeyPressed(e.getKeyCode()); |
---|
| 70 | } |
---|
| 71 | |
---|
| 72 | @Override |
---|
| 73 | public void keyReleased(KeyEvent e) { |
---|
| 74 | } |
---|
| 75 | }); |
---|
| 76 | |
---|
| 77 | commandLine.setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, Collections.<AWTKeyStroke> emptySet()); |
---|
| 78 | |
---|
| 79 | completionPopup = new JPopupMenu(); |
---|
| 80 | |
---|
| 81 | // completionPopupAction = new AbstractAction() { |
---|
| 82 | // @Override |
---|
| 83 | // public void actionPerformed(ActionEvent event) { |
---|
| 84 | // JMenuItem item = (JMenuItem) event.getSource(); |
---|
| 85 | // commandLine.setText(item.getText()); |
---|
| 86 | // } |
---|
| 87 | // }; |
---|
| 88 | |
---|
| 89 | sendButton = new JButton("Send"); |
---|
| 90 | sendButton.setName("send"); |
---|
| 91 | sendButton.addActionListener(new ActionListener() { |
---|
| 92 | |
---|
| 93 | public void actionPerformed(final ActionEvent e) { |
---|
| 94 | sendCurrent(); |
---|
| 95 | } |
---|
| 96 | }); |
---|
| 97 | |
---|
| 98 | final Box cmdBox = new Box(BoxLayout.LINE_AXIS); |
---|
| 99 | cmdBox.add(commandLine); |
---|
| 100 | cmdBox.add(Box.createHorizontalStrut(10)); |
---|
| 101 | cmdBox.add(sendButton); |
---|
| 102 | cmdBox.setBorder(BorderFactory.createEmptyBorder(0, 7, 7, 7)); |
---|
| 103 | |
---|
| 104 | panel.add(cmdBox, BorderLayout.PAGE_END); |
---|
| 105 | |
---|
| 106 | getSwing().addWindowListener(new WindowAdapter() { |
---|
| 107 | public void windowOpened(WindowEvent e) { |
---|
| 108 | commandLine.requestFocus(); |
---|
| 109 | } |
---|
| 110 | }); |
---|
| 111 | |
---|
| 112 | |
---|
| 113 | } |
---|
| 114 | |
---|
| 115 | |
---|
| 116 | protected abstract void findCompletionPropositions(String prefix); |
---|
| 117 | |
---|
| 118 | @SuppressWarnings("serial") |
---|
| 119 | protected void processCompletionResult(String prefix, List<String> propositions) { |
---|
| 120 | assert isActive(); |
---|
| 121 | if (propositions.isEmpty()) { |
---|
| 122 | return; |
---|
| 123 | } |
---|
| 124 | if (!commandLine.getText().equals(prefix)) { |
---|
| 125 | return; |
---|
| 126 | } |
---|
| 127 | if (propositions.size() == 1) { |
---|
| 128 | commandLine.setText(propositions.get(0)); |
---|
| 129 | return; |
---|
| 130 | } |
---|
| 131 | |
---|
| 132 | Iterator<String> i = propositions.iterator(); |
---|
| 133 | String common = i.next(); |
---|
| 134 | while (i.hasNext()) { |
---|
| 135 | common = Strings.commonPrefix(common, i.next()); |
---|
| 136 | } |
---|
| 137 | if (!prefix.equals(common)) { |
---|
| 138 | commandLine.setText(common); |
---|
| 139 | return; |
---|
| 140 | } |
---|
| 141 | |
---|
| 142 | completionPopup.setVisible(false); |
---|
| 143 | completionPopup.removeAll(); |
---|
| 144 | |
---|
| 145 | for (final String c : propositions) { |
---|
| 146 | completionPopup.add(new JMenuItem(new AbstractAction(c) { |
---|
| 147 | @Override |
---|
| 148 | public void actionPerformed(ActionEvent e) { |
---|
| 149 | commandLine.setText(c); |
---|
| 150 | } |
---|
| 151 | })); |
---|
| 152 | } |
---|
| 153 | |
---|
| 154 | |
---|
| 155 | double width = commandLine.getFont().getStringBounds(prefix, new FontRenderContext(null, false, false)).getWidth(); |
---|
| 156 | completionPopup.show(commandLine, (int) width, 0); |
---|
| 157 | completionPopup.requestFocus(); |
---|
| 158 | } |
---|
| 159 | |
---|
| 160 | protected JPopupMenu completionPopup; |
---|
| 161 | |
---|
| 162 | |
---|
| 163 | private void onKeyPressed(int keyCode) { |
---|
| 164 | if (keyCode == KeyEvent.VK_TAB) { |
---|
| 165 | try { |
---|
| 166 | findCompletionPropositions(commandLine.getText()); |
---|
| 167 | } catch (FramsticksException e) { |
---|
| 168 | handle(e); |
---|
| 169 | } |
---|
| 170 | return; |
---|
| 171 | } |
---|
| 172 | |
---|
| 173 | if (keyCode == KeyEvent.VK_UP || keyCode == KeyEvent.VK_DOWN) { |
---|
| 174 | boolean up = (keyCode == KeyEvent.VK_UP); |
---|
| 175 | if (history.isEmpty()) { |
---|
| 176 | return; |
---|
| 177 | } |
---|
| 178 | |
---|
| 179 | String line; |
---|
| 180 | if (up) { |
---|
| 181 | if (historyCursor == null) { |
---|
| 182 | historyCursor = history.listIterator(0); |
---|
| 183 | } |
---|
| 184 | if (historyCursor.hasNext()) { |
---|
| 185 | line = historyCursor.next(); |
---|
| 186 | } else { |
---|
| 187 | historyCursor = null; |
---|
| 188 | line = ""; |
---|
| 189 | } |
---|
| 190 | } else { |
---|
| 191 | if (historyCursor == null) { |
---|
| 192 | historyCursor = history.listIterator(history.size()); |
---|
| 193 | } |
---|
| 194 | if (historyCursor.hasPrevious()) { |
---|
| 195 | line = historyCursor.previous(); |
---|
| 196 | } else { |
---|
| 197 | historyCursor = null; |
---|
| 198 | line = ""; |
---|
| 199 | } |
---|
| 200 | } |
---|
| 201 | commandLine.setText(line); |
---|
| 202 | return; |
---|
| 203 | } |
---|
| 204 | |
---|
| 205 | if (keyCode == KeyEvent.VK_ENTER) { |
---|
| 206 | sendCurrent(); |
---|
| 207 | return; |
---|
| 208 | } |
---|
| 209 | |
---|
| 210 | } |
---|
| 211 | |
---|
| 212 | protected abstract void sendImplementation(String line); |
---|
| 213 | |
---|
| 214 | /** |
---|
| 215 | * Sends message to manager and adds message to console frame. |
---|
| 216 | * |
---|
| 217 | * Message is not put into the text area by that method, because |
---|
| 218 | * in DirectConsole it is done by means of listening on Connection. |
---|
| 219 | */ |
---|
| 220 | public void sendCurrent() { |
---|
| 221 | assert isActive(); |
---|
| 222 | String line = commandLine.getText(); |
---|
| 223 | history.remove(line); |
---|
| 224 | history.add(0, line); |
---|
| 225 | historyCursor = null; |
---|
| 226 | commandLine.setText(""); |
---|
| 227 | |
---|
| 228 | sendImplementation(line); |
---|
| 229 | |
---|
| 230 | } |
---|
| 231 | |
---|
| 232 | /** |
---|
| 233 | * Adds query to console window. |
---|
| 234 | * |
---|
| 235 | * @param line Line of string query. |
---|
| 236 | */ |
---|
| 237 | protected void paintLine(String line) { |
---|
| 238 | consolePainter.userLine(line); |
---|
| 239 | } |
---|
| 240 | |
---|
| 241 | |
---|
| 242 | } |
---|