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

Preserve expanded state of Large Aero Weapon Bays #961

Merged
merged 1 commit into from
Nov 16, 2021
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
51 changes: 41 additions & 10 deletions src/megameklab/com/ui/util/BayWeaponCriticalTree.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,7 @@
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.StringJoiner;
import java.util.Vector;
import java.util.*;

import javax.swing.JLabel;
import javax.swing.JMenu;
Expand Down Expand Up @@ -90,6 +82,10 @@ public class BayWeaponCriticalTree extends JTree {
private final EntitySource eSource;
private final DefaultTreeModel model;
private RefreshListener refresh;

/** Stores a unique transient ID for each Bay to allow restoring the expanded state when the loadout changes. */
private final Map<Mounted, Integer> bayIdMap = new HashMap<>();
private int bayIdCounter;

public BayWeaponCriticalTree(int location, EntitySource eSource, RefreshListener refresh) {
this(location, eSource, refresh, FORWARD);
Expand Down Expand Up @@ -129,9 +125,11 @@ public void updateRefresh(RefreshListener refresh) {
}

public void rebuild() {
List<Integer> expandedBays = getExpandedBayIds();
TreeNode root = initRoot();
model.setRoot(root);
setRootVisible(root.getChildCount() == 0);
restoreExpandedBays(expandedBays);
}

private int slotCount(Mounted bay) {
Expand Down Expand Up @@ -608,6 +606,7 @@ private class BayNode extends EquipmentNode {

BayNode(Mounted object) {
super(object);
bayIdMap.computeIfAbsent(object, m -> bayIdCounter++);
}

public boolean isCapital() {
Expand Down Expand Up @@ -1498,4 +1497,36 @@ public void removeExported(String selection, int action) {
refresh.refreshStatus();
refresh.refreshSummary();
}
}

private void restoreExpandedBays(List<Integer> expandedBayIds) {
TreeNode root = (TreeNode) model.getRoot();
if ((root == null) || (expandedBayIds == null)) {
return;
}
for (int n = 0; n < model.getChildCount(root); n++) {
Object node = model.getChild(root, n);
if (node instanceof BayNode) {
if (expandedBayIds.contains(bayIdMap.get(((BayNode) node).getMounted()))) {
Object[] pathObjs = new Object[2];
pathObjs[0] = root;
pathObjs[1] = node;
expandPath(new TreePath(pathObjs));
}
}
}
}

private List<Integer> getExpandedBayIds() {
List<Integer> result = new ArrayList<>();
for (int i = 0; i < getRowCount(); i++) {
TreePath currPath = getPathForRow(i);
if (isExpanded(currPath)) {
Object entry = currPath.getLastPathComponent();
if (entry instanceof BayNode) {
result.add(bayIdMap.get(((BayNode) entry).getMounted()));
}
}
}
return result;
}
}