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

Last change on this file since 9463 was 9463, checked in by bastiK, 8 years ago

applied #12368 - disable focus on multiline labels (patch by kolesar)

  • 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
69 * that dimension.
70 */
71 @Override
72 public Dimension getPreferredSize() {
73 // Without this check it will result in an infinite loop calling
74 // getPreferredSize. Remember the old bounds and only recalculate if
75 // the size actually changed.
76 if (this.getBounds().equals(oldbounds) && oldPreferred != null) {
77 return oldPreferred;
78 }
79 oldbounds = this.getBounds();
80
81 Dimension superPreferred = super.getPreferredSize();
82 // Make it not larger than required
83 int width = Math.min(superPreferred.width, maxWidth);
84
85 // Calculate suitable width and height
86 final View v = (View) super.getClientProperty(BasicHTML.propertyKey);
87
88 if (v == null) {
89 return superPreferred;
90 }
91
92 v.setSize(width, 0);
93 int w = (int) Math.ceil(v.getPreferredSpan(View.X_AXIS));
94 int h = (int) Math.ceil(v.getPreferredSpan(View.Y_AXIS));
95
96 oldPreferred = new Dimension(w, h);
97 return oldPreferred;
98 }
99}
Note: See TracBrowser for help on using the repository browser.