source: java/main/src/main/java/com/framsticks/gui/console/InteractiveConsole.java @ 97

Last change on this file since 97 was 97, checked in by psniegowski, 11 years ago

HIGHLIGHTS:

  • add proper exception passing between communication sides:

if exception occur during handling client request, it is
automatically passed as comment to error response.

it may be used to snoop communication between peers

  • fix algorithm choosing text controls in GUI
  • allow GUI testing in virtual frame buffer (xvfb)

FEST had some problem with xvfb but workaround was found

supports tab-completion based on requests history

CHANGELOG:
Further improve handling of exceptions in GUI.

Add StatusBar? implementing ExceptionResultHandler?.

Make completion processing asynchronous.

Minor changes.

Improve completion in console.

Improve history in InteractiveConsole?.

First working version of DirectConsole?.

Minor changes.

Make Connection.address non final.

It is more suitable to use in configuration.

Improvement of consoles.

Improve PopupMenu? and closing of FrameJoinable?.

Fix BrowserTest?.

Found bug with FEST running under xvfb.

JButtonFixture.click() is not working under xvfb.
GuiTest? has wrapper which uses JButton.doClick() directly.

Store CompositeParam? param in TreeNode?.

Simplify ClientSideManagedConnection? connecting.

There is now connectedFunctor needed, ApplicationRequests? can be
send right after creation. They are buffered until the version
and features are negotiated.

Narow down interface of ClientSideManagedConnection?.

Allow that connection specialization send only
ApplicationRequests?.

Improve policy of text control choosing.

Change name of Genotype in BrowserTest?.

Make BrowserTest? change name of Genotype.

Minor change.

First working draft of TrackConsole?.

Simplify Consoles.

More improvements with gui joinables.

Unify initialization on gui joinables.

More rework of Frame based entities.

Refactorize structure of JFrames based entities.

Extract GuiTest? from BrowserBaseTest?.

Reorganize Console classes structure.

Add Collection view to JoinableCollection?.

Configure timeout in testing.

Minor changes.

Rework connections hierarchy.

Add Mode to the get operation.

Make get and set in Tree take PrimitiveParam?.

Unify naming of operations.

Make RunAt? use the given ExceptionHandler?.

It wraps the virtual runAt() method call with
try-catch passing exception to handler.

Force RunAt? to include ExceptionHandler?.

Improve ClientAtServer?.

Minor change.

Another sweep with FindBugs?.

Rename Instance to Tree.

Minor changes.

Minor changes.

Further clarify semantics of Futures.

Add FutureHandler?.

FutureHandler? is refinement of Future, that proxifies
exception handling to ExceptionResultHandler? given
at construction time.

Remove StateFunctor? (use Future<Void> instead).

Make Connection use Future<Void>.

Unparametrize *ResponseFuture?.

Remove StateCallback? not needed anymore.

Distinguish between sides of ResponseFuture?.

Base ResponseCallback? on Future (now ResponseFuture?).

Make asynchronous store taking Future for flags.

Implement storeValue in ObjectInstance?.

File size: 5.5 KB
Line 
1package com.framsticks.gui.console;
2
3import java.awt.AWTKeyStroke;
4import java.awt.BorderLayout;
5import java.awt.KeyboardFocusManager;
6import java.awt.event.ActionEvent;
7import java.awt.event.ActionListener;
8import java.awt.event.KeyEvent;
9import java.awt.event.KeyListener;
10import java.awt.event.WindowAdapter;
11import java.awt.event.WindowEvent;
12import java.awt.font.FontRenderContext;
13import java.util.Collections;
14import java.util.Iterator;
15import java.util.LinkedList;
16import java.util.List;
17import java.util.ListIterator;
18
19import javax.swing.AbstractAction;
20import javax.swing.BorderFactory;
21import javax.swing.Box;
22import javax.swing.BoxLayout;
23import javax.swing.JButton;
24import javax.swing.JMenuItem;
25import javax.swing.JPopupMenu;
26import javax.swing.JTextField;
27
28import com.framsticks.params.annotations.FramsClassAnnotation;
29import com.framsticks.util.FramsticksException;
30import com.framsticks.util.lang.Strings;
31
32@FramsClassAnnotation
33public 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}
Note: See TracBrowser for help on using the repository browser.