Java Code Examples for com.codename1.ui.TextArea#getLines()

The following examples show how to use com.codename1.ui.TextArea#getLines() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BlackBerryImplementation.java    From CodenameOne with GNU General Public License v2.0 4 votes vote down vote up
public void run() {
    // prevent a race condition by copying the global scope
    TextArea t = lightweightEdit;
    if (t != null) {
        Dimension d = t.getPreferredSize();
        t.setText(text);
        Dimension current = t.getPreferredSize();

        Form f = t.getComponentForm();

        // allows the lightweight text area to grow to fill up the screen
        if (d.getHeight() != current.getHeight() || d.getWidth() != current.getWidth()) {
            f.getComponentForm().revalidate();
        }

        int currentLineLength = 0;
        int numOfChars = 0;
        int currentY = 0;
        while (numOfChars <= cursorLocation && currentY < t.getLines()) {
            String currentLine = t.getTextAt(currentY);
            currentLineLength = currentLine.length();
            if (numOfChars + currentLineLength < text.length()
                    && (text.charAt(numOfChars + currentLineLength) == '\n'
                    || text.charAt(numOfChars + currentLineLength) == ' ')) {
                currentLineLength++;
            }
            numOfChars += currentLineLength;
            currentY++;
        }
        int cursorY = Math.max(0, currentY - 1);
        com.codename1.ui.Font textFont = t.getStyle().getFont();
        int rowsGap = t.getRowsGap();
        int lineHeight = textFont.getHeight() + rowsGap;
        t.scrollRectToVisible(t.getScrollX(), cursorY * lineHeight, t.getWidth(), lineHeight, t);
        
        app.invokeLater(new Runnable() {
            public void run() {
                canvas.updateRIMLayout();
            }
        });
    }
}
 
Example 2
Source File: DefaultLookAndFeel.java    From CodenameOne with GNU General Public License v2.0 4 votes vote down vote up
@Override
public Spans calculateTextAreaSpan(TextSelection sel, TextArea ta) {
    Spans out = sel.newSpans();
    //setFG(g, ta);
    //Span out = sel.newSpan(ta);
    int line = ta.getLines();
    //int oX = g.getClipX();
    //int oY = g.getClipY();
    //int oWidth = g.getClipWidth();
    //int oHeight = g.getClipHeight();
    Font f = ta.getStyle().getFont();
    int fontHeight = f.getHeight();

    int align = reverseAlignForBidi(ta);

    int leftPadding = ta.getStyle().getPaddingLeft(ta.isRTL());
    int rightPadding = ta.getStyle().getPaddingRight(ta.isRTL());
    int topPadding = ta.getStyle().getPaddingTop();
    switch (ta.getVerticalAlignment()) {
        case Component.CENTER :
            topPadding += Math.max(0, (ta.getInnerHeight() - (ta.getRowsGap() + fontHeight) * line)/2);
            break;
        case Component.BOTTOM :
            topPadding += Math.max(0, (ta.getInnerHeight() - (ta.getRowsGap() + fontHeight) * line));
    }
    //boolean shouldBreak = false;
    int posOffset = 0;
    int lastRowBottom = 0;
    for (int i = 0; i < line; i++) {
        Span rowSpan = sel.newSpan(ta);
        int x = ta.getX() + leftPadding;
        int y = ta.getY() +  topPadding +
                (ta.getRowsGap() + fontHeight) * i;
        int adjustedY = Math.max(y, lastRowBottom);
        int yDiff = adjustedY - y;
        y = adjustedY;
        
        //if(Rectangle.intersects(x, y, ta.getWidth(), fontHeight, oX, oY, oWidth, oHeight)) {
            
            String rowText = (String) ta.getTextAt(i);
            //display ******** if it is a password field
            String displayText = "";
            if ((ta.getConstraint() & TextArea.PASSWORD) != 0) {
                int rlen = rowText.length();
                for (int j = 0; j < rlen; j++) {
                    displayText += passwordChar;
                }
            } else {
                displayText = rowText;
            }
            posOffset = ta.getText().indexOf(rowText, posOffset);
            switch(align) {
                case Component.RIGHT:
            		x = ta.getX() + ta.getWidth() - rightPadding - f.stringWidth(displayText);
                    break;
                case Component.CENTER:
                    x+= (ta.getWidth()-leftPadding-rightPadding-f.stringWidth(displayText))/2;
                    break;
            }
            //int nextY = ta.getY() +  topPadding + (ta.getRowsGap() + fontHeight) * (i + 2);
            //if this is the last line to display and there is more content and isEndsWith3Points() is true
            //add "..." at the last row
            if(ta.isEndsWith3Points() && ta.getGrowLimit() == (i + 1) && ta.getGrowLimit() != line){
                if(displayText.length() > 3){
                    displayText = displayText.substring(0, displayText.length() - 3);
                }
                //g.drawString(displayText + "...", x, y ,ta.getStyle().getTextDecoration()); 
                append(sel, ta, rowSpan, displayText + "...", f, posOffset, x, y, getSelectionHeight(f) - yDiff);
                lastRowBottom = rowSpan.getBounds().getY() + rowSpan.getBounds().getHeight();
                rowSpan = rowSpan.translate(ta.getAbsoluteX() - sel.getSelectionRoot().getAbsoluteX() - ta.getX(), ta.getAbsoluteY() - sel.getSelectionRoot().getAbsoluteY() - ta.getY());
                out.add(rowSpan);
                return out;
            }else{            
                //g.drawString(displayText, x, y ,ta.getStyle().getTextDecoration());
                append(sel, ta, rowSpan, displayText, f, posOffset, x, y, getSelectionHeight(f) - yDiff);
                lastRowBottom = rowSpan.getBounds().getY() + rowSpan.getBounds().getHeight();
                rowSpan = rowSpan.translate(ta.getAbsoluteX() - sel.getSelectionRoot().getAbsoluteX() - ta.getX(), ta.getAbsoluteY() - sel.getSelectionRoot().getAbsoluteY() - ta.getY());
                out.add(rowSpan);
            }
            posOffset += displayText.length();
            //shouldBreak = true;
        //}else{
        //    if(shouldBreak){
        //        break;
        //    }
        //}
    }
    return out;
}
 
Example 3
Source File: DefaultLookAndFeel.java    From CodenameOne with GNU General Public License v2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
public void drawTextArea(Graphics g, TextArea ta) {
    setFG(g, ta);
    int line = ta.getLines();
    int oX = g.getClipX();
    int oY = g.getClipY();
    int oWidth = g.getClipWidth();
    int oHeight = g.getClipHeight();
    Font f = ta.getStyle().getFont();
    int fontHeight = f.getHeight();

    int align = reverseAlignForBidi(ta);

    int leftPadding = ta.getStyle().getPaddingLeft(ta.isRTL());
    int rightPadding = ta.getStyle().getPaddingRight(ta.isRTL());
    int topPadding = ta.getStyle().getPaddingTop();
    switch (ta.getVerticalAlignment()) {
        case Component.CENTER :
            topPadding += Math.max(0, (ta.getInnerHeight() - ta.getRowsGap()*(line-1) - fontHeight* line)/2);
            break;
        case Component.BOTTOM :
            topPadding += Math.max(0, (ta.getInnerHeight() - ta.getRowsGap()*(line-1) - fontHeight* line));
    }
    boolean shouldBreak = false;
    
    for (int i = 0; i < line; i++) {
        int x = ta.getX() + leftPadding;
        int y = ta.getY() +  topPadding +
                (ta.getRowsGap() + fontHeight) * i;
        if(Rectangle.intersects(x, y, ta.getWidth(), fontHeight, oX, oY, oWidth, oHeight)) {
            
            String rowText = (String) ta.getTextAt(i);
            //display ******** if it is a password field
            String displayText = "";
            if ((ta.getConstraint() & TextArea.PASSWORD) != 0) {
                int rlen = rowText.length();
                for (int j = 0; j < rlen; j++) {
                    displayText += passwordChar;
                }
            } else {
                displayText = rowText;
            }

            switch(align) {
                case Component.RIGHT:
            		x = ta.getX() + ta.getWidth() - rightPadding - f.stringWidth(displayText);
                    break;
                case Component.CENTER:
                    x+= (ta.getWidth()-leftPadding-rightPadding-f.stringWidth(displayText))/2;
                    break;
            }
            int nextY = ta.getY() +  topPadding + (ta.getRowsGap() + fontHeight) * (i + 2);
            //if this is the last line to display and there is more content and isEndsWith3Points() is true
            //add "..." at the last row
            if(ta.isEndsWith3Points() && ta.getGrowLimit() == (i + 1) && ta.getGrowLimit() != line){
                if(displayText.length() > 3){
                    displayText = displayText.substring(0, displayText.length() - 3);
                }
                g.drawString(displayText + "...", x, y ,ta.getStyle().getTextDecoration());                
                return;
            }else{            
                g.drawString(displayText, x, y ,ta.getStyle().getTextDecoration());
            }
            shouldBreak = true;
        }else{
            if(shouldBreak){
                break;
            }
        }
    }
}
 
Example 4
Source File: DefaultLookAndFeel.java    From CodenameOne with GNU General Public License v2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
public Dimension getTextAreaSize(TextArea ta, boolean pref) {
    int prefW = 0;
    int prefH = 0;
    Style style = ta.getStyle();
    Font f = style.getFont();

    //if this is a text field the preferred size should be the text width
    if (ta.getRows() == 1) {
        prefW = f.stringWidth(ta.getText());
    } else {
        prefW = f.charWidth(TextArea.getWidestChar()) * ta.getColumns();
    }
    int rows;
    if(pref) {
        rows = ta.getActualRows();
    } else {
        rows = ta.getLines();
    }
    prefH = (f.getHeight() + ta.getRowsGap()) * rows;
    if(!ta.isActAsLabel()) {
        int columns = ta.getColumns();
        String str = "";
        for (int iter = 0; iter < columns; iter++) {
            str += TextArea.getWidestChar();
        }
        if(columns > 0) {
            prefW = Math.max(prefW, f.stringWidth(str));
        }
    }
    prefH = Math.max(prefH, rows * f.getHeight());

    prefW += style.getPaddingRightNoRTL() + style.getPaddingLeftNoRTL();
    prefH += style.getPaddingTop() + style.getPaddingBottom();
    if(style.getBorder() != null) {
        prefW = Math.max(style.getBorder().getMinimumWidth(), prefW);
        prefH = Math.max(style.getBorder().getMinimumHeight(), prefH);
    }
    if(isBackgroundImageDetermineSize() && style.getBgImage() != null) {
        prefW = Math.max(style.getBgImage().getWidth(), prefW);
        prefH = Math.max(style.getBgImage().getHeight(), prefH);
    }

    return new Dimension(prefW, prefH);
}