mirror of
https://github.com/JFormDesigner/FlatLaf.git
synced 2026-02-11 06:27:13 -06:00
TextField and PasswordField: reduced duplicate code
This commit is contained in:
@@ -19,15 +19,12 @@ package com.formdev.flatlaf.ui;
|
||||
import static com.formdev.flatlaf.util.UIScale.scale;
|
||||
import java.awt.Dimension;
|
||||
import java.awt.Graphics;
|
||||
import java.awt.Graphics2D;
|
||||
import java.awt.event.FocusEvent;
|
||||
import java.awt.event.FocusListener;
|
||||
import javax.swing.JComponent;
|
||||
import javax.swing.LookAndFeel;
|
||||
import javax.swing.UIManager;
|
||||
import javax.swing.plaf.ComponentUI;
|
||||
import javax.swing.plaf.basic.BasicPasswordFieldUI;
|
||||
import javax.swing.text.JTextComponent;
|
||||
import com.formdev.flatlaf.util.SystemInfo;
|
||||
|
||||
/**
|
||||
@@ -46,7 +43,7 @@ public class FlatPasswordFieldUI
|
||||
protected int focusWidth;
|
||||
protected int minimumWidth;
|
||||
|
||||
private Handler handler;
|
||||
private FocusListener focusListener;
|
||||
|
||||
public static ComponentUI createUI( JComponent c ) {
|
||||
return new FlatPasswordFieldUI();
|
||||
@@ -77,41 +74,21 @@ public class FlatPasswordFieldUI
|
||||
protected void installListeners() {
|
||||
super.installListeners();
|
||||
|
||||
getComponent().addFocusListener( getHandler() );
|
||||
focusListener = new FlatUIUtils.RepaintFocusListener( getComponent() );
|
||||
getComponent().addFocusListener( focusListener );
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void uninstallListeners() {
|
||||
super.uninstallListeners();
|
||||
|
||||
getComponent().removeFocusListener( getHandler() );
|
||||
|
||||
handler = null;
|
||||
}
|
||||
|
||||
public Handler getHandler() {
|
||||
if( handler == null )
|
||||
handler = new Handler();
|
||||
return handler;
|
||||
getComponent().removeFocusListener( focusListener );
|
||||
focusListener = null;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void paintBackground( Graphics g ) {
|
||||
JTextComponent c = getComponent();
|
||||
|
||||
FlatUIUtils.paintParentBackground( g, c );
|
||||
|
||||
Graphics2D g2 = (Graphics2D) g.create();
|
||||
try {
|
||||
FlatUIUtils.setRenderingHints( g2 );
|
||||
|
||||
float focusWidth = (c.getBorder() instanceof FlatBorder) ? scale( (float) this.focusWidth ) : 0;
|
||||
|
||||
g2.setColor( c.getBackground() );
|
||||
FlatUIUtils.fillRoundRectangle( g2, 0, 0, c.getWidth(), c.getHeight(), focusWidth, 0 );
|
||||
} finally {
|
||||
g2.dispose();
|
||||
}
|
||||
FlatTextFieldUI.paintBackground( g, getComponent(), focusWidth );
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -129,20 +106,4 @@ public class FlatPasswordFieldUI
|
||||
size.width = Math.max( size.width, scale( minimumWidth + (focusWidth * 2) ) );
|
||||
return size;
|
||||
}
|
||||
|
||||
//---- class Handler ------------------------------------------------------
|
||||
|
||||
private class Handler
|
||||
implements FocusListener
|
||||
{
|
||||
@Override
|
||||
public void focusGained( FocusEvent e ) {
|
||||
getComponent().repaint();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void focusLost( FocusEvent e ) {
|
||||
getComponent().repaint();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,7 +21,6 @@ import java.awt.Container;
|
||||
import java.awt.Dimension;
|
||||
import java.awt.Graphics;
|
||||
import java.awt.Graphics2D;
|
||||
import java.awt.event.FocusEvent;
|
||||
import java.awt.event.FocusListener;
|
||||
import javax.swing.JComboBox;
|
||||
import javax.swing.JComponent;
|
||||
@@ -47,7 +46,7 @@ public class FlatTextFieldUI
|
||||
protected int focusWidth;
|
||||
protected int minimumWidth;
|
||||
|
||||
private Handler handler;
|
||||
private FocusListener focusListener;
|
||||
|
||||
public static ComponentUI createUI( JComponent c ) {
|
||||
return new FlatTextFieldUI();
|
||||
@@ -74,38 +73,34 @@ public class FlatTextFieldUI
|
||||
protected void installListeners() {
|
||||
super.installListeners();
|
||||
|
||||
getComponent().addFocusListener( getHandler() );
|
||||
focusListener = new FlatUIUtils.RepaintFocusListener( getComponent() );
|
||||
getComponent().addFocusListener( focusListener );
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void uninstallListeners() {
|
||||
super.uninstallListeners();
|
||||
|
||||
getComponent().removeFocusListener( getHandler() );
|
||||
|
||||
handler = null;
|
||||
}
|
||||
|
||||
public Handler getHandler() {
|
||||
if( handler == null )
|
||||
handler = new Handler();
|
||||
return handler;
|
||||
getComponent().removeFocusListener( focusListener );
|
||||
focusListener = null;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void paintBackground( Graphics g ) {
|
||||
JTextComponent c = getComponent();
|
||||
paintBackground( g, getComponent(), focusWidth );
|
||||
}
|
||||
|
||||
static void paintBackground( Graphics g, JTextComponent c, int focusWidth ) {
|
||||
FlatUIUtils.paintParentBackground( g, c );
|
||||
|
||||
Graphics2D g2 = (Graphics2D) g.create();
|
||||
try {
|
||||
FlatUIUtils.setRenderingHints( g2 );
|
||||
|
||||
float focusWidth = (c.getBorder() instanceof FlatBorder) ? scale( (float) this.focusWidth ) : 0;
|
||||
float fFocusWidth = (c.getBorder() instanceof FlatBorder) ? scale( (float) focusWidth ) : 0;
|
||||
|
||||
g2.setColor( c.getBackground() );
|
||||
FlatUIUtils.fillRoundRectangle( g2, 0, 0, c.getWidth(), c.getHeight(), focusWidth, 0 );
|
||||
FlatUIUtils.fillRoundRectangle( g2, 0, 0, c.getWidth(), c.getHeight(), fFocusWidth, 0 );
|
||||
} finally {
|
||||
g2.dispose();
|
||||
}
|
||||
@@ -132,20 +127,4 @@ public class FlatTextFieldUI
|
||||
size.width = Math.max( size.width, scale( minimumWidth + (focusWidth * 2) ) );
|
||||
return size;
|
||||
}
|
||||
|
||||
//---- class Handler ------------------------------------------------------
|
||||
|
||||
private class Handler
|
||||
implements FocusListener
|
||||
{
|
||||
@Override
|
||||
public void focusGained( FocusEvent e ) {
|
||||
getComponent().repaint();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void focusLost( FocusEvent e ) {
|
||||
getComponent().repaint();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,6 +26,8 @@ import java.awt.Insets;
|
||||
import java.awt.Rectangle;
|
||||
import java.awt.RenderingHints;
|
||||
import java.awt.Shape;
|
||||
import java.awt.event.FocusEvent;
|
||||
import java.awt.event.FocusListener;
|
||||
import java.awt.event.MouseAdapter;
|
||||
import java.awt.event.MouseEvent;
|
||||
import java.awt.geom.Path2D;
|
||||
@@ -251,10 +253,10 @@ public class FlatUIUtils
|
||||
public static class HoverListener
|
||||
extends MouseAdapter
|
||||
{
|
||||
private final JComponent repaintComponent;
|
||||
private final Component repaintComponent;
|
||||
private final Consumer<Boolean> hoverChanged;
|
||||
|
||||
public HoverListener( JComponent repaintComponent, Consumer<Boolean> hoverChanged ) {
|
||||
public HoverListener( Component repaintComponent, Consumer<Boolean> hoverChanged ) {
|
||||
this.repaintComponent = repaintComponent;
|
||||
this.hoverChanged = hoverChanged;
|
||||
}
|
||||
@@ -276,4 +278,26 @@ public class FlatUIUtils
|
||||
repaintComponent.repaint();
|
||||
}
|
||||
}
|
||||
|
||||
//---- class RepaintFocusListener -----------------------------------------
|
||||
|
||||
public static class RepaintFocusListener
|
||||
implements FocusListener
|
||||
{
|
||||
private final Component repaintComponent;
|
||||
|
||||
public RepaintFocusListener( Component repaintComponent ) {
|
||||
this.repaintComponent = repaintComponent;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void focusGained( FocusEvent e ) {
|
||||
repaintComponent.repaint();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void focusLost( FocusEvent e ) {
|
||||
repaintComponent.repaint();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,8 +28,6 @@ import java.awt.Insets;
|
||||
import java.awt.LayoutManager;
|
||||
import java.awt.Rectangle;
|
||||
import java.awt.Shape;
|
||||
import java.awt.event.FocusEvent;
|
||||
import java.awt.event.FocusListener;
|
||||
import java.awt.geom.Rectangle2D;
|
||||
import java.text.ParseException;
|
||||
import java.util.Calendar;
|
||||
@@ -159,18 +157,7 @@ public class FlatDatePickerUI
|
||||
editor.setName( "dateField" );
|
||||
editor.setBorder( BorderFactory.createEmptyBorder() );
|
||||
editor.setOpaque( false );
|
||||
editor.addFocusListener( new FocusListener() {
|
||||
@Override
|
||||
public void focusLost( FocusEvent e ) {
|
||||
if( datePicker != null )
|
||||
datePicker.repaint();
|
||||
}
|
||||
@Override
|
||||
public void focusGained( FocusEvent e ) {
|
||||
if( datePicker != null )
|
||||
datePicker.repaint();
|
||||
}
|
||||
} );
|
||||
editor.addFocusListener( new FlatUIUtils.RepaintFocusListener( datePicker ) );
|
||||
return editor;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user