major optimization updates
[debian/openrocket] / src / net / sf / openrocket / gui / components / DescriptionArea.java
index 264953f7c9c2c6d676e38c8740c33e3114642692..1e37bb6f2a7c8f961a1f1014838411fa3df6eff9 100644 (file)
 package net.sf.openrocket.gui.components;
 
+import java.awt.Color;
 import java.awt.Dimension;
+import java.awt.Font;
 import java.awt.Rectangle;
 
+import javax.swing.JEditorPane;
 import javax.swing.JPanel;
 import javax.swing.JScrollPane;
 import javax.swing.ScrollPaneConstants;
-
-import net.miginfocom.swing.MigLayout;
+import javax.swing.SwingUtilities;
 
 public class DescriptionArea extends JScrollPane {
-
-       private ResizeLabel text;
-       private MigLayout layout;
-       private JPanel panel;
        
+       private final JEditorPane editorPane;
+       
+       
+       /**
+        * Construct a description area with the specified number of rows, default description font size,
+        * being opaque.
+        * 
+        * @param rows  the number of rows
+        */
        public DescriptionArea(int rows) {
-               this(rows, -2);
+               this("", rows, -1);
        }
        
+       /**
+        * Construct a description area with the specified number of rows and size, being opaque.
+        * 
+        * @param rows  the number of rows.
+        * @param size  the font size difference compared to the default font size.
+        */
        public DescriptionArea(int rows, float size) {
+               this("", rows, size);
+       }
+       
+       /**
+        * Construct an opaque description area with the specified number of rows, size and text, being opaque.
+        * 
+        * @param text  the initial text.
+        * @param rows  the number of rows.
+        * @param size  the font size difference compared to the default font size.
+        */
+       public DescriptionArea(String text, int rows, float size) {
+               this(text, rows, size, true);
+       }
+       
+       /**
+        * Constructor with all options.
+        * 
+        * @param text          the text for the description area.
+        * @param rows          the number of rows to set
+        * @param size          the relative font size in points (positive or negative)
+        * @param opaque        if <code>false</code> the background color will be set to the background color
+        *                                      of a default JPanel (simulation non-opaque)
+        */
+       public DescriptionArea(String text, int rows, float size, boolean opaque) {
                super(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
                                ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
                
-               layout = new MigLayout("ins 0 2px, fill");
-               panel = new JPanel(layout);
+               editorPane = new JEditorPane("text/html", "");
+               Font font = editorPane.getFont();
+               editorPane.setFont(font.deriveFont(font.getSize2D() + size));
+               editorPane.setEditable(false);
                
-               text = new ResizeLabel(" ",size);
-               text.validate();
-               Dimension dim = text.getPreferredSize();
-               dim.height = (dim.height+2)*rows + 2;
-               this.setPreferredSize(dim);
+               if (!opaque) {
+                       Color bg = new JPanel().getBackground();
+                       editorPane.setBackground(new Color(bg.getRed(), bg.getGreen(), bg.getBlue()));
+                       this.setOpaque(true);
+               }
                
-               panel.add(text, "growx");
+               // Calculate correct height
+               editorPane.setText("abc");
+               Dimension oneline = editorPane.getPreferredSize();
+               editorPane.setText("abc<br>def");
+               Dimension twolines = editorPane.getPreferredSize();
+               editorPane.setText("");
                
-               this.setViewportView(panel);
-               this.revalidate();
+               int lineheight = twolines.height - oneline.height;
+               int extraheight = oneline.height - lineheight;
+               
+               Dimension dim = editorPane.getPreferredSize();
+               dim.height = lineheight * rows + extraheight + 2;
+               this.setPreferredSize(dim);
+               
+               this.setViewportView(editorPane);
+               this.setText(text);
        }
        
        public void setText(String txt) {
-               if (!txt.startsWith("<html>"))
-                       txt = "<html>" + txt;
-               text.setText(txt);
-       }
-       
-       
-       @Override
-       public void validate() {
-               
-               Rectangle dim = this.getViewportBorderBounds();
-               layout.setComponentConstraints(text, "width "+ dim.width + ", growx");
-               super.validate();
-               text.validate();
-
+               editorPane.setText(txt);
+               editorPane.revalidate();
+               SwingUtilities.invokeLater(new Runnable() {
+                       
+                       @Override
+                       public void run() {
+                               editorPane.scrollRectToVisible(new Rectangle(0, 0, 1, 1));
+                       }
+                       
+               });
+               editorPane.scrollRectToVisible(new Rectangle(0, 0, 1, 1));
        }
        
 }