refactor(gui): don't use map for store open tabs order (#1940)

This commit is contained in:
Skylot 2023-07-06 17:14:05 +01:00
parent 74a2d7d5e7
commit 3e4c6a9c51
No known key found for this signature in database
GPG Key ID: 1E23F5B52567AA39
6 changed files with 55 additions and 60 deletions

View File

@ -139,7 +139,7 @@ public class RenameMappingsGui {
if (currentNode != null) {
// close opened tab
TabbedPane tabbedPane = mainWindow.getTabbedPane();
ContentPanel openedTab = tabbedPane.getOpenTabs().get(currentNode);
ContentPanel openedTab = tabbedPane.getTabByNode(currentNode);
if (openedTab != null) {
tabbedPane.closeCodePanel(openedTab);
}

View File

@ -4,9 +4,7 @@ import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.swing.BorderFactory;
import javax.swing.JButton;
@ -133,12 +131,11 @@ public class TabComponent extends JPanel {
closeTab.addActionListener(e -> tabbedPane.closeCodePanel(contentPanel));
menu.add(closeTab);
Map<JNode, ContentPanel> openTabs = tabbedPane.getOpenTabs();
if (openTabs.size() > 1) {
List<ContentPanel> tabs = tabbedPane.getTabs();
if (tabs.size() > 1) {
JMenuItem closeOther = new JMenuItem(NLS.str("tabs.closeOthers"));
closeOther.addActionListener(e -> {
List<ContentPanel> contentPanels = new ArrayList<>(openTabs.values());
for (ContentPanel panel : contentPanels) {
for (ContentPanel panel : tabs) {
if (panel != contentPanel) {
tabbedPane.closeCodePanel(panel);
}
@ -150,12 +147,11 @@ public class TabComponent extends JPanel {
closeAll.addActionListener(e -> tabbedPane.closeAllTabs());
menu.add(closeAll);
List<ContentPanel> contentPanels = new ArrayList<>(openTabs.values());
if (contentPanel != ListUtils.last(contentPanels)) {
if (contentPanel != ListUtils.last(tabs)) {
JMenuItem closeAllRight = new JMenuItem(NLS.str("tabs.closeAllRight"));
closeAllRight.addActionListener(e -> {
boolean pastCurrentPanel = false;
for (ContentPanel panel : contentPanels) {
for (ContentPanel panel : tabs) {
if (!pastCurrentPanel) {
if (panel == contentPanel) {
pastCurrentPanel = true;
@ -170,15 +166,14 @@ public class TabComponent extends JPanel {
menu.addSeparator();
ContentPanel selectedContentPanel = tabbedPane.getSelectedContentPanel();
for (final Map.Entry<JNode, ContentPanel> entry : openTabs.entrySet()) {
final ContentPanel cp = entry.getValue();
if (cp == selectedContentPanel) {
for (ContentPanel tab : tabs) {
if (tab == selectedContentPanel) {
continue;
}
JNode node = entry.getKey();
JNode node = tab.getNode();
final String clsName = node.makeLongString();
JMenuItem item = new JMenuItem(clsName);
item.addActionListener(e -> tabbedPane.setSelectedComponent(cp));
item.addActionListener(e -> tabbedPane.setSelectedComponent(tab));
item.setIcon(node.getIcon());
menu.add(item);
}

View File

@ -7,7 +7,7 @@ import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@ -41,7 +41,7 @@ public class TabbedPane extends JTabbedPane {
private static final Logger LOG = LoggerFactory.getLogger(TabbedPane.class);
private final transient MainWindow mainWindow;
private final transient Map<JNode, ContentPanel> openTabs = new LinkedHashMap<>();
private final transient Map<JNode, ContentPanel> tabsMap = new HashMap<>();
private final transient JumpManager jumps = new JumpManager();
private transient ContentPanel curTab;
@ -54,7 +54,7 @@ public class TabbedPane extends JTabbedPane {
addMouseWheelListener(event -> {
int direction = event.getWheelRotation();
if (openTabs.isEmpty() || direction == 0) {
if (getTabCount() == 0 || direction == 0) {
return;
}
direction = (direction < 0) ? -1 : 1; // normalize direction
@ -226,12 +226,13 @@ public class TabbedPane extends JTabbedPane {
}
}
private void showCode(JumpPosition jumpPos) {
private @Nullable ContentPanel showCode(JumpPosition jumpPos) {
ContentPanel contentPanel = getContentPanel(jumpPos.getNode());
if (contentPanel != null) {
scrollToPos(contentPanel, jumpPos.getPos());
selectTab(contentPanel);
}
return contentPanel;
}
public boolean showNode(JNode node) {
@ -263,10 +264,9 @@ public class TabbedPane extends JTabbedPane {
}
public void smaliJump(JClass cls, int pos, boolean debugMode) {
ContentPanel panel = getOpenTabs().get(cls);
ContentPanel panel = getTabByNode(cls);
if (panel == null) {
showCode(new JumpPosition(cls, 1));
panel = getOpenTabs().get(cls);
panel = showCode(new JumpPosition(cls, 1));
if (panel == null) {
throw new JadxRuntimeException("Failed to open panel for JClass: " + cls);
}
@ -295,7 +295,7 @@ public class TabbedPane extends JTabbedPane {
public List<EditorViewState> getEditorViewStates() {
ContentPanel selected = getSelectedContentPanel();
List<EditorViewState> states = new ArrayList<>();
for (ContentPanel panel : openTabs.values()) {
for (ContentPanel panel : getTabs()) {
EditorViewState viewState;
if (panel instanceof IViewStateSupport) {
viewState = ((IViewStateSupport) panel).getEditorViewState();
@ -339,34 +339,46 @@ public class TabbedPane extends JTabbedPane {
}
private void addContentPanel(ContentPanel contentPanel) {
openTabs.put(contentPanel.getNode(), contentPanel);
tabsMap.put(contentPanel.getNode(), contentPanel);
int tabCount = getTabCount();
add(contentPanel, tabCount);
setTabComponentAt(tabCount, makeTabComponent(contentPanel));
}
public void closeCodePanel(ContentPanel contentPanel) {
openTabs.remove(contentPanel.getNode());
tabsMap.remove(contentPanel.getNode());
remove(contentPanel);
contentPanel.dispose();
}
@Nullable
private ContentPanel getContentPanel(JNode node) {
ContentPanel panel = openTabs.get(node);
if (panel == null) {
panel = node.getContentPanel(this);
if (panel == null) {
return null;
}
FocusManager.listen(panel);
addContentPanel(panel);
public List<ContentPanel> getTabs() {
List<ContentPanel> list = new ArrayList<>(getTabCount());
for (int i = 0; i < getTabCount(); i++) {
list.add((ContentPanel) getComponentAt(i));
}
return panel;
return list;
}
public @Nullable ContentPanel getTabByNode(JNode node) {
return tabsMap.get(node);
}
private @Nullable ContentPanel getContentPanel(JNode node) {
ContentPanel panel = getTabByNode(node);
if (panel != null) {
return panel;
}
ContentPanel newPanel = node.getContentPanel(this);
if (newPanel == null) {
return null;
}
FocusManager.listen(newPanel);
addContentPanel(newPanel);
return newPanel;
}
public void refresh(JNode node) {
ContentPanel panel = openTabs.get(node);
ContentPanel panel = getTabByNode(node);
if (panel != null) {
setTabComponentAt(indexOfComponent(panel), makeTabComponent(panel));
fireStateChanged();
@ -387,7 +399,7 @@ public class TabbedPane extends JTabbedPane {
JNode node = ((ContentPanel) getComponentAt(i)).getNode();
ContentPanel panel = node.getContentPanel(this);
FocusManager.listen(panel);
openTabs.put(node, panel);
tabsMap.put(node, panel);
setComponentAt(i, panel);
setTabComponentAt(i, makeTabComponent(panel));
}
@ -404,32 +416,21 @@ public class TabbedPane extends JTabbedPane {
}
public void closeAllTabs() {
List<ContentPanel> contentPanels = new ArrayList<>(openTabs.values());
for (ContentPanel panel : contentPanels) {
for (ContentPanel panel : getTabs()) {
closeCodePanel(panel);
}
}
public Map<JNode, ContentPanel> getOpenTabs() {
return openTabs;
}
public void loadSettings() {
for (ContentPanel panel : openTabs.values()) {
panel.loadSettings();
}
int tabCount = getTabCount();
for (int i = 0; i < tabCount; i++) {
Component tabComponent = getTabComponentAt(i);
if (tabComponent instanceof TabComponent) {
((TabComponent) tabComponent).loadSettings();
}
for (int i = 0; i < getTabCount(); i++) {
((ContentPanel) getComponentAt(i)).loadSettings();
((TabComponent) getTabComponentAt(i)).loadSettings();
}
}
public void reset() {
closeAllTabs();
openTabs.clear();
tabsMap.clear();
jumps.reset();
curTab = null;
lastTab = null;

View File

@ -116,7 +116,7 @@ public abstract class AbstractCodeArea extends RSyntaxTextArea {
public void actionPerformed(ActionEvent e) {
boolean wrap = !getLineWrap();
settings.setCodeAreaLineWrap(wrap);
contentPanel.getTabbedPane().getOpenTabs().values().forEach(v -> {
contentPanel.getTabbedPane().getTabs().forEach(v -> {
if (v instanceof AbstractCodeContentPanel) {
AbstractCodeArea codeArea = ((AbstractCodeContentPanel) v).getCodeArea();
setCodeAreaLineWrap(codeArea, wrap);

View File

@ -74,7 +74,7 @@ public final class SmaliArea extends AbstractCodeArea {
public void actionPerformed(ActionEvent e) {
JadxSettings settings = getContentPanel().getTabbedPane().getMainWindow().getSettings();
settings.setSmaliAreaShowBytecode(!settings.getSmaliAreaShowBytecode());
contentPanel.getTabbedPane().getOpenTabs().values().forEach(v -> {
contentPanel.getTabbedPane().getTabs().forEach(v -> {
if (v instanceof ClassCodeContentPanel) {
switchModel();
((ClassCodeContentPanel) v).getSmaliCodeArea().refresh();

View File

@ -8,7 +8,6 @@ import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.function.Consumer;
@ -216,10 +215,10 @@ public class RenameDialog extends JDialog {
}
private void refreshTabs(TabbedPane tabbedPane, Set<JClass> updatedClasses) {
for (Map.Entry<JNode, ContentPanel> entry : tabbedPane.getOpenTabs().entrySet()) {
JClass rootClass = entry.getKey().getRootClass();
for (ContentPanel tab : tabbedPane.getTabs()) {
JClass rootClass = tab.getNode().getRootClass();
if (updatedClasses.remove(rootClass)) {
ClassCodeContentPanel contentPanel = (ClassCodeContentPanel) entry.getValue();
ClassCodeContentPanel contentPanel = (ClassCodeContentPanel) tab;
CodeArea codeArea = (CodeArea) contentPanel.getJavaCodePanel().getCodeArea();
codeArea.refreshClass();
}