帆软报表设计器源代码。
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

103 lines
3.3 KiB

package com.fine.theme.light.ui;
import com.fine.theme.utils.FineClientProperties;
import com.fine.theme.utils.FineUIScale;
import com.fine.theme.utils.FineUIUtils;
import com.formdev.flatlaf.ui.FlatComboBoxUI;
import com.formdev.flatlaf.ui.FlatUIUtils;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;
import javax.swing.SwingConstants;
import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.basic.ComboPopup;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.Rectangle;
/**
* 提供 {@link javax.swing.JComboBox} 的UI类
*
* @author Levy.Xie
* @since 11.0
* Created on 2023/12/07
*/
public class FineComboBoxUI extends FlatComboBoxUI {
public static ComponentUI createUI(JComponent c) {
return new FineComboBoxUI();
}
@Override
protected JButton createArrowButton() {
return new FineComboBoxButton();
}
protected class FineComboBoxButton extends FlatComboBoxButton {
@Override
protected void paintArrow(Graphics2D g) {
if (isPopupVisible(comboBox)) {
setDirection(SwingConstants.NORTH);
} else {
setDirection(SwingConstants.SOUTH);
}
super.paintArrow(g);
}
}
@Override
public Dimension getMinimumSize(JComponent c) {
// ComboBox基于子组件计算适配尺寸性能一般,仅考虑部分ComboBox进行适配计算,其他采用默认值
if (isAdaptiveComboBox(c)) {
Dimension dimension = super.getMinimumSize(c);
return new Dimension(Math.min(dimension.width,
FineUIUtils.getAndScaleInt("ComboBox.maximumWidth", 400)), dimension.height);
}
return FineUIScale.scale(new Dimension(
FlatUIUtils.getUIInt("ComboBox.minimumWidth", 72),
FlatUIUtils.getUIInt("ComboBox.comboHeight", 24)
));
}
static boolean isAdaptiveComboBox(JComponent c) {
Object value = c.getClientProperty(FineClientProperties.COMBO_BOX_TYPE);
if (value instanceof String) {
return FineClientProperties.ADAPTIVE_COMBO_BOX.equals(value);
}
return false;
}
@Override
protected ComboPopup createPopup() {
return new FineComboPopup(this.comboBox);
}
protected class FineComboPopup extends FlatComboPopup {
protected FineComboPopup(JComboBox combo) {
super(combo);
}
@Override
protected Rectangle computePopupBounds(int px, int py, int pw, int ph) {
Rectangle fitRectangle = super.computePopupBounds(px, py, pw, ph);
// 限制最大宽度,如超出则高度预留展示横向滚动条所需宽度
int comboWidth = comboBox.getWidth();
if (fitRectangle.width > comboWidth) {
return new Rectangle(px, py, comboWidth, fitRectangle.height + FlatUIUtils.getUIInt("ScrollBar.width", 10));
}
return fitRectangle;
}
@Override
protected JScrollPane createScroller() {
return new JScrollPane( list,
ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
}
}
}