diff --git a/game-app/game-headed/src/main/java/games/strategy/triplea/ui/CommentPanel.java b/game-app/game-headed/src/main/java/games/strategy/triplea/ui/CommentPanel.java index 8bd306aa994..5098c4ab10b 100644 --- a/game-app/game-headed/src/main/java/games/strategy/triplea/ui/CommentPanel.java +++ b/game-app/game-headed/src/main/java/games/strategy/triplea/ui/CommentPanel.java @@ -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; @@ -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; @@ -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);