From 3595f59b882377df1e4110d7178253e4693801e9 Mon Sep 17 00:00:00 2001 From: Karl Tauber Date: Thu, 29 Aug 2019 21:33:55 +0200 Subject: [PATCH] Tree: Tree.expandedIcon and Tree.collapsedIcon icons added --- .../java/com/formdev/flatlaf/FlatLaf.java | 2 +- .../flatlaf/icons/FlatAbstractIcon.java | 83 +++++++++++++++++++ .../flatlaf/icons/FlatTreeCollapsedIcon.java | 47 +++++++++++ .../flatlaf/icons/FlatTreeExpandedIcon.java | 47 +++++++++++ .../formdev/flatlaf/FlatDarkLaf.properties | 6 ++ .../com/formdev/flatlaf/FlatLaf.properties | 6 ++ .../formdev/flatlaf/FlatLightLaf.properties | 6 ++ .../formdev/flatlaf/FlatTestLaf.properties | 6 ++ 8 files changed, 202 insertions(+), 1 deletion(-) create mode 100644 flatlaf-core/src/main/java/com/formdev/flatlaf/icons/FlatAbstractIcon.java create mode 100644 flatlaf-core/src/main/java/com/formdev/flatlaf/icons/FlatTreeCollapsedIcon.java create mode 100644 flatlaf-core/src/main/java/com/formdev/flatlaf/icons/FlatTreeExpandedIcon.java diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/FlatLaf.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/FlatLaf.java index 167be64f..f46acff9 100644 --- a/flatlaf-core/src/main/java/com/formdev/flatlaf/FlatLaf.java +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/FlatLaf.java @@ -234,7 +234,7 @@ public abstract class FlatLaf return parseBorder( value ); // icons - if( key.endsWith( ".icon" ) ) + if( key.endsWith( ".icon" ) || key.endsWith( "Icon" ) ) return parseInstance( value ); // insets diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/icons/FlatAbstractIcon.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/icons/FlatAbstractIcon.java new file mode 100644 index 00000000..d7bc951c --- /dev/null +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/icons/FlatAbstractIcon.java @@ -0,0 +1,83 @@ +/* + * 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.icons; + +import static com.formdev.flatlaf.util.UIScale.*; +import java.awt.Color; +import java.awt.Component; +import java.awt.Graphics; +import java.awt.Graphics2D; +import javax.swing.Icon; +import javax.swing.plaf.UIResource; +import com.formdev.flatlaf.ui.FlatUIUtils; +import com.formdev.flatlaf.util.UIScale; + +/** + * Base class for icons that scales width and height, creates and initializes + * a scaled graphics context for icon painting. + * + * Subclasses do not need to scale icon painting. + * + * @author Karl Tauber + */ +public abstract class FlatAbstractIcon + implements Icon, UIResource +{ + protected final int width; + protected final int height; + protected final Color color; + + public FlatAbstractIcon( int width, int height, Color color ) { + this.width = width; + this.height = height; + this.color = color; + } + + @Override + public void paintIcon( Component c, Graphics g, int x, int y ) { + Graphics2D g2 = (Graphics2D) g.create(); + try { + FlatUIUtils.setRenderingHints( g2 ); + + // for testing +// g2.setColor( Color.blue ); +// g2.drawRect( x, y, getIconWidth() - 1, getIconHeight() - 1 ); + + g2.translate( x, y ); + UIScale.scaleGraphics( g2 ); + + if( color != null ) + g2.setColor( color ); + + paintIcon( g2 ); + } finally { + g2.dispose(); + } + } + + protected abstract void paintIcon( Graphics2D g2 ); + + @Override + public int getIconWidth() { + return scale( width ); + } + + @Override + public int getIconHeight() { + return scale( height ); + } +} diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/icons/FlatTreeCollapsedIcon.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/icons/FlatTreeCollapsedIcon.java new file mode 100644 index 00000000..593abb15 --- /dev/null +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/icons/FlatTreeCollapsedIcon.java @@ -0,0 +1,47 @@ +/* + * 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.icons; + +import java.awt.Graphics2D; +import java.awt.geom.Path2D; +import javax.swing.UIManager; + +/** + * "collapsed" icon for {@link javax.swing.JTree}. + * + * @uiDefault Tree.icon.collapsedColor Color + * + * @author Karl Tauber + */ +public class FlatTreeCollapsedIcon + extends FlatAbstractIcon +{ + public FlatTreeCollapsedIcon() { + super( 11, 11, UIManager.getColor( "Tree.icon.collapsedColor" ) ); + } + + @Override + protected void paintIcon( Graphics2D g ) { + Path2D arrow = new Path2D.Float(); + arrow.moveTo( 2, 1 ); + arrow.lineTo( 2, 10 ); + arrow.lineTo( 10, 5.5 ); + arrow.closePath(); + + g.fill( arrow ); + } +} diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/icons/FlatTreeExpandedIcon.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/icons/FlatTreeExpandedIcon.java new file mode 100644 index 00000000..96abef69 --- /dev/null +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/icons/FlatTreeExpandedIcon.java @@ -0,0 +1,47 @@ +/* + * 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.icons; + +import java.awt.Graphics2D; +import java.awt.geom.Path2D; +import javax.swing.UIManager; + +/** + * "expanded" icon for {@link javax.swing.JTree}. + * + * @uiDefault Tree.icon.expandedColor Color + * + * @author Karl Tauber + */ +public class FlatTreeExpandedIcon + extends FlatAbstractIcon +{ + public FlatTreeExpandedIcon() { + super( 11, 11, UIManager.getColor( "Tree.icon.expandedColor" ) ); + } + + @Override + protected void paintIcon( Graphics2D g ) { + Path2D arrow = new Path2D.Float(); + arrow.moveTo( 1, 2 ); + arrow.lineTo( 10, 2 ); + arrow.lineTo( 5.5, 10 ); + arrow.closePath(); + + g.fill( arrow ); + } +} diff --git a/flatlaf-core/src/main/resources/com/formdev/flatlaf/FlatDarkLaf.properties b/flatlaf-core/src/main/resources/com/formdev/flatlaf/FlatDarkLaf.properties index 75adf1cf..d9560bd3 100644 --- a/flatlaf-core/src/main/resources/com/formdev/flatlaf/FlatDarkLaf.properties +++ b/flatlaf-core/src/main/resources/com/formdev/flatlaf/FlatDarkLaf.properties @@ -140,3 +140,9 @@ TabbedPane.disabledUnderlineColor=7a7a7a TabbedPane.hoverColor=2e3133 TabbedPane.focusColor=3d4b5c TabbedPane.contentAreaColor=323232 + + +#---- Tree ---- + +Tree.icon.expandedColor=adadad +Tree.icon.collapsedColor=adadad 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 58ef2145..5f904231 100644 --- a/flatlaf-core/src/main/resources/com/formdev/flatlaf/FlatLaf.properties +++ b/flatlaf-core/src/main/resources/com/formdev/flatlaf/FlatLaf.properties @@ -168,3 +168,9 @@ TextField.margin=@textComponentMargin TextPane.border=com.formdev.flatlaf.ui.FlatMarginBorder TextPane.background=@textComponentBackground TextPane.margin=@textComponentMargin + + +#---- Tree ---- + +Tree.expandedIcon=com.formdev.flatlaf.icons.FlatTreeExpandedIcon +Tree.collapsedIcon=com.formdev.flatlaf.icons.FlatTreeCollapsedIcon diff --git a/flatlaf-core/src/main/resources/com/formdev/flatlaf/FlatLightLaf.properties b/flatlaf-core/src/main/resources/com/formdev/flatlaf/FlatLightLaf.properties index f4864401..e92e5dde 100644 --- a/flatlaf-core/src/main/resources/com/formdev/flatlaf/FlatLightLaf.properties +++ b/flatlaf-core/src/main/resources/com/formdev/flatlaf/FlatLightLaf.properties @@ -140,3 +140,9 @@ TabbedPane.disabledUnderlineColor=ababab TabbedPane.hoverColor=d9d9d9 TabbedPane.focusColor=dae4ed TabbedPane.contentAreaColor=bfbfbf + + +#---- Tree ---- + +Tree.icon.expandedColor=afafaf +Tree.icon.collapsedColor=afafaf diff --git a/flatlaf-core/src/test/resources/com/formdev/flatlaf/FlatTestLaf.properties b/flatlaf-core/src/test/resources/com/formdev/flatlaf/FlatTestLaf.properties index 579b32a5..f161ea7d 100644 --- a/flatlaf-core/src/test/resources/com/formdev/flatlaf/FlatTestLaf.properties +++ b/flatlaf-core/src/test/resources/com/formdev/flatlaf/FlatTestLaf.properties @@ -134,3 +134,9 @@ TabbedPane.disabledUnderlineColor=7a7a7a TabbedPane.hoverColor=eeeeee TabbedPane.focusColor=dddddd TabbedPane.contentAreaColor=bbbbbb + + +#---- Tree ---- + +Tree.icon.expandedColor=ad00ad +Tree.icon.collapsedColor=00adad