Skip to content

Commit b968169

Browse files
committed
working non-wonky key typing pass-through
The key was using keyTyped instead of keyDown, which allows closing and passing the key type to the parent, which inserts the typed letter.. Also select position 0 by default. Also show and clear instructions.
1 parent 954a473 commit b968169

File tree

4 files changed

+51
-16
lines changed

4 files changed

+51
-16
lines changed

src/main/java/edu/umich/soar/visualsoar/components/AutocompletePopup.java

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,5 @@
11
package edu.umich.soar.visualsoar.components;
22

3-
import javax.swing.BorderFactory;
4-
import javax.swing.JList;
5-
import javax.swing.JPanel;
6-
import javax.swing.JPopupMenu;
7-
import javax.swing.JScrollPane;
8-
import javax.swing.ListSelectionModel;
9-
import javax.swing.text.BadLocationException;
10-
import javax.swing.text.JTextComponent;
113
import java.awt.BorderLayout;
124
import java.awt.Color;
135
import java.awt.Dimension;
@@ -22,8 +14,15 @@
2214
import java.util.Set;
2315
import java.util.Vector;
2416
import java.util.function.Consumer;
17+
import javax.swing.BorderFactory;
18+
import javax.swing.JList;
19+
import javax.swing.JPanel;
20+
import javax.swing.JPopupMenu;
21+
import javax.swing.JScrollPane;
22+
import javax.swing.ListSelectionModel;
23+
import javax.swing.text.BadLocationException;
24+
import javax.swing.text.JTextComponent;
2525

26-
// WIP; need to draw out state diagram. Log 40 minutes.
2726
public class AutocompletePopup extends JPopupMenu {
2827

2928
private static final Set<Integer> CURSOR_MOVEMENT_PASSTHROUGH_KEYS =
@@ -41,6 +40,7 @@ public AutocompletePopup(
4140
int maxVisibleRows = Math.min(suggestions.size(), 10); // Limit to 10 rows max
4241
// Create the suggestion list
4342
JList<String> suggestionList = new JList<>(new Vector<>(suggestions));
43+
suggestionList.setSelectedIndex(0);
4444
suggestionList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
4545
suggestionList.setVisibleRowCount(maxVisibleRows);
4646

@@ -74,11 +74,6 @@ public void mouseMoved(MouseEvent e) {
7474
new KeyAdapter() {
7575
@Override
7676
public void keyPressed(KeyEvent e) {
77-
if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
78-
setVisible(false);
79-
e.consume();
80-
return;
81-
}
8277
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
8378
int index = suggestionList.getSelectedIndex();
8479
if (index >= 0) {
@@ -89,6 +84,8 @@ public void keyPressed(KeyEvent e) {
8984
return;
9085
}
9186
}
87+
// UP/DOWN keys navigate selection; custom handling here allows wrapping between
88+
// start/end of list
9289
if (e.getKeyCode() == KeyEvent.VK_UP || e.getKeyCode() == KeyEvent.VK_DOWN) {
9390
int index = suggestionList.getSelectedIndex();
9491
if (index == -1) {
@@ -106,13 +103,20 @@ public void keyPressed(KeyEvent e) {
106103
e.consume();
107104
return;
108105
}
106+
// Unambiguous cursor movement keys are passed through to parent after closing this
107+
// popup
109108
if (CURSOR_MOVEMENT_PASSTHROUGH_KEYS.contains(e.getKeyCode())) {
110109
setVisible(false);
111-
// Pass the event to the parent component to handle cursor movement
112110
parent.dispatchEvent(e);
113-
return;
114111
}
115112
}
113+
114+
@Override
115+
public void keyTyped(KeyEvent e) {
116+
// typing closes the popup and types in the parent
117+
setVisible(false);
118+
parent.dispatchEvent(e);
119+
}
116120
});
117121

118122
// Calculate the preferred height based on the number of items
@@ -145,4 +149,9 @@ public void keyPressed(KeyEvent e) {
145149
ex.printStackTrace();
146150
}
147151
}
152+
153+
154+
public String shortInstructions() {
155+
return "UP/DOWN to select; ENTER to confirm";
156+
}
148157
}

src/main/java/edu/umich/soar/visualsoar/mainframe/feedback/FeedbackManager.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,10 @@ public void clearFeedback() {
9090
onCountUpdate.accept(feedbackList.getListSize());
9191
}
9292

93+
public void clearStatusBar() {
94+
statusBar.setText("");
95+
}
96+
9397
/**
9498
* Method updates the status bar text with a message
9599
*/

src/main/java/edu/umich/soar/visualsoar/ruleeditor/EditorPane.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ public static void setCurrentSelectionOccurrenceHighlightColor(Color c) {
3535
occurrenceHighlightPainter = new DefaultHighlighter.DefaultHighlightPainter(c);
3636
}
3737

38+
protected void processKeyEvent(KeyEvent e) {
39+
super.processKeyEvent(e);
40+
}
41+
3842
public EditorPane() {
3943

4044
if (Prefs.highlightingEnabled.getBoolean()) {

src/main/java/edu/umich/soar/visualsoar/ruleeditor/RuleEditor.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1845,6 +1845,24 @@ private void showAutocompletePopup(int pos, String userType, List<String> comple
18451845
return;
18461846
}
18471847
autocompletePopup = new AutocompletePopup(editorPane, pos, completeMatches, (selected) -> insertCompletion(pos, userType, selected));
1848+
MainFrame.getMainFrame().getFeedbackManager().setStatusBarMsg(autocompletePopup.shortInstructions());
1849+
autocompletePopup.addPopupMenuListener(new PopupMenuListener() {
1850+
@Override
1851+
public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
1852+
1853+
}
1854+
1855+
@Override
1856+
public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
1857+
MainFrame.getMainFrame().getFeedbackManager().clearStatusBar();
1858+
autocompletePopup = null;
1859+
}
1860+
1861+
@Override
1862+
public void popupMenuCanceled(PopupMenuEvent e) {
1863+
1864+
}
1865+
});
18481866
}
18491867

18501868
private void hideAutocompletePopup() {

0 commit comments

Comments
 (0)