TextField implemented

This commit is contained in:
Karl Tauber
2019-08-21 13:24:33 +02:00
parent b320918d83
commit fdbcb52629
9 changed files with 318 additions and 2 deletions

View File

@@ -18,17 +18,20 @@ package com.formdev.flatlaf;
import java.awt.Color;
import java.awt.Font;
import java.awt.Insets;
import java.awt.Toolkit;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import javax.swing.UIDefaults;
import javax.swing.UIDefaults.LazyValue;
import javax.swing.plaf.ColorUIResource;
import javax.swing.plaf.FontUIResource;
import javax.swing.plaf.InsetsUIResource;
import javax.swing.plaf.basic.BasicLookAndFeel;
import javax.swing.plaf.metal.MetalLookAndFeel;
import com.formdev.flatlaf.util.SystemInfo;
@@ -211,6 +214,10 @@ public abstract class FlatLaf
if( key.endsWith( ".icon" ) )
return parseInstance( value );
// insets
if( key.endsWith( ".margin" ) )
return parseInsets( value );
// colors
ColorUIResource color = parseColor( value );
if( color != null )
@@ -240,6 +247,20 @@ public abstract class FlatLaf
};
}
private Insets parseInsets( String value ) {
List<String> numbers = split( value, ',' );
try {
return new InsetsUIResource(
Integer.parseInt( numbers.get( 0 ) ),
Integer.parseInt( numbers.get( 1 ) ),
Integer.parseInt( numbers.get( 2 ) ),
Integer.parseInt( numbers.get( 3 ) ) );
} catch( NumberFormatException ex ) {
System.err.println( "invalid insets '" + value + "'" );
throw ex;
}
}
private ColorUIResource parseColor( String value ) {
try {
if( value.length() == 6 ) {
@@ -267,4 +288,18 @@ public abstract class FlatLaf
}
return null;
}
private static List<String> split( String str, char delim ) {
ArrayList<String> strs = new ArrayList<>();
int delimIndex = str.indexOf( delim );
int index = 0;
while( delimIndex >= 0 ) {
strs.add( str.substring( index, delimIndex ) );
index = delimIndex + 1;
delimIndex = str.indexOf( delim, index );
}
strs.add( str.substring( index ) );
return strs;
}
}

View File

@@ -0,0 +1,86 @@
/*
* Copyright 2019 FormDev Software GmbH
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.formdev.flatlaf.ui;
import static com.formdev.flatlaf.util.UIScale.*;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.Paint;
import javax.swing.UIManager;
import javax.swing.plaf.basic.BasicBorders;
import javax.swing.text.JTextComponent;
/**
* Border for various components (e.g. {@link javax.swing.JTextField}).
*
* @author Karl Tauber
*/
public class FlatBorder
extends BasicBorders.MarginBorder
{
@Override
public void paintBorder( Component c, Graphics g, int x, int y, int width, int height ) {
Graphics2D g2 = (Graphics2D) g.create();
try {
FlatUIUtils.setRenderingHints( g2 );
float focusWidth = getFocusWidth();
float lineWidth = getLineWidth();
if( c.hasFocus() ) {
g2.setColor( UIManager.getColor( "Component.focusColor" ) );
FlatUIUtils.paintOutlineBorder( g2, x, y, width, height, focusWidth, lineWidth, 0 );
}
g2.setPaint( getBorderColor( c ) );
FlatUIUtils.drawRoundRectangle( g2, x, y, width, height, focusWidth, lineWidth, 0 );
} finally {
g2.dispose();
}
}
private Paint getBorderColor( Component c ) {
boolean editable = !(c instanceof JTextComponent) || ((JTextComponent)c).isEditable();
return UIManager.getColor( c.isEnabled() && editable
? (c.hasFocus()
? "Component.focusedBorderColor"
: "Component.borderColor")
: "Component.disabledBorderColor" );
}
@Override
public Insets getBorderInsets( Component c, Insets insets ) {
float ow = getFocusWidth() + getLineWidth();
insets = super.getBorderInsets( c, insets );
insets.top = round( scale( (float) insets.top ) + ow );
insets.left = round( scale( (float) insets.left ) + ow );
insets.bottom = round( scale( (float) insets.bottom ) + ow );
insets.right = round( scale( (float) insets.right ) + ow );
return insets;
}
protected float getFocusWidth() {
return FlatUIUtils.getFocusWidth();
}
protected float getLineWidth() {
return FlatUIUtils.getLineWidth();
}
}

View File

@@ -0,0 +1,98 @@
/*
* Copyright 2019 FormDev Software GmbH
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.formdev.flatlaf.ui;
import java.awt.Color;
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.UIManager;
import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.basic.BasicTextFieldUI;
import javax.swing.text.JTextComponent;
/**
* Provides the Flat LaF UI delegate for {@link javax.swing.JTextField}.
*
* @author Karl Tauber
*/
public class FlatTextFieldUI
extends BasicTextFieldUI
{
private final Handler handler = new Handler();
public static ComponentUI createUI( JComponent c ) {
return new FlatTextFieldUI();
}
@Override
protected void installListeners() {
super.installListeners();
getComponent().addFocusListener( handler );
}
@Override
protected void uninstallListeners() {
super.uninstallListeners();
getComponent().removeFocusListener( handler );
}
@Override
protected void paintBackground( Graphics g ) {
JTextComponent c = getComponent();
FlatUIUtils.paintParentBackground( g, c );
Graphics2D g2 = (Graphics2D) g.create();
try {
FlatUIUtils.setRenderingHints( g2 );
float focusWidth = FlatUIUtils.getFocusWidth();
g2.setColor( getBackground( c ) );
FlatUIUtils.fillRoundRectangle( g2, 0, 0, c.getWidth(), c.getHeight(), focusWidth, 0 );
} finally {
g2.dispose();
}
}
private Color getBackground( JTextComponent c ) {
return c.isEditable() && c.isEnabled()
? c.getBackground()
: UIManager.getColor( "TextField.disabledBackground" );
}
//---- class Handler ------------------------------------------------------
private class Handler
implements FocusListener
{
@Override
public void focusGained( FocusEvent e ) {
getComponent().repaint();
}
@Override
public void focusLost( FocusEvent e ) {
getComponent().repaint();
}
}
}

View File

@@ -70,7 +70,7 @@ CheckBox.icon.selectedBorderColor=6B6B6B
CheckBox.icon.focusedBorderColor=466D94
CheckBox.icon.selectedFocusedBorderColor=466D94
CheckBox.icon.background=43494A
CheckBox.icon.disabledBackground=3C3F41
CheckBox.icon.disabledBackground=@background
CheckBox.icon.selectedBackground=43494A
CheckBox.icon.checkmarkColor=A7A7A7
CheckBox.icon.disabledCheckmarkColor=606060
@@ -78,9 +78,18 @@ CheckBox.icon.disabledCheckmarkColor=606060
#---- Component ----
Component.borderColor=646464
Component.disabledBorderColor=646464
Component.focusedBorderColor=466d94
Component.focusColor=3d6185
#---- Label ----
Label.disabledForeground=@disabledText
#---- TextField ----
TextField.background=45494A
TextField.disabledBackground=@background

View File

@@ -20,6 +20,7 @@ ButtonUI=com.formdev.flatlaf.ui.FlatButtonUI
CheckBoxUI=com.formdev.flatlaf.ui.FlatCheckBoxUI
LabelUI=com.formdev.flatlaf.ui.FlatLabelUI
RadioButtonUI=com.formdev.flatlaf.ui.FlatRadioButtonUI
TextFieldUI=com.formdev.flatlaf.ui.FlatTextFieldUI
#---- Button ----
@@ -44,3 +45,9 @@ Component.arc=5
RadioButton.border=com.formdev.flatlaf.ui.FlatMarginBorder
RadioButton.icon=com.formdev.flatlaf.ui.FlatRadioButtonIcon
#---- TextField ----
TextField.border=com.formdev.flatlaf.ui.FlatBorder
TextField.margin=2,6,2,6

View File

@@ -70,7 +70,7 @@ CheckBox.icon.selectedBorderColor=4982CC
CheckBox.icon.focusedBorderColor=7B9FC7
CheckBox.icon.selectedFocusedBorderColor=ACCFF7
CheckBox.icon.background=FFFFFF
CheckBox.icon.disabledBackground=F2F2F2
CheckBox.icon.disabledBackground=@background
CheckBox.icon.selectedBackground=4D89C9
CheckBox.icon.checkmarkColor=FFFFFF
CheckBox.icon.disabledCheckmarkColor=ABABAB
@@ -78,9 +78,18 @@ CheckBox.icon.disabledCheckmarkColor=ABABAB
#---- Component ----
Component.borderColor=c4c4c4
Component.disabledBorderColor=cfcfcf
Component.focusedBorderColor=87afda
Component.focusColor=97c3f3
#---- Label ----
Label.disabledForeground=@disabledText
#---- TextField ----
TextField.background=ffffff
TextField.disabledBackground=@background

View File

@@ -66,6 +66,11 @@ public class FlatComponentsTest
JRadioButton radioButton2 = new JRadioButton();
JRadioButton radioButton3 = new JRadioButton();
JRadioButton radioButton4 = new JRadioButton();
JLabel textFieldLabel = new JLabel();
JTextField textField1 = new JTextField();
JTextField textField2 = new JTextField();
JTextField textField3 = new JTextField();
JTextField textField4 = new JTextField();
//======== this ========
setLayout(new MigLayout(
@@ -177,6 +182,30 @@ public class FlatComponentsTest
radioButton4.setSelected(true);
radioButton4.setEnabled(false);
add(radioButton4, "cell 4 3");
//---- textFieldLabel ----
textFieldLabel.setText("JTextField:");
add(textFieldLabel, "cell 0 4");
//---- textField1 ----
textField1.setText("editable");
add(textField1, "cell 1 4,growx");
//---- textField2 ----
textField2.setText("disabled");
textField2.setEnabled(false);
add(textField2, "cell 2 4,growx");
//---- textField3 ----
textField3.setText("not editable");
textField3.setEditable(false);
add(textField3, "cell 3 4,growx");
//---- textField4 ----
textField4.setText("not editable disabled");
textField4.setEnabled(false);
textField4.setEditable(false);
add(textField4, "cell 4 4,growx");
// JFormDesigner - End of component initialization //GEN-END:initComponents
}

View File

@@ -144,6 +144,40 @@ new FormModel {
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 4 3"
} )
add( new FormComponent( "javax.swing.JLabel" ) {
name: "textFieldLabel"
"text": "JTextField:"
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 0 4"
} )
add( new FormComponent( "javax.swing.JTextField" ) {
name: "textField1"
"text": "editable"
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 1 4,growx"
} )
add( new FormComponent( "javax.swing.JTextField" ) {
name: "textField2"
"text": "disabled"
"enabled": false
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 2 4,growx"
} )
add( new FormComponent( "javax.swing.JTextField" ) {
name: "textField3"
"text": "not editable"
"editable": false
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 3 4,growx"
} )
add( new FormComponent( "javax.swing.JTextField" ) {
name: "textField4"
"text": "not editable disabled"
"enabled": false
"editable": false
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 4 4,growx"
} )
}, new FormLayoutConstraints( null ) {
"location": new java.awt.Point( 0, 0 )
"size": new java.awt.Dimension( 580, 300 )

View File

@@ -55,6 +55,9 @@ CheckBox.icon.disabledCheckmarkColor=ABABAB
#---- Component ----
Component.borderColor=ff0000
Component.disabledBorderColor=000088
Component.focusedBorderColor=466d94
Component.focusColor=97c3f3
#Component.focusWidth=5
#Component.arc=8
@@ -64,3 +67,9 @@ Component.focusColor=97c3f3
Label.foreground=008800
Label.disabledForeground=000088
#---- TextField ----
TextField.background=ffffff
TextField.disabledBackground=f2f2f2