FileDocCategorySizeDatePackage
TextView.javaAPI DocJMF 2.1.1e6699Mon May 12 12:20:30 BST 2003jmapps.ui

TextView

public class TextView extends Canvas

Fields Summary
private String
strText
private Insets
insetsBorder
private int
nPreferredWidth
private static Image
imageBuffer
Constructors Summary
public TextView(String strText)



          
        this.strText = strText;
    
Methods Summary
public java.awt.DimensiongetPreferredSize()

        Dimension      dim;

        dim = new Dimension ( nPreferredWidth, 0 );
        dim.width += insetsBorder.left + insetsBorder.right;
//        dim.height += insetsBorder.top + insetsBorder.bottom;
        if ( nPreferredWidth > 0 )
            dim = getPreferredSize ( dim.width );
        return ( dim );
    
public java.awt.DimensiongetPreferredSize(int nPreferredWidth)

        int             i;
        int             nLength;
        char            character;
        int             nBegin;
        int             nEnd;
        String          strLine;
        int             nLineWidth;
        int             nLineCount;
        Dimension       dim;
        Font            font;
        FontMetrics     fontMetrics;


        dim = new Dimension ( 0, 0 );
        nPreferredWidth -= insetsBorder.left + insetsBorder.right;
        font = this.getFont ();
        fontMetrics = this.getFontMetrics ( font );

        nLength = strText.length ();
        nEnd = 0;
        nLineCount = 0;
        for ( i = 0;  i < nLength;  i++ ) {
            nBegin = nEnd;
            do {
                nEnd = i;
                do {
                    i++;
                    character = strText.charAt (i);
                } while ( i < nLength - 1  &&  Character.isLetterOrDigit(character) );

                strLine = strText.substring ( nBegin, i );
                nLineWidth = fontMetrics.stringWidth ( strLine );
                if ( i >= nLength - 1  &&  nLineWidth < nPreferredWidth )
                    nEnd = i - 1;
                if ( character == '\n"  ||  character == Character.LINE_SEPARATOR )
                    nEnd = i;
            } while ( i < nLength - 1  &&  nLineWidth < nPreferredWidth
                                    &&  character != '\n"
                                    &&  character != Character.LINE_SEPARATOR );

            strLine = strText.substring ( nBegin, nEnd );
            nLineWidth = fontMetrics.stringWidth ( strLine );
            dim.width = Math.max ( dim.width, nLineWidth );

            nLineCount++;

            do {
                nEnd++;
                character = strText.charAt ( nEnd );
            } while ( nEnd < nLength - 1  &&  Character.isSpaceChar(character) );
            i = nEnd;
        }

        dim.height = nLineCount * fontMetrics.getHeight();
        dim.height += insetsBorder.top + insetsBorder.bottom;
        dim.width += insetsBorder.left + insetsBorder.right + 2;
        return ( dim );
    
public booleanisDoubleBuffered()

        return ( true );
    
public voidpaint(java.awt.Graphics g)

        int             i;
        int             nLength;
        char            character;
        int             nBegin;
        int             nEnd;
        String          strLine;
        int             nPreferredWidth;
        int             nLineWidth;
        int             nLineHeight;
        int             nOffsetY;
        Font            font;
        FontMetrics     fontMetrics;
        Dimension       dim;


        dim = this.getSize ();
        nPreferredWidth = dim.width - (insetsBorder.left + insetsBorder.right);
        font = this.getFont ();
        fontMetrics = this.getFontMetrics ( font );
        nLineHeight = fontMetrics.getHeight();
        nOffsetY = insetsBorder.top + fontMetrics.getMaxAscent();

        nLength = strText.length ();
        nEnd = 0;
        for ( i = 0;  i < nLength;  i++ ) {
            nBegin = nEnd;
            do {
                nEnd = i;
                do {
                    i++;
                    character = strText.charAt (i);
                } while ( i < nLength - 2  &&  Character.isLetterOrDigit(character) );

                strLine = strText.substring ( nBegin, i );
                nLineWidth = fontMetrics.stringWidth ( strLine );
                if ( i >= nLength - 1  &&  nLineWidth < nPreferredWidth )
                    nEnd = i - 1;
                if ( character == '\n"  ||  character == Character.LINE_SEPARATOR )
                    nEnd = i;
            } while ( i < nLength - 1  &&  nLineWidth < nPreferredWidth
                                    &&  character != '\n"
                                    &&  character != Character.LINE_SEPARATOR );

            do {
                nEnd++;
                character = strText.charAt ( nEnd );
            } while ( nEnd < nLength - 1  &&  Character.isSpaceChar(character) );
            i = nEnd;
            if ( i == nLength - 1 )
                i++;

            character = strText.charAt ( i - 1 );
            if ( character == '\n"  ||  character == Character.LINE_SEPARATOR )
                strLine = strText.substring ( nBegin, i - 1 );
            else
                strLine = strText.substring ( nBegin, i );
            g.drawString ( strLine, insetsBorder.left, nOffsetY );
            nOffsetY += nLineHeight;
        }
    
public voidsetPreferredWidth(int nPreferredWidth)

        this.nPreferredWidth = nPreferredWidth;
    
public voidsetText(java.lang.String strText)

        this.strText = strText;
        this.repaint();
    
public voidupdate(java.awt.Graphics graphics)

        int         nWidth = 0;
        int         nHeight = 0;
        Dimension   dim;
        Graphics    graphicsNew;


        dim = this.getSize ();
        if ( imageBuffer != null ) {
            nWidth = imageBuffer.getWidth ( this );
            nHeight = imageBuffer.getHeight ( this );
        }
        if ( imageBuffer == null  ||  nWidth < dim.width  ||  nHeight < dim.height ) {
            nWidth = Math.max ( nWidth, dim.width );
            nHeight = Math.max ( nHeight, dim.height );
            imageBuffer = this.createImage ( nWidth, nHeight );
        }

        graphicsNew = imageBuffer.getGraphics ();
        if ( graphicsNew == null )
            graphicsNew = graphics;

        super.update ( graphicsNew );
        
        if ( graphicsNew != graphics )
            graphics.drawImage ( imageBuffer, 0, 0, dim.width, dim.height, this );