⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 javaviewfactory.java

📁 UCS (Ultra Corba Simulator) is one more powerful corba client/servant simulator tool than other simi
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/**
 * (c) 2007 UCS_2008
 *
 * Project    UCS (Ultra Corba Simulator)
 * Subproject CorbaMNQ
 * File       JavaViewFactory.java
 * Created on Sep 13, 2007 by ucs_2008
 *
 * History:
 * Date(Y.M.D) User           Reason (plus CR, LM, Fault number)
 *
 */

package com.corba.mnq.ui.text.java;

import com.corba.mnq.ui.text.SegmentExt;

import java.awt.Color;
import java.awt.Graphics;

import javax.swing.text.BadLocationException;
import javax.swing.text.Element;
import javax.swing.text.JTextComponent;
import javax.swing.text.PlainView;
import javax.swing.text.Segment;
import javax.swing.text.Utilities;
import javax.swing.text.View;
import javax.swing.text.ViewFactory;

import java.util.HashMap;
import java.util.Map;

/**
 * name: "JavaViewFactory"
 * 
 * @author ucs_2008
 */
public class JavaViewFactory implements ViewFactory {

    public static final String KEYWORD = "keyword";

    public static final String OPERATOR = "operator";

    // public static final String CLASS = "class"; not used anymore
    // since
    // version 1.7.3

    public static final String RESERVED_WORD = "reserved_word";

    private static Map javaSpecialWordsMap;

    public JavaViewFactory() {
        super();
        /**
         * TODO: bisogna invocare un metodo che setta la lista delle
         * classi nel classpath, al momento della colorazione delle
         * parole in checkTextToComment si JavaDocument (bisogna
         * valutare se si deve passare qualcosa di EJEArea in
         * JavaDocument o viceversa)
         */
        // URL classListURL =
        // getClass().getResource("/resources/classlist.txt");
        // if (keywordMap == null)
        // keywordMap = getJavaSpecialWordsMap(classListURL);
        getJavaSpecialWordsMap();
    }

    public static Map getJavaSpecialWordsMap() {
        javaSpecialWordsMap = new HashMap();
        javaSpecialWordsMap.put("abstract", KEYWORD);
        javaSpecialWordsMap.put("assert", KEYWORD);
        javaSpecialWordsMap.put("boolean", KEYWORD);
        javaSpecialWordsMap.put("break", KEYWORD);
        javaSpecialWordsMap.put("byte", KEYWORD);
        javaSpecialWordsMap.put("case", KEYWORD);
        javaSpecialWordsMap.put("catch", KEYWORD);
        javaSpecialWordsMap.put("char", KEYWORD);
        javaSpecialWordsMap.put("class", KEYWORD);
        javaSpecialWordsMap.put("const", RESERVED_WORD);
        javaSpecialWordsMap.put("continue", KEYWORD);
        javaSpecialWordsMap.put("default", KEYWORD);
        javaSpecialWordsMap.put("do", KEYWORD);
        javaSpecialWordsMap.put("double", KEYWORD);
        javaSpecialWordsMap.put("else", KEYWORD);
        String version = System.getProperty("java.version");
        if (version.startsWith("1.5")) {
            javaSpecialWordsMap.put("enum", KEYWORD);
            javaSpecialWordsMap.put(((char) 64 + "interface"), KEYWORD);
            // is simboli non funzionano!
        }
        javaSpecialWordsMap.put("extends", KEYWORD);
        javaSpecialWordsMap.put("false", KEYWORD);
        javaSpecialWordsMap.put("final", KEYWORD);
        javaSpecialWordsMap.put("finally", KEYWORD);
        javaSpecialWordsMap.put("float", KEYWORD);
        javaSpecialWordsMap.put("for", KEYWORD);
        javaSpecialWordsMap.put("goto", RESERVED_WORD);
        javaSpecialWordsMap.put("if", KEYWORD);
        javaSpecialWordsMap.put("implements", KEYWORD);
        javaSpecialWordsMap.put("import", KEYWORD);
        javaSpecialWordsMap.put("instanceof", KEYWORD);
        javaSpecialWordsMap.put("int", KEYWORD);
        javaSpecialWordsMap.put("interface", KEYWORD);
        javaSpecialWordsMap.put("long", KEYWORD);
        javaSpecialWordsMap.put("native", KEYWORD);
        javaSpecialWordsMap.put("new", KEYWORD);
        javaSpecialWordsMap.put("null", KEYWORD);
        javaSpecialWordsMap.put("package", KEYWORD);
        javaSpecialWordsMap.put("private", KEYWORD);
        javaSpecialWordsMap.put("protected", KEYWORD);
        javaSpecialWordsMap.put("public", KEYWORD);
        javaSpecialWordsMap.put("return", KEYWORD);
        javaSpecialWordsMap.put("short", KEYWORD);
        javaSpecialWordsMap.put("static", KEYWORD);
        javaSpecialWordsMap.put("strictfp", KEYWORD);
        javaSpecialWordsMap.put("super", KEYWORD);
        javaSpecialWordsMap.put("switch", KEYWORD);
        javaSpecialWordsMap.put("synchronized", KEYWORD);
        javaSpecialWordsMap.put("this", KEYWORD);
        javaSpecialWordsMap.put("throw", KEYWORD);
        javaSpecialWordsMap.put("throws", KEYWORD);
        javaSpecialWordsMap.put("transient", KEYWORD);
        javaSpecialWordsMap.put("true", KEYWORD);
        javaSpecialWordsMap.put("try", KEYWORD);
        javaSpecialWordsMap.put("void", KEYWORD);
        javaSpecialWordsMap.put("volatile", KEYWORD);
        javaSpecialWordsMap.put("while", KEYWORD);

        /*
         * if (classListURL != null) try { BufferedReader in = new
         * BufferedReader(new InputStreamReader(
         * classListURL.openStream())); String className; while
         * ((className = in.readLine()) != null)
         * javaSpecialWordsMap.put(className, CLASS); } catch
         * (Exception e) { e.printStackTrace(); }
         */
        return javaSpecialWordsMap;
    }

    public View create(Element elem) {
        return new JavaView(elem);
    }

    class JavaView extends PlainView {

        public JavaView(Element elem) {
            super(elem);
        }

        protected int drawUnselectedText(Graphics g, int x, int y, int startOffset, int endOffset)
                throws BadLocationException {
            int length = (endOffset < getDocument().getLength() ? endOffset : getDocument()
                    .getLength())
                    - startOffset;
            return scanParagraph(g, x, y, startOffset, length);
        }

        private boolean isParagraphInComment(int startOffset) {
            if (startOffset > 0) {
                Element root = getDocument().getDefaultRootElement();
                Element previousElement = root.getElement(root.getElementIndex(startOffset - 1));
                if (previousElement.getAttributes().containsAttribute(
                        JavaDocument.MULTILINE_COMMENT, JavaDocument.MULTILINE_COMMENT))
                    return true;
            }
            return false;
        }

        private int scanParagraph(Graphics g, int x, int y, int startPosition, int length)
                throws BadLocationException {
            SegmentExt content = new SegmentExt();
            JTextComponent host = (JTextComponent) getContainer();

            getDocument().getText(startPosition, length, content);
            boolean inComment = isParagraphInComment(startPosition);
            for (int wordIndex = 0; wordIndex < content.length();) {
                char indexedChar = content.charAt(wordIndex);

                if (Character.isJavaIdentifierStart(indexedChar) && !inComment) {
                    String scannedIdentifier = scanIdentifier(content, wordIndex);
                    String tokenType = (String) javaSpecialWordsMap.get(scannedIdentifier);
                    int scannedIdentifierLength = scannedIdentifier.length();

                    Color color = getColorForTokenType(tokenType);

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

                    wordIndex = wordIndex + scannedIdentifierLength;
                } else if (Character.isDigit(indexedChar) && !inComment) {
                    String scannedNumericLiteral = scanNumericLiteral(content, wordIndex);

                    int scannedNumericLiteralLength = scannedNumericLiteral.length();

                    Color color = (Color) host
                            .getClientProperty(JavaTextEditor.NUMERIC_LITERAL_COLOR);

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

                    wordIndex = wordIndex + scannedNumericLiteralLength;
                } else if (indexedChar == '\"' && !inComment) {
                    int scannedStringLength = scanStringLiteral(content, wordIndex);

                    Segment text = getLineBuffer();
                    getDocument().getText(startPosition + wordIndex, scannedStringLength, text);
                    g.setColor((Color) host.getClientProperty(JavaTextEditor.STRING_LITERAL_COLOR));
                    x = Utilities.drawTabbedText(text, x, y, g, this, startPosition + wordIndex);

                    wordIndex = wordIndex + scannedStringLength;
                } else if (indexedChar == '\'' && !inComment) {
                    int scannedCharLength = scanCharLiteral(content, wordIndex);

                    Segment text = getLineBuffer();
                    getDocument().getText(startPosition + wordIndex, scannedCharLength, text);
                    g.setColor((Color) host.getClientProperty(JavaTextEditor.CHAR_LITERAL_COLOR));
                    x = Utilities.drawTabbedText(text, x, y, g, this, startPosition + wordIndex);

                    wordIndex = wordIndex + scannedCharLength;
                } else if (isSingleLineCommentStart(content, wordIndex) && !inComment) {
                    int scannedCommentLength = scanSingleLineComment(content, wordIndex);

                    Segment text = getLineBuffer();
                    getDocument().getText(startPosition + wordIndex, scannedCommentLength, text);
                    g.setColor((Color) host
                            .getClientProperty(JavaTextEditor.SINGLE_LINE_COMMENT_COLOR));
                    x = Utilities.drawTabbedText(text, x, y, g, this, startPosition + wordIndex);

                    wordIndex = wordIndex + scannedCommentLength;
                } else if (isMultiLineCommentEnd(content, wordIndex) && inComment) {
                    Segment text = getLineBuffer();
                    getDocument().getText(startPosition + wordIndex, 2, text);
                    g.setColor((Color) host
                            .getClientProperty(JavaTextEditor.MULTI_LINE_COMMENT_COLOR));
                    x = Utilities.drawTabbedText(text, x, y, g, this, startPosition + wordIndex);

                    inComment = false;
                    wordIndex = wordIndex + 2;
                } else if (isMultiLineCommentStart(content, wordIndex)) {
                    int scannedCommentLength = scanMultiLineComment(content, wordIndex);

                    Segment text = getLineBuffer();
                    getDocument().getText(startPosition + wordIndex, scannedCommentLength, text);
                    g.setColor((Color) host
                            .getClientProperty(JavaTextEditor.MULTI_LINE_COMMENT_COLOR));
                    x = Utilities.drawTabbedText(text, x, y, g, this, startPosition + wordIndex);

                    wordIndex = wordIndex + scannedCommentLength;
                    // } else if (isClass(indexedChar, wordIndex) &&
                    // !inComment)
                    // {
                    // Color color = (Color) host
                    // .getClientProperty(EJEArea.COMMON_WORD_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 if (isOperator(indexedChar) && !inComment) {
                    Color color = (Color) host.getClientProperty(JavaTextEditor.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(JavaTextEditor.MULTI_LINE_COMMENT_COLOR);
                    else
                        color = host.getForeground();

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

⌨️ 快捷键说明

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