-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
413 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
src/main/java/the/bytecode/club/uikit/tabpopup/AbstractJTabbedPanePopupMenuHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package the.bytecode.club.uikit.tabpopup; | ||
|
||
import java.awt.Component; | ||
|
||
import javax.swing.JPopupMenu; | ||
import javax.swing.JTabbedPane; | ||
|
||
/** | ||
* Show PopupMenu on Tabs | ||
* @author su | ||
* | ||
*/ | ||
public abstract class AbstractJTabbedPanePopupMenuHandler extends JTabbedPanePopupEventHandler implements ITabPopupEventListener { | ||
|
||
public AbstractJTabbedPanePopupMenuHandler(JTabbedPane tabbedPane) { | ||
super(tabbedPane); | ||
registerPopupEventListener(this); | ||
} | ||
|
||
@Override | ||
public void onTabPopupEvent(JTabbedPane tabbedPane, int index, TabPopupEvent e) { | ||
JPopupMenu popupMenu = toBuildTabPopupMenu(tabbedPane, e.getPopupOnTab()); | ||
popupTabMenuWithEvent(popupMenu, e); | ||
} | ||
|
||
public abstract JPopupMenu toBuildTabPopupMenu(JTabbedPane tabbedPane, Component popupOnTab); | ||
|
||
|
||
public static void popupTabMenuWithEvent(JPopupMenu popupMenu, TabPopupEvent e) { | ||
popupMenu.show(e.getComponent(), e.getX(), e.getY()); | ||
} | ||
|
||
} |
13 changes: 13 additions & 0 deletions
13
src/main/java/the/bytecode/club/uikit/tabpopup/ITabPopupEventListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package the.bytecode.club.uikit.tabpopup; | ||
|
||
import javax.swing.JTabbedPane; | ||
|
||
public interface ITabPopupEventListener { | ||
/** | ||
* | ||
* @param tabbedPane | ||
* @param index, index of tab | ||
* @param e | ||
*/ | ||
public void onTabPopupEvent(JTabbedPane tabbedPane, int index, TabPopupEvent e); | ||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/the/bytecode/club/uikit/tabpopup/ITabZeroComponentEventListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package the.bytecode.club.uikit.tabpopup; | ||
|
||
import javax.swing.JTabbedPane; | ||
|
||
public interface ITabZeroComponentEventListener { | ||
/** | ||
* | ||
* @param tabbedPane | ||
*/ | ||
public void onTabZeroComponent(JTabbedPane tabbedPane); | ||
} |
58 changes: 58 additions & 0 deletions
58
src/main/java/the/bytecode/club/uikit/tabpopup/JTabbedPanePopupEventHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package the.bytecode.club.uikit.tabpopup; | ||
|
||
import java.awt.Component; | ||
import java.awt.event.MouseAdapter; | ||
import java.awt.event.MouseEvent; | ||
|
||
import javax.swing.JTabbedPane; | ||
|
||
/** | ||
* Register PopupEvent Handler on TabbedPane | ||
* @author su | ||
* | ||
*/ | ||
public class JTabbedPanePopupEventHandler { | ||
protected final JTabbedPane tabbedPane; | ||
private ITabPopupEventListener tabPopupEventListener; | ||
|
||
public JTabbedPanePopupEventHandler(JTabbedPane tabbedPane) { | ||
super(); | ||
this.tabbedPane = tabbedPane; | ||
this.registerMouseEventListener(); | ||
} | ||
|
||
private void registerMouseEventListener() { | ||
this.tabbedPane.addMouseListener(new MouseAdapter() { | ||
@Override | ||
public void mousePressed(MouseEvent e) { | ||
tryTriggerTabPopupEvent(e); | ||
} | ||
|
||
@Override | ||
public void mouseReleased(MouseEvent e) { | ||
tryTriggerTabPopupEvent(e); | ||
} | ||
}); | ||
} | ||
|
||
public void registerPopupEventListener(ITabPopupEventListener tabPopupEventListener) { | ||
this.tabPopupEventListener = tabPopupEventListener; | ||
} | ||
|
||
protected void tryTriggerTabPopupEvent(MouseEvent e) { | ||
if (e.isPopupTrigger()) { | ||
int index = tabbedPane.indexAtLocation(e.getX(), e.getY()); | ||
if (index != -1) { | ||
Component popupOnTab = tabbedPane.getComponentAt(index); | ||
if (this.tabPopupEventListener != null) { | ||
this.tabPopupEventListener.onTabPopupEvent(tabbedPane, index, new TabPopupEvent(e, popupOnTab)); | ||
} | ||
} | ||
} | ||
} | ||
|
||
public JTabbedPane getTabbedPane() { | ||
return tabbedPane; | ||
} | ||
|
||
} |
26 changes: 26 additions & 0 deletions
26
src/main/java/the/bytecode/club/uikit/tabpopup/TabPopupEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package the.bytecode.club.uikit.tabpopup; | ||
|
||
import java.awt.Component; | ||
import java.awt.event.MouseEvent; | ||
|
||
public class TabPopupEvent extends MouseEvent { | ||
/** | ||
* | ||
*/ | ||
private static final long serialVersionUID = 2510164400674753411L; | ||
|
||
private final Component popupOnTab; | ||
|
||
public TabPopupEvent(MouseEvent e, Component popupOnTab) { | ||
super(e.getComponent(), e.getID(), e.getWhen(), e.getModifiers(), | ||
e.getX(), e.getY(), e.getClickCount(), e.isPopupTrigger(), e.getButton()); | ||
|
||
this.popupOnTab = popupOnTab; | ||
} | ||
|
||
public Component getPopupOnTab() { | ||
return popupOnTab; | ||
} | ||
|
||
|
||
} |
87 changes: 87 additions & 0 deletions
87
src/main/java/the/bytecode/club/uikit/tabpopup/closer/JTabbedPaneCloser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package the.bytecode.club.uikit.tabpopup.closer; | ||
|
||
import java.awt.Component; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import javax.swing.JTabbedPane; | ||
|
||
import the.bytecode.club.uikit.tabpopup.ITabZeroComponentEventListener; | ||
|
||
/** | ||
* Provide function of closing tabs | ||
* @author su | ||
* | ||
*/ | ||
public class JTabbedPaneCloser { | ||
private JTabbedPane tabbedPane; | ||
private ITabZeroComponentEventListener tabZeroComponentEventListener; | ||
|
||
public JTabbedPaneCloser(JTabbedPane tabbedPane) { | ||
super(); | ||
this.tabbedPane = tabbedPane; | ||
} | ||
|
||
public JTabbedPaneCloser(JTabbedPane tabbedPane, ITabZeroComponentEventListener tabZeroComponentEventListener) { | ||
this(tabbedPane); | ||
this.tabZeroComponentEventListener = tabZeroComponentEventListener; | ||
} | ||
|
||
public void removeComponent(Component component) { | ||
this.tabbedPane.remove(component); | ||
tryTriggerTabZeroComponentEvent(); | ||
} | ||
|
||
public void removeOtherComponents(Component component) { | ||
removeOtherComponents(component, false); | ||
} | ||
|
||
protected void removeOtherComponents(Component component, boolean equalStop) { | ||
int i = this.tabbedPane.getTabCount(); | ||
while (i-- > 0) { | ||
Component c = this.tabbedPane.getComponentAt(i); | ||
if (c != component) { | ||
this.tabbedPane.remove(i); | ||
} else if (equalStop) { | ||
break ; | ||
} | ||
} | ||
|
||
tryTriggerTabZeroComponentEvent(); | ||
} | ||
|
||
public void removeLeftComponents(Component component) { | ||
int count = this.tabbedPane.getTabCount(); | ||
int i = 0; | ||
List<Component> removeTabs = new ArrayList<>(); | ||
do { | ||
Component c = this.tabbedPane.getComponentAt(i); | ||
if (c != component) { | ||
removeTabs.add(c); | ||
} else { | ||
break ; | ||
} | ||
} while (i++ < count); | ||
|
||
for (Component c : removeTabs) { | ||
this.tabbedPane.remove(c); | ||
} | ||
|
||
tryTriggerTabZeroComponentEvent(); | ||
} | ||
|
||
public void removeRightComponents(Component component) { | ||
removeOtherComponents(component, true); | ||
} | ||
|
||
public void removeAllComponents() { | ||
this.tabbedPane.removeAll(); | ||
tryTriggerTabZeroComponentEvent(); | ||
} | ||
|
||
private void tryTriggerTabZeroComponentEvent() { | ||
if (this.tabbedPane.getTabCount() == 0 && tabZeroComponentEventListener != null) { | ||
tabZeroComponentEventListener.onTabZeroComponent(this.tabbedPane); | ||
} | ||
} | ||
} |
Oops, something went wrong.