From f7cbea77292cfb620b33674bda7858d33a0250a0 Mon Sep 17 00:00:00 2001 From: asvitkine Date: Sun, 22 May 2022 08:53:07 -0400 Subject: [PATCH] Clean up some code in HistoryPanel. No logic changes. (#10482) --- .../triplea/ui/history/HistoryPanel.java | 62 +++++-------------- 1 file changed, 14 insertions(+), 48 deletions(-) diff --git a/game-app/game-core/src/main/java/games/strategy/triplea/ui/history/HistoryPanel.java b/game-app/game-core/src/main/java/games/strategy/triplea/ui/history/HistoryPanel.java index 6bf973d9cda..40958512073 100644 --- a/game-app/game-core/src/main/java/games/strategy/triplea/ui/history/HistoryPanel.java +++ b/game-app/game-core/src/main/java/games/strategy/triplea/ui/history/HistoryPanel.java @@ -1,5 +1,6 @@ package games.strategy.triplea.ui.history; +import com.google.common.base.Preconditions; import games.strategy.engine.data.GameData; import games.strategy.engine.data.GamePlayer; import games.strategy.engine.history.HistoryNode; @@ -12,11 +13,13 @@ import java.awt.GridBagLayout; import java.awt.Insets; import java.awt.Rectangle; +import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Collection; +import java.util.Collections; import java.util.Deque; import java.util.Enumeration; import java.util.Optional; @@ -57,19 +60,11 @@ public HistoryPanel( final HistoryDetailsPanel details, final JPopupMenu popup, final UiContext uiContext) { - mouseOverPanel = false; - mouseWasOverPanel = false; + Preconditions.checkState(data.areChangesOnlyInSwingEventThread()); + this.data = data; + this.details = details; final MouseListener mouseFocusListener = - new MouseListener() { - @Override - public void mouseReleased(final MouseEvent e) {} - - @Override - public void mousePressed(final MouseEvent e) {} - - @Override - public void mouseClicked(final MouseEvent e) {} - + new MouseAdapter() { @Override public void mouseExited(final MouseEvent e) { mouseOverPanel = false; @@ -81,12 +76,7 @@ public void mouseEntered(final MouseEvent e) { } }; addMouseListener(mouseFocusListener); - this.data = data; - this.details = details; setLayout(new BorderLayout()); - if (!this.data.areChangesOnlyInSwingEventThread()) { - throw new IllegalStateException(); - } tree = new JTree(this.data.getHistory()); // Register the tree with the tooltip manager to make the tooltips we set work. ToolTipManager.sharedInstance().registerComponent(tree); @@ -166,7 +156,7 @@ public void popupMenuWillBecomeVisible(final PopupMenuEvent pme) {} 0)); add(buttons, BorderLayout.SOUTH); tree.addMouseListener( - new MouseListener() { + new MouseAdapter() { @Override public void mouseClicked(final MouseEvent me) { if (SwingUtilities.isRightMouseButton(me)) { @@ -204,12 +194,6 @@ public void mouseEntered(final MouseEvent me) { public void mouseExited(final MouseEvent me) { mouseOverPanel = false; } - - @Override - public void mousePressed(final MouseEvent me) {} - - @Override - public void mouseReleased(final MouseEvent me) {} }); tree.addTreeSelectionListener(this::treeSelectionChanged); } @@ -219,8 +203,7 @@ private void previous() { tree.setSelectionInterval(0, 0); return; } - final TreePath path = tree.getSelectionPath(); - final TreeNode selected = (TreeNode) path.getLastPathComponent(); + final TreeNode selected = getCurrentNode(); final Enumeration nodeEnum = ((DefaultMutableTreeNode) tree.getModel().getRoot()).depthFirstEnumeration(); TreeNode previous = null; @@ -261,8 +244,7 @@ private void next() { tree.setSelectionInterval(0, 0); return; } - final TreePath path = tree.getSelectionPath(); - final TreeNode selected = (TreeNode) path.getLastPathComponent(); + final TreeNode selected = getCurrentNode(); final Enumeration nodeEnum = ((DefaultMutableTreeNode) tree.getModel().getRoot()).preorderEnumeration(); TreeNode next = null; @@ -318,14 +300,6 @@ public void clearCurrentPopupNode() { currentPopupNode = null; } - private void addToStayExpanded(final Enumeration paths) { - final Collection expandPaths = new ArrayList<>(); - while (paths.hasMoreElements()) { - expandPaths.add(paths.nextElement()); - } - stayExpandedPaths.addAll(expandPaths); - } - /** * collapses parents of last path if it is not in the list of expanded path until the new path is * a descendant. @@ -348,12 +322,7 @@ private void collapseUpFromLastParent(final TreePath newPath) { * @param parentPath tree path for which descendants should be check. */ private boolean stayExpandedContainsDescendantOf(final TreePath parentPath) { - for (final TreePath currentPath : stayExpandedPaths) { - if (parentPath.isDescendant(currentPath)) { - return true; - } - } - return false; + return stayExpandedPaths.stream().anyMatch(parentPath::isDescendant); } /** @@ -392,11 +361,8 @@ private void collapseExpanded(final TreePath newPath) { /** Selects the most recent history node, expanding the tree if necessary. */ public void goToEnd() { final HistoryNode last; - try { - data.acquireWriteLock(); + try (GameData.Unlocker ignored = data.acquireWriteLock()) { last = data.getHistory().getLastNode(); - } finally { - data.releaseWriteLock(); } final TreePath path = new TreePath(last.getPath()); final TreePath parent = path.getParentPath(); @@ -411,7 +377,7 @@ public void goToEnd() { collapseExpanded(path); collapseUpFromLastParent(parent); final Rectangle rect = tree.getPathBounds(path); - rect.setRect(0, rect.getY(), rect.getWidth(), rect.getHeight()); + rect.x = 0; tree.scrollRectToVisible(rect); } else { if (!mouseWasOverPanel) { @@ -422,7 +388,7 @@ public void goToEnd() { root = root.getParentPath(); } final Enumeration expandedDescendants = tree.getExpandedDescendants(root); - addToStayExpanded(expandedDescendants); + stayExpandedPaths.addAll(Collections.list(expandedDescendants)); } else { collapseUpFromLastParent(parent); }