|
| 1 | +package the.bytecode.club.bytecodeviewer.gui.resourceviewer; |
| 2 | + |
| 3 | +import com.android.tools.r8.internal.Cl; |
| 4 | +import com.github.weisj.darklaf.components.CloseButton; |
| 5 | +import com.github.weisj.darklaf.listener.MouseClickListener; |
| 6 | +import the.bytecode.club.bytecodeviewer.gui.components.listeners.MouseClickedListener; |
| 7 | +import the.bytecode.club.bytecodeviewer.translation.TranslatedStrings; |
| 8 | + |
| 9 | +import javax.swing.*; |
| 10 | +import java.awt.*; |
| 11 | +import java.awt.event.ActionEvent; |
| 12 | +import java.awt.event.ActionListener; |
| 13 | +import java.awt.event.MouseEvent; |
| 14 | +import java.awt.event.MouseListener; |
| 15 | + |
| 16 | +public class CloseButtonComponent extends JPanel { |
| 17 | + |
| 18 | + private final JTabbedPane pane; |
| 19 | + |
| 20 | + String title = ""; |
| 21 | + |
| 22 | + public CloseButtonComponent(final JTabbedPane pane) { |
| 23 | + super(new FlowLayout(FlowLayout.LEFT, 0, 0)); |
| 24 | + if (pane == null) { |
| 25 | + throw new NullPointerException("TabbedPane is null"); |
| 26 | + } |
| 27 | + |
| 28 | + this.pane = pane; |
| 29 | + setOpaque(false); |
| 30 | + JLabel label = new JLabel() { |
| 31 | + public String getText() { |
| 32 | + int i = pane.indexOfTabComponent(CloseButtonComponent.this); |
| 33 | + if (i != -1) { |
| 34 | + return pane.getTitleAt(i); |
| 35 | + } |
| 36 | + |
| 37 | + return null; |
| 38 | + } |
| 39 | + }; |
| 40 | + |
| 41 | + add(label); |
| 42 | + label.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 5)); |
| 43 | + JButton button = new CloseButton(); |
| 44 | + add(button); |
| 45 | + |
| 46 | + JPopupMenu rightClickMenu = new JPopupMenu(); |
| 47 | + JMenuItem closeAllTabs = new JMenuItem(String.valueOf(TranslatedStrings.CLOSE_ALL_BUT_THIS)); |
| 48 | + JMenuItem closeTab = new JMenuItem(String.valueOf(TranslatedStrings.CLOSE_TAB)); |
| 49 | + |
| 50 | + rightClickMenu.add(closeAllTabs); |
| 51 | + rightClickMenu.add(closeTab); |
| 52 | + button.setComponentPopupMenu(rightClickMenu); |
| 53 | + |
| 54 | + button.addMouseListener(new MouseClickedListener(e -> |
| 55 | + { |
| 56 | + if (pane.indexOfTabComponent(CloseButtonComponent.this) != -1) |
| 57 | + pane.remove(pane.indexOfTabComponent(CloseButtonComponent.this)); |
| 58 | + })); |
| 59 | + |
| 60 | + closeTab.addActionListener(e -> |
| 61 | + { |
| 62 | + if (pane.indexOfTabComponent(CloseButtonComponent.this) != -1) |
| 63 | + pane.remove(pane.indexOfTabComponent(CloseButtonComponent.this)); |
| 64 | + }); |
| 65 | + closeAllTabs.addActionListener(e -> |
| 66 | + { |
| 67 | + |
| 68 | + while (true) { |
| 69 | + if (pane.getTabCount() <= 1) |
| 70 | + return; |
| 71 | + |
| 72 | + if (pane.indexOfTabComponent(CloseButtonComponent.this) != 0) |
| 73 | + pane.remove(0); |
| 74 | + else |
| 75 | + pane.remove(1); |
| 76 | + } |
| 77 | + }); |
| 78 | + |
| 79 | + setBorder(BorderFactory.createEmptyBorder(2, 0, 0, 0)); |
| 80 | + } |
| 81 | + |
| 82 | +} |
0 commit comments