Skip to content

Commit ad68181

Browse files
committed
Document #navigateToNextObfuscatedToken async requirements
1 parent 357b3ec commit ad68181

File tree

2 files changed

+29
-21
lines changed

2 files changed

+29
-21
lines changed

enigma-swing/src/main/java/cuchaz/enigma/gui/panels/EditorPanel.java

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ public EditorPanel(Gui gui) {
125125
customizeEditor(this.editor);
126126
this.editor.addCaretListener(event -> onCaretMove(event.getDot(), this.mouseIsPressed));
127127

128-
// Remove tabulator from focus traversal keys (we give it a different meaning)
128+
// Remove the tab key from focus traversal keys (we give it a different meaning)
129129
Set<AWTKeyStroke> focusTraversalKeys = new HashSet<>(this.editor.getFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS));
130130
focusTraversalKeys.removeIf(key -> key.getKeyCode() == KeyEvent.VK_TAB);
131131
this.editor.setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, focusTraversalKeys);
@@ -681,28 +681,34 @@ private void showReference0(EntryReference<Entry<?>, Entry<?>> reference) {
681681
}
682682
}
683683

684+
/**
685+
* Navigate to the next obfuscated token that can be renamed.
686+
*
687+
* <p>If the tokens are damaged, then this method should not be called
688+
* synchronously. Instead, the call should be wrapped in a
689+
* {@link SwingUtilities#invokeLater(Runnable)}. Failing to do so
690+
* will induce invalid token highlighting regions.
691+
*/
684692
public void navigateToNextObfuscatedToken() {
685-
SwingUtilities.invokeLater(() -> {
686-
int caretPos = this.getEditor().getCaretPosition();
687-
Token token = this.getToken(this.getEditor().getCaretPosition());
688-
NavigableSet<Token> obfuscatedTokens = this.getSource().getTokenStore().getByType().get(RenamableTokenType.OBFUSCATED);
689-
Token next;
690-
691-
if (token == null) {
692-
next = obfuscatedTokens.higher(new Token(caretPos, caretPos, null));
693-
} else {
694-
next = obfuscatedTokens.higher(token);
695-
}
693+
int caretPos = this.getEditor().getCaretPosition();
694+
Token token = this.getToken(this.getEditor().getCaretPosition());
695+
NavigableSet<Token> obfuscatedTokens = this.getSource().getTokenStore().getByType().get(RenamableTokenType.OBFUSCATED);
696+
Token next;
696697

697-
if (next == null) {
698-
// Wrap to start of document
699-
next = obfuscatedTokens.pollFirst();
700-
}
698+
if (token == null) {
699+
next = obfuscatedTokens.higher(new Token(caretPos, caretPos, null));
700+
} else {
701+
next = obfuscatedTokens.higher(token);
702+
}
701703

702-
if (next != null) {
703-
this.navigateToToken(next);
704-
}
705-
});
704+
if (next == null) {
705+
// Wrap to start of document
706+
next = obfuscatedTokens.pollFirst();
707+
}
708+
709+
if (next != null) {
710+
this.navigateToToken(next);
711+
}
706712
}
707713

708714
public void navigateToToken(Token token) {

enigma-swing/src/main/java/cuchaz/enigma/gui/panels/IdentifierPanel.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import javax.swing.JComboBox;
1414
import javax.swing.JLabel;
1515
import javax.swing.JPanel;
16+
import javax.swing.SwingUtilities;
1617

1718
import cuchaz.enigma.EnigmaProject;
1819
import cuchaz.enigma.gui.EditableType;
@@ -202,7 +203,8 @@ public void onStopEditing(ConvertingTextField field, StopEditingCause cause) {
202203
doRename(field.getText());
203204

204205
if (cause == StopEditingCause.TAB && e != null) {
205-
e.navigateToNextObfuscatedToken();
206+
// invokeLater as per the method's javadocs
207+
SwingUtilities.invokeLater(e::navigateToNextObfuscatedToken);
206208
}
207209
}
208210

0 commit comments

Comments
 (0)