Styling: support ComboBox

This commit is contained in:
Karl Tauber
2021-06-21 20:58:04 +02:00
parent 24a9fa1ccc
commit 0c51dfe19c
2 changed files with 97 additions and 18 deletions

View File

@@ -40,6 +40,7 @@ import java.awt.event.MouseListener;
import java.awt.geom.Rectangle2D;
import java.beans.PropertyChangeListener;
import java.lang.ref.WeakReference;
import java.util.Map;
import javax.swing.AbstractAction;
import javax.swing.BorderFactory;
import javax.swing.CellRendererPane;
@@ -67,6 +68,7 @@ import javax.swing.plaf.basic.BasicComboPopup;
import javax.swing.plaf.basic.ComboPopup;
import javax.swing.text.JTextComponent;
import com.formdev.flatlaf.FlatClientProperties;
import com.formdev.flatlaf.ui.FlatStyleSupport.Styleable;
import com.formdev.flatlaf.util.SystemInfo;
import com.formdev.flatlaf.util.UIScale;
@@ -110,39 +112,47 @@ import com.formdev.flatlaf.util.UIScale;
public class FlatComboBoxUI
extends BasicComboBoxUI
{
protected int minimumWidth;
protected int editorColumns;
protected String buttonStyle;
protected String arrowType;
@Styleable protected int minimumWidth;
@Styleable protected int editorColumns;
@Styleable protected String buttonStyle;
@Styleable protected String arrowType;
protected boolean isIntelliJTheme;
protected Color borderColor;
protected Color disabledBorderColor;
@Styleable protected Color borderColor;
@Styleable protected Color disabledBorderColor;
protected Color editableBackground;
protected Color focusedBackground;
protected Color disabledBackground;
protected Color disabledForeground;
@Styleable protected Color editableBackground;
@Styleable protected Color focusedBackground;
@Styleable protected Color disabledBackground;
@Styleable protected Color disabledForeground;
protected Color buttonBackground;
protected Color buttonEditableBackground;
protected Color buttonFocusedBackground;
protected Color buttonArrowColor;
protected Color buttonDisabledArrowColor;
protected Color buttonHoverArrowColor;
protected Color buttonPressedArrowColor;
@Styleable protected Color buttonBackground;
@Styleable protected Color buttonEditableBackground;
@Styleable protected Color buttonFocusedBackground;
@Styleable protected Color buttonArrowColor;
@Styleable protected Color buttonDisabledArrowColor;
@Styleable protected Color buttonHoverArrowColor;
@Styleable protected Color buttonPressedArrowColor;
protected Color popupFocusedBackground;
@Styleable protected Color popupFocusedBackground;
private MouseListener hoverListener;
protected boolean hover;
protected boolean pressed;
private WeakReference<Component> lastRendererComponent;
private Map<String, Object> oldStyleValues;
public static ComponentUI createUI( JComponent c ) {
return new FlatComboBoxUI();
}
@Override
public void installUI( JComponent c ) {
super.installUI( c );
applyStyle( FlatStyleSupport.getStyle( comboBox ) );
}
@Override
protected void installListeners() {
super.installListeners();
@@ -250,6 +260,8 @@ public class FlatComboBoxUI
popupFocusedBackground = null;
oldStyleValues = null;
MigLayoutVisualPadding.uninstall( comboBox );
}
@@ -329,6 +341,11 @@ public class FlatComboBoxUI
comboBox.repaint();
else if( FlatClientProperties.MINIMUM_WIDTH.equals( propertyName ) )
comboBox.revalidate();
else if( FlatClientProperties.STYLE.equals( propertyName ) ) {
applyStyle( e.getNewValue() );
comboBox.revalidate();
comboBox.repaint();
}
};
}
@@ -403,6 +420,29 @@ public class FlatComboBoxUI
return new FlatComboBoxButton();
}
/**
* @since TODO
*/
protected void applyStyle( Object style ) {
int oldEditorColumns = editorColumns;
oldStyleValues = FlatStyleSupport.parseAndApply( oldStyleValues, style, this::applyStyleProperty );
if( arrowButton instanceof FlatComboBoxButton )
((FlatComboBoxButton)arrowButton).updateStyle();
if( popup instanceof FlatComboPopup )
((FlatComboPopup)popup).updateStyle();
if( editorColumns != oldEditorColumns && editor instanceof JTextField )
((JTextField)editor).setColumns( editorColumns );
}
/**
* @since TODO
*/
protected Object applyStyleProperty( String key, Object value ) {
return FlatStyleSupport.applyToAnnotatedObject( this, key, value );
}
@Override
public void update( Graphics g, JComponent c ) {
float focusWidth = FlatUIUtils.getBorderFocusWidth( c );
@@ -632,6 +672,11 @@ public class FlatComboBoxUI
hoverForeground, hoverBackground, pressedForeground, pressedBackground );
}
protected void updateStyle() {
updateStyle( arrowType, buttonArrowColor, buttonDisabledArrowColor,
buttonHoverArrowColor, null, buttonPressedArrowColor, null );
}
@Override
protected boolean isHover() {
return super.isHover() || (!comboBox.isEditable() ? hover : false);
@@ -727,6 +772,10 @@ public class FlatComboBoxUI
super.configureList();
list.setCellRenderer( new PopupListCellRenderer() );
updateStyle();
}
void updateStyle() {
if( popupFocusedBackground != null )
list.setBackground( popupFocusedBackground );
}

View File

@@ -118,6 +118,36 @@ public class FlatStylingTests
radioButton( ui );
}
@Test
void comboBox() {
FlatComboBoxUI ui = new FlatComboBoxUI();
// create arrow button
ui.installUI( new JComboBox<>() );
ui.applyStyle( "minimumWidth: 100" );
ui.applyStyle( "editorColumns: 10" );
ui.applyStyle( "buttonStyle: auto" );
ui.applyStyle( "arrowType: chevron" );
ui.applyStyle( "borderColor: #fff" );
ui.applyStyle( "disabledBorderColor: #fff" );
ui.applyStyle( "editableBackground: #fff" );
ui.applyStyle( "focusedBackground: #fff" );
ui.applyStyle( "disabledBackground: #fff" );
ui.applyStyle( "disabledForeground: #fff" );
ui.applyStyle( "buttonBackground: #fff" );
ui.applyStyle( "buttonFocusedBackground: #fff" );
ui.applyStyle( "buttonEditableBackground: #fff" );
ui.applyStyle( "buttonArrowColor: #fff" );
ui.applyStyle( "buttonDisabledArrowColor: #fff" );
ui.applyStyle( "buttonHoverArrowColor: #fff" );
ui.applyStyle( "buttonPressedArrowColor: #fff" );
ui.applyStyle( "popupFocusedBackground: #fff" );
}
@Test
void editorPane() {
FlatEditorPaneUI ui = new FlatEditorPaneUI();