Browse Source

PopupMenu: Implement ScrollPopupMenu based on the new DarkPopupMenuUI.KEY_MAX_POPUP_SIZE property.

Previously ScrollPopupMenu duplicated the implementation of DarkPopupMenuUI.
pull/245/head
weisj 3 years ago
parent
commit
606949875d
No known key found for this signature in database
GPG Key ID: 31124CB75461DA2A
  1. 106
      core/src/main/java/com/github/weisj/darklaf/components/ScrollPopupMenu.java
  2. 11
      core/src/main/java/com/github/weisj/darklaf/components/text/TextFieldHistoryPopup.java

106
core/src/main/java/com/github/weisj/darklaf/components/ScrollPopupMenu.java

@ -21,112 +21,42 @@
*/
package com.github.weisj.darklaf.components;
import java.awt.*;
import java.awt.Dimension;
import javax.swing.*;
import javax.swing.JPopupMenu;
import com.github.weisj.darklaf.ui.popupmenu.PopupMenuContainer;
import com.github.weisj.darklaf.util.PropertyKey;
import com.github.weisj.darklaf.ui.popupmenu.DarkPopupMenuUI;
/** @author Jannis Weis */
public class ScrollPopupMenu extends JPopupMenu {
private final PopupMenuContainer popupMenuContainer;
private int maxHeight;
private Popup popup;
private int posX;
private int posY;
private boolean isVisible;
private final Dimension maxSize = new Dimension(-1, -1);
public ScrollPopupMenu(final int maxHeight) {
popupMenuContainer = new PopupMenuContainer();
this.maxHeight = maxHeight;
}
/**
* Set the maximum height of the popup. If the size is larger than the specified maximum height the
* content will be wrapped inside a scroll pane.
*
* <p>
* Note: A value of less than or equal to 0 indicates that the height should not be limited.
*
* @param maxHeight the max height to use.
*/
public void setMaxHeight(final int maxHeight) {
this.maxHeight = maxHeight;
}
protected void showPopup() {
isVisible = true;
popup = createPopup();
popup.show();
{
putClientProperty(DarkPopupMenuUI.KEY_MAX_POPUP_SIZE, maxSize);
}
/**
* Get the scroll pane of the popup.
*
* @return scroll pane;
*/
public JScrollPane getScrollPane() {
return popupMenuContainer.getScrollPane();
}
@Override
public boolean isVisible() {
return isVisible;
public ScrollPopupMenu(final int maxHeight) {
maxSize.height = maxHeight;
}
@Override
public void setLocation(final int x, final int y) {
posX = x;
posY = y;
public ScrollPopupMenu(final int maxWidth, final int maxHeight) {
maxSize.setSize(maxWidth, maxHeight);
}
@Override
public void setVisible(final boolean b) {
if (b == isVisible()) {
return;
}
if (b) {
if (isPopupMenu()) {
MenuElement[] menuElements = new MenuElement[1];
if (getSubElements().length > 0) {
menuElements = new MenuElement[2];
menuElements[1] = getSubElements()[0];
}
menuElements[0] = this;
MenuSelectionManager.defaultManager().setSelectedPath(menuElements);
}
firePopupMenuWillBecomeVisible();
showPopup();
firePropertyChange(PropertyKey.VISIBLE, Boolean.FALSE, Boolean.TRUE);
} else {
hidePopup();
}
public int getMaxHeight() {
return maxSize.height;
}
protected void hidePopup() {
if (popup != null) {
firePopupMenuWillBecomeInvisible();
popup.hide();
isVisible = false;
popup = null;
firePropertyChange(PropertyKey.VISIBLE, Boolean.TRUE, Boolean.FALSE);
if (isPopupMenu()) {
MenuSelectionManager.defaultManager().clearSelectedPath();
}
}
public int getMaxWidth() {
return maxSize.width;
}
private Popup createPopup() {
return popupMenuContainer.createPopup(this, posX, posY, -1, maxHeight);
public void setMaxHeight(final int maxHeight) {
maxSize.height = maxHeight;
}
@Override
public void pack() {}
private boolean isPopupMenu() {
Component invoker = getInvoker();
return ((invoker != null) && !(invoker instanceof JMenu));
public void setMaxWidth(final int maxWidth) {
maxSize.width = maxWidth;
}
}

11
core/src/main/java/com/github/weisj/darklaf/components/text/TextFieldHistoryPopup.java

@ -122,15 +122,6 @@ public class TextFieldHistoryPopup extends ScrollPopupMenu implements SearchList
@Override
public void show(final Component invoker, final int x, final int y) {
if (history.size() == 0) return;
super.show(invoker, x, y);
}
@Override
protected void showPopup() {
if (history.size() == 0) {
firePopupMenuCanceled();
return;
}
this.removeAll();
LinkedList<String> list = new LinkedList<>(history);
Iterator<String> itr = list.descendingIterator();
@ -138,7 +129,7 @@ public class TextFieldHistoryPopup extends ScrollPopupMenu implements SearchList
String item = itr.next();
add(new JMenuItem(new PlainAction(item, () -> textField.setText(item))));
}
super.showPopup();
super.show(invoker, x, y);
}
/** Clear all entries from the history. */

Loading…
Cancel
Save