Skip to content

Commit 8cbe762

Browse files
committed
WIP fixed backspace behavior
1 parent 4bd92d6 commit 8cbe762

File tree

3 files changed

+30
-42
lines changed

3 files changed

+30
-42
lines changed

src/main/java/edu/umich/soar/visualsoar/components/AutocompleteData.java renamed to src/main/java/edu/umich/soar/visualsoar/components/AutocompleteContext.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
import java.util.List;
77
import java.util.stream.Collectors;
88

9-
/** Manage the suggestion list for auto-completion of a user's typing. */
10-
public class AutocompleteData {
9+
/** Manages the suggestion list for auto-completion of a user's typing. */
10+
public class AutocompleteContext {
1111
private String currentInput;
1212
private final List<String> allSuggestions;
1313
private List<String> currentSuggestions = Collections.emptyList();
@@ -16,15 +16,15 @@ public class AutocompleteData {
1616
* @param currentInput The user's input so far this auto-completion.
1717
* @param allSuggestions All suggestions to be filtered down by the user's input
1818
*/
19-
public AutocompleteData(@NotNull String currentInput, @NotNull List<String> allSuggestions) {
19+
public AutocompleteContext(@NotNull String currentInput, @NotNull List<String> allSuggestions) {
2020
this.currentInput = currentInput;
2121
this.allSuggestions = allSuggestions;
2222
updateSuggestions();
2323
}
2424

2525
/**
26-
* Append to current input string and re-filter the suggestion list to those matching the
27-
* current input.
26+
* Append to current input string and re-filter the suggestion list to those matching the current
27+
* input.
2828
*
2929
* @param newInput character to append to the current input
3030
*/
@@ -69,6 +69,10 @@ public List<String> filteredSuggestions() {
6969
return currentSuggestions;
7070
}
7171

72+
/**
73+
* @param index Index of selection in current filtered suggestions
74+
* @return completion using that index and the current input
75+
*/
7276
public String getCompletion(int index) {
7377
String selection = currentSuggestions.get(index);
7478
return selection.substring(currentInput.length());

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

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public class AutocompletePopup extends JPopupMenu {
3636
KeyEvent.VK_HOME,
3737
KeyEvent.VK_END);
3838
private final JList<String> suggestionList = new JList<>();
39-
private final AutocompleteData autocompleteData;
39+
private final AutocompleteContext autocompleteContext;
4040

4141
public AutocompletePopup(
4242
@NotNull JTextComponent parent,
@@ -45,7 +45,7 @@ public AutocompletePopup(
4545
@NotNull List<String> suggestions,
4646
@NotNull Consumer<String> onCompletion) {
4747
super();
48-
this.autocompleteData = new AutocompleteData(inputSoFar, suggestions);
48+
this.autocompleteContext = new AutocompleteContext(inputSoFar, suggestions);
4949

5050
int maxVisibleRows = Math.min(suggestions.size(), 10); // Limit to 10 rows max
5151
updateSuggestionList();
@@ -59,7 +59,7 @@ public AutocompletePopup(
5959
public void mouseClicked(MouseEvent e) {
6060
int index = suggestionList.locationToIndex(e.getPoint());
6161
if (index >= 0) {
62-
String selected = autocompleteData.getCompletion(index);
62+
String selected = autocompleteContext.getCompletion(index);
6363
onCompletion.accept(selected);
6464
setVisible(false);
6565
}
@@ -82,10 +82,22 @@ public void mouseMoved(MouseEvent e) {
8282
new KeyAdapter() {
8383
@Override
8484
public void keyPressed(KeyEvent e) {
85+
if (e.getKeyCode() == KeyEvent.VK_BACK_SPACE) {
86+
87+
if (autocompleteContext.canDelete()) {
88+
autocompleteContext.deleteInput();
89+
updateSuggestionList();
90+
} else{
91+
setVisible(false);
92+
}
93+
94+
parent.dispatchEvent(e);
95+
return;
96+
}
8597
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
8698
int index = suggestionList.getSelectedIndex();
8799
if (index >= 0) {
88-
String selected = autocompleteData.getCompletion(index);
100+
String selected = autocompleteContext.getCompletion(index);
89101
onCompletion.accept(selected);
90102
setVisible(false);
91103
e.consume();
@@ -115,24 +127,18 @@ public void keyPressed(KeyEvent e) {
115127
// popup
116128
if (CURSOR_MOVEMENT_PASSTHROUGH_KEYS.contains(e.getKeyCode())) {
117129
setVisible(false);
118-
parent.dispatchEvent(e); // / TODO: is this needed?
130+
parent.dispatchEvent(e);
119131
return;
120132
}
121133
}
122134

123135
@Override
124136
public void keyTyped(KeyEvent e) {
125137
char typedChar = e.getKeyChar();
126-
if (e.getKeyCode() == KeyEvent.VK_BACK_SPACE) {
127-
if (!autocompleteData.canDelete()) {
128-
setVisible(false);
129-
return;
130-
}
131-
autocompleteData.deleteInput();
132-
} else {
133-
autocompleteData.appendInput(typedChar);
138+
if (Character.isDefined(typedChar) && !Character.isISOControl(typedChar)) {
139+
autocompleteContext.appendInput(typedChar);
134140
}
135-
if (autocompleteData.filteredSuggestions().isEmpty()) {
141+
if (autocompleteContext.filteredSuggestions().isEmpty()) {
136142
// Close the popup if no suggestions match
137143
setVisible(false);
138144
}
@@ -174,7 +180,7 @@ public void keyTyped(KeyEvent e) {
174180
}
175181

176182
private void updateSuggestionList() {
177-
suggestionList.setListData(new Vector<>(autocompleteData.filteredSuggestions()));
183+
suggestionList.setListData(new Vector<>(autocompleteContext.filteredSuggestions()));
178184
suggestionList.setSelectedIndex(0);
179185
}
180186

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

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1883,28 +1883,6 @@ private void insertCompletion(String completion) {
18831883
}
18841884
}
18851885

1886-
private void updatePopupMenu(int pos, String initialUserType) {
1887-
if (autocompletePopup == null) {
1888-
return; // No active popup to update
1889-
}
1890-
1891-
try {
1892-
int caretPos = editorPane.getCaretPosition();
1893-
String text = editorPane.getText(0, caretPos);
1894-
int lastSeparator = Math.max(text.lastIndexOf(" "), text.lastIndexOf("."));
1895-
String currentUserType = text.substring(lastSeparator + 1);
1896-
1897-
List<String> filteredMatches = getMatchingStrings(currentUserType, text);
1898-
if (filteredMatches.isEmpty()) {
1899-
hideAutocompletePopup();
1900-
} else {
1901-
showAutocompletePopup(pos, currentUserType, filteredMatches);
1902-
}
1903-
} catch (BadLocationException ex) {
1904-
ex.printStackTrace();
1905-
}
1906-
}
1907-
19081886
/**
19091887
* getMatchingStrings
19101888
* <p>

0 commit comments

Comments
 (0)