001package horstmann.ch08_umleditor;
002import java.awt.Dimension;
003import java.awt.Graphics2D;
004import java.awt.geom.Rectangle2D;
005import java.io.Serializable;
006import java.util.StringTokenizer;
007
008import javax.swing.JLabel;
009
010/**
011   A string that can extend over multiple lines.
012 */
013@SuppressWarnings("all")
014public class MultiLineString implements Cloneable, Serializable
015{
016        /**
017      Constructs an empty, centered, normal size multiline
018      string that is not underlined.
019         */
020        public MultiLineString()
021        {
022                text = "";
023                justification = CENTER;
024                size = NORMAL;
025                underlined = false;
026        }
027        /**
028      Sets the value of the text property.
029      @param newValue the text of the multiline string
030         */
031        public void setText(String newValue) { text = newValue; }
032        /**
033      Gets the value of the text property.
034      @return the text of the multiline string
035         */
036        public String getText() { return text; }
037        /**
038      Sets the value of the justification property.
039      @param newValue the justification, one of LEFT, CENTER,
040      RIGHT
041         */
042        public void setJustification(int newValue) { justification = newValue; }
043        /**
044      Gets the value of the justification property.
045      @return the justification, one of LEFT, CENTER,
046      RIGHT
047         */
048        public int getJustification() { return justification; }
049        /**
050      Gets the value of the underlined property.
051      @return true if the text is underlined
052         */
053        public boolean isUnderlined() { return underlined; }
054        /**
055      Sets the value of the underlined property.
056      @param newValue true to underline the text
057         */
058        public void setUnderlined(boolean newValue) { underlined = newValue; }
059        /**
060      Sets the value of the size property.
061      @param newValue the size, one of SMALL, NORMAL, LARGE
062         */
063        public void setSize(int newValue) { size = newValue; }
064        /**
065      Gets the value of the size property.
066      @return the size, one of SMALL, NORMAL, LARGE
067         */
068        public int getSize() { return size; }
069
070        public String toString()
071        {
072                return text.replace('\n', '|');
073        }
074
075        private void setLabelText()
076        {
077                StringBuilder prefix = new StringBuilder();
078                StringBuilder suffix = new StringBuilder();
079                StringBuilder htmlText = new StringBuilder();
080                prefix.append(" ");
081                suffix.insert(0, " ");
082                if (underlined)
083                {
084                        prefix.append("<u>");
085                        suffix.insert(0, "</u>");
086                }
087                if (size == LARGE)
088                {
089                        prefix.append("<font size=\"+1\">");
090                        suffix.insert(0, "</font>");
091                }
092                if (size == SMALL)
093                {
094                        prefix.append("<font size=\"-1\">");
095                        suffix.insert(0, "</font>");
096                }
097                htmlText.append("<html>");
098                StringTokenizer tokenizer = new StringTokenizer(text, "\n");
099                boolean first = true;
100                while (tokenizer.hasMoreTokens())
101                {
102                        if (first) first = false; else htmlText.append("<br>");
103                        htmlText.append(prefix);
104                        htmlText.append(tokenizer.nextToken());
105                        htmlText.append(suffix);
106                }
107                htmlText.append("</html>");
108                label.setText(htmlText.toString());
109                if (justification == LEFT) label.setHorizontalAlignment(JLabel.LEFT);
110                else if (justification == CENTER) label.setHorizontalAlignment(JLabel.CENTER);
111                else if (justification == RIGHT) label.setHorizontalAlignment(JLabel.RIGHT);
112        }
113
114        /**
115      Gets the bounding rectangle for this multiline string.
116      @param g2 the graphics context
117      @return the bounding rectangle (with top left corner (0,0))
118         */
119        public Rectangle2D getBounds(Graphics2D g2)
120        {
121                if (text.length() == 0) return new Rectangle2D.Double();
122                setLabelText();
123                Dimension dim = label.getPreferredSize();
124                return new Rectangle2D.Double(0, 0, dim.getWidth(), dim.getHeight());
125        }
126
127        /**
128      Draws this multiline string inside a given rectangle
129      @param g2 the graphics context
130      @param r the rectangle into which to place this multiline string
131         */
132        public void draw(Graphics2D g2, Rectangle2D r)
133        {
134                setLabelText();
135                label.setFont(g2.getFont());
136                label.setBounds(0, 0, (int) r.getWidth(), (int) r.getHeight());
137                g2.translate(r.getX(), r.getY());
138                label.paint(g2);
139                g2.translate(-r.getX(), -r.getY());
140        }
141
142        public Object clone()
143        {
144                try
145                {
146                        return super.clone();
147                }
148                catch (CloneNotSupportedException exception)
149                {
150                        return null;
151                }
152        }
153
154        public static final int LEFT = 0;
155        public static final int CENTER = 1;
156        public static final int RIGHT = 2;
157        public static final int LARGE = 3;
158        public static final int NORMAL = 4;
159        public static final int SMALL = 5;
160
161        private static final int GAP = 3;
162
163        private String text;
164        private int justification;
165        private int size;
166        private boolean underlined;
167
168        private static JLabel label = new JLabel();
169}