|
|
@ -39,69 +39,89 @@ import com.github.weisj.darklaf.util.DarkUIUtil; |
|
|
|
|
|
|
|
|
|
|
|
public class PopupMenuContainer extends JPanel { |
|
|
|
public class PopupMenuContainer extends JPanel { |
|
|
|
|
|
|
|
|
|
|
|
private final JScrollPane scrollPane; |
|
|
|
private JScrollPane scrollPane; |
|
|
|
private final MenuKeyListener menuKeyListener; |
|
|
|
private JPanel view; |
|
|
|
private final PopupMenuListener menuListener; |
|
|
|
|
|
|
|
private final JPanel view; |
|
|
|
private MenuKeyListener menuKeyListener; |
|
|
|
|
|
|
|
private PopupMenuListener menuListener; |
|
|
|
private JPopupMenu popupMenu; |
|
|
|
private JPopupMenu popupMenu; |
|
|
|
|
|
|
|
|
|
|
|
public PopupMenuContainer() { |
|
|
|
public PopupMenuContainer() { |
|
|
|
super(new BorderLayout()); |
|
|
|
super(new BorderLayout()); |
|
|
|
view = new JPanel(new BorderLayout()); |
|
|
|
} |
|
|
|
OverlayScrollPane overlayScrollPane = createScrollPane(view); |
|
|
|
|
|
|
|
scrollPane = overlayScrollPane.getScrollPane(); |
|
|
|
|
|
|
|
add(overlayScrollPane, BorderLayout.CENTER); |
|
|
|
|
|
|
|
menuKeyListener = new MenuKeyListener() { |
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
|
|
public void menuKeyTyped(final MenuKeyEvent e) {} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
|
|
public void menuKeyPressed(final MenuKeyEvent e) { |
|
|
|
|
|
|
|
SwingUtilities.invokeLater(() -> { |
|
|
|
|
|
|
|
if (popupMenu == null) return; |
|
|
|
|
|
|
|
MenuElement[] path = e.getMenuSelectionManager().getSelectedPath(); |
|
|
|
|
|
|
|
if (path.length == 0) { |
|
|
|
|
|
|
|
return; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
Rectangle bounds = path[path.length - 1].getComponent().getBounds(); |
|
|
|
|
|
|
|
Rectangle r = SwingUtilities.convertRectangle(popupMenu, bounds, scrollPane); |
|
|
|
|
|
|
|
scrollPane.getViewport().scrollRectToVisible(r); |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
private void initComponents() { |
|
|
|
public void menuKeyReleased(final MenuKeyEvent e) {} |
|
|
|
if (view == null) { |
|
|
|
}; |
|
|
|
view = new JPanel(new BorderLayout()); |
|
|
|
menuListener = new PopupMenuAdapter() { |
|
|
|
OverlayScrollPane overlayScrollPane = createScrollPane(view); |
|
|
|
@Override |
|
|
|
scrollPane = overlayScrollPane.getScrollPane(); |
|
|
|
public void popupMenuWillBecomeInvisible(final PopupMenuEvent e) { |
|
|
|
add(overlayScrollPane, BorderLayout.CENTER); |
|
|
|
onHide(); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
protected MenuKeyListener getMenuKeyListener() { |
|
|
|
public void popupMenuCanceled(final PopupMenuEvent e) { |
|
|
|
if (menuKeyListener == null) { |
|
|
|
onHide(); |
|
|
|
menuKeyListener = new MenuKeyListener() { |
|
|
|
} |
|
|
|
@Override |
|
|
|
|
|
|
|
public void menuKeyTyped(final MenuKeyEvent e) {} |
|
|
|
|
|
|
|
|
|
|
|
private void onHide() { |
|
|
|
@Override |
|
|
|
if (popupMenu == null) return; |
|
|
|
public void menuKeyPressed(final MenuKeyEvent e) { |
|
|
|
popupMenu.removePopupMenuListener(this); |
|
|
|
SwingUtilities.invokeLater(() -> { |
|
|
|
popupMenu.removeMenuKeyListener(menuKeyListener); |
|
|
|
if (popupMenu == null) return; |
|
|
|
} |
|
|
|
MenuElement[] path = e.getMenuSelectionManager().getSelectedPath(); |
|
|
|
}; |
|
|
|
if (path.length == 0) { |
|
|
|
|
|
|
|
return; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
Rectangle bounds = path[path.length - 1].getComponent().getBounds(); |
|
|
|
|
|
|
|
Rectangle r = SwingUtilities.convertRectangle(popupMenu, bounds, scrollPane); |
|
|
|
|
|
|
|
scrollPane.getViewport().scrollRectToVisible(r); |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
|
|
public void menuKeyReleased(final MenuKeyEvent e) {} |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return menuKeyListener; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
protected PopupMenuListener getMenuListener() { |
|
|
|
|
|
|
|
if (menuListener == null) { |
|
|
|
|
|
|
|
menuListener = new PopupMenuAdapter() { |
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
|
|
public void popupMenuWillBecomeInvisible(final PopupMenuEvent e) { |
|
|
|
|
|
|
|
onHide(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
|
|
public void popupMenuCanceled(final PopupMenuEvent e) { |
|
|
|
|
|
|
|
onHide(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void onHide() { |
|
|
|
|
|
|
|
if (popupMenu == null) return; |
|
|
|
|
|
|
|
popupMenu.removePopupMenuListener(this); |
|
|
|
|
|
|
|
popupMenu.removeMenuKeyListener(menuKeyListener); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return menuListener; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public void setPopupMenu(final JPopupMenu popupMenu) { |
|
|
|
public void setPopupMenu(final JPopupMenu popupMenu) { |
|
|
|
|
|
|
|
MenuKeyListener keyListener = getMenuKeyListener(); |
|
|
|
|
|
|
|
PopupMenuListener popupMenuListener = getMenuListener(); |
|
|
|
if (this.popupMenu != null) { |
|
|
|
if (this.popupMenu != null) { |
|
|
|
this.popupMenu.removeMenuKeyListener(menuKeyListener); |
|
|
|
this.popupMenu.removeMenuKeyListener(keyListener); |
|
|
|
this.popupMenu.removePopupMenuListener(menuListener); |
|
|
|
this.popupMenu.removePopupMenuListener(popupMenuListener); |
|
|
|
} |
|
|
|
} |
|
|
|
this.popupMenu = popupMenu; |
|
|
|
this.popupMenu = popupMenu; |
|
|
|
if (popupMenu != null) { |
|
|
|
if (popupMenu != null) { |
|
|
|
popupMenu.removeMenuKeyListener(menuKeyListener); |
|
|
|
popupMenu.removeMenuKeyListener(keyListener); |
|
|
|
popupMenu.removePopupMenuListener(menuListener); |
|
|
|
popupMenu.removePopupMenuListener(popupMenuListener); |
|
|
|
popupMenu.addMenuKeyListener(menuKeyListener); |
|
|
|
popupMenu.addMenuKeyListener(keyListener); |
|
|
|
popupMenu.addPopupMenuListener(menuListener); |
|
|
|
popupMenu.addPopupMenuListener(popupMenuListener); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -113,6 +133,7 @@ public class PopupMenuContainer extends JPanel { |
|
|
|
popupMenu.setBorderPainted(true); |
|
|
|
popupMenu.setBorderPainted(true); |
|
|
|
return PopupFactory.getSharedInstance().getPopup(popupMenu.getInvoker(), popupMenu, posX, posY); |
|
|
|
return PopupFactory.getSharedInstance().getPopup(popupMenu.getInvoker(), popupMenu, posX, posY); |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
|
|
|
|
initComponents(); |
|
|
|
int increment = 1; |
|
|
|
int increment = 1; |
|
|
|
if (popupMenu.getComponentCount() > 0) { |
|
|
|
if (popupMenu.getComponentCount() > 0) { |
|
|
|
increment = Math.max(1, popupMenu.getComponent(0).getPreferredSize().height / 2); |
|
|
|
increment = Math.max(1, popupMenu.getComponent(0).getPreferredSize().height / 2); |
|
|
|