01: package ob.text;
02:
03: import java.awt.*;
04: import com.sun.portal.log.common.PortalLogger;
05:
06: public class ExpandableText extends ob.text.MultiText {
07: public int m_nLookAhead = 15;
08: protected int m_nFixedWidth = 100;
09:
10: public ExpandableText() {
11: this ("");
12: }
13:
14: public ExpandableText(String s) {
15: super (s);
16: m_nFixedWidth = 100;
17: setAutoWrap(true);
18: setAllowEnter(false);
19: setInsets(new Insets(0, 0, 0, 0));
20: }
21:
22: public void update() {
23: sizeToFit();
24: super .update();
25: }
26:
27: public void setFixedWidth(int nWidth) {
28: m_nFixedWidth = nWidth;
29: }
30:
31: public void sizeToFit() {
32:
33: String text = getText();
34: Font font = (getFont() != null) ? getFont() : defaultfont;
35: FontMetrics fm = getFontMetrics(font);
36: int nWidth = fm.stringWidth(text);
37: if (m_nFixedWidth > 0 && nWidth + m_nLookAhead > m_nFixedWidth) {
38: nWidth = m_nFixedWidth;
39: setAutoWrap(true);
40: } else {
41: setAutoWrap(false);
42: nWidth += m_nLookAhead;
43: }
44:
45: if (getBounds().width != nWidth)
46: setBounds(getBounds().x, getBounds().y, nWidth,
47: getBounds().height);
48: }
49:
50: protected void onVerticalScroll() {
51: int marker = 0;
52: int y = 0;
53: for (int i = 0; i <= cursorPoint.y; i++) {
54: marker += ((Paragraph) m_vParagraphs.elementAt(i))
55: .getYSpan();
56: if (marker > y_offset) {
57: y = marker - y_offset;
58: }
59: }
60: Paragraph p = (Paragraph) m_vParagraphs
61: .elementAt(cursorPoint.y);
62: y = y - p.getYSpan() + p.getCursorPoint().y;
63:
64: int nBoardWidth = 2;
65: if (m_nBorderStyle == NONE)
66: nBoardWidth = 0;
67: else if (m_nBorderStyle == NORMAL)
68: nBoardWidth = 1;
69:
70: if ((y + m_inTextInsets.top + m_inTextInsets.bottom + 2 * nBoardWidth) > (getSize().height)) {
71: y_offset += (p.getFontMetrics().getHeight());
72: } else if (y < 0 && y_offset > 0) {
73: y_offset -= (p.getFontMetrics().getHeight());
74: }
75: }
76: }
|