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 | super.initializeGui(); |
---|
57 | |
---|
58 | commandLine = new JTextField(); |
---|
59 | |
---|
60 | commandLine.addKeyListener(new KeyListener() { |
---|
61 | @Override |
---|
62 | public void keyTyped(KeyEvent e) { |
---|
63 | |
---|
64 | } |
---|
65 | |
---|
66 | @Override |
---|
67 | public void keyPressed(KeyEvent e) { |
---|
68 | onKeyPressed(e.getKeyCode()); |
---|
69 | } |
---|
70 | |
---|
71 | @Override |
---|
72 | public void keyReleased(KeyEvent e) { |
---|
73 | } |
---|
74 | }); |
---|
75 | |
---|
76 | commandLine.setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, Collections.<AWTKeyStroke> emptySet()); |
---|
77 | |
---|
78 | completionPopup = new JPopupMenu(); |
---|
79 | |
---|
80 | // completionPopupAction = new AbstractAction() { |
---|
81 | // @Override |
---|
82 | // public void actionPerformed(ActionEvent event) { |
---|
83 | // JMenuItem item = (JMenuItem) event.getSource(); |
---|
84 | // commandLine.setText(item.getText()); |
---|
85 | // } |
---|
86 | // }; |
---|
87 | |
---|
88 | sendButton = new JButton("Send"); |
---|
89 | sendButton.setName("send"); |
---|
90 | sendButton.addActionListener(new ActionListener() { |
---|
91 | |
---|
92 | public void actionPerformed(final ActionEvent e) { |
---|
93 | sendCurrent(); |
---|
94 | } |
---|
95 | }); |
---|
96 | |
---|
97 | final Box cmdBox = new Box(BoxLayout.LINE_AXIS); |
---|
98 | cmdBox.add(commandLine); |
---|
99 | cmdBox.add(Box.createHorizontalStrut(10)); |
---|
100 | cmdBox.add(sendButton); |
---|
101 | cmdBox.setBorder(BorderFactory.createEmptyBorder(0, 7, 7, 7)); |
---|
102 | |
---|
103 | panel.add(cmdBox, BorderLayout.PAGE_END); |
---|
104 | |
---|
105 | getSwing().addWindowListener(new WindowAdapter() { |
---|
106 | public void windowOpened(WindowEvent e) { |
---|
107 | commandLine.requestFocus(); |
---|
108 | } |
---|
109 | }); |
---|
110 | } |
---|
111 | |
---|
112 | |
---|
113 | protected abstract void findCompletionPropositions(String prefix); |
---|
114 | |
---|
115 | @SuppressWarnings("serial") |
---|
116 | protected void processCompletionResult(String prefix, List<String> propositions) { |
---|
117 | assert isActive(); |
---|
118 | if (propositions.isEmpty()) { |
---|
119 | return; |
---|
120 | } |
---|
121 | if (!commandLine.getText().equals(prefix)) { |
---|
122 | return; |
---|
123 | } |
---|
124 | if (propositions.size() == 1) { |
---|
125 | commandLine.setText(propositions.get(0)); |
---|
126 | return; |
---|
127 | } |
---|
128 | |
---|
129 | Iterator<String> i = propositions.iterator(); |
---|
130 | String common = i.next(); |
---|
131 | while (i.hasNext()) { |
---|
132 | common = Strings.commonPrefix(common, i.next()); |
---|
133 | } |
---|
134 | if (!prefix.equals(common)) { |
---|
135 | commandLine.setText(common); |
---|
136 | return; |
---|
137 | } |
---|
138 | |
---|
139 | completionPopup.setVisible(false); |
---|
140 | completionPopup.removeAll(); |
---|
141 | |
---|
142 | for (final String c : propositions) { |
---|
143 | completionPopup.add(new JMenuItem(new AbstractAction(c) { |
---|
144 | @Override |
---|
145 | public void actionPerformed(ActionEvent e) { |
---|
146 | commandLine.setText(c); |
---|
147 | } |
---|
148 | })); |
---|
149 | } |
---|
150 | |
---|
151 | |
---|
152 | double width = commandLine.getFont().getStringBounds(prefix, new FontRenderContext(null, false, false)).getWidth(); |
---|
153 | completionPopup.show(commandLine, (int) width, 0); |
---|
154 | completionPopup.requestFocus(); |
---|
155 | } |
---|
156 | |
---|
157 | protected JPopupMenu completionPopup; |
---|
158 | |
---|
159 | |
---|
160 | private void onKeyPressed(int keyCode) { |
---|
161 | if (keyCode == KeyEvent.VK_TAB) { |
---|
162 | try { |
---|
163 | findCompletionPropositions(commandLine.getText()); |
---|
164 | } catch (FramsticksException e) { |
---|
165 | handle(e); |
---|
166 | } |
---|
167 | return; |
---|
168 | } |
---|
169 | |
---|
170 | if (keyCode == KeyEvent.VK_UP || keyCode == KeyEvent.VK_DOWN) { |
---|
171 | boolean up = (keyCode == KeyEvent.VK_UP); |
---|
172 | if (history.isEmpty()) { |
---|
173 | return; |
---|
174 | } |
---|
175 | |
---|
176 | String line; |
---|
177 | if (up) { |
---|
178 | if (historyCursor == null) { |
---|
179 | historyCursor = history.listIterator(0); |
---|
180 | } |
---|
181 | if (historyCursor.hasNext()) { |
---|
182 | line = historyCursor.next(); |
---|
183 | } else { |
---|
184 | historyCursor = null; |
---|
185 | line = ""; |
---|
186 | } |
---|
187 | } else { |
---|
188 | if (historyCursor == null) { |
---|
189 | historyCursor = history.listIterator(history.size()); |
---|
190 | } |
---|
191 | if (historyCursor.hasPrevious()) { |
---|
192 | line = historyCursor.previous(); |
---|
193 | } else { |
---|
194 | historyCursor = null; |
---|
195 | line = ""; |
---|
196 | } |
---|
197 | } |
---|
198 | commandLine.setText(line); |
---|
199 | return; |
---|
200 | } |
---|
201 | |
---|
202 | if (keyCode == KeyEvent.VK_ENTER) { |
---|
203 | sendCurrent(); |
---|
204 | return; |
---|
205 | } |
---|
206 | |
---|
207 | } |
---|
208 | |
---|
209 | protected abstract void sendImplementation(String line); |
---|
210 | |
---|
211 | /** |
---|
212 | * Sends message to manager and adds message to console frame. |
---|
213 | * |
---|
214 | * Message is not put into the text area by that method, because |
---|
215 | * in DirectConsole it is done by means of listening on Connection. |
---|
216 | */ |
---|
217 | public void sendCurrent() { |
---|
218 | assert isActive(); |
---|
219 | String line = commandLine.getText(); |
---|
220 | history.remove(line); |
---|
221 | history.add(0, line); |
---|
222 | historyCursor = null; |
---|
223 | commandLine.setText(""); |
---|
224 | |
---|
225 | sendImplementation(line); |
---|
226 | |
---|
227 | } |
---|
228 | |
---|
229 | /** |
---|
230 | * Adds query to console window. |
---|
231 | * |
---|
232 | * @param line Line of string query. |
---|
233 | */ |
---|
234 | protected void paintLine(String line) { |
---|
235 | consolePainter.userLine(line); |
---|
236 | } |
---|
237 | |
---|
238 | |
---|
239 | } |
---|