Fixed exception when clicking a class that's already open.

This commit is contained in:
Cody 2023-01-25 21:34:00 -08:00
parent ae6911658c
commit aa9b4bacf4

View file

@ -50,15 +50,14 @@ import static the.bytecode.club.bytecodeviewer.Constants.BLOCK_TAB_MENU;
* @since 09/26/2011
*/
public class Workspace extends TranslatedVisibleComponent
{
public class Workspace extends TranslatedVisibleComponent {
public final DraggableTabbedPane tabs;
public final JPanel buttonPanel;
public final JButton refreshClass;
public final Set<String> openedTabs = new HashSet<>();
public Workspace()
{
public Workspace() {
super("Workspace", TranslatedComponents.WORK_SPACE);
this.tabs = new DraggableTabbedPane();
@ -68,7 +67,7 @@ public class Workspace extends TranslatedVisibleComponent
JMenuItem closeTab = new JMenuItem("Close Tab");
closeTab.addActionListener(e ->
{
TabExitButton tabExitButton = (TabExitButton) ((JPopupMenu)((JMenuItem) e.getSource()).getParent()).getInvoker();
TabExitButton tabExitButton = (TabExitButton) ((JPopupMenu) ((JMenuItem) e.getSource()).getParent()).getInvoker();
final int index = tabExitButton.getTabIndex();
if (index != -1)
@ -77,11 +76,10 @@ public class Workspace extends TranslatedVisibleComponent
closeAllTabs.addActionListener(e ->
{
TabExitButton tabExitButton = (TabExitButton) ((JPopupMenu)((JMenuItem) e.getSource()).getParent()).getInvoker();
TabExitButton tabExitButton = (TabExitButton) ((JPopupMenu) ((JMenuItem) e.getSource()).getParent()).getInvoker();
final int index = tabExitButton.getTabIndex();
while (true)
{
while (true) {
if (tabs.getTabCount() <= 1)
return;
@ -92,36 +90,34 @@ public class Workspace extends TranslatedVisibleComponent
}
});
tabs.addMouseListener(new MouseListener()
{
tabs.addMouseListener(new MouseListener() {
@Override
public void mouseClicked(MouseEvent e) { }
@Override
public void mouseEntered(MouseEvent arg0) { }
@Override
public void mouseExited(MouseEvent arg0) { }
public void mouseClicked(MouseEvent e) {
}
@Override
public void mousePressed(MouseEvent e)
{
public void mouseEntered(MouseEvent arg0) {
}
@Override
public void mouseExited(MouseEvent arg0) {
}
@Override
public void mousePressed(MouseEvent e) {
if (BLOCK_TAB_MENU)
return;
if (e.getButton() == 3)
{
if (e.getButton() == 3) {
Rectangle bounds = new Rectangle(1, 1, e.getX(), e.getY());
for (int i = 0; i < BytecodeViewer.viewer.workPane.tabs.getTabCount(); i++)
{
for (int i = 0; i < BytecodeViewer.viewer.workPane.tabs.getTabCount(); i++) {
Component c = BytecodeViewer.viewer.workPane.tabs.getTabComponentAt(i);
if (c != null && bounds.intersects(c.getBounds()))
{
if (c != null && bounds.intersects(c.getBounds())) {
popUp.setVisible(true);
closeAllTabs.setText(TranslatedStrings.CLOSE_TAB + ": " + ((TabbedPane) c).tabName);
closeTab.setText(TranslatedStrings.CLOSE_TAB + ": " + ((TabbedPane) c).tabName);
}
else
{
} else {
popUp.setVisible(false);
}
}
@ -129,7 +125,8 @@ public class Workspace extends TranslatedVisibleComponent
}
@Override
public void mouseReleased(MouseEvent e) { }
public void mouseReleased(MouseEvent e) {
}
});
popUp.add(closeAllTabs);
@ -144,7 +141,7 @@ public class Workspace extends TranslatedVisibleComponent
buttonPanel = new JPanel(new FlowLayout());
refreshClass = new TranslatedJButton("Refresh", TranslatedComponents.REFRESH);
refreshClass.addActionListener((event)->
refreshClass.addActionListener((event) ->
{
refreshClass.setEnabled(false);
Thread t = new Thread(() -> new WorkspaceRefresh(event).run(), "Refresh");
@ -163,24 +160,20 @@ public class Workspace extends TranslatedVisibleComponent
}
//load class resources
public void addClassResource(final ResourceContainer container, final String name)
{
public void addClassResource(final ResourceContainer container, final String name) {
addResource(container, name, new ClassViewer(container, name));
}
//Load file resources
public void addFileResource(final ResourceContainer container, final String name)
{
public void addFileResource(final ResourceContainer container, final String name) {
addResource(container, name, new FileViewer(container, name));
}
private void addResource(final ResourceContainer container, final String name, final ResourceViewer resourceView)
{
private void addResource(final ResourceContainer container, final String name, final ResourceViewer resourceView) {
// Warn user and prevent 'nothing' from opening if no Decompiler is selected
if(BytecodeViewer.viewer.viewPane1.getSelectedDecompiler() == Decompiler.NONE &&
if (BytecodeViewer.viewer.viewPane1.getSelectedDecompiler() == Decompiler.NONE &&
BytecodeViewer.viewer.viewPane2.getSelectedDecompiler() == Decompiler.NONE &&
BytecodeViewer.viewer.viewPane3.getSelectedDecompiler() == Decompiler.NONE)
{
BytecodeViewer.viewer.viewPane3.getSelectedDecompiler() == Decompiler.NONE) {
BytecodeViewer.showMessage(TranslatedStrings.SUGGESTED_FIX_NO_DECOMPILER_WARNING.toString());
return;
}
@ -191,21 +184,17 @@ public class Workspace extends TranslatedVisibleComponent
final String workingName = container.getWorkingName(name);
//create a new tab if the resource isn't opened currently
if (!openedTabs.contains(workingName))
{
if (!openedTabs.contains(workingName)) {
addResourceToTab(resourceView, workingName, container.name, name);
}
else //if the resource is already opened select this tab as the active one
} else //if the resource is already opened select this tab as the active one
{
//TODO openedTabs could be changed to a HashMap<String, Integer> for faster lookups
//search through each tab
for(int i = 0; i < tabs.getTabCount(); i++)
{
for (int i = 0; i < tabs.getTabCount(); i++) {
//find the matching resource and open it
ResourceViewer tab = ((TabbedPane)tabs.getTabComponentAt(i)).resource;
if(tab.resource.workingName.equals(workingName))
{
ResourceViewer tab = (ResourceViewer) tabs.getComponentAt(i);
if (tab.resource.workingName.equals(workingName)) {
tabs.setSelectedIndex(i);
break;
}
@ -213,10 +202,9 @@ public class Workspace extends TranslatedVisibleComponent
}
}
public void addResourceToTab(ResourceViewer resourceView, String workingName, String containerName, String name)
{
public void addResourceToTab(ResourceViewer resourceView, String workingName, String containerName, String name) {
//start processing the resource to be viewed
if(resourceView instanceof ClassViewer)
if (resourceView instanceof ClassViewer)
resourceView.refresh(null);
//add the resource view to the tabs
@ -250,8 +238,7 @@ public class Workspace extends TranslatedVisibleComponent
return tabs.getComponents();
}
public void resetWorkspace()
{
public void resetWorkspace() {
tabs.removeAll();
tabs.updateUI();
}