ac26ae837d7c01737c9e7c853d0fd8de1e84805d
[debian/openrocket] / src / net / sf / openrocket / gui / components / DescriptionArea.java
1 package net.sf.openrocket.gui.components;
2
3 import java.awt.Dimension;
4 import java.awt.Font;
5 import java.awt.Rectangle;
6
7 import javax.swing.JEditorPane;
8 import javax.swing.JScrollPane;
9 import javax.swing.ScrollPaneConstants;
10 import javax.swing.SwingUtilities;
11
12 public class DescriptionArea extends JScrollPane {
13
14         private final JEditorPane editorPane;
15         
16         public DescriptionArea(int rows) {
17                 this(rows, -1);
18         }
19         
20         public DescriptionArea(int rows, float size) {
21                 super(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
22                                 ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
23                 
24                 editorPane = new JEditorPane("text/html", "");
25                 Font font = editorPane.getFont();
26                 editorPane.setFont(font.deriveFont(font.getSize2D() + size));
27                 editorPane.setEditable(false);
28                 
29                 // Calculate correct height
30                 editorPane.setText("abc");
31                 Dimension oneline = editorPane.getPreferredSize();
32                 editorPane.setText("abc<br>def");
33                 Dimension twolines = editorPane.getPreferredSize();
34                 editorPane.setText("");
35                 
36                 int lineheight = twolines.height - oneline.height;
37                 int extraheight = oneline.height - lineheight;
38                 
39                 Dimension dim = editorPane.getPreferredSize();
40                 dim.height = lineheight * rows + extraheight + 2;
41                 this.setPreferredSize(dim);
42                 
43                 this.setViewportView(editorPane);
44         }
45         
46         public void setText(String txt) {
47                 editorPane.setText(txt);
48                 editorPane.revalidate();
49                 SwingUtilities.invokeLater(new Runnable() {
50
51                         @Override
52                         public void run() {
53                                 editorPane.scrollRectToVisible(new Rectangle(0,0,1,1));
54                         }
55                         
56                 });
57                 editorPane.scrollRectToVisible(new Rectangle(0,0,1,1));
58         }
59         
60 }