Skip to content

Commit

Permalink
Clean up some code in HistoryPanel. No logic changes. (#10482)
Browse files Browse the repository at this point in the history
  • Loading branch information
asvitkine authored May 22, 2022
1 parent 6280d03 commit f7cbea7
Showing 1 changed file with 14 additions and 48 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -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);
}
Expand All @@ -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<TreeNode> nodeEnum =
((DefaultMutableTreeNode) tree.getModel().getRoot()).depthFirstEnumeration();
TreeNode previous = null;
Expand Down Expand Up @@ -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<TreeNode> nodeEnum =
((DefaultMutableTreeNode) tree.getModel().getRoot()).preorderEnumeration();
TreeNode next = null;
Expand Down Expand Up @@ -318,14 +300,6 @@ public void clearCurrentPopupNode() {
currentPopupNode = null;
}

private void addToStayExpanded(final Enumeration<TreePath> paths) {
final Collection<TreePath> 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.
Expand All @@ -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);
}

/**
Expand Down Expand Up @@ -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();
Expand All @@ -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) {
Expand All @@ -422,7 +388,7 @@ public void goToEnd() {
root = root.getParentPath();
}
final Enumeration<TreePath> expandedDescendants = tree.getExpandedDescendants(root);
addToStayExpanded(expandedDescendants);
stayExpandedPaths.addAll(Collections.list(expandedDescendants));
} else {
collapseUpFromLastParent(parent);
}
Expand Down

0 comments on commit f7cbea7

Please sign in to comment.