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