textboxlfimpl.java

来自「This is a resource based on j2me embedde」· Java 代码 · 共 711 行 · 第 1/2 页

JAVA
711
字号
                    TextFieldSkin.IMAGE_BG_UE);            } else {                CGraphicsUtil.drawDropShadowBox(g, 0, 0, width, height,                    TextFieldSkin.COLOR_BORDER_UE,                    TextFieldSkin.COLOR_BORDER_SHD_UE,                     TextFieldSkin.COLOR_BG_UE);            }        }        // We need to translate by 1 more pixel horizontally         // to reserve space for cursor in the empty textfield        g.translate(TextFieldSkin.PAD_H + 1, TextFieldSkin.PAD_V);        // Input session should be retrieved from the current Display        TextInputSession is = getInputSession();        paint(g, tf.buffer,              is.getPendingChar(),              tf.constraints,               ScreenSkin.FONT_INPUT_TEXT,               (editable ? TextFieldSkin.COLOR_FG : TextFieldSkin.COLOR_FG_UE),               width - (2 * (TextFieldSkin.PAD_H)), height, 0,                Text.NORMAL, cursor, myInfo);         if (!scrollInitialized) {            setVerticalScroll();            scrollInitialized = true;        }                g.translate(-(TextFieldSkin.PAD_H + 1), -(TextFieldSkin.PAD_V));        if (usePreferredX) {            cursor.preferredX = cursor.x +                (myInfo.lineStart[myInfo.cursorLine] == cursor.index ?                 ScreenSkin.FONT_INPUT_TEXT.charWidth(                                                      tf.buffer.charAt(cursor.index)) :                 0);        }                g.translate(-TextFieldSkin.BOX_MARGIN, -TextFieldSkin.BOX_MARGIN);    }    /**     * Get character index at the pointer position     *     * @param x pointer x coordinate     * @param y pointer y coordinate     * @return the character index     */    protected int getIndexAt(int x, int y) {        x -= contentBounds[X] +            TextFieldSkin.BOX_MARGIN +            TextFieldSkin.PAD_H;        y -= contentBounds[Y] +            TextFieldSkin.BOX_MARGIN +            TextFieldSkin.PAD_V;        int id = -1;        // the pointer is inside of the content         if (x >= 0 && y >= 0) {                        int numLines = myInfo.topVis + y / ScreenSkin.FONT_INPUT_TEXT.getHeight();            id = tf.buffer.length();                        // the cursor has to be moved to the symbol the pointer is clicked at            // if pointer is out of text just move the cursor at the last text position            // if pointer is out of line just move the cursor at the last line position            if (numLines < myInfo.numLines) {                char[] data = tf.buffer.toCharArray();                int i = 1;                int startId = myInfo.lineStart[numLines];                for (; i <= myInfo.lineEnd[numLines] - startId; i++) {                    if (x <= ScreenSkin.FONT_INPUT_TEXT.charsWidth(data, startId, i)) {                        break;                    }                }                id = startId + i - 1;            }        }        return id;    }    /**     * Used internally to set the vertical scroll position     */    boolean setVerticalScroll() {        ScreenLFImpl lf = null;        if (tf != null &&            tf.owner != null &&            (lf = (ScreenLFImpl)tf.owner.getLF()) != null &&            myInfo != null) {            return lf.setVerticalScroll(myInfo.getScrollPosition(),                                 myInfo.getScrollProportion());                  }        return false;    }    /**     * Scroll content inside of the form.     * @param scrollType scrollType. Scroll type can be one of the following     * @see ScrollBarLayer.SCROLL_NONE      * @see ScrollBarLayer.SCROLL_PAGEUP     * @see ScrollBarLayer.SCROLL_PAGEDOWN     * @see ScrollBarLayer.SCROLL_LINEUP     * @see ScrollBarLayer.SCROLL_LINEDOWN or     * @see ScrollBarLayer.SCROLL_THUMBTRACK     * @param thumbPosition     */    void uCallScrollContent(int scrollType, int thumbPosition) {        if (Logging.REPORT_LEVEL <= Logging.INFORMATION) {            Logging.report(Logging.INFORMATION,                            LogChannels.LC_HIGHUI,                           "TextBoxLFImpl.uCallScrollContent scrollType=" + scrollType +                            " thumbPosition=" + thumbPosition);         }                switch (scrollType) {            case ScrollBarLayer.SCROLL_PAGEUP:                uScrollViewport(Canvas.UP);                break;            case ScrollBarLayer.SCROLL_PAGEDOWN:                uScrollViewport(Canvas.DOWN);                break;            case ScrollBarLayer.SCROLL_LINEUP:                uScrollByLine(Canvas.UP);                break;            case ScrollBarLayer.SCROLL_LINEDOWN:                uScrollByLine(Canvas.DOWN);                break;            case ScrollBarLayer.SCROLL_THUMBTRACK:                uScrollAt(thumbPosition);                break;            default:                break;        }    }    /**     * Perform a page flip in the given direction. This method will     * attempt to scroll the view to show as much of the next page     * as possible. It uses the locations and bounds of the items on     * the page to best determine a new location - taking into account     * items which may lie on page boundaries as well as items which     * may span several pages.     *     * @param dir the direction of the flip, either DOWN or UP     */    protected void uScrollViewport(int dir) {        int lines = myInfo.scrollByPage(dir == Canvas.UP ?                                        TextInfo.BACK : TextInfo.FORWARD);        if (lines != 0) {            if (editable) {                // accept the word if the PTI is currently enabled                acceptPTI();                cursor.y += ScreenSkin.FONT_INPUT_TEXT.getHeight() * lines;                cursor.option = Text.PAINT_GET_CURSOR_INDEX;            }            updateTextInfo();        }    }    /**     * Perform a line scrolling in the given direction. This method will     * attempt to scroll the view to show next/previous line.     *     * @param dir the direction of the flip, either DOWN or UP     */    protected void uScrollByLine(int dir) {        int oldTopVis = myInfo.topVis;        if (myInfo.scroll(dir == Canvas.UP ? TextInfo.BACK : TextInfo.FORWARD)) {            if (editable) {                // accept the word if the PTI is currently enabled                acceptPTI();                cursor.y += (myInfo.topVis - oldTopVis) * ScreenSkin.FONT_INPUT_TEXT.getHeight();                cursor.option = Text.PAINT_GET_CURSOR_INDEX;            }            updateTextInfo();        }    }    /**     * Perform a scrolling at the given position.      * @param context position       */    protected void uScrollAt(int position) {        int oldTopVis = myInfo.topVis;        myInfo.topVis  = ((myInfo.height - myInfo.visLines * ScreenSkin.FONT_INPUT_TEXT.getHeight()) *                          position / 100) / ScreenSkin.FONT_INPUT_TEXT.getHeight();                if (myInfo.topVis < 0) {            myInfo.topVis = 0;        } else if (myInfo.topVis + myInfo.visLines > myInfo.numLines) {            myInfo.topVis = myInfo.numLines - myInfo.visLines;        }                if (myInfo.topVis != oldTopVis) {            if (editable) {                // accept the word if the PTI is currently enabled                acceptPTI();                cursor.y += (myInfo.topVis - oldTopVis) * ScreenSkin.FONT_INPUT_TEXT.getHeight();                cursor.option = Text.PAINT_GET_CURSOR_INDEX;            }            myInfo.isModified = myInfo.scrollY = true;            updateTextInfo();        }    }    /**     * Move the text cursor in the given direction     *     * @param dir direction to move     * @return true if the cursor was moved, false otherwise     */    boolean moveCursor(int dir) {        boolean keyUsed = false;        switch (dir) {	case Canvas.LEFT:	    if (editable) {            keyClicked(dir);            if (ScreenSkin.RL_DIRECTION) {                if (cursor.index < tf.buffer.length()) {                    cursor.index++;                }            } else {                if (cursor.index > 0) {                    cursor.index--;                }            }            cursor.option = Text.PAINT_USE_CURSOR_INDEX;            myInfo.isModified = myInfo.scrollX = keyUsed = true;        } else {            if (ScreenSkin.RL_DIRECTION) {                keyUsed = myInfo.scroll(TextInfo.FORWARD);            } else {                keyUsed = myInfo.scroll(TextInfo.BACK);            }        }	    break;	case Canvas.RIGHT:	    if (editable) {            keyClicked(dir);            if (ScreenSkin.RL_DIRECTION) {                if (cursor.index > 0) {                    cursor.index--;                }            } else {                if (cursor.index < tf.buffer.length()) {                    cursor.index++;                }            }            cursor.option = Text.PAINT_USE_CURSOR_INDEX;            myInfo.isModified = myInfo.scrollX = keyUsed = true;        } else {            if (ScreenSkin.RL_DIRECTION) {                 keyUsed = myInfo.scroll(TextInfo.BACK);            } else {                keyUsed = myInfo.scroll(TextInfo.FORWARD);            }        }	    break;	    	case Canvas.UP:	    if (editable) {            keyClicked(dir);            cursor.y -= ScreenSkin.FONT_INPUT_TEXT.getHeight();            if (cursor.y > 0) {                cursor.option = Text.PAINT_GET_CURSOR_INDEX;                myInfo.isModified = myInfo.scrollY = keyUsed = true;            } else {                 cursor.y += ScreenSkin.FONT_INPUT_TEXT.getHeight();            }	    } else {            keyUsed = myInfo.scroll(TextInfo.BACK);	    }        break;                case Canvas.DOWN:            if (editable) {                keyClicked(dir);                cursor.y += ScreenSkin.FONT_INPUT_TEXT.getHeight();                if (cursor.y <= myInfo.height) {                    cursor.option = Text.PAINT_GET_CURSOR_INDEX;                    myInfo.isModified = myInfo.scrollY = keyUsed = true;                } else {                    cursor.y -= ScreenSkin.FONT_INPUT_TEXT.getHeight();                }            } else {                keyUsed = myInfo.scroll(TextInfo.FORWARD);            }            break;        default:            // no-op            break;        }                updateTextInfo();                return keyUsed;    }         /**     * Called by the system to notify this Item it is being shown     *     * <p>The default implementation of this method updates     * the 'visible' state     */    void lCallShowNotify() {        super.lCallShowNotify();        this.scrollInitialized = false;         }        /**     * This is a utility function to calculate the anchor point     * for the InputModeIndicator layer. Override TextFieldLFImpl     * version for effeciency.     * @return anchor (x, y, w, h)     */    protected int[] getInputModeAnchor() {        ScreenLFImpl sLF = (ScreenLFImpl)tf.owner.getLF();                int space = TextFieldSkin.BOX_MARGIN                    + Font.getDefaultFont().getHeight();                return new int[] {            sLF.viewport[WIDTH] - TextFieldSkin.BOX_MARGIN - 4                + getCurrentDisplay().getWindow().getBodyAnchorX(),            getCurrentDisplay().getWindow().getBodyAnchorY(),            sLF.viewport[HEIGHT] - space - 4,                                space};    }    /**     * Returns true if the keyCode is used as 'enter' (user types in \n)     * ('select' plays the role of 'enter' in some input modes).     *     * @param keyCode key code     * @return true if key code is the one for newline, false otherwise     */    public boolean isNewlineKey(int keyCode) {        return EventConstants.SYSTEM_KEY_SELECT ==            KeyConverter.getSystemKey(keyCode);    }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?