source: josm/trunk/src/org/openstreetmap/josm/gui/widgets/JMultilineLabel.java@ 13661

Last change on this file since 13661 was 13157, checked in by Don-vip, 6 years ago

see #15550 - better (?) resizing of note tooltips

  • Property svn:eol-style set to native
File size: 3.9 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.widgets;
3
4import java.awt.Dimension;
5import java.awt.Rectangle;
6
7import javax.swing.JEditorPane;
8import javax.swing.plaf.basic.BasicHTML;
9import javax.swing.text.View;
10
11/**
12 * Creates a normal label that will wrap its contents if there less width than
13 * required to print it in one line. Additionally the maximum width of the text
14 * can be set using <code>setMaxWidth</code>.
15 *
16 * Note that this won't work if JMultilineLabel is put into a JScrollBox or
17 * similar as the bounds will never change. Instead scrollbars will be displayed.
18 *
19 * @since 6340
20 */
21public class JMultilineLabel extends JEditorPane {
22 private int maxWidth = Integer.MAX_VALUE;
23 private Rectangle oldbounds;
24 private Dimension oldPreferred;
25
26 /**
27 * Constructs a normal label but adds HTML tags if not already done so.
28 * Supports both newline characters (<code>\n</code>) as well as the HTML
29 * <code>&lt;br&gt;</code> to insert new lines.
30 *
31 * Use setMaxWidth to limit the width of the label.
32 * @param text The text to display
33 */
34 public JMultilineLabel(String text) {
35 this(text, false);
36 }
37
38 /**
39 * Constructs a normal label but adds HTML tags if not already done so.
40 * Supports both newline characters (<code>\n</code>) as well as the HTML
41 * <code>&lt;br&gt;</code> to insert new lines.
42 *
43 * Use setMaxWidth to limit the width of the label.
44 * @param text The text to display
45 * @param allBold If {@code true}, makes all text to be displayed in bold
46 */
47 public JMultilineLabel(String text, boolean allBold) {
48 this(text, allBold, false);
49 }
50
51 /**
52 * Constructs a normal label but adds HTML tags if not already done so.
53 * Supports both newline characters (<code>\n</code>) as well as the HTML
54 * <code>&lt;br&gt;</code> to insert new lines.
55 *
56 * Use setMaxWidth to limit the width of the label.
57 * @param text The text to display
58 * @param allBold If {@code true}, makes all text to be displayed in bold
59 * @param focusable indicates whether this label is focusable
60 * @since 13157
61 */
62 public JMultilineLabel(String text, boolean allBold, boolean focusable) {
63 JosmEditorPane.makeJLabelLike(this, allBold);
64 String html = text.trim().replaceAll("\n", "<br>");
65 if (!html.startsWith("<html>")) {
66 html = "<html>" + html + "</html>";
67 }
68 setFocusable(focusable);
69 super.setText(html);
70 }
71
72 /**
73 * Set the maximum width. Use this method instead of setMaximumSize because
74 * this saves a little bit of overhead and is actually taken into account.
75 *
76 * @param width the maximum width
77 */
78 public void setMaxWidth(int width) {
79 this.maxWidth = width;
80 }
81
82 /**
83 * Tries to determine a suitable height for the given contents and return that dimension.
84 */
85 @Override
86 public Dimension getPreferredSize() {
87 // Without this check it will result in an infinite loop calling getPreferredSize.
88 // Remember the old bounds and only recalculate if the size actually changed.
89 if (oldPreferred != null && this.getBounds().equals(oldbounds)) {
90 return oldPreferred;
91 }
92 oldbounds = this.getBounds();
93
94 Dimension superPreferred = super.getPreferredSize();
95 // Make it not larger than required
96 int width = Math.min(superPreferred.width, maxWidth);
97
98 // Calculate suitable width and height
99 final View v = (View) super.getClientProperty(BasicHTML.propertyKey);
100
101 if (v == null) {
102 return superPreferred;
103 }
104
105 v.setSize(width, 0);
106 int w = (int) Math.ceil(v.getPreferredSpan(View.X_AXIS));
107 int h = (int) Math.ceil(v.getPreferredSpan(View.Y_AXIS));
108
109 oldPreferred = new Dimension(w, h);
110 return oldPreferred;
111 }
112}
Note: See TracBrowser for help on using the repository browser.