diff --git a/jadx-gui/src/main/java/jadx/gui/plugins/mappings/RenameMappingsGui.java b/jadx-gui/src/main/java/jadx/gui/plugins/mappings/RenameMappingsGui.java index 5ed93b15..6224c000 100644 --- a/jadx-gui/src/main/java/jadx/gui/plugins/mappings/RenameMappingsGui.java +++ b/jadx-gui/src/main/java/jadx/gui/plugins/mappings/RenameMappingsGui.java @@ -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); } diff --git a/jadx-gui/src/main/java/jadx/gui/ui/TabComponent.java b/jadx-gui/src/main/java/jadx/gui/ui/TabComponent.java index 0f0e8aa1..cdce81c1 100644 --- a/jadx-gui/src/main/java/jadx/gui/ui/TabComponent.java +++ b/jadx-gui/src/main/java/jadx/gui/ui/TabComponent.java @@ -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 openTabs = tabbedPane.getOpenTabs(); - if (openTabs.size() > 1) { + List tabs = tabbedPane.getTabs(); + if (tabs.size() > 1) { JMenuItem closeOther = new JMenuItem(NLS.str("tabs.closeOthers")); closeOther.addActionListener(e -> { - List 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 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 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); } diff --git a/jadx-gui/src/main/java/jadx/gui/ui/TabbedPane.java b/jadx-gui/src/main/java/jadx/gui/ui/TabbedPane.java index fb827d2f..d270348f 100644 --- a/jadx-gui/src/main/java/jadx/gui/ui/TabbedPane.java +++ b/jadx-gui/src/main/java/jadx/gui/ui/TabbedPane.java @@ -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 openTabs = new LinkedHashMap<>(); + private final transient Map 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 getEditorViewStates() { ContentPanel selected = getSelectedContentPanel(); List 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 getTabs() { + List 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 contentPanels = new ArrayList<>(openTabs.values()); - for (ContentPanel panel : contentPanels) { + for (ContentPanel panel : getTabs()) { closeCodePanel(panel); } } - public Map 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; diff --git a/jadx-gui/src/main/java/jadx/gui/ui/codearea/AbstractCodeArea.java b/jadx-gui/src/main/java/jadx/gui/ui/codearea/AbstractCodeArea.java index ba093c81..834d4673 100644 --- a/jadx-gui/src/main/java/jadx/gui/ui/codearea/AbstractCodeArea.java +++ b/jadx-gui/src/main/java/jadx/gui/ui/codearea/AbstractCodeArea.java @@ -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); diff --git a/jadx-gui/src/main/java/jadx/gui/ui/codearea/SmaliArea.java b/jadx-gui/src/main/java/jadx/gui/ui/codearea/SmaliArea.java index a996670c..8f9c517b 100644 --- a/jadx-gui/src/main/java/jadx/gui/ui/codearea/SmaliArea.java +++ b/jadx-gui/src/main/java/jadx/gui/ui/codearea/SmaliArea.java @@ -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(); diff --git a/jadx-gui/src/main/java/jadx/gui/ui/dialog/RenameDialog.java b/jadx-gui/src/main/java/jadx/gui/ui/dialog/RenameDialog.java index a05a44fd..3b162b31 100644 --- a/jadx-gui/src/main/java/jadx/gui/ui/dialog/RenameDialog.java +++ b/jadx-gui/src/main/java/jadx/gui/ui/dialog/RenameDialog.java @@ -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 updatedClasses) { - for (Map.Entry 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(); }