Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. /
 
 package org.apache.cayenne.swing.components.textpane;
 
 import java.util.Map;
 
 
 
 public class TextPaneView extends PlainView {
 
     private static HashMap<PatternSyntaxStylepatternSyntaxStyle;
     private static Pattern patternComment;
     private static Pattern patternCommentStart;
 
     private static SyntaxStyle syntaxStyleComment;
     private static HashMap<PatternSyntaxStylepatternValue;
 
     static {
          = new HashMap<PatternSyntaxStyle>();
          = new HashMap<PatternSyntaxStyle>();
     }
 
     public TextPaneView(Element elemSyntaxConstant syntaxConstants) {
 
         super(elem);
         TextPaneStyleMap style = new TextPaneStyleMap();
 
         if (.size() == 0) {
 
             String[] keywords = syntaxConstants.getKEYWORDS();
             String[] keywords2 = syntaxConstants.getKEYWORDS2();
             String[] operators = syntaxConstants.getOPERATORS();
             String[] types = syntaxConstants.getTYPES();
 
             for (int i = 0; i < keywords.lengthi++) {
                 String patern = "(" + keywords[i] + ")";
                 .put(Pattern.compile(patern.
                         | .), style.syntaxStyleMap
                         .get(.));
             }
             for (int i = 0; i < keywords2.lengthi++) {
                 String patern = "(" + keywords2[i] + ")";
                 .put(Pattern.compile(patern.
                         | .), style.syntaxStyleMap
                         .get(.));
             }
             for (int i = 0; i < operators.lengthi++) {
                 String patern = "(" + operators[i] + ")";
                 .put(Pattern.compile(patern.
                         | .), style.syntaxStyleMap
                         .get(.));
             }
             for (int i = 0; i < types.lengthi++) {
                 String patern = "(" + types[i] + ")";
                 .put(Pattern.compile(patern.
                         | .), style.syntaxStyleMap
                         .get(.));
             }
         }
 
         if (.size() == 0) {
             .put(
                     Pattern.compile(.),
                     style.syntaxStyleMap.get(.));
            // patternValue.put(Pattern.compile(SQLSyntaxConstants.STRING_TEXT,
            // Pattern.UNICODE_CASE | Pattern.CASE_INSENSITIVE),
            // style.sqlSyntaxStyleMap.get(SQLStyleTypes.STRING));
        }
         = Pattern.compile(
                .,
                . | .);
         = Pattern.compile(
                .,
                . | .);
         = style.syntaxStyleMap.get(.);
    }
    @Override
    protected int drawUnselectedText(Graphics graphicsint xint yint p0int p1)
            throws BadLocationException {
        boolean lineComment = false;
        HashMap<IntegerIntegercomment = new HashMap<IntegerInteger>();
        HashMap<IntegerIntegercommentInLine = new HashMap<IntegerInteger>();
        StyledDocument doc = (StyledDocumentgetDocument();
        String text = doc.getText(p0p1 - p0);
        Segment segment = getLineBuffer();
        Matcher m = .matcher(doc.getText(0, doc.getLength()));
        int maxEnd = 0;
        while (m.find()) {
            comment.put(m.start(), m.end());
            if (maxEnd < m.end()) {
                maxEnd = m.end();
            }
        }
        Matcher m3 = .matcher(doc.getText(0, doc.getLength()));
        while (m3.find()) {
            if (maxEnd < m3.start()) {
                comment.put(m3.start(), doc.getLength());
                break;
            }
        }
        int j = 0;
        for (Map.Entry<IntegerIntegerentry : comment.entrySet()) {
            if (p0 >= entry.getKey() && p1 <= entry.getValue()) {
                lineComment = true;
                break;
            }
            else if (p0 <= entry.getKey() && p1 >= entry.getValue()) {
                commentInLine.put(entry.getKey() - p0entry.getValue() - p0);
            }
            else if (p0 <= entry.getKey()
                    && p1 >= entry.getKey()
                    && p1 < entry.getValue()) {
                commentInLine.put(entry.getKey() - p0p1 - p0);
            }
            else if (p0 <= entry.getValue()
                    && p1 >= entry.getValue()
                    && p0 > entry.getKey()) {
                commentInLine.put(0, entry.getValue() - p0);
            }
            j++;
        }
        SortedMap<IntegerIntegerstartMap = new TreeMap<IntegerInteger>();
        SortedMap<IntegerSyntaxStylesyntaxStyleMap = new TreeMap<IntegerSyntaxStyle>();
        if (lineComment) {
            startMap.put(0, text.length());
            syntaxStyleMap.put(0, );
        }
        else {
            for (Map.Entry<IntegerIntegerentryCommentInLine : commentInLine
                    .entrySet()) {
                startMap.put(entryCommentInLine.getKey(), entryCommentInLine.getValue());
                syntaxStyleMap.put(entryCommentInLine.getKey(), );
            }
            // Match all regexes on this snippet, store positions
            for (Map.Entry<PatternSyntaxStyleentry : .entrySet()) {
                Matcher matcher = entry.getKey().matcher(text);
                while (matcher.find()) {
                     if ((text.length() == matcher.end()
                            || text.charAt(matcher.end()) == '\t'
                            || text.charAt(matcher.end()) == ' ' || text.charAt(matcher
                            .end()) == '\n')
                            && (matcher.start() == 0
                                    || text.charAt(matcher.start() - 1) == '\t'
                                    || text.charAt(matcher.start() - 1) == '\n'
                                    || text.charAt(matcher.start() - 1) == ' ')) {
                        boolean inComment = false;
                        for (Map.Entry<IntegerIntegerentryCommentInLine : commentInLine
                                .entrySet()) {
                            if (matcher.start(1) >= entryCommentInLine.getKey()
                                    && matcher.end() <= entryCommentInLine.getValue()) {
                                inComment = true;
                            }
                        }
                        if (!inComment) {
                            startMap.put(matcher.start(1), matcher.end());
                            syntaxStyleMap.put(matcher.start(1), entry.getValue());
                        }
                    }
                }
            }
            for (Map.Entry<PatternSyntaxStyleentry : .entrySet()) {
                Matcher matcher = entry.getKey().matcher(text);
                while (matcher.find()) {
                    if ((text.length() == matcher.end()
                            || text.charAt(matcher.end()) == ' '
                            || text.charAt(matcher.end()) == ')'
                            || text.charAt(matcher.end()) == '\t' || text.charAt(matcher
                            .end()) == '\n')
                            && (matcher.start() == 0
                                    || text.charAt(matcher.start() - 1) == '\t'
                                    || text.charAt(matcher.start() - 1) == ' '
                                    || text.charAt(matcher.start() - 1) == '=' || text
                                    .charAt(matcher.start() - 1) == '(')) {
                        boolean inComment = false;
                        for (Map.Entry<IntegerIntegerentryCommentInLine : commentInLine
                                .entrySet()) {
                            if (matcher.start() >= entryCommentInLine.getKey()
                                    && matcher.end() <= entryCommentInLine.getValue()) {
                                inComment = true;
                            }
                        }
                        if (!inComment) {
                            startMap.put(matcher.start(), matcher.end());
                            syntaxStyleMap.put(matcher.start(), entry.getValue());
                        }
                    }
                }
            }
        }
        // TODO: check the map for overlapping parts
        int i = 0;
        // Colour the parts
        for (Map.Entry<IntegerIntegerentry : startMap.entrySet()) {
            int start = entry.getKey();
            int end = entry.getValue();
            if (i < start) {
                graphics.setColor(.);
                graphics.setFont(.);
                doc.getText(p0 + istart - isegment);
                x = Utilities.drawTabbedText(segmentxygraphicsthisi);
            }
            graphics.setFont(syntaxStyleMap.get(start).getFont());
            graphics.setColor(syntaxStyleMap.get(start).getColor());
            i = end;
            doc.getText(p0 + starti - startsegment);
            x = Utilities.drawTabbedText(segmentxygraphicsthisstart);
        }
        // Paint possible remaining text black
        if (i < text.length()) {
            graphics.setColor(.);
            graphics.setFont(.);
            doc.getText(p0 + itext.length() - isegment);
            x = Utilities.drawTabbedText(segmentxygraphicsthisi);
        }
        return x;
    }
New to GrepCode? Check out our FAQ X