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

The following examples show how to use com.codename1.ui.TextArea#getTextAt() . 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: DefaultLookAndFeel.java    From CodenameOne with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Similar to getText() but works properly with password fields
 */
protected String getTextFieldString(TextArea ta) {
    String txt = ta.getText();
    String text;
    if(ta.isSingleLineTextArea()){
        text = txt;
    }else{
        text = (String) ta.getTextAt(ta.getCursorY());
        if(ta.getCursorPosition() + text.length() < txt.length()){
            char c = txt.charAt(ta.getCursorPosition() + text.length());
            if(c == '\n'){
                text += "\n";
            }else if(c == ' '){
                text += " ";            
            }
        }
    }
    
    String displayText = "";
    if ((ta.getConstraint() & TextArea.PASSWORD) != 0) {
        // show the last character in a password field
        if (ta.isPendingCommit()) {
            if (text.length() > 0) {
                int tlen = text.length();
                for (int j = 0; j < tlen - 1; j++) {
                    displayText += passwordChar;
                }
                displayText += text.charAt(text.length() - 1);
            }
        } else {
            for (int j = 0; j < text.length(); j++) {
                displayText += passwordChar;
            }
        }
    } else {
        displayText = text;
    }
    return displayText;
}
 
Example 2
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 3
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 4
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;
            }
        }
    }
}