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

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

HIGHLIGHTS:

for Joinables running

CHANGELOG:
Add WorkPackageLogic? and classes representing prime experiment state.

Add classes for PrimeExperiment? state.

Extract single netload routine in Simulator.

Working netload with dummy content in PrimeExperiment?.

More development with NetLoadSaveLogic? and PrimeExperiment?.

Improvement around prime.

Improve BufferedDispatcher?.isActive logic.

Add prime-all.xml configuration.

Manual connecting to existing simulators from GUI.

Guard in SimulatorConnector? against expdef mismatch.

Guard against empty target dispatcher in BufferedDispatcher?.

Make BufferedDispatcher? a Dispatcher (and Joinable).

Minor improvements.

Done StackedJoinable?, improve Experiment.

Develop StackedJoinable?.

Add StackedJoinable? utility joinables controller.

Add dependency on apache-commons-lang.

Add ready ListChange? on Simulators.

Improve hints in ListChange?.

Several improvements.

Found bug with dispatching in Experiment.

Minor improvements.

Fix bug with early finishing Server.

Many changes in Dispatching.

Fix bug with connection.

Do not obfuscate log with socket related exceptions.

Add SocketClosedException?.

Add SimulatorConnector?.

Work out conception of experiment composing of logics building blocks.

Rename SinkInterface? to Sink.

Move saving of Accesses into AccessOperations?.

Some improvements to Experiment.

Improve joinables.

Fix issue with joinables closing.

Add direct and managed consoles to popup menu.

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                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}
Note: See TracBrowser for help on using the repository browser.