[729] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
| 2 |
|
---|
[1169] | 3 | // This class was taken from
|
---|
[729] | 4 | // http://forum.java.sun.com/thread.jspa?threadID=459705&messageID=2104021
|
---|
| 5 | // - Removed hardcoded margin
|
---|
| 6 | // - Added constructor
|
---|
| 7 |
|
---|
| 8 | package org.openstreetmap.josm.gui;
|
---|
| 9 |
|
---|
| 10 | import java.awt.Dimension;
|
---|
| 11 | import java.awt.Graphics;
|
---|
| 12 | import java.awt.Graphics2D;
|
---|
| 13 | import java.awt.Insets;
|
---|
| 14 | import java.awt.font.FontRenderContext;
|
---|
| 15 | import java.awt.font.LineBreakMeasurer;
|
---|
| 16 | import java.awt.font.TextAttribute;
|
---|
| 17 | import java.text.AttributedCharacterIterator;
|
---|
| 18 | import java.text.AttributedString;
|
---|
| 19 | import java.awt.font.TextLayout;
|
---|
| 20 |
|
---|
| 21 | import javax.swing.JComponent;
|
---|
| 22 |
|
---|
| 23 | public class JMultilineLabel extends JComponent {
|
---|
[1169] | 24 | private String text;
|
---|
| 25 | private int maxWidth = Integer.MAX_VALUE;
|
---|
| 26 | private boolean justify;
|
---|
| 27 | private final FontRenderContext frc = new FontRenderContext(null, false, false);
|
---|
[729] | 28 |
|
---|
[1169] | 29 | public JMultilineLabel(String description) {
|
---|
| 30 | super();
|
---|
| 31 | setText(description);
|
---|
| 32 | }
|
---|
[729] | 33 |
|
---|
[1169] | 34 | private void morph() {
|
---|
| 35 | revalidate();
|
---|
| 36 | repaint();
|
---|
| 37 | }
|
---|
[729] | 38 |
|
---|
[1169] | 39 | public String getText() {
|
---|
| 40 | return text;
|
---|
| 41 | }
|
---|
[729] | 42 |
|
---|
[1169] | 43 | public void setText(String text) {
|
---|
| 44 | String old = this.text;
|
---|
| 45 | this.text = text;
|
---|
| 46 | firePropertyChange("text", old, this.text);
|
---|
| 47 | if ((old == null) ? text!=null : !old.equals(text))
|
---|
| 48 | morph();
|
---|
| 49 | }
|
---|
[729] | 50 |
|
---|
[1169] | 51 | public int getMaxWidth() {
|
---|
| 52 | return maxWidth;
|
---|
| 53 | }
|
---|
[729] | 54 |
|
---|
[1169] | 55 | public void setMaxWidth(int maxWidth) {
|
---|
| 56 | if (maxWidth <= 0)
|
---|
| 57 | throw new IllegalArgumentException();
|
---|
| 58 | int old = this.maxWidth;
|
---|
| 59 | this.maxWidth = maxWidth;
|
---|
| 60 | firePropertyChange("maxWidth", old, this.maxWidth);
|
---|
| 61 | if (old != this.maxWidth)
|
---|
| 62 | morph();
|
---|
| 63 | }
|
---|
[729] | 64 |
|
---|
[1169] | 65 | public boolean isJustified() {
|
---|
| 66 | return justify;
|
---|
| 67 | }
|
---|
[729] | 68 |
|
---|
[1169] | 69 | public void setJustified(boolean justify) {
|
---|
| 70 | boolean old = this.justify;
|
---|
| 71 | this.justify = justify;
|
---|
| 72 | firePropertyChange("justified", old, this.justify);
|
---|
| 73 | if (old != this.justify)
|
---|
| 74 | repaint();
|
---|
| 75 | }
|
---|
[729] | 76 |
|
---|
[1169] | 77 | public Dimension getPreferredSize() {
|
---|
| 78 | return paintOrGetSize(null, getMaxWidth());
|
---|
| 79 | }
|
---|
[729] | 80 |
|
---|
[1169] | 81 | public Dimension getMinimumSize() {
|
---|
| 82 | return getPreferredSize();
|
---|
| 83 | }
|
---|
[729] | 84 |
|
---|
[1169] | 85 | protected void paintComponent(Graphics g) {
|
---|
| 86 | super.paintComponent(g);
|
---|
| 87 | paintOrGetSize((Graphics2D)g, getWidth());
|
---|
| 88 | }
|
---|
[729] | 89 |
|
---|
[1169] | 90 | private Dimension paintOrGetSize(Graphics2D g, int width) {
|
---|
| 91 | Insets insets = getInsets();
|
---|
| 92 | width -= insets.left + insets.right;
|
---|
| 93 | float w = insets.left + insets.right;
|
---|
| 94 | float x = insets.left, y=insets.top;
|
---|
[1407] | 95 |
|
---|
[1169] | 96 | if (width > 0 && text != null && text.length() > 0) {
|
---|
[1407] | 97 | String[] lines = getText().split("\n");
|
---|
[1397] | 98 | for(String line : lines) {
|
---|
| 99 | // Insert a space so new lines get rendered
|
---|
| 100 | if(line.length() == 0) line = " ";
|
---|
| 101 | AttributedString as = new AttributedString(line);
|
---|
| 102 | as.addAttribute(TextAttribute.FONT, getFont());
|
---|
| 103 | AttributedCharacterIterator aci = as.getIterator();
|
---|
| 104 | LineBreakMeasurer lbm = new LineBreakMeasurer(aci, frc);
|
---|
| 105 | float max = 0;
|
---|
| 106 | while (lbm.getPosition() < aci.getEndIndex()) {
|
---|
| 107 | TextLayout textLayout = lbm.nextLayout(width);
|
---|
| 108 | if (g != null && isJustified() && textLayout.getVisibleAdvance() > 0.80 * width)
|
---|
| 109 | textLayout = textLayout.getJustifiedLayout(width);
|
---|
| 110 | if (g != null)
|
---|
| 111 | textLayout.draw(g, x, y + textLayout.getAscent());
|
---|
| 112 | y += textLayout.getDescent() + textLayout.getLeading() + textLayout.getAscent();
|
---|
| 113 | max = Math.max(max, textLayout.getVisibleAdvance());
|
---|
| 114 | }
|
---|
| 115 | w += max;
|
---|
[1169] | 116 | }
|
---|
| 117 | }
|
---|
| 118 | return new Dimension((int)Math.ceil(w), (int)Math.ceil(y) + insets.bottom);
|
---|
| 119 | }
|
---|
[729] | 120 | }
|
---|