mirror of
https://github.com/JFormDesigner/FlatLaf.git
synced 2025-12-07 22:40:53 +03:00
Styling: support TextArea, TextPane and EditorPane
This commit is contained in:
@@ -23,6 +23,8 @@ import java.awt.Graphics;
|
||||
import java.awt.Graphics2D;
|
||||
import java.awt.event.FocusListener;
|
||||
import java.beans.PropertyChangeEvent;
|
||||
import java.util.Map;
|
||||
import java.util.function.Consumer;
|
||||
import javax.swing.JComponent;
|
||||
import javax.swing.JEditorPane;
|
||||
import javax.swing.UIManager;
|
||||
@@ -30,6 +32,7 @@ import javax.swing.plaf.ComponentUI;
|
||||
import javax.swing.plaf.basic.BasicEditorPaneUI;
|
||||
import javax.swing.text.JTextComponent;
|
||||
import com.formdev.flatlaf.FlatClientProperties;
|
||||
import com.formdev.flatlaf.ui.FlatStyleSupport.Styleable;
|
||||
import com.formdev.flatlaf.util.HiDPIUtils;
|
||||
|
||||
/**
|
||||
@@ -61,17 +64,25 @@ import com.formdev.flatlaf.util.HiDPIUtils;
|
||||
public class FlatEditorPaneUI
|
||||
extends BasicEditorPaneUI
|
||||
{
|
||||
protected int minimumWidth;
|
||||
@Styleable protected int minimumWidth;
|
||||
protected boolean isIntelliJTheme;
|
||||
protected Color focusedBackground;
|
||||
@Styleable protected Color focusedBackground;
|
||||
|
||||
private Object oldHonorDisplayProperties;
|
||||
private FocusListener focusListener;
|
||||
private Map<String, Object> oldStyleValues;
|
||||
|
||||
public static ComponentUI createUI( JComponent c ) {
|
||||
return new FlatEditorPaneUI();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void installUI( JComponent c ) {
|
||||
super.installUI( c );
|
||||
|
||||
applyStyle( FlatStyleSupport.getStyle( c ) );
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void installDefaults() {
|
||||
super.installDefaults();
|
||||
@@ -115,17 +126,37 @@ public class FlatEditorPaneUI
|
||||
@Override
|
||||
protected void propertyChange( PropertyChangeEvent e ) {
|
||||
super.propertyChange( e );
|
||||
propertyChange( getComponent(), e );
|
||||
propertyChange( getComponent(), e, this::applyStyle );
|
||||
}
|
||||
|
||||
static void propertyChange( JTextComponent c, PropertyChangeEvent e ) {
|
||||
static void propertyChange( JTextComponent c, PropertyChangeEvent e, Consumer<Object> applyStyle ) {
|
||||
switch( e.getPropertyName() ) {
|
||||
case FlatClientProperties.MINIMUM_WIDTH:
|
||||
c.revalidate();
|
||||
break;
|
||||
|
||||
case FlatClientProperties.COMPONENT_STYLE:
|
||||
applyStyle.accept( e.getNewValue() );
|
||||
c.revalidate();
|
||||
c.repaint();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @since TODO
|
||||
*/
|
||||
protected void applyStyle( Object style ) {
|
||||
oldStyleValues = FlatStyleSupport.parseAndApply( oldStyleValues, style, this::applyStyleProperty );
|
||||
}
|
||||
|
||||
/**
|
||||
* @since TODO
|
||||
*/
|
||||
protected Object applyStyleProperty( String key, Object value ) {
|
||||
return FlatStyleSupport.applyToAnnotatedObject( this, key, value );
|
||||
}
|
||||
|
||||
@Override
|
||||
public Dimension getPreferredSize( JComponent c ) {
|
||||
return applyMinimumWidth( c, super.getPreferredSize( c ), minimumWidth );
|
||||
|
||||
@@ -22,6 +22,7 @@ import java.awt.Graphics;
|
||||
import java.awt.Graphics2D;
|
||||
import java.awt.event.FocusListener;
|
||||
import java.beans.PropertyChangeEvent;
|
||||
import java.util.Map;
|
||||
import javax.swing.JComponent;
|
||||
import javax.swing.JTextArea;
|
||||
import javax.swing.UIManager;
|
||||
@@ -29,6 +30,7 @@ import javax.swing.plaf.ComponentUI;
|
||||
import javax.swing.plaf.UIResource;
|
||||
import javax.swing.plaf.basic.BasicTextAreaUI;
|
||||
import javax.swing.text.JTextComponent;
|
||||
import com.formdev.flatlaf.ui.FlatStyleSupport.Styleable;
|
||||
import com.formdev.flatlaf.util.HiDPIUtils;
|
||||
|
||||
/**
|
||||
@@ -60,14 +62,15 @@ import com.formdev.flatlaf.util.HiDPIUtils;
|
||||
public class FlatTextAreaUI
|
||||
extends BasicTextAreaUI
|
||||
{
|
||||
protected int minimumWidth;
|
||||
@Styleable protected int minimumWidth;
|
||||
protected boolean isIntelliJTheme;
|
||||
protected Color background;
|
||||
protected Color disabledBackground;
|
||||
protected Color inactiveBackground;
|
||||
protected Color focusedBackground;
|
||||
@Styleable protected Color disabledBackground;
|
||||
@Styleable protected Color inactiveBackground;
|
||||
@Styleable protected Color focusedBackground;
|
||||
|
||||
private FocusListener focusListener;
|
||||
private Map<String, Object> oldStyleValues;
|
||||
|
||||
public static ComponentUI createUI( JComponent c ) {
|
||||
return new FlatTextAreaUI();
|
||||
@@ -77,7 +80,7 @@ public class FlatTextAreaUI
|
||||
public void installUI( JComponent c ) {
|
||||
super.installUI( c );
|
||||
|
||||
updateBackground();
|
||||
applyStyle( FlatStyleSupport.getStyle( c ) );
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -122,7 +125,7 @@ public class FlatTextAreaUI
|
||||
@Override
|
||||
protected void propertyChange( PropertyChangeEvent e ) {
|
||||
super.propertyChange( e );
|
||||
FlatEditorPaneUI.propertyChange( getComponent(), e );
|
||||
FlatEditorPaneUI.propertyChange( getComponent(), e, this::applyStyle );
|
||||
|
||||
switch( e.getPropertyName() ) {
|
||||
case "editable":
|
||||
@@ -132,6 +135,21 @@ public class FlatTextAreaUI
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @since TODO
|
||||
*/
|
||||
protected void applyStyle( Object style ) {
|
||||
oldStyleValues = FlatStyleSupport.parseAndApply( oldStyleValues, style, this::applyStyleProperty );
|
||||
updateBackground();
|
||||
}
|
||||
|
||||
/**
|
||||
* @since TODO
|
||||
*/
|
||||
protected Object applyStyleProperty( String key, Object value ) {
|
||||
return FlatStyleSupport.applyToAnnotatedObject( this, key, value );
|
||||
}
|
||||
|
||||
private void updateBackground() {
|
||||
JTextComponent c = getComponent();
|
||||
|
||||
|
||||
@@ -22,11 +22,13 @@ import java.awt.Graphics;
|
||||
import java.awt.Graphics2D;
|
||||
import java.awt.event.FocusListener;
|
||||
import java.beans.PropertyChangeEvent;
|
||||
import java.util.Map;
|
||||
import javax.swing.JComponent;
|
||||
import javax.swing.JEditorPane;
|
||||
import javax.swing.UIManager;
|
||||
import javax.swing.plaf.ComponentUI;
|
||||
import javax.swing.plaf.basic.BasicTextPaneUI;
|
||||
import com.formdev.flatlaf.ui.FlatStyleSupport.Styleable;
|
||||
import com.formdev.flatlaf.util.HiDPIUtils;
|
||||
|
||||
/**
|
||||
@@ -58,17 +60,25 @@ import com.formdev.flatlaf.util.HiDPIUtils;
|
||||
public class FlatTextPaneUI
|
||||
extends BasicTextPaneUI
|
||||
{
|
||||
protected int minimumWidth;
|
||||
@Styleable protected int minimumWidth;
|
||||
protected boolean isIntelliJTheme;
|
||||
protected Color focusedBackground;
|
||||
@Styleable protected Color focusedBackground;
|
||||
|
||||
private Object oldHonorDisplayProperties;
|
||||
private FocusListener focusListener;
|
||||
private Map<String, Object> oldStyleValues;
|
||||
|
||||
public static ComponentUI createUI( JComponent c ) {
|
||||
return new FlatTextPaneUI();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void installUI( JComponent c ) {
|
||||
super.installUI( c );
|
||||
|
||||
applyStyle( FlatStyleSupport.getStyle( c ) );
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void installDefaults() {
|
||||
super.installDefaults();
|
||||
@@ -112,7 +122,21 @@ public class FlatTextPaneUI
|
||||
@Override
|
||||
protected void propertyChange( PropertyChangeEvent e ) {
|
||||
super.propertyChange( e );
|
||||
FlatEditorPaneUI.propertyChange( getComponent(), e );
|
||||
FlatEditorPaneUI.propertyChange( getComponent(), e, this::applyStyle );
|
||||
}
|
||||
|
||||
/**
|
||||
* @since TODO
|
||||
*/
|
||||
protected void applyStyle( Object style ) {
|
||||
oldStyleValues = FlatStyleSupport.parseAndApply( oldStyleValues, style, this::applyStyleProperty );
|
||||
}
|
||||
|
||||
/**
|
||||
* @since TODO
|
||||
*/
|
||||
protected Object applyStyleProperty( String key, Object value ) {
|
||||
return FlatStyleSupport.applyToAnnotatedObject( this, key, value );
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -27,6 +27,7 @@ import javax.swing.JFormattedTextField;
|
||||
import javax.swing.JPasswordField;
|
||||
import javax.swing.JRadioButton;
|
||||
import javax.swing.JSplitPane;
|
||||
import javax.swing.JTextArea;
|
||||
import javax.swing.JTextField;
|
||||
import javax.swing.UIManager;
|
||||
import org.junit.jupiter.api.Test;
|
||||
@@ -80,6 +81,14 @@ public class FlatStylingTests
|
||||
radioButton( ui );
|
||||
}
|
||||
|
||||
@Test
|
||||
void editorPane() {
|
||||
FlatEditorPaneUI ui = new FlatEditorPaneUI();
|
||||
|
||||
ui.applyStyle( "minimumWidth: 100" );
|
||||
ui.applyStyle( "focusedBackground: #fff" );
|
||||
}
|
||||
|
||||
@Test
|
||||
void formattedTextField() {
|
||||
FlatFormattedTextFieldUI ui = new FlatFormattedTextFieldUI();
|
||||
@@ -266,6 +275,18 @@ public class FlatStylingTests
|
||||
ui.applyStyle( "gripGap: 2" );
|
||||
}
|
||||
|
||||
@Test
|
||||
void textArea() {
|
||||
FlatTextAreaUI ui = new FlatTextAreaUI();
|
||||
|
||||
ui.installUI( new JTextArea() );
|
||||
|
||||
ui.applyStyle( "minimumWidth: 100" );
|
||||
ui.applyStyle( "disabledBackground: #fff" );
|
||||
ui.applyStyle( "inactiveBackground: #fff" );
|
||||
ui.applyStyle( "focusedBackground: #fff" );
|
||||
}
|
||||
|
||||
@Test
|
||||
void textField() {
|
||||
FlatTextFieldUI ui = new FlatTextFieldUI();
|
||||
@@ -286,6 +307,14 @@ public class FlatStylingTests
|
||||
flatTextBorder( style -> ui.applyStyle( style ) );
|
||||
}
|
||||
|
||||
@Test
|
||||
void textPane() {
|
||||
FlatTextPaneUI ui = new FlatTextPaneUI();
|
||||
|
||||
ui.applyStyle( "minimumWidth: 100" );
|
||||
ui.applyStyle( "focusedBackground: #fff" );
|
||||
}
|
||||
|
||||
//---- component borders --------------------------------------------------
|
||||
|
||||
private void flatTextBorder( Consumer<String> applyStyle ) {
|
||||
|
||||
Reference in New Issue
Block a user