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

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

HIGHLIGHTS:

CHANGELOG:
Get data also on tree expansion.

Use nice framstick icon for empty nodes.

Update panel after reload if it is current.

Add shallow reload procedure.

Cut Gui prefix from several tree classes.

Bring back counter of GuiTreeNode?.

Use IdentityHashMap? were it is more appriopriate.

Remove TreeListener?.

Do not use TreeListener? in GUI.

Minor change.

Done migration to GuiTreeModel?.

BrowserTest? in that version always crashes frams.linux.

Move rendering implementation into GuiAbstractNode?.

Use hand-crafted list in GuiTreeNode?.

Generally, it would be a great place for WeakIdentityHashMap?
(but there is none in Java Collection Framework).

Remove superfluous logging.

Fix bug in GuiTreeNode?.

Use IdentityHashMap? instead of HashMap?.

Improve structure update.

Filter out invalid uids in UniqueListAccess?.

Improve TreeCellRenderer?.

Add filtering in TrackConsole?.

Improve TreeModel?.

More changes.

More improvements.

More changes.

Remove TreeNode?.

Support MetaNode? in the GuiTreeModel?.

Implement more in GuiTreeModel?.

Add CompositeParam? interface to FramsClass? and AccessInterface?.

Allow access by number to UniqueList?.

Add UidComparator?.

Use TreeMap? as a default accessee in unique list.

It keeps order of keys.

Introduce classes to use with new TreeModel?.

Another step.

Migrate from TreeNode? to Node in many places.

Remove some uses of TreeNode? as DefaultMutableTreeNode?.

Remove Path from TreeNode? interface.

Remove Path from TreeNode?.

Add Path recration from node feature.

Reworking TreeCellRenderer?.

Minor change of TreeOperations? interface.

Remove last methods from TreeNode?.

Another minor step.

Do not store reference to TreeAtFrame? in TreeNode?.

Add proxy exceptionHandler to StatusBar?.

Move panels management to TreeAtFrame?.

Store localChanges in the NodeAtFrame?.

More cleanup.

Move name computing to TreeCellRenderer?.

Move tooltip and icon computations to TreeCellRenderer?.

More dispatches removed.

Remove most dispatching from TreeNode?.

TreeNode? does not actually redispatch tasks.

Make Tree embedded in Browser use SwingDispatcher?.

Make lazy binding of Tree with Dispatcher.

Minor changes.

Organizational change in AbstractTree?.

Make AbstractTree? compose from Thread instead of inherit from it.

Make SwingDispatcher? and AtOnceDispatcher? Joinable compatible.

Add ListPanelProvider?.

Improve Controls readonly and enabled handling.

Properly pass ExceptionHandlers? in more places.

Make Tree.get accept ValueParam?.

  • This is to allow access number of list elements.

Remove not needed get redirection in ClientAtServer?.

Rename tryResolve to tryGet.

Unify tryResolveAndGet into tryResolve.

Remove resolveTop from Tree interface.

Make Tree.get accept Future<Path>.

Use get to implement resolveTop also in ObjectTree?.

Unify resolveTop and get in RemoteTree?.

Another minor step.

More minor changes in tree operations.

Minor organizational changes.

In RemoteTree? first fetch info for root.

Reworking resolving.

Minor changes.

Make ListAccess? return proxy iterators (instead of creating temporary collection).

Let AccessInterface? return Iterable<Param>.

Improve resolving.

More improvements.

First working completion in ManagedConsole?.

Rename resolve to resolveTop.

This reflects the actuall functionality.

Change semantic of tryResolve and tryResolveAndGet.

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        protected abstract void findCompletionPropositions(String prefix);
115
116        @SuppressWarnings("serial")
117        protected void processCompletionResult(String prefix, List<String> propositions) {
118                assert isActive();
119                if (propositions.isEmpty()) {
120                        return;
121                }
122                if (!commandLine.getText().equals(prefix)) {
123                        return;
124                }
125                if (propositions.size() == 1) {
126                        commandLine.setText(propositions.get(0));
127                        return;
128                }
129
130                Iterator<String> i = propositions.iterator();
131                String common = i.next();
132                while (i.hasNext()) {
133                        common = Strings.commonPrefix(common, i.next());
134                }
135                if (!prefix.equals(common)) {
136                        commandLine.setText(common);
137                        return;
138                }
139
140                completionPopup.setVisible(false);
141                completionPopup.removeAll();
142
143                for (final String c : propositions) {
144                        completionPopup.add(new JMenuItem(new AbstractAction(c) {
145                                @Override
146                                public void actionPerformed(ActionEvent e) {
147                                        commandLine.setText(c);
148                                }
149                        }));
150                }
151
152
153                double width = commandLine.getFont().getStringBounds(prefix, new FontRenderContext(null, false, false)).getWidth();
154                completionPopup.show(commandLine, (int) width, 0);
155                completionPopup.requestFocus();
156        }
157
158        protected JPopupMenu completionPopup;
159
160
161        private void onKeyPressed(int keyCode) {
162                if (keyCode == KeyEvent.VK_TAB) {
163                        try {
164                                findCompletionPropositions(commandLine.getText());
165                        } catch (FramsticksException e) {
166                                handle(e);
167                        }
168                        return;
169                }
170
171                if (keyCode == KeyEvent.VK_UP || keyCode == KeyEvent.VK_DOWN) {
172                        boolean up = (keyCode == KeyEvent.VK_UP);
173                        if (history.isEmpty()) {
174                                return;
175                        }
176
177                        String line;
178                        if (up) {
179                                if (historyCursor == null) {
180                                        historyCursor = history.listIterator(0);
181                                }
182                                if (historyCursor.hasNext()) {
183                                        line = historyCursor.next();
184                                } else {
185                                        historyCursor = null;
186                                        line = "";
187                                }
188                        } else {
189                                if (historyCursor == null) {
190                                        historyCursor = history.listIterator(history.size());
191                                }
192                                if (historyCursor.hasPrevious()) {
193                                        line = historyCursor.previous();
194                                } else {
195                                        historyCursor = null;
196                                        line = "";
197                                }
198                        }
199                        commandLine.setText(line);
200                        return;
201                }
202
203                if (keyCode == KeyEvent.VK_ENTER) {
204                        sendCurrent();
205                        return;
206                }
207
208        }
209
210        protected abstract void sendImplementation(String line);
211
212        /**
213         * Sends message to manager and adds message to console frame.
214         *
215         * Message is not put into the text area by that method, because
216         * in DirectConsole it is done by means of listening on Connection.
217         */
218        public void sendCurrent() {
219                assert isActive();
220                String line = commandLine.getText();
221                history.remove(line);
222                history.add(0, line);
223                historyCursor = null;
224                commandLine.setText("");
225
226                sendImplementation(line);
227
228        }
229
230        /**
231         * Adds query to console window.
232         *
233         * @param line Line of string query.
234         */
235        protected void paintLine(String line) {
236                consolePainter.userLine(line);
237        }
238
239
240}
Note: See TracBrowser for help on using the repository browser.