diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/FlatDefaultsAddon.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/FlatDefaultsAddon.java new file mode 100644 index 00000000..ef7c03d0 --- /dev/null +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/FlatDefaultsAddon.java @@ -0,0 +1,42 @@ +/* + * 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 java.io.InputStream; + +/** + * Addon for FlatLaf UI defaults. + * + * Allows loading of additional .properties files from addon JARs. + * {@link java.util.ServiceLoader} is used to load extensions of this class from addon JARs. + * + * If you extend this class in a addon JAR, you also have to add a text file named + * {@code META-INF/services/com.formdev.flatlaf.FlatDefaultsAddon} + * to the addon JAR. The file must contain a single line with the class name. + * + * See 'flatlaf-swingx' addon for an example + * + * @author Karl Tauber + */ +public abstract class FlatDefaultsAddon +{ + /** + * Finds an addon .properties file for the given LaF class and returns + * it as input stream. Or {@code null} if not found. + */ + public abstract InputStream getDefaults( Class lafClass ); +} 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 73810cb6..c15c72d2 100644 --- a/flatlaf-core/src/main/java/com/formdev/flatlaf/FlatLaf.java +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/FlatLaf.java @@ -37,6 +37,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Properties; +import java.util.ServiceLoader; import java.util.function.Function; import javax.swing.AbstractButton; import javax.swing.JLabel; @@ -272,12 +273,22 @@ public abstract class FlatLaf try { // load properties files Properties properties = new Properties(); + ServiceLoader addonLoader = ServiceLoader.load( FlatDefaultsAddon.class ); for( Class lafClass : lafClasses ) { + // load core properties String propertiesName = "/" + lafClass.getName().replace( '.', '/' ) + ".properties"; try( InputStream in = lafClass.getResourceAsStream( propertiesName ) ) { if( in != null ) properties.load( in ); } + + // load properties from addons + for( FlatDefaultsAddon addon : addonLoader ) { + try( InputStream in = addon.getDefaults( lafClass ) ) { + if( in != null ) + properties.load( in ); + } + } } Function resolver = value -> { diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatButtonUI.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatButtonUI.java index a1f51672..51b8139d 100644 --- a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatButtonUI.java +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatButtonUI.java @@ -218,7 +218,7 @@ public class FlatButtonUI paintText( g, b, textRect, text, b.isEnabled() ? getForeground( b ) : disabledText ); } - static void paintText( Graphics g, AbstractButton b, Rectangle textRect, String text, Color foreground ) { + public static void paintText( Graphics g, AbstractButton b, Rectangle textRect, String text, Color foreground ) { FontMetrics fm = b.getFontMetrics( b.getFont() ); int mnemonicIndex = FlatLaf.isShowMnemonics() ? b.getDisplayedMnemonicIndex() : -1; diff --git a/flatlaf-swingx/build.gradle.kts b/flatlaf-swingx/build.gradle.kts new file mode 100644 index 00000000..f55e5716 --- /dev/null +++ b/flatlaf-swingx/build.gradle.kts @@ -0,0 +1,34 @@ +/* + * 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. + */ + +version = rootProject.version + +plugins { + `java-library` +} + +dependencies { + implementation( project( ":flatlaf-core" ) ) + implementation( "org.swinglabs.swingx:swingx-all:1.6.5-1" ) + + testImplementation( "org.swinglabs.swingx:swingx-beaninfo:1.6.5-1" ) + testImplementation( "com.miglayout:miglayout-swing:5.2" ) +} + +java { + sourceCompatibility = JavaVersion.VERSION_1_8 + targetCompatibility = JavaVersion.VERSION_1_8 +} diff --git a/flatlaf-swingx/src/main/java/com/formdev/flatlaf/swingx/FlatSwingXDefaultsAddon.java b/flatlaf-swingx/src/main/java/com/formdev/flatlaf/swingx/FlatSwingXDefaultsAddon.java new file mode 100644 index 00000000..e17f121a --- /dev/null +++ b/flatlaf-swingx/src/main/java/com/formdev/flatlaf/swingx/FlatSwingXDefaultsAddon.java @@ -0,0 +1,41 @@ +/* + * 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.swingx; + +import java.io.InputStream; +import com.formdev.flatlaf.FlatDefaultsAddon; + +/** + * SwingX addon for FlatLaf. + * + * @author Karl Tauber + */ +public class FlatSwingXDefaultsAddon + extends FlatDefaultsAddon +{ + /** + * Finds SwingX addon .properties file for the given LaF class + * in the same package as this class. + */ + @Override + public InputStream getDefaults( Class lafClass ) { + Class addonClass = this.getClass(); + String propertiesName = "/" + addonClass.getPackage().getName().replace( '.', '/' ) + + '/' + lafClass.getSimpleName() + ".properties"; + return addonClass.getResourceAsStream( propertiesName ); + } +} diff --git a/flatlaf-swingx/src/main/java/com/formdev/flatlaf/swingx/ui/FlatHyperlinkUI.java b/flatlaf-swingx/src/main/java/com/formdev/flatlaf/swingx/ui/FlatHyperlinkUI.java new file mode 100644 index 00000000..ccd4557f --- /dev/null +++ b/flatlaf-swingx/src/main/java/com/formdev/flatlaf/swingx/ui/FlatHyperlinkUI.java @@ -0,0 +1,85 @@ +/* + * 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.swingx.ui; + +import java.awt.Color; +import java.awt.Graphics; +import java.awt.Graphics2D; +import java.awt.Rectangle; +import java.awt.geom.Rectangle2D; +import javax.swing.AbstractButton; +import javax.swing.JComponent; +import javax.swing.UIManager; +import javax.swing.plaf.ComponentUI; +import org.jdesktop.swingx.plaf.basic.BasicHyperlinkUI; +import com.formdev.flatlaf.ui.FlatButtonUI; +import com.formdev.flatlaf.ui.FlatUIUtils; +import com.formdev.flatlaf.util.UIScale; + +/** + * Provides the Flat LaF UI delegate for {@link org.jdesktop.swingx.JXHyperlink}. + * + * @uiDefault Hyperlink.disabledText Color + * + * @author Karl Tauber + */ +public class FlatHyperlinkUI + extends BasicHyperlinkUI +{ + protected Color disabledText; + + public static ComponentUI createUI( JComponent c ) { + return new FlatHyperlinkUI(); + } + + @Override + protected void installDefaults( AbstractButton b ) { + super.installDefaults( b ); + + disabledText = UIManager.getColor( "Hyperlink.disabledText" ); + } + + @Override + protected void uninstallDefaults( AbstractButton b ) { + super.uninstallDefaults( b ); + + disabledText = null; + } + + @Override + public void paint( Graphics g, JComponent c ) { + FlatUIUtils.setRenderingHints( (Graphics2D) g ); + + super.paint( g, c ); + } + + @Override + protected void paintText( Graphics g, AbstractButton b, Rectangle textRect, String text ) { + FlatButtonUI.paintText( g, b, textRect, text, b.isEnabled() ? b.getForeground() : disabledText ); + + if( b.getModel().isRollover() ) + paintUnderline( g, textRect ); + } + + private void paintUnderline( Graphics g, Rectangle rect ) { + int descent = g.getFontMetrics().getDescent(); + + ((Graphics2D)g).fill( new Rectangle2D.Float( + rect.x, (rect.y + rect.height) - descent + UIScale.scale( 1f ), + rect.width, UIScale.scale( 1f ) ) ); + } +} diff --git a/flatlaf-swingx/src/main/resources/META-INF/services/com.formdev.flatlaf.FlatDefaultsAddon b/flatlaf-swingx/src/main/resources/META-INF/services/com.formdev.flatlaf.FlatDefaultsAddon new file mode 100644 index 00000000..954c63a8 --- /dev/null +++ b/flatlaf-swingx/src/main/resources/META-INF/services/com.formdev.flatlaf.FlatDefaultsAddon @@ -0,0 +1 @@ +com.formdev.flatlaf.swingx.FlatSwingXDefaultsAddon diff --git a/flatlaf-swingx/src/main/resources/com/formdev/flatlaf/swingx/FlatDarkLaf.properties b/flatlaf-swingx/src/main/resources/com/formdev/flatlaf/swingx/FlatDarkLaf.properties new file mode 100644 index 00000000..1f83fdd9 --- /dev/null +++ b/flatlaf-swingx/src/main/resources/com/formdev/flatlaf/swingx/FlatDarkLaf.properties @@ -0,0 +1,21 @@ +# +# 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. +# + +#---- Hyperlink ---- + +Hyperlink.linkColor=589df6 +Hyperlink.visitedColor=@@Hyperlink.linkColor +Hyperlink.disabledText=@disabledText diff --git a/flatlaf-swingx/src/main/resources/com/formdev/flatlaf/swingx/FlatLaf.properties b/flatlaf-swingx/src/main/resources/com/formdev/flatlaf/swingx/FlatLaf.properties new file mode 100644 index 00000000..f7cc6921 --- /dev/null +++ b/flatlaf-swingx/src/main/resources/com/formdev/flatlaf/swingx/FlatLaf.properties @@ -0,0 +1,19 @@ +# +# 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. +# + +#---- UI delegates ---- + +HyperlinkUI=com.formdev.flatlaf.swingx.ui.FlatHyperlinkUI diff --git a/flatlaf-swingx/src/main/resources/com/formdev/flatlaf/swingx/FlatLightLaf.properties b/flatlaf-swingx/src/main/resources/com/formdev/flatlaf/swingx/FlatLightLaf.properties new file mode 100644 index 00000000..04d9ba41 --- /dev/null +++ b/flatlaf-swingx/src/main/resources/com/formdev/flatlaf/swingx/FlatLightLaf.properties @@ -0,0 +1,21 @@ +# +# 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. +# + +#---- Hyperlink ---- + +Hyperlink.linkColor=4a78c2 +Hyperlink.visitedColor=@@Hyperlink.linkColor +Hyperlink.disabledText=@disabledText diff --git a/settings.gradle.kts b/settings.gradle.kts index 82e75f4e..d30d0d94 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -17,4 +17,5 @@ rootProject.name = "FlatLaf" include( "flatlaf-core" ) +include( "flatlaf-swingx" ) include( "flatlaf-demo" )