updates for 0.9.4
[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
17         public DescriptionArea(int rows) {
18                 this("", rows, -1);
19         }
20         public DescriptionArea(int rows, float size) {
21                 this("", rows, size);
22         }
23         
24         public DescriptionArea(String text, int rows, float size) {
25                 super(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
26                                 ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
27                 
28                 editorPane = new JEditorPane("text/html", "");
29                 Font font = editorPane.getFont();
30                 editorPane.setFont(font.deriveFont(font.getSize2D() + size));
31                 editorPane.setEditable(false);
32                 
33                 // Calculate correct height
34                 editorPane.setText("abc");
35                 Dimension oneline = editorPane.getPreferredSize();
36                 editorPane.setText("abc<br>def");
37                 Dimension twolines = editorPane.getPreferredSize();
38                 editorPane.setText("");
39                 
40                 int lineheight = twolines.height - oneline.height;
41                 int extraheight = oneline.height - lineheight;
42                 
43                 Dimension dim = editorPane.getPreferredSize();
44                 dim.height = lineheight * rows + extraheight + 2;
45                 this.setPreferredSize(dim);
46                 
47                 this.setViewportView(editorPane);
48                 this.setText(text);
49         }
50         
51         public void setText(String txt) {
52                 editorPane.setText(txt);
53                 editorPane.revalidate();
54                 SwingUtilities.invokeLater(new Runnable() {
55
56                         @Override
57                         public void run() {
58                                 editorPane.scrollRectToVisible(new Rectangle(0,0,1,1));
59                         }
60                         
61                 });
62                 editorPane.scrollRectToVisible(new Rectangle(0,0,1,1));
63         }
64         
65 }