ComboBox basic implementation

This commit is contained in:
Karl Tauber
2019-08-22 23:31:19 +02:00
parent 24ba48be8c
commit a20dffc8cd
10 changed files with 402 additions and 102 deletions

View File

@@ -64,12 +64,8 @@ public class FlatBorder
}
protected Paint getBorderColor( Component c ) {
boolean editable = !(c instanceof JTextComponent) || ((JTextComponent)c).isEditable();
return UIManager.getColor( c.isEnabled() && editable
? (isFocused( c )
? "Component.focusedBorderColor"
: "Component.borderColor")
: "Component.disabledBorderColor" );
boolean enabled = c.isEnabled() && (!(c instanceof JTextComponent) || ((JTextComponent)c).isEditable());
return FlatUIUtils.getBorderColor( enabled, isFocused( c ) );
}
protected boolean isFocused( Component c ) {

View File

@@ -0,0 +1,124 @@
/*
* 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.scale;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.geom.Path2D;
import java.awt.geom.Rectangle2D;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.UIManager;
import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.basic.BasicArrowButton;
import javax.swing.plaf.basic.BasicComboBoxUI;
/**
* Provides the Flat LaF UI delegate for {@link javax.swing.JComboBox}.
*
* @author Karl Tauber
*/
public class FlatComboBoxUI
extends BasicComboBoxUI
{
public static ComponentUI createUI( JComponent c ) {
return new FlatComboBoxUI();
}
@Override
protected JButton createArrowButton() {
return new FlatArrowButton();
}
@Override
public void update( Graphics g, JComponent c ) {
if( c.isOpaque() ) {
FlatUIUtils.paintParentBackground( g, c );
if( c.isEnabled() || comboBox.isEditable() ) {
Graphics2D g2 = (Graphics2D) g;
FlatUIUtils.setRenderingHints( g2 );
int width = c.getWidth();
int height = c.getHeight();
float focusWidth = FlatUIUtils.getFocusWidth();
float arc = FlatUIUtils.getComponentArc();
int arrowX = arrowButton.getX();
if( c.isEnabled() ) {
// paint background
g2.setColor( c.getBackground() );
FlatUIUtils.fillRoundRectangle( g2, 0, 0, width, height, focusWidth, arc );
// paint arrow button background
g2.setColor( UIManager.getColor( comboBox.isEditable()
? "ComboBox.buttonEditableBackground"
: "ComboBox.buttonBackground" ) );
Shape oldClip = g2.getClip();
g2.clipRect( arrowX, 0, width - arrowX, height );
FlatUIUtils.fillRoundRectangle( g2, 0, 0, width, height, focusWidth, arc );
g2.setClip( oldClip );
}
if( comboBox.isEditable() ) {
// paint vertical line between value and arrow button
g2.setColor( FlatUIUtils.getBorderColor( comboBox.isEnabled(), false ) );
g2.fill( new Rectangle2D.Float( arrowX, focusWidth, scale( 1f ), height - (focusWidth * 2) ) );
}
}
}
paint( g, c );
}
//---- class FlatArrowButton ----------------------------------------------
private static class FlatArrowButton
extends BasicArrowButton
{
FlatArrowButton() {
super( SOUTH, Color.WHITE, Color.WHITE, Color.WHITE, Color.WHITE );
setOpaque( false );
setBorder( null );
}
@Override
public void paint( Graphics g ) {
FlatUIUtils.setRenderingHints( (Graphics2D) g );
int w = scale( 9 );
int h = scale( 5 );
int x = Math.round( (getWidth() - w) / 2f );
int y = Math.round( (getHeight() - h) / 2f );
Path2D arrow = new Path2D.Float();
arrow.moveTo( x, y );
arrow.lineTo( x + w, y );
arrow.lineTo( x + (w / 2f), y + h );
arrow.closePath();
g.setColor( UIManager.getColor( isEnabled()
? "ComboBox.buttonArrowColor"
: "ComboBox.buttonDisabledArrowColor" ) );
((Graphics2D)g).fill( arrow );
}
}
}

View File

@@ -0,0 +1,31 @@
/*
* 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;
/**
* Border for various components (e.g. {@link javax.swing.JComboBox}).
*
* @author Karl Tauber
*/
public class FlatRoundBorder
extends FlatBorder
{
@Override
protected float getArc() {
return FlatUIUtils.getComponentArc();
}
}

View File

@@ -17,6 +17,7 @@
package com.formdev.flatlaf.ui;
import static com.formdev.flatlaf.util.UIScale.scale;
import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.Graphics2D;
@@ -57,6 +58,12 @@ public class FlatUIUtils
return scale( (float) getUIInt( "Button.arc", 6 ) );
}
public static Color getBorderColor( boolean enabled, boolean focused ) {
return UIManager.getColor( enabled
? (focused ? "Component.focusedBorderColor" : "Component.borderColor")
: "Component.disabledBorderColor" );
}
/**
* Sets rendering hints used for painting.
*/
@@ -132,7 +139,7 @@ public class FlatUIUtils
float x2 = x1 + width;
float y2 = y1 + height;
float outerArc = arc > 0 ? arc + focusWidth - UIScale.scale( 2f ) : focusWidth;
float outerArc = (arc > 0) ? arc + focusWidth - UIScale.scale( 2f ) : focusWidth;
Path2D outerRect = createOutlinePath( x1, y1, x2, y2, outerArc );
float ow = focusWidth + lineWidth;

View File

@@ -82,6 +82,15 @@ CheckBox.icon.checkmarkColor=A7A7A7
CheckBox.icon.disabledCheckmarkColor=606060
#---- ComboBox ----
ComboBox.background=@textComponentBackground
ComboBox.buttonBackground=@textComponentBackground
ComboBox.buttonEditableBackground=404445
ComboBox.buttonArrowColor=9A9DA1
ComboBox.buttonDisabledArrowColor=585858
#---- Component ----
Component.borderColor=646464

View File

@@ -18,6 +18,7 @@
ButtonUI=com.formdev.flatlaf.ui.FlatButtonUI
CheckBoxUI=com.formdev.flatlaf.ui.FlatCheckBoxUI
ComboBoxUI=com.formdev.flatlaf.ui.FlatComboBoxUI
EditorPaneUI=com.formdev.flatlaf.ui.FlatEditorPaneUI
FormattedTextFieldUI=com.formdev.flatlaf.ui.FlatFormattedTextFieldUI
LabelUI=com.formdev.flatlaf.ui.FlatLabelUI
@@ -48,6 +49,12 @@ CheckBox.border=com.formdev.flatlaf.ui.FlatMarginBorder
CheckBox.icon=com.formdev.flatlaf.ui.FlatCheckBoxIcon
#---- ComboBox ----
ComboBox.border=com.formdev.flatlaf.ui.FlatRoundBorder
ComboBox.padding=1,6,1,6
#---- Component ----
Component.focusWidth=2

View File

@@ -82,6 +82,15 @@ CheckBox.icon.checkmarkColor=FFFFFF
CheckBox.icon.disabledCheckmarkColor=ABABAB
#---- ComboBox ----
ComboBox.background=@textComponentBackground
ComboBox.buttonBackground=@textComponentBackground
ComboBox.buttonEditableBackground=fafafa
ComboBox.buttonArrowColor=666666
ComboBox.buttonDisabledArrowColor=ABABAB
#---- Component ----
Component.borderColor=c4c4c4