create changelog entry
[debian/openrocket] / core / 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         /**
20          * Construct a description area with the specified number of rows, default description font size,
21          * being opaque.
22          * 
23          * @param rows  the number of rows
24          */
25         public DescriptionArea(int rows) {
26                 this("", rows, -1);
27         }
28         
29         /**
30          * Construct a description area with the specified number of rows and size, being opaque.
31          * 
32          * @param rows  the number of rows.
33          * @param size  the font size difference compared to the default font size.
34          */
35         public DescriptionArea(int rows, float size) {
36                 this("", rows, size);
37         }
38         
39         /**
40          * Construct an opaque description area with the specified number of rows, size and text, being opaque.
41          * 
42          * @param text  the initial text.
43          * @param rows  the number of rows.
44          * @param size  the font size difference compared to the default font size.
45          */
46         public DescriptionArea(String text, int rows, float size) {
47                 this(text, rows, size, true);
48         }
49         
50         /**
51          * Constructor with all options.
52          * 
53          * @param text          the text for the description area.
54          * @param rows          the number of rows to set
55          * @param size          the relative font size in points (positive or negative)
56          * @param opaque        if <code>false</code> the background color will be set to the background color
57          *                                      of a default JPanel (simulation non-opaque)
58          */
59         public DescriptionArea(String text, int rows, float size, boolean opaque) {
60                 super(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
61                                 ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
62                 
63                 editorPane = new JEditorPane("text/html", "");
64                 Font font = editorPane.getFont();
65                 editorPane.setFont(font.deriveFont(font.getSize2D() + size));
66                 editorPane.setEditable(false);
67                 
68                 if (!opaque) {
69                         Color bg = new JPanel().getBackground();
70                         editorPane.setBackground(new Color(bg.getRed(), bg.getGreen(), bg.getBlue()));
71                         this.setOpaque(true);
72                 }
73                 
74                 // Calculate correct height
75                 editorPane.setText("abc");
76                 Dimension oneline = editorPane.getPreferredSize();
77                 editorPane.setText("abc<br>def");
78                 Dimension twolines = editorPane.getPreferredSize();
79                 editorPane.setText("");
80                 
81                 int lineheight = twolines.height - oneline.height;
82                 int extraheight = oneline.height - lineheight;
83                 
84                 Dimension dim = editorPane.getPreferredSize();
85                 dim.height = lineheight * rows + extraheight + 2;
86                 this.setPreferredSize(dim);
87                 
88                 this.setViewportView(editorPane);
89                 this.setText(text);
90         }
91         
92         public void setText(String txt) {
93                 editorPane.setText(txt);
94                 editorPane.revalidate();
95                 SwingUtilities.invokeLater(new Runnable() {
96                         
97                         @Override
98                         public void run() {
99                                 editorPane.scrollRectToVisible(new Rectangle(0, 0, 1, 1));
100                         }
101                         
102                 });
103                 editorPane.scrollRectToVisible(new Rectangle(0, 0, 1, 1));
104         }
105         
106 }