From e004401772d019b557db20a84100a17f7f5663cd Mon Sep 17 00:00:00 2001 From: Karl Tauber Date: Sat, 21 Sep 2019 18:12:58 +0200 Subject: [PATCH] multi-line ToolTip --- .../java/com/formdev/flatlaf/FlatLaf.java | 2 +- .../com/formdev/flatlaf/ui/FlatToolTipUI.java | 91 +++++++++++++++++++ .../com/formdev/flatlaf/FlatLaf.properties | 2 +- .../formdev/flatlaf/FlatComponentsTest.java | 5 + .../formdev/flatlaf/FlatComponentsTest.jfd | 6 ++ 5 files changed, 104 insertions(+), 2 deletions(-) create mode 100644 flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatToolTipUI.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 cc2e0a23..80582e3a 100644 --- a/flatlaf-core/src/main/java/com/formdev/flatlaf/FlatLaf.java +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/FlatLaf.java @@ -411,7 +411,7 @@ public abstract class FlatLaf } } - private static List split( String str, char delim ) { + public static List split( String str, char delim ) { ArrayList strs = new ArrayList<>(); int delimIndex = str.indexOf( delim ); int index = 0; diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatToolTipUI.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatToolTipUI.java new file mode 100644 index 00000000..abdd6e4e --- /dev/null +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatToolTipUI.java @@ -0,0 +1,91 @@ +/* + * 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 java.awt.FontMetrics; +import java.awt.Graphics; +import java.awt.Graphics2D; +import java.awt.Insets; +import java.util.List; +import javax.swing.JComponent; +import javax.swing.JToolTip; +import javax.swing.SwingUtilities; +import javax.swing.plaf.ComponentUI; +import javax.swing.plaf.basic.BasicToolTipUI; +import com.formdev.flatlaf.FlatLaf; + +/** + * Provides the Flat LaF UI delegate for {@link javax.swing.JToolTip}. + * + * @author Karl Tauber + */ +public class FlatToolTipUI + extends BasicToolTipUI +{ + private static ComponentUI instance; + + public static ComponentUI createUI( JComponent c ) { + if( instance == null ) + instance = new FlatToolTipUI(); + return instance; + } + + @Override + public Dimension getPreferredSize( JComponent c ) { + if( isMultiLine( c ) ) { + FontMetrics fm = c.getFontMetrics( c.getFont() ); + Insets insets = c.getInsets(); + + List lines = FlatLaf.split( ((JToolTip)c).getTipText(), '\n' ); + int width = 0; + int height = fm.getHeight() * Math.max( lines.size(), 1 ); + for( String line : lines ) + width = Math.max( width, SwingUtilities.computeStringWidth( fm, line ) ); + + return new Dimension( insets.left + width + insets.right, insets.top + height + insets.bottom ); + } else + return super.getPreferredSize( c ); + } + + @Override + public void paint( Graphics g, JComponent c ) { + if( isMultiLine( c ) ) { + FontMetrics fm = c.getFontMetrics( c.getFont() ); + Insets insets = c.getInsets(); + + FlatUIUtils.setRenderingHints( (Graphics2D) g ); + g.setColor( c.getForeground() ); + + List lines = FlatLaf.split( ((JToolTip)c).getTipText(), '\n' ); + + int x = insets.left; + int y = insets.top - fm.getDescent(); + int lineHeight = fm.getHeight(); + for( String line : lines ) { + y += lineHeight; + g.drawString( line, x, y ); + } + } else + super.paint( g, c ); + } + + private boolean isMultiLine( JComponent c ) { + String text = ((JToolTip)c).getTipText(); + return c.getClientProperty( "html" ) == null && text != null && text.indexOf( '\n' ) >= 0; + } +} 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 5ce227e3..bdf7653d 100644 --- a/flatlaf-core/src/main/resources/com/formdev/flatlaf/FlatLaf.properties +++ b/flatlaf-core/src/main/resources/com/formdev/flatlaf/FlatLaf.properties @@ -50,7 +50,7 @@ TextPaneUI=com.formdev.flatlaf.ui.FlatTextPaneUI ToggleButtonUI=com.formdev.flatlaf.ui.FlatToggleButtonUI ToolBarUI=com.formdev.flatlaf.ui.FlatToolBarUI ToolBarSeparatorUI=com.formdev.flatlaf.ui.FlatToolBarSeparatorUI -ToolTipUI=javax.swing.plaf.basic.BasicToolTipUI +ToolTipUI=com.formdev.flatlaf.ui.FlatToolTipUI TreeUI=com.formdev.flatlaf.ui.FlatTreeUI ViewportUI=com.formdev.flatlaf.ui.FlatViewportUI diff --git a/flatlaf-core/src/test/java/com/formdev/flatlaf/FlatComponentsTest.java b/flatlaf-core/src/test/java/com/formdev/flatlaf/FlatComponentsTest.java index 52b7ff7c..142e7b58 100644 --- a/flatlaf-core/src/test/java/com/formdev/flatlaf/FlatComponentsTest.java +++ b/flatlaf-core/src/test/java/com/formdev/flatlaf/FlatComponentsTest.java @@ -156,6 +156,7 @@ public class FlatComponentsTest indeterminateCheckBox = new JCheckBox(); JLabel toolTipLabel = new JLabel(); JToolTip toolTip1 = new JToolTip(); + JToolTip toolTip2 = new JToolTip(); JLabel toolBarLabel = new JLabel(); JToolBar toolBar1 = new JToolBar(); JButton button4 = new JButton(); @@ -775,6 +776,10 @@ public class FlatComponentsTest toolTip1.setTipText("Some text in tool tip."); add(toolTip1, "cell 1 20 3 1"); + //---- toolTip2 ---- + toolTip2.setTipText("Tool tip with\nmultiple\nlines."); + add(toolTip2, "cell 1 20 3 1"); + //---- toolBarLabel ---- toolBarLabel.setText("JToolBar:"); add(toolBarLabel, "cell 0 21"); diff --git a/flatlaf-core/src/test/java/com/formdev/flatlaf/FlatComponentsTest.jfd b/flatlaf-core/src/test/java/com/formdev/flatlaf/FlatComponentsTest.jfd index 2296912e..a9c09aff 100644 --- a/flatlaf-core/src/test/java/com/formdev/flatlaf/FlatComponentsTest.jfd +++ b/flatlaf-core/src/test/java/com/formdev/flatlaf/FlatComponentsTest.jfd @@ -770,6 +770,12 @@ new FormModel { }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { "value": "cell 1 20 3 1" } ) + add( new FormComponent( "javax.swing.JToolTip" ) { + name: "toolTip2" + "tipText": "Tool tip with\nmultiple\nlines." + }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { + "value": "cell 1 20 3 1" + } ) add( new FormComponent( "javax.swing.JLabel" ) { name: "toolBarLabel" "text": "JToolBar:"