Merge pull request #464 from Bl3nd/master

Exception with finding and opening an already opened resource
This commit is contained in:
Konloch 2023-01-26 12:47:18 -08:00 committed by GitHub
commit d5558a4b25
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -50,211 +50,198 @@ import static the.bytecode.club.bytecodeviewer.Constants.BLOCK_TAB_MENU;
* @since 09/26/2011 * @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 final DraggableTabbedPane tabs;
{ public final JPanel buttonPanel;
super("Workspace", TranslatedComponents.WORK_SPACE); public final JButton refreshClass;
public final Set<String> openedTabs = new HashSet<>();
this.tabs = new DraggableTabbedPane(); public Workspace() {
super("Workspace", TranslatedComponents.WORK_SPACE);
JPopupMenu popUp = new JPopupMenu(); this.tabs = new DraggableTabbedPane();
JMenuItem closeAllTabs = new JMenuItem("Close All But This");
JMenuItem closeTab = new JMenuItem("Close Tab");
closeTab.addActionListener(e ->
{
TabExitButton tabExitButton = (TabExitButton) ((JPopupMenu)((JMenuItem) e.getSource()).getParent()).getInvoker();
final int index = tabExitButton.getTabIndex();
if (index != -1) JPopupMenu popUp = new JPopupMenu();
tabs.remove(index); JMenuItem closeAllTabs = new JMenuItem("Close All But This");
}); JMenuItem closeTab = new JMenuItem("Close Tab");
closeTab.addActionListener(e ->
{
TabExitButton tabExitButton = (TabExitButton) ((JPopupMenu) ((JMenuItem) e.getSource()).getParent()).getInvoker();
final int index = tabExitButton.getTabIndex();
closeAllTabs.addActionListener(e -> if (index != -1)
{ tabs.remove(index);
TabExitButton tabExitButton = (TabExitButton) ((JPopupMenu)((JMenuItem) e.getSource()).getParent()).getInvoker(); });
final int index = tabExitButton.getTabIndex();
while (true) closeAllTabs.addActionListener(e ->
{ {
if (tabs.getTabCount() <= 1) TabExitButton tabExitButton = (TabExitButton) ((JPopupMenu) ((JMenuItem) e.getSource()).getParent()).getInvoker();
return; final int index = tabExitButton.getTabIndex();
if (index != 0) while (true) {
tabs.remove(0); if (tabs.getTabCount() <= 1)
else return;
tabs.remove(1);
}
});
tabs.addMouseListener(new MouseListener() if (index != 0)
{ tabs.remove(0);
@Override else
public void mouseClicked(MouseEvent e) { } tabs.remove(1);
@Override }
public void mouseEntered(MouseEvent arg0) { } });
@Override
public void mouseExited(MouseEvent arg0) { }
@Override tabs.addMouseListener(new MouseListener() {
public void mousePressed(MouseEvent e) @Override
{ public void mouseClicked(MouseEvent e) {
if (BLOCK_TAB_MENU) }
return;
if (e.getButton() == 3) @Override
{ public void mouseEntered(MouseEvent arg0) {
Rectangle bounds = new Rectangle(1, 1, e.getX(), e.getY()); }
for (int i = 0; i < BytecodeViewer.viewer.workPane.tabs.getTabCount(); i++) @Override
{ public void mouseExited(MouseEvent arg0) {
Component c = BytecodeViewer.viewer.workPane.tabs.getTabComponentAt(i); }
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
{
popUp.setVisible(false);
}
}
}
}
@Override @Override
public void mouseReleased(MouseEvent e) { } public void mousePressed(MouseEvent e) {
}); if (BLOCK_TAB_MENU)
return;
popUp.add(closeAllTabs); if (e.getButton() == 3) {
popUp.add(closeTab); Rectangle bounds = new Rectangle(1, 1, e.getX(), e.getY());
if (!BLOCK_TAB_MENU) for (int i = 0; i < BytecodeViewer.viewer.workPane.tabs.getTabCount(); i++) {
tabs.setComponentPopupMenu(popUp); Component c = BytecodeViewer.viewer.workPane.tabs.getTabComponentAt(i);
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 {
popUp.setVisible(false);
}
}
}
}
getContentPane().setLayout(new BorderLayout()); @Override
getContentPane().add(tabs, BorderLayout.CENTER); public void mouseReleased(MouseEvent e) {
}
});
buttonPanel = new JPanel(new FlowLayout()); popUp.add(closeAllTabs);
popUp.add(closeTab);
refreshClass = new TranslatedJButton("Refresh", TranslatedComponents.REFRESH); if (!BLOCK_TAB_MENU)
refreshClass.addActionListener((event)-> tabs.setComponentPopupMenu(popUp);
{
refreshClass.setEnabled(false);
Thread t = new Thread(() -> new WorkspaceRefresh(event).run(), "Refresh");
t.start();
});
buttonPanel.add(refreshClass); getContentPane().setLayout(new BorderLayout());
buttonPanel.setVisible(false); getContentPane().add(tabs, BorderLayout.CENTER);
getContentPane().add(buttonPanel, BorderLayout.SOUTH); buttonPanel = new JPanel(new FlowLayout());
tabs.addContainerListener(new TabRemovalEvent()); refreshClass = new TranslatedJButton("Refresh", TranslatedComponents.REFRESH);
tabs.addChangeListener(arg0 -> buttonPanel.setVisible(tabs.getSelectedIndex() != -1)); refreshClass.addActionListener((event) ->
{
refreshClass.setEnabled(false);
Thread t = new Thread(() -> new WorkspaceRefresh(event).run(), "Refresh");
t.start();
});
this.setVisible(true); buttonPanel.add(refreshClass);
} buttonPanel.setVisible(false);
//load class resources getContentPane().add(buttonPanel, BorderLayout.SOUTH);
public void addClassResource(final ResourceContainer container, final String name)
{
addResource(container, name, new ClassViewer(container, name));
}
//Load file resources tabs.addContainerListener(new TabRemovalEvent());
public void addFileResource(final ResourceContainer container, final String name) tabs.addChangeListener(arg0 -> buttonPanel.setVisible(tabs.getSelectedIndex() != -1));
{
addResource(container, name, new FileViewer(container, name));
}
private void addResource(final ResourceContainer container, final String name, final ResourceViewer resourceView) this.setVisible(true);
{ }
// Warn user and prevent 'nothing' from opening if no Decompiler is selected
if(BytecodeViewer.viewer.viewPane1.getSelectedDecompiler() == Decompiler.NONE &&
BytecodeViewer.viewer.viewPane2.getSelectedDecompiler() == Decompiler.NONE &&
BytecodeViewer.viewer.viewPane3.getSelectedDecompiler() == Decompiler.NONE)
{
BytecodeViewer.showMessage(TranslatedStrings.SUGGESTED_FIX_NO_DECOMPILER_WARNING.toString());
return;
}
//unlock the refresh button //load class resources
BytecodeViewer.viewer.workPane.refreshClass.setEnabled(true); public void addClassResource(final ResourceContainer container, final String name) {
addResource(container, name, new ClassViewer(container, name));
}
final String workingName = container.getWorkingName(name); //Load file resources
public void addFileResource(final ResourceContainer container, final String name) {
addResource(container, name, new FileViewer(container, name));
}
//create a new tab if the resource isn't opened currently private void addResource(final ResourceContainer container, final String name, final ResourceViewer resourceView) {
if (!openedTabs.contains(workingName)) // Warn user and prevent 'nothing' from opening if no Decompiler is selected
{ if (BytecodeViewer.viewer.viewPane1.getSelectedDecompiler() == Decompiler.NONE &&
addResourceToTab(resourceView, workingName, container.name, name); BytecodeViewer.viewer.viewPane2.getSelectedDecompiler() == Decompiler.NONE &&
} BytecodeViewer.viewer.viewPane3.getSelectedDecompiler() == Decompiler.NONE) {
else //if the resource is already opened select this tab as the active one BytecodeViewer.showMessage(TranslatedStrings.SUGGESTED_FIX_NO_DECOMPILER_WARNING.toString());
{ return;
//TODO openedTabs could be changed to a HashMap<String, Integer> for faster lookups }
//search through each tab //unlock the refresh button
for(int i = 0; i < tabs.getTabCount(); i++) BytecodeViewer.viewer.workPane.refreshClass.setEnabled(true);
{
//find the matching resource and open it
ResourceViewer tab = ((TabbedPane)tabs.getTabComponentAt(i)).resource;
if(tab.resource.workingName.equals(workingName))
{
tabs.setSelectedIndex(i);
break;
}
}
}
}
public void addResourceToTab(ResourceViewer resourceView, String workingName, String containerName, String name) final String workingName = container.getWorkingName(name);
{
//start processing the resource to be viewed
if(resourceView instanceof ClassViewer)
resourceView.refresh(null);
//add the resource view to the tabs //create a new tab if the resource isn't opened currently
tabs.add(resourceView); if (!openedTabs.contains(workingName)) {
addResourceToTab(resourceView, workingName, container.name, name);
} 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
//get the resource view index //search through each tab
final int tabIndex = tabs.indexOfComponent(resourceView); for (int i = 0; i < tabs.getTabCount(); i++) {
//find the matching resource and open it
ResourceViewer tab = (ResourceViewer) tabs.getComponentAt(i);
if (tab.resource.workingName.equals(workingName)) {
tabs.setSelectedIndex(i);
break;
}
}
}
}
//create a new tabbed pane public void addResourceToTab(ResourceViewer resourceView, String workingName, String containerName, String name) {
resourceView.tabbedPane = new TabbedPane(tabIndex, workingName, containerName, name, tabs, resourceView); //start processing the resource to be viewed
resourceView.resource.workingName = workingName; if (resourceView instanceof ClassViewer)
resourceView.refresh(null);
//set the tabs index //add the resource view to the tabs
tabs.setTabComponentAt(tabIndex, new CloseButtonComponent(tabs)); tabs.add(resourceView);
//open the tab that was just added //get the resource view index
tabs.setSelectedIndex(tabIndex); final int tabIndex = tabs.indexOfComponent(resourceView);
//set resource as opened in a tab //create a new tabbed pane
openedTabs.add(workingName); resourceView.tabbedPane = new TabbedPane(tabIndex, workingName, containerName, name, tabs, resourceView);
resourceView.resource.workingName = workingName;
//refresh the tab title //set the tabs index
resourceView.refreshTitle(); tabs.setTabComponentAt(tabIndex, new CloseButtonComponent(tabs));
}
public ResourceViewer getActiveResource() { //open the tab that was just added
return (ResourceViewer) tabs.getSelectedComponent(); tabs.setSelectedIndex(tabIndex);
}
public Component[] getLoadedViewers() { //set resource as opened in a tab
return tabs.getComponents(); openedTabs.add(workingName);
}
public void resetWorkspace() //refresh the tab title
{ resourceView.refreshTitle();
tabs.removeAll(); }
tabs.updateUI();
}
private static final long serialVersionUID = 6542337997679487946L; public ResourceViewer getActiveResource() {
return (ResourceViewer) tabs.getSelectedComponent();
}
public Component[] getLoadedViewers() {
return tabs.getComponents();
}
public void resetWorkspace() {
tabs.removeAll();
tabs.updateUI();
}
private static final long serialVersionUID = 6542337997679487946L;
} }