Skip to content

Commit f6bacda

Browse files
committed
Tabs: Renamed CloseButtonComponent to a more suitable name and close button popup didn't remove the tab from the openedTabs array (which wouldn't allow that resource to be opened again).
1 parent 6ef288f commit f6bacda

3 files changed

Lines changed: 31 additions & 19 deletions

File tree

src/main/java/the/bytecode/club/bytecodeviewer/gui/resourceviewer/CloseButtonComponent.java renamed to src/main/java/the/bytecode/club/bytecodeviewer/gui/resourceviewer/TabComponent.java

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,27 @@
11
package the.bytecode.club.bytecodeviewer.gui.resourceviewer;
22

33
import com.github.weisj.darklaf.components.CloseButton;
4+
import the.bytecode.club.bytecodeviewer.BytecodeViewer;
45
import the.bytecode.club.bytecodeviewer.gui.components.listeners.MouseClickedListener;
6+
import the.bytecode.club.bytecodeviewer.gui.resourceviewer.viewer.ResourceViewer;
57
import the.bytecode.club.bytecodeviewer.translation.TranslatedStrings;
68

79
import javax.swing.*;
810
import java.awt.*;
911
import java.awt.event.MouseEvent;
1012

11-
public class CloseButtonComponent extends JPanel {
13+
public class TabComponent extends JPanel {
1214

13-
private final JTabbedPane pane;
14-
15-
public CloseButtonComponent(final JTabbedPane pane) {
15+
public TabComponent(final JTabbedPane pane) {
1616
super(new FlowLayout(FlowLayout.LEFT, 0, 0));
1717
if (pane == null) {
1818
throw new NullPointerException("TabbedPane is null");
1919
}
2020

21-
this.pane = pane;
2221
setOpaque(false);
2322
JLabel label = new JLabel() {
2423
public String getText() {
25-
int i = pane.indexOfTabComponent(CloseButtonComponent.this);
24+
int i = pane.indexOfTabComponent(TabComponent.this);
2625
if (i != -1) {
2726
return pane.getTitleAt(i);
2827
}
@@ -31,8 +30,8 @@ public String getText() {
3130
}
3231
};
3332

34-
add(label);
3533
label.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 5));
34+
add(label);
3635
JButton button = new CloseButton();
3736
add(button);
3837

@@ -49,30 +48,45 @@ public String getText() {
4948
if (e.getButton() != MouseEvent.BUTTON1) // left-click
5049
return;
5150

52-
if (pane.indexOfTabComponent(CloseButtonComponent.this) != -1)
53-
pane.remove(pane.indexOfTabComponent(CloseButtonComponent.this));
51+
if (pane.indexOfTabComponent(TabComponent.this) != -1) {
52+
int i = pane.indexOfTabComponent(TabComponent.this);
53+
removeTab(i);
54+
pane.remove(pane.indexOfTabComponent(TabComponent.this));
55+
}
5456
}));
5557

5658
closeTab.addActionListener(e ->
5759
{
58-
if (pane.indexOfTabComponent(CloseButtonComponent.this) != -1)
59-
pane.remove(pane.indexOfTabComponent(CloseButtonComponent.this));
60+
if (pane.indexOfTabComponent(TabComponent.this) != -1) {
61+
int i = pane.indexOfTabComponent(TabComponent.this);
62+
removeTab(i);
63+
pane.remove(pane.indexOfTabComponent(TabComponent.this));
64+
}
6065
});
66+
6167
closeAllTabs.addActionListener(e ->
6268
{
6369

6470
while (true) {
6571
if (pane.getTabCount() <= 1)
6672
return;
6773

68-
if (pane.indexOfTabComponent(CloseButtonComponent.this) != 0)
74+
if (pane.indexOfTabComponent(TabComponent.this) != 0) {
75+
removeTab(0);
6976
pane.remove(0);
70-
else
77+
} else {
78+
removeTab(1);
7179
pane.remove(1);
80+
}
7281
}
7382
});
7483

7584
setBorder(BorderFactory.createEmptyBorder(2, 0, 0, 0));
7685
}
7786

87+
private void removeTab(int index) {
88+
ResourceViewer resourceViewer = (ResourceViewer) BytecodeViewer.viewer.workPane.tabs.getComponentAt(index);
89+
BytecodeViewer.viewer.workPane.openedTabs.remove(resourceViewer.resource.workingName);
90+
}
91+
7892
}

src/main/java/the/bytecode/club/bytecodeviewer/gui/resourceviewer/TabRemovalEvent.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@
2727
/**
2828
* @author Konloch
2929
* @since 6/24/2021
30+
* @deprecated Removal pending. <br>
31+
* Disabled due to a bug when dragging a component, it got "removed" resulting in another
32+
* tab being opened when clicking the same class in the file resource rather than opening the already opened file.
3033
*/
3134
public class TabRemovalEvent implements ContainerListener
3235
{

src/main/java/the/bytecode/club/bytecodeviewer/gui/resourceviewer/Workspace.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,9 @@
1515

1616
import javax.swing.*;
1717
import java.awt.*;
18-
import java.awt.event.MouseEvent;
19-
import java.awt.event.MouseListener;
2018
import java.util.HashSet;
2119
import java.util.Set;
2220

23-
import static the.bytecode.club.bytecodeviewer.Constants.BLOCK_TAB_MENU;
24-
2521
/***************************************************************************
2622
* Bytecode Viewer (BCV) - Java & Android Reverse Engineering Suite *
2723
* Copyright (C) 2014 Kalen 'Konloch' Kinloch - http://bytecodeviewer.com *
@@ -83,7 +79,6 @@ public Workspace() {
8379

8480
getContentPane().add(buttonPanel, BorderLayout.SOUTH);
8581

86-
tabs.addContainerListener(new TabRemovalEvent());
8782
tabs.addChangeListener(arg0 -> buttonPanel.setVisible(tabs.getSelectedIndex() != -1));
8883

8984
this.setVisible(true);
@@ -148,7 +143,7 @@ public void addResourceToTab(ResourceViewer resourceView, String workingName, St
148143
resourceView.resource.workingName = workingName;
149144

150145
//set the tabs index
151-
tabs.setTabComponentAt(tabIndex, new CloseButtonComponent(tabs));
146+
tabs.setTabComponentAt(tabIndex, new TabComponent(tabs));
152147

153148
//open the tab that was just added
154149
tabs.setSelectedIndex(tabIndex);

0 commit comments

Comments
 (0)