mirror of
https://github.com/JFormDesigner/FlatLaf.git
synced 2025-12-09 08:15:09 +03:00
multi-line ToolTip
This commit is contained in:
@@ -411,7 +411,7 @@ public abstract class FlatLaf
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static List<String> split( String str, char delim ) {
|
public static List<String> split( String str, char delim ) {
|
||||||
ArrayList<String> strs = new ArrayList<>();
|
ArrayList<String> strs = new ArrayList<>();
|
||||||
int delimIndex = str.indexOf( delim );
|
int delimIndex = str.indexOf( delim );
|
||||||
int index = 0;
|
int index = 0;
|
||||||
|
|||||||
@@ -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<String> 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<String> 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -50,7 +50,7 @@ TextPaneUI=com.formdev.flatlaf.ui.FlatTextPaneUI
|
|||||||
ToggleButtonUI=com.formdev.flatlaf.ui.FlatToggleButtonUI
|
ToggleButtonUI=com.formdev.flatlaf.ui.FlatToggleButtonUI
|
||||||
ToolBarUI=com.formdev.flatlaf.ui.FlatToolBarUI
|
ToolBarUI=com.formdev.flatlaf.ui.FlatToolBarUI
|
||||||
ToolBarSeparatorUI=com.formdev.flatlaf.ui.FlatToolBarSeparatorUI
|
ToolBarSeparatorUI=com.formdev.flatlaf.ui.FlatToolBarSeparatorUI
|
||||||
ToolTipUI=javax.swing.plaf.basic.BasicToolTipUI
|
ToolTipUI=com.formdev.flatlaf.ui.FlatToolTipUI
|
||||||
TreeUI=com.formdev.flatlaf.ui.FlatTreeUI
|
TreeUI=com.formdev.flatlaf.ui.FlatTreeUI
|
||||||
ViewportUI=com.formdev.flatlaf.ui.FlatViewportUI
|
ViewportUI=com.formdev.flatlaf.ui.FlatViewportUI
|
||||||
|
|
||||||
|
|||||||
@@ -156,6 +156,7 @@ public class FlatComponentsTest
|
|||||||
indeterminateCheckBox = new JCheckBox();
|
indeterminateCheckBox = new JCheckBox();
|
||||||
JLabel toolTipLabel = new JLabel();
|
JLabel toolTipLabel = new JLabel();
|
||||||
JToolTip toolTip1 = new JToolTip();
|
JToolTip toolTip1 = new JToolTip();
|
||||||
|
JToolTip toolTip2 = new JToolTip();
|
||||||
JLabel toolBarLabel = new JLabel();
|
JLabel toolBarLabel = new JLabel();
|
||||||
JToolBar toolBar1 = new JToolBar();
|
JToolBar toolBar1 = new JToolBar();
|
||||||
JButton button4 = new JButton();
|
JButton button4 = new JButton();
|
||||||
@@ -775,6 +776,10 @@ public class FlatComponentsTest
|
|||||||
toolTip1.setTipText("Some text in tool tip.");
|
toolTip1.setTipText("Some text in tool tip.");
|
||||||
add(toolTip1, "cell 1 20 3 1");
|
add(toolTip1, "cell 1 20 3 1");
|
||||||
|
|
||||||
|
//---- toolTip2 ----
|
||||||
|
toolTip2.setTipText("Tool tip with\nmultiple\nlines.");
|
||||||
|
add(toolTip2, "cell 1 20 3 1");
|
||||||
|
|
||||||
//---- toolBarLabel ----
|
//---- toolBarLabel ----
|
||||||
toolBarLabel.setText("JToolBar:");
|
toolBarLabel.setText("JToolBar:");
|
||||||
add(toolBarLabel, "cell 0 21");
|
add(toolBarLabel, "cell 0 21");
|
||||||
|
|||||||
@@ -770,6 +770,12 @@ new FormModel {
|
|||||||
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
|
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
|
||||||
"value": "cell 1 20 3 1"
|
"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" ) {
|
add( new FormComponent( "javax.swing.JLabel" ) {
|
||||||
name: "toolBarLabel"
|
name: "toolBarLabel"
|
||||||
"text": "JToolBar:"
|
"text": "JToolBar:"
|
||||||
|
|||||||
Reference in New Issue
Block a user