idlviewfactory.java

来自「UCS (Ultra Corba Simulator) is one more 」· Java 代码 · 共 437 行 · 第 1/2 页

JAVA
437
字号
                    wordIndex = wordIndex + scannedCharLength;
                } else if (isSingleLineCommentStart(content, wordIndex) && !inComment) {
                    int scannedCommentLength = 2; // scanSingleLineComment(content,
                    // wordIndex);

                    Segment text = getLineBuffer();
                    getDocument().getText(startPosition + wordIndex, scannedCommentLength, text);
                    g.setColor((Color) host
                            .getClientProperty(IdlTextEditor.SINGLE_LINE_COMMENT_COLOR));
                    x = Utilities.drawTabbedText(text, x, y, g, this, startPosition + wordIndex);
                    wordIndex = wordIndex + scannedCommentLength;
                    inSingleComment = true;
                } else if (isSingleLineCommentEnd(content, wordIndex) && inSingleComment) {
                    Segment text = getLineBuffer();
                    getDocument().getText(startPosition + wordIndex, 1, text);
                    g.setColor((Color) host
                            .getClientProperty(IdlTextEditor.SINGLE_LINE_COMMENT_COLOR));
                    x = Utilities.drawTabbedText(text, x, y, g, this, startPosition + wordIndex);
                    wordIndex = wordIndex + 1;
                    inSingleComment = false;
                } else if (isMultiLineCommentEnd(content, wordIndex) && inComment) {
                    Segment text = getLineBuffer();
                    getDocument().getText(startPosition + wordIndex, 2, text);
                    g.setColor((Color) host
                            .getClientProperty(IdlTextEditor.MULTI_LINE_COMMENT_COLOR));
                    x = Utilities.drawTabbedText(text, x, y, g, this, startPosition + wordIndex);
                    wordIndex = wordIndex + 2;
                    inComment = false;
                } else if (isMultiLineCommentStart(content, wordIndex) && !inSingleComment) {
                    int scannedCommentLength = 2;// scanMultiLineComment(content,
                    // wordIndex);
                    Segment text = getLineBuffer();
                    getDocument().getText(startPosition + wordIndex, scannedCommentLength, text);
                    g.setColor((Color) host
                            .getClientProperty(IdlTextEditor.MULTI_LINE_COMMENT_COLOR));
                    x = Utilities.drawTabbedText(text, x, y, g, this, startPosition + wordIndex);
                    wordIndex = wordIndex + scannedCommentLength;
                    inComment = true;
                } else if (isOperator(indexedChar) && !inComment) {
                    Color color = (Color) host.getClientProperty(IdlTextEditor.OPERATOR_COLOR);

                    Segment text = getLineBuffer();
                    getDocument().getText(startPosition + wordIndex, 1, text);
                    g.setColor(color);
                    x = Utilities.drawTabbedText(text, x, y, g, this, startPosition + wordIndex);

                    wordIndex = wordIndex + 1;
                } else {
                    Color color;
                    if (inComment) {
                        color = (Color) host
                                .getClientProperty(IdlTextEditor.MULTI_LINE_COMMENT_COLOR);
                    } else if (inSingleComment) {
                        color = (Color) host
                                .getClientProperty(IdlTextEditor.SINGLE_LINE_COMMENT_COLOR);
                    } else {
                        color = Color.BLACK;
                    }

                    Segment text = getLineBuffer();
                    getDocument().getText(startPosition + wordIndex, 1, text);
                    g.setColor(color);
                    x = Utilities.drawTabbedText(text, x, y, g, this, startPosition + wordIndex);
                    wordIndex++;
                }
            }
            SegmentCache.releaseSharedSegment(content);
            return x;
        }

        private Color getColorForTokenType(String tokenType) {
            JTextComponent host = (JTextComponent) getContainer();
            if (tokenType == KEYWORD)
                return (Color) host.getClientProperty(IdlTextEditor.KEYWORD_COLOR);
            else if (tokenType == TYPE) {
                return (Color) host.getClientProperty(IdlTextEditor.TYPE_COLOR);
            } else
                return host.getForeground();
        }

        private boolean isSingleLineCommentStart(SegmentExt string, int index) {
            if (string.charAt(index) == '/' && index + 1 < string.length()
                    && string.charAt(index + 1) == '/')
                return true;

            return false;
        }

        private boolean isSingleLineCommentEnd(SegmentExt string, int index) {
            if (string.charAt(index) == '\n')
                return true;

            return false;
        }

        private int scanSingleLineComment(SegmentExt string, int index) {
            int commentLength = 0;
            for (commentLength = 0; commentLength < (string.length() - index); commentLength++) {
                char commentChar = string.charAt(index + commentLength);
                if (commentChar == '\n')
                    break;
            }
            return commentLength;
        }

        private boolean isMultiLineCommentStart(SegmentExt string, int index) {
            if (string.charAt(index) == '/' && index + 1 < string.length()
                    && string.charAt(index + 1) == '*')
                return true;

            return false;
        }

        private boolean isMultiLineCommentEnd(SegmentExt string, int index) {
            if (string.charAt(index) == '*' && index + 1 < string.length()
                    && string.charAt(index + 1) == '/')
                return true;

            return false;
        }

        private Element getRowAt(int offset) {
            Element element = getDocument().getDefaultRootElement();
            int rowNumber = element.getElementIndex(offset);
            return element.getElement(rowNumber);
        }

        private int scanMultiLineComment(SegmentExt string, int index) {
            int commentLength = 0;
            boolean starFound = false;
            for (commentLength = 0; commentLength < (string.length() - index); commentLength++) {
                char commentChar = string.charAt(index + commentLength);
                if (starFound && commentChar == '/') {
                    commentLength++;
                    break;
                }
                starFound = false;
                if (commentChar == '\n')
                    break;
                else if (commentChar == '*')
                    starFound = true;
            }
            return commentLength;
        }

        private int scanStringLiteral(SegmentExt string, int index) {
            int stringLength = 1;
            boolean backslash = false;
            for (stringLength = 1; stringLength < (string.length() - index); stringLength++) {
                char stringChar = string.charAt(index + stringLength);
                if (stringChar == '\\')
                    backslash = true;
                else if (stringChar == '\n')
                    break;
                else if (stringChar == '"' && !backslash) {
                    stringLength++;
                    break;
                } else if (backslash)
                    backslash = false;
            }
            return stringLength;
        }

        private int scanCharLiteral(SegmentExt string, int index) {
            int charLength = 1;
            boolean backslash = false;
            for (charLength = 1; charLength < (string.length() - index); charLength++) {
                char charChar = string.charAt(index + charLength);
                if (charChar == '\\')
                    backslash = true;
                else if (charChar == '\n')
                    break;
                else if (charChar == '\'' && !backslash) {
                    charLength++;
                    break;
                } else if (backslash)
                    backslash = false;
            }
            return charLength;
        }

        private String scanIdentifier(Segment stringSegment, int index) {
            String string = new String(stringSegment.array, stringSegment.offset,
                    stringSegment.count);
            int identifierLength = 0;
            if (!Character.isJavaIdentifierStart(string.charAt(index)))
                return "";
            else
                for (identifierLength = 1; identifierLength < (string.length() - index); identifierLength++) {
                    char identifierChar = string.charAt(index + identifierLength);
                    if (!Character.isJavaIdentifierPart(string.charAt(index + identifierLength)))
                        break;
                }

            return string.substring(index, index + identifierLength);
        }

        private String scanNumericLiteral(Segment stringSegment, int index) {
            String string = new String(stringSegment.array, stringSegment.offset,
                    stringSegment.count);
            int identifierLength = 0;
            for (identifierLength = 0; identifierLength < (string.length() - index); identifierLength++) {
                char identifierChar = string.charAt(index + identifierLength);
                if (!Character.isDigit(string.charAt(index + identifierLength)))
                    break;
            }
            return string.substring(index, index + identifierLength);
        }

        private boolean isOperator(char c) {
            return (c == '-' || c == '+' || c == '*' || c == '/' || c == '<' || c == '>'
                    || c == '!' || c == '~' || c == '%' || c == '^' || c == '&' || c == '|'
                    || c == '=' || c == '.');
        }
    }
}
/* EOF */

⌨️ 快捷键说明

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