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

Last change on this file since 11921 was 11452, checked in by Don-vip, 7 years ago

sonar - fb-contrib:SEO_SUBOPTIMAL_EXPRESSION_ORDER - Performance - Method orders expressions in a conditional in a sub optimal way

  • Property svn:eol-style set to native
File size: 3.3 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 JosmEditorPane.makeJLabelLike(this, allBold);
49 String html = text.trim().replaceAll("\n", "<br>");
50 if (!html.startsWith("<html>")) {
51 html = "<html>" + html + "</html>";
52 }
53 setFocusable(false);
54 super.setText(html);
55 }
56
57 /**
58 * Set the maximum width. Use this method instead of setMaximumSize because
59 * this saves a little bit of overhead and is actually taken into account.
60 *
61 * @param width the maximum width
62 */
63 public void setMaxWidth(int width) {
64 this.maxWidth = width;
65 }
66
67 /**
68 * Tries to determine a suitable height for the given contents and return that dimension.
69 */
70 @Override
71 public Dimension getPreferredSize() {
72 // Without this check it will result in an infinite loop calling getPreferredSize.
73 // Remember the old bounds and only recalculate if the size actually changed.
74 if (oldPreferred != null && this.getBounds().equals(oldbounds)) {
75 return oldPreferred;
76 }
77 oldbounds = this.getBounds();
78
79 Dimension superPreferred = super.getPreferredSize();
80 // Make it not larger than required
81 int width = Math.min(superPreferred.width, maxWidth);
82
83 // Calculate suitable width and height
84 final View v = (View) super.getClientProperty(BasicHTML.propertyKey);
85
86 if (v == null) {
87 return superPreferred;
88 }
89
90 v.setSize(width, 0);
91 int w = (int) Math.ceil(v.getPreferredSpan(View.X_AXIS));
92 int h = (int) Math.ceil(v.getPreferredSpan(View.Y_AXIS));
93
94 oldPreferred = new Dimension(w, h);
95 return oldPreferred;
96 }
97}
Note: See TracBrowser for help on using the repository browser.