From 21978086310f7346b2a93019c05e3f5c37b96a0f Mon Sep 17 00:00:00 2001 From: Karl Tauber Date: Thu, 28 Nov 2019 10:33:26 +0100 Subject: [PATCH] ComboBox: fixed issues with NetBeans org.openide.awt.ColorComboBox component --- CHANGELOG.md | 2 + .../formdev/flatlaf/ui/FlatComboBoxUI.java | 45 ++++++- flatlaf-testing/build.gradle.kts | 1 + .../testing/netbeans/FlatNetBeansTest.java | 110 ++++++++++++++++++ .../testing/netbeans/FlatNetBeansTest.jfd | 63 ++++++++++ 5 files changed, 219 insertions(+), 2 deletions(-) create mode 100644 flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/netbeans/FlatNetBeansTest.java create mode 100644 flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/netbeans/FlatNetBeansTest.jfd diff --git a/CHANGELOG.md b/CHANGELOG.md index 6a17c3bc..54d02b89 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,8 @@ FlatLaf Change Log check whether the current look and feel is FlatLaf. - Fixed selection background of checkbox in table cell. - Fixed jittery submenu rendering on Mac. (issue #10) +- ComboBox: Fixed issues with NetBeans `org.openide.awt.ColorComboBox` + component. - Hex color values in `.properties` files now must start with a `#` character. - SwingX: Fixed too wide border when using date picker as table cell editor. (issue #24) diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatComboBoxUI.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatComboBoxUI.java index 7e1fe752..2ca6f050 100644 --- a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatComboBoxUI.java +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatComboBoxUI.java @@ -34,6 +34,7 @@ import java.awt.event.MouseListener; import java.awt.geom.Rectangle2D; import java.beans.PropertyChangeEvent; import java.beans.PropertyChangeListener; +import java.lang.ref.WeakReference; import javax.swing.BorderFactory; import javax.swing.JButton; import javax.swing.JComboBox; @@ -107,6 +108,8 @@ public class FlatComboBoxUI private MouseListener hoverListener; private boolean hover; + private WeakReference lastRendererComponent; + public static ComponentUI createUI( JComponent c ) { return new FlatComboBoxUI(); } @@ -342,11 +345,11 @@ public class FlatComboBoxUI @SuppressWarnings( "unchecked" ) public void paintCurrentValue( Graphics g, Rectangle bounds, boolean hasFocus ) { ListCellRenderer renderer = comboBox.getRenderer(); - CellPaddingBorder.uninstall( renderer ); + uninstallCellPaddingBorder( renderer ); Component c = renderer.getListCellRendererComponent( listBox, comboBox.getSelectedItem(), -1, false, false ); c.setFont( comboBox.getFont() ); c.applyComponentOrientation( comboBox.getComponentOrientation() ); - CellPaddingBorder.uninstall( c ); + uninstallCellPaddingBorder( c ); boolean enabled = comboBox.isEnabled(); c.setForeground( enabled ? comboBox.getForeground() : disabledForeground ); @@ -375,6 +378,30 @@ public class FlatComboBoxUI return isIntelliJTheme ? FlatUIUtils.getParentBackground( c ) : disabledBackground; } + @Override + protected Dimension getDefaultSize() { + @SuppressWarnings( "unchecked" ) + ListCellRenderer renderer = comboBox.getRenderer(); + uninstallCellPaddingBorder( renderer ); + + Dimension size = super.getDefaultSize(); + + uninstallCellPaddingBorder( renderer ); + return size; + } + + @Override + protected Dimension getDisplaySize() { + @SuppressWarnings( "unchecked" ) + ListCellRenderer renderer = comboBox.getRenderer(); + uninstallCellPaddingBorder( renderer ); + + Dimension displaySize = super.getDisplaySize(); + + uninstallCellPaddingBorder( renderer ); + return displaySize; + } + @Override protected Dimension getSizeForComponent( Component comp ) { Dimension size = super.getSizeForComponent( comp ); @@ -398,6 +425,14 @@ public class FlatComboBoxUI return null; } + private void uninstallCellPaddingBorder( Object o ) { + CellPaddingBorder.uninstall( o ); + if( lastRendererComponent != null ) { + CellPaddingBorder.uninstall( lastRendererComponent ); + lastRendererComponent = null; + } + } + //---- class FlatComboPopup ----------------------------------------------- @SuppressWarnings( { "rawtypes", "unchecked" } ) @@ -481,6 +516,7 @@ public class FlatComboBoxUI { ListCellRenderer renderer = comboBox.getRenderer(); CellPaddingBorder.uninstall( renderer ); + CellPaddingBorder.uninstall( lastRendererComponent ); Component c = renderer.getListCellRendererComponent( list, value, index, isSelected, cellHasFocus ); c.applyComponentOrientation( comboBox.getComponentOrientation() ); @@ -491,6 +527,8 @@ public class FlatComboBoxUI paddingBorder.install( (JComponent) c ); } + lastRendererComponent = (c != renderer) ? new WeakReference<>( c ) : null; + return c; } } @@ -525,6 +563,9 @@ public class FlatComboBoxUI } static void uninstall( Object o ) { + if( o instanceof WeakReference ) + o = ((WeakReference)o).get(); + if( !(o instanceof JComponent) ) return; diff --git a/flatlaf-testing/build.gradle.kts b/flatlaf-testing/build.gradle.kts index 16e23182..4fd94cba 100644 --- a/flatlaf-testing/build.gradle.kts +++ b/flatlaf-testing/build.gradle.kts @@ -32,6 +32,7 @@ dependencies { implementation( "org.swinglabs.swingx:swingx-all:1.6.5-1" ) implementation( "org.swinglabs.swingx:swingx-beaninfo:1.6.5-1" ) implementation( "com.jidesoft:jide-oss:3.6.18" ) + implementation( "org.netbeans.api:org-openide-awt:RELEASE112" ) } java { diff --git a/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/netbeans/FlatNetBeansTest.java b/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/netbeans/FlatNetBeansTest.java new file mode 100644 index 00000000..612ced09 --- /dev/null +++ b/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/netbeans/FlatNetBeansTest.java @@ -0,0 +1,110 @@ +/* + * 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.testing.netbeans; + +import java.awt.*; +import javax.swing.*; +import com.formdev.flatlaf.testing.*; +import com.formdev.flatlaf.testing.FlatTestFrame; +import net.miginfocom.swing.*; +import org.openide.awt.*; + +/** + * @author Karl Tauber + */ +public class FlatNetBeansTest + extends FlatTestPanel +{ + public static void main( String[] args ) { + SwingUtilities.invokeLater( () -> { + FlatTestFrame frame = FlatTestFrame.create( args, "FlatNetBeansTest" ); + frame.showFrame( FlatNetBeansTest::new ); + } ); + } + + FlatNetBeansTest() { + initComponents(); + } + + private void initComponents() { + // JFormDesigner - Component initialization - DO NOT MODIFY //GEN-BEGIN:initComponents + JLabel colorComboBoxLabel = new JLabel(); + ColorComboBox colorComboBox1 = new ColorComboBox(); + JComboBox comboBox4 = new JComboBox<>(); + ColorComboBox colorComboBox2 = new ColorComboBox(); + JComboBox comboBox3 = new JComboBox<>(); + + //======== this ======== + setLayout(new MigLayout( + "insets dialog,hidemode 3", + // columns + "[]" + + "[fill]" + + "[fill]", + // rows + "[]" + + "[]" + + "[]")); + + //---- colorComboBoxLabel ---- + colorComboBoxLabel.setText("ColorComboBox:"); + add(colorComboBoxLabel, "cell 0 0"); + add(colorComboBox1, "cell 1 0"); + + //---- comboBox4 ---- + comboBox4.setModel(new DefaultComboBoxModel<>(new String[] { + "JComboBox", + "a", + "bb", + "ccc", + "dd", + "e", + "ff", + "ggg", + "hh", + "i", + "jj", + "kkk" + })); + add(comboBox4, "cell 2 0,growx"); + + //---- colorComboBox2 ---- + colorComboBox2.setSelectedColor(new Color(176, 62, 62)); + add(colorComboBox2, "cell 1 1"); + + //---- comboBox3 ---- + comboBox3.setModel(new DefaultComboBoxModel<>(new String[] { + "JComboBox", + "a", + "bb", + "ccc", + "dd", + "e", + "ff", + "ggg", + "hh", + "i", + "jj", + "kkk" + })); + add(comboBox3, "cell 1 2,growx"); + // JFormDesigner - End of component initialization //GEN-END:initComponents + } + + // JFormDesigner - Variables declaration - DO NOT MODIFY //GEN-BEGIN:variables + // JFormDesigner - End of variables declaration //GEN-END:variables +} diff --git a/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/netbeans/FlatNetBeansTest.jfd b/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/netbeans/FlatNetBeansTest.jfd new file mode 100644 index 00000000..013ccdfd --- /dev/null +++ b/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/netbeans/FlatNetBeansTest.jfd @@ -0,0 +1,63 @@ +JFDML JFormDesigner: "7.0.0.0.194" Java: "11.0.2" encoding: "UTF-8" + +new FormModel { + contentType: "form/swing" + root: new FormRoot { + auxiliary() { + "JavaCodeGenerator.defaultVariableLocal": true + } + add( new FormContainer( "com.formdev.flatlaf.testing.FlatTestPanel", new FormLayoutManager( class net.miginfocom.swing.MigLayout ) { + "$layoutConstraints": "insets dialog,hidemode 3" + "$columnConstraints": "[][fill][fill]" + "$rowConstraints": "[][][]" + } ) { + name: "this" + add( new FormComponent( "javax.swing.JLabel" ) { + name: "colorComboBoxLabel" + "text": "ColorComboBox:" + }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { + "value": "cell 0 0" + } ) + add( new FormComponent( "org.openide.awt.ColorComboBox" ) { + name: "colorComboBox1" + }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { + "value": "cell 1 0" + } ) + add( new FormComponent( "javax.swing.JComboBox" ) { + name: "comboBox4" + "model": &DefaultComboBoxModel0 new javax.swing.DefaultComboBoxModel { + selectedItem: "JComboBox" + addElement( "JComboBox" ) + addElement( "a" ) + addElement( "bb" ) + addElement( "ccc" ) + addElement( "dd" ) + addElement( "e" ) + addElement( "ff" ) + addElement( "ggg" ) + addElement( "hh" ) + addElement( "i" ) + addElement( "jj" ) + addElement( "kkk" ) + } + }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { + "value": "cell 2 0,growx" + } ) + add( new FormComponent( "org.openide.awt.ColorComboBox" ) { + name: "colorComboBox2" + "selectedColor": new java.awt.Color( 176, 62, 62, 255 ) + }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { + "value": "cell 1 1" + } ) + add( new FormComponent( "javax.swing.JComboBox" ) { + name: "comboBox3" + "model": #DefaultComboBoxModel0 + }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { + "value": "cell 1 2,growx" + } ) + }, new FormLayoutConstraints( null ) { + "location": new java.awt.Point( 0, 0 ) + "size": new java.awt.Dimension( 500, 500 ) + } ) + } +}