Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ArrayIndexOutOfBoundsException in CommentPanel. #11786

Merged
merged 1 commit into from
Jul 17, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@
@Slf4j
class CommentPanel extends JPanel {
private static final long serialVersionUID = -9122162393288045888L;

private static final Pattern COMMENT_PATTERN = Pattern.compile("^COMMENT: (.*)");

private JTextPane text;
private JScrollPane scrollPane;
private JTextField nextMessage;
Expand Down Expand Up @@ -134,16 +137,17 @@ public void treeStructureChanged(final TreeModelEvent e) {
private void readHistoryTreeEvent(final TreeModelEvent e) {
SwingAction.invokeNowOrLater(
() -> {
final Document doc = text.getDocument();
final HistoryNode node = (HistoryNode) e.getTreePath().getLastPathComponent();
final TreeNode child =
node == null ? null : (node.getChildCount() > 0 ? node.getLastChild() : null);
final String title =
child != null
? (child instanceof Event ? ((Event) child).getDescription() : child.toString())
: (node != null ? node.getTitle() : "");
final Pattern p = Pattern.compile("^COMMENT: (.*)");
final Matcher m = p.matcher(title);
final Object[] children = e.getChildren();
final Object child =
(children != null && children.length > 0) ? children[children.length - 1] : null;
final String title;
if (child != null) {
title = (child instanceof Event) ? ((Event) child).getDescription() : child.toString();
} else {
final HistoryNode node = (HistoryNode) e.getTreePath().getLastPathComponent();
title = (node != null) ? node.getTitle() : "";
}
final Matcher m = COMMENT_PATTERN.matcher(title);
if (m.matches()) {
final GamePlayer gamePlayer;
final String player;
Expand All @@ -156,6 +160,7 @@ private void readHistoryTreeEvent(final TreeModelEvent e) {
final Icon icon = iconMap.get(gamePlayer);
try {
// insert into ui document
final Document doc = text.getDocument();
final String prefix = " " + player + "(" + round + ") : ";
text.insertIcon(icon);
doc.insertString(doc.getLength(), prefix, bold);
Expand Down