Label and ToolTip: made inserting BASE_SIZE rule into HTML text more reliable

This commit is contained in:
Karl Tauber
2021-02-04 15:10:27 +01:00
parent e5d3c08821
commit be507de6c1
3 changed files with 30 additions and 6 deletions

View File

@@ -110,14 +110,26 @@ public class FlatLabelUI
c.getClientProperty( "html.disable" ) != Boolean.TRUE &&
needsFontBaseSize( text ) )
{
int headIndex = text.indexOf( "<head>" );
// BASE_SIZE rule is parsed in javax.swing.text.html.StyleSheet.addRule()
String style = "<style>BASE_SIZE " + c.getFont().getSize() + "</style>";
if( headIndex < 0 )
style = "<head>" + style + "</head>";
int insertIndex = headIndex >= 0 ? (headIndex + "<head>".length()) : "<html>".length();
String lowerText = text.toLowerCase();
int headIndex;
int styleIndex;
int insertIndex;
if( (headIndex = lowerText.indexOf( "<head>" )) >= 0 ) {
// there is a <head> tag --> insert after <head> tag
insertIndex = headIndex + "<head>".length();
} else if( (styleIndex = lowerText.indexOf( "<style>" )) >= 0 ) {
// there is a <style> tag --> insert before <style> tag
insertIndex = styleIndex;
} else {
// no <head> or <style> tag --> insert <head> tag after <html> tag
style = "<head>" + style + "</head>";
insertIndex = "<html>".length();
}
text = text.substring( 0, insertIndex )
+ style
+ text.substring( insertIndex );

View File

@@ -145,6 +145,7 @@ public class FlatHtmlTest
JLabel label48 = new JLabel();
JLabel label54 = new JLabel();
JLabel label56 = new JLabel();
JLabel label57 = new JLabel();
//======== this ========
setLayout(new MigLayout(
@@ -443,6 +444,7 @@ public class FlatHtmlTest
"[]para" +
"[]para" +
"[]" +
"[]" +
"[]"));
//---- label22 ----
@@ -588,6 +590,10 @@ public class FlatHtmlTest
//---- label56 ----
label56.setText("<html><head><style>body { color: red }</style></head>leading <big>red</big> trailing</html>");
panel2.add(label56, "cell 0 8 7 1");
//---- label57 ----
label57.setText("<html><style>body { color: red }</style><p>leading <big>red</big> trailing</p></html>");
panel2.add(label57, "cell 0 9 7 1");
}
add(panel2, "cell 4 2");
// JFormDesigner - End of component initialization //GEN-END:initComponents

View File

@@ -372,7 +372,7 @@ new FormModel {
add( new FormContainer( "javax.swing.JPanel", new FormLayoutManager( class net.miginfocom.swing.MigLayout ) {
"$layoutConstraints": "insets 0,hidemode 3"
"$columnConstraints": "[fill]para[fill][fill][fill][fill][fill][fill]"
"$rowConstraints": "[][][][][][]para[]para[][]"
"$rowConstraints": "[][][][][][]para[]para[][][]"
} ) {
name: "panel2"
auxiliary() {
@@ -594,6 +594,12 @@ new FormModel {
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 0 8 7 1"
} )
add( new FormComponent( "javax.swing.JLabel" ) {
name: "label57"
"text": "<html><style>body { color: red }</style><p>leading <big>red</big> trailing</p></html>"
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 0 9 7 1"
} )
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 4 2"
} )