enhanced motor handling, bug fixes, initial optimization code
[debian/openrocket] / src / net / sf / openrocket / gui / components / DescriptionArea.java
1 package net.sf.openrocket.gui.components;
2
3 import java.awt.Color;
4 import java.awt.Dimension;
5 import java.awt.Font;
6 import java.awt.Rectangle;
7
8 import javax.swing.JEditorPane;
9 import javax.swing.JPanel;
10 import javax.swing.JScrollPane;
11 import javax.swing.ScrollPaneConstants;
12 import javax.swing.SwingUtilities;
13
14 public class DescriptionArea extends JScrollPane {
15         
16         private final JEditorPane editorPane;
17         
18         
19         public DescriptionArea(int rows) {
20                 this("", rows, -1);
21         }
22         
23         public DescriptionArea(int rows, float size) {
24                 this("", rows, size);
25         }
26         
27         public DescriptionArea(String text, int rows, float size) {
28                 this(text, rows, size, true);
29         }
30         
31         /**
32          * Constructor with all options.
33          * 
34          * @param text          the text for the description area.
35          * @param rows          the number of rows to set
36          * @param size          the relative font size in points (positive or negative)
37          * @param opaque        if <code>false</code> the background color will be set to the background color
38          *                                      of a default JPanel (simulation non-opaque)
39          */
40         public DescriptionArea(String text, int rows, float size, boolean opaque) {
41                 super(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
42                                 ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
43                 
44                 editorPane = new JEditorPane("text/html", "");
45                 Font font = editorPane.getFont();
46                 editorPane.setFont(font.deriveFont(font.getSize2D() + size));
47                 editorPane.setEditable(false);
48                 
49                 if (!opaque) {
50                         Color bg = new JPanel().getBackground();
51                         editorPane.setBackground(new Color(bg.getRed(), bg.getGreen(), bg.getBlue()));
52                         this.setOpaque(true);
53                 }
54                 
55                 // Calculate correct height
56                 editorPane.setText("abc");
57                 Dimension oneline = editorPane.getPreferredSize();
58                 editorPane.setText("abc<br>def");
59                 Dimension twolines = editorPane.getPreferredSize();
60                 editorPane.setText("");
61                 
62                 int lineheight = twolines.height - oneline.height;
63                 int extraheight = oneline.height - lineheight;
64                 
65                 Dimension dim = editorPane.getPreferredSize();
66                 dim.height = lineheight * rows + extraheight + 2;
67                 this.setPreferredSize(dim);
68                 
69                 this.setViewportView(editorPane);
70                 this.setText(text);
71         }
72         
73         public void setText(String txt) {
74                 editorPane.setText(txt);
75                 editorPane.revalidate();
76                 SwingUtilities.invokeLater(new Runnable() {
77                         
78                         @Override
79                         public void run() {
80                                 editorPane.scrollRectToVisible(new Rectangle(0, 0, 1, 1));
81                         }
82                         
83                 });
84                 editorPane.scrollRectToVisible(new Rectangle(0, 0, 1, 1));
85         }
86         
87 }