create changelog entry
[debian/openrocket] / core / src / net / sf / openrocket / gui / components / StyledLabel.java
1 package net.sf.openrocket.gui.components;
2
3 import java.awt.Font;
4
5 import javax.swing.JLabel;
6 import javax.swing.SwingConstants;
7
8 /**
9  * A resizeable and styleable JLabel.  The method {@link #resizeFont(float)} changes the 
10  * current font size by the given (positive or negative) amount.  The change is relative 
11  * to the current font size.  The method {@link #setFontStyle(Style)} sets the style
12  * (bold/italic) of the font.
13  * <p>
14  * A nice small text is achievable by  <code>new ResizeLabel("My text", -2);</code>
15  * 
16  * @author Sampo Niskanen <sampo.niskanen@iki.fi>
17  */
18
19 public class StyledLabel extends JLabel {
20         
21         public enum Style {
22                 PLAIN(Font.PLAIN),
23                 BOLD(Font.BOLD),
24                 ITALIC(Font.ITALIC),
25                 BOLD_ITALIC(Font.BOLD | Font.ITALIC);
26                 
27                 private int style;
28                 Style(int fontStyle) {
29                         this.style = fontStyle;
30                 }
31                 public int getFontStyle() {
32                         return style;
33                 }
34         }
35         
36         
37         
38         public StyledLabel() {
39                 this("", SwingConstants.LEADING, 0f);
40         }
41         
42         public StyledLabel(String text) {
43                 this(text, SwingConstants.LEADING, 0f);
44         }
45         
46         public StyledLabel(float size) {
47                 this("", SwingConstants.LEADING, size);
48         }
49         
50         public StyledLabel(String text, float size) {
51                 this(text, SwingConstants.LEADING, size);
52         }
53         
54         public StyledLabel(String text, int horizontalAlignment, float size) {
55                 super(text, horizontalAlignment);
56                 resizeFont(size);
57                 checkPreferredSize(size, Style.PLAIN);
58         }
59         
60         
61
62         public StyledLabel(Style style) {
63                 this("", SwingConstants.LEADING, 0f, style);
64         }
65         
66         public StyledLabel(String text, Style style) {
67                 this(text, SwingConstants.LEADING, 0f, style);
68         }
69         
70         public StyledLabel(float size, Style style) {
71                 this("", SwingConstants.LEADING, size, style);
72         }
73         
74         public StyledLabel(String text, float size, Style style) {
75                 this(text, SwingConstants.LEADING, size, style);
76         }
77         
78         public StyledLabel(String text, int horizontalAlignment, float size, Style style) {
79                 super(text, horizontalAlignment);
80                 resizeFont(size);
81                 setFontStyle(style);
82                 checkPreferredSize(size, style);
83         }
84         
85         
86         
87         
88         private void checkPreferredSize(float size, Style style) {
89                 String str = this.getText();
90                 if (str.startsWith("<html>") && str.indexOf("<br") < 0) {
91                         StyledLabel label = new StyledLabel("plaintext", size, style);
92                         label.validate();
93                         //System.out.println("Plain-text label: " + label.getPreferredSize());
94                         //System.out.println("HTML label: " + this.getPreferredSize());
95                 }
96         }
97         
98         
99         
100         public void resizeFont(float size) {
101                 Font font = this.getFont();
102                 font = font.deriveFont(font.getSize2D()+size);
103                 this.setFont(font);
104         }
105         
106         public void setFontStyle(Style style) {
107                 Font font = this.getFont();
108                 font = font.deriveFont(style.getFontStyle());
109                 this.setFont(font);
110         }
111 }