From 90f020cae77c615f5f10816b0c68a91ea355447a Mon Sep 17 00:00:00 2001 From: Karl Tauber Date: Sat, 7 Sep 2019 11:12:41 +0200 Subject: [PATCH] ColorChooser implementation --- .../flatlaf/ui/FlatColorChooserUI.java | 58 ++++++++++++++++++ .../com/formdev/flatlaf/FlatLaf.properties | 7 +++ .../com/formdev/flatlaf/FlatChooserTest.java | 60 +++++++++++++++++++ .../com/formdev/flatlaf/FlatChooserTest.jfd | 31 ++++++++++ 4 files changed, 156 insertions(+) create mode 100644 flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatColorChooserUI.java create mode 100644 flatlaf-core/src/test/java/com/formdev/flatlaf/FlatChooserTest.java create mode 100644 flatlaf-core/src/test/java/com/formdev/flatlaf/FlatChooserTest.jfd diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatColorChooserUI.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatColorChooserUI.java new file mode 100644 index 00000000..d260083c --- /dev/null +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatColorChooserUI.java @@ -0,0 +1,58 @@ +/* + * 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.Dimension; +import javax.swing.JComponent; +import javax.swing.UIManager; +import javax.swing.plaf.ComponentUI; +import javax.swing.plaf.basic.BasicColorChooserUI; +import com.formdev.flatlaf.util.UIScale; + +/** + * Provides the Flat LaF UI delegate for {@link javax.swing.JColorChooser}. + * + * @uiDefault ColorChooser.showPreviewPanelText boolean + * @uiDefault ColorChooser.swatchesSwatchSize Dimension + * @uiDefault ColorChooser.swatchesRecentSwatchSize Dimension + * + * @author Karl Tauber + */ +public class FlatColorChooserUI + extends BasicColorChooserUI +{ + public static ComponentUI createUI( JComponent c ) { + return new FlatColorChooserUI(); + } + + @Override + public void installUI( JComponent c ) { + if( UIScale.getUserScaleFactor() != 1f ) { + // temporary scale swatch sizes + Dimension swatchSize = UIManager.getDimension( "ColorChooser.swatchesSwatchSize" ); + Dimension swatchSize2 = UIManager.getDimension( "ColorChooser.swatchesRecentSwatchSize" ); + UIManager.put( "ColorChooser.swatchesSwatchSize", UIScale.scale( swatchSize ) ); + UIManager.put( "ColorChooser.swatchesRecentSwatchSize", UIScale.scale( swatchSize2 ) ); + + super.installUI( c ); + + UIManager.put( "ColorChooser.swatchesSwatchSize", null ); + UIManager.put( "ColorChooser.swatchesRecentSwatchSize", null ); + } else + super.installUI( c ); + } +} diff --git a/flatlaf-core/src/main/resources/com/formdev/flatlaf/FlatLaf.properties b/flatlaf-core/src/main/resources/com/formdev/flatlaf/FlatLaf.properties index 7bd8e90f..c5dfef94 100644 --- a/flatlaf-core/src/main/resources/com/formdev/flatlaf/FlatLaf.properties +++ b/flatlaf-core/src/main/resources/com/formdev/flatlaf/FlatLaf.properties @@ -19,6 +19,7 @@ ButtonUI=com.formdev.flatlaf.ui.FlatButtonUI CheckBoxUI=com.formdev.flatlaf.ui.FlatCheckBoxUI CheckBoxMenuItemUI=com.formdev.flatlaf.ui.FlatCheckBoxMenuItemUI +ColorChooserUI=com.formdev.flatlaf.ui.FlatColorChooserUI ComboBoxUI=com.formdev.flatlaf.ui.FlatComboBoxUI EditorPaneUI=com.formdev.flatlaf.ui.FlatEditorPaneUI FormattedTextFieldUI=com.formdev.flatlaf.ui.FlatFormattedTextFieldUI @@ -78,6 +79,12 @@ CheckBoxMenuItem.checkIcon=com.formdev.flatlaf.icons.FlatCheckBoxMenuItemIcon CheckBoxMenuItem.arrowIcon=com.formdev.flatlaf.icons.FlatMenuItemArrowIcon +#---- ColorChooser ---- + +ColorChooser.swatchesSwatchSize=16,16 +ColorChooser.swatchesRecentSwatchSize=16,16 + + #---- ComboBox ---- ComboBox.border=com.formdev.flatlaf.ui.FlatRoundBorder diff --git a/flatlaf-core/src/test/java/com/formdev/flatlaf/FlatChooserTest.java b/flatlaf-core/src/test/java/com/formdev/flatlaf/FlatChooserTest.java new file mode 100644 index 00000000..1083a768 --- /dev/null +++ b/flatlaf-core/src/test/java/com/formdev/flatlaf/FlatChooserTest.java @@ -0,0 +1,60 @@ +/* + * 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; + +import javax.swing.*; +import net.miginfocom.swing.*; + +/** + * @author Karl Tauber + */ +public class FlatChooserTest + extends JPanel +{ + public static void main( String[] args ) { + FlatTestFrame frame = FlatTestFrame.create( args, "FlatChooserTest" ); + frame.showFrame( new FlatChooserTest() ); + } + + FlatChooserTest() { + initComponents(); + } + + private void initComponents() { + // JFormDesigner - Component initialization - DO NOT MODIFY //GEN-BEGIN:initComponents + JLabel colorChooserLabel = new JLabel(); + JColorChooser colorChooser1 = new JColorChooser(); + + //======== this ======== + setLayout(new MigLayout( + "insets 0,hidemode 3,gap 5 5,ltr", + // columns + "[]" + + "[]", + // rows + "[top]")); + + //---- colorChooserLabel ---- + colorChooserLabel.setText("JColorChooser:"); + add(colorChooserLabel, "cell 0 0"); + add(colorChooser1, "cell 1 0"); + // 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-core/src/test/java/com/formdev/flatlaf/FlatChooserTest.jfd b/flatlaf-core/src/test/java/com/formdev/flatlaf/FlatChooserTest.jfd new file mode 100644 index 00000000..35dae4ff --- /dev/null +++ b/flatlaf-core/src/test/java/com/formdev/flatlaf/FlatChooserTest.jfd @@ -0,0 +1,31 @@ +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( "javax.swing.JPanel", new FormLayoutManager( class net.miginfocom.swing.MigLayout ) { + "$layoutConstraints": "insets 0,hidemode 3,gap 5 5,ltr" + "$columnConstraints": "[][]" + "$rowConstraints": "[top]" + } ) { + name: "this" + add( new FormComponent( "javax.swing.JLabel" ) { + name: "colorChooserLabel" + "text": "JColorChooser:" + }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { + "value": "cell 0 0" + } ) + add( new FormComponent( "javax.swing.JColorChooser" ) { + name: "colorChooser1" + }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { + "value": "cell 1 0" + } ) + }, new FormLayoutConstraints( null ) { + "location": new java.awt.Point( 0, 0 ) + "size": new java.awt.Dimension( 790, 715 ) + } ) + } +}