KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > languages > features > BraceHighlighting


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.languages.features;
21
22 import java.util.ConcurrentModificationException JavaDoc;
23 import java.util.HashMap JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.Map JavaDoc;
27 import java.util.WeakHashMap JavaDoc;
28 import javax.swing.text.BadLocationException JavaDoc;
29 import org.netbeans.api.languages.LanguagesManager;
30 import org.netbeans.api.languages.ParseException;
31 import org.netbeans.api.lexer.Token;
32 import org.netbeans.api.lexer.TokenHierarchy;
33 import org.netbeans.api.lexer.TokenSequence;
34 import org.netbeans.editor.BaseDocument;
35 import org.netbeans.editor.BaseDocument;
36 import org.netbeans.editor.ext.ExtSyntaxSupport;
37 import org.netbeans.modules.languages.Feature;
38 import org.netbeans.modules.languages.Language;
39 import org.netbeans.modules.languages.LanguagesManagerImpl;
40 import org.netbeans.modules.languages.lexer.STokenId;
41
42
43 /**
44  *
45  * @author Dan Prusa
46  */

47 public class BraceHighlighting extends ExtSyntaxSupport {
48
49     public BraceHighlighting (BaseDocument doc) {
50         super (doc);
51     }
52
53     public int[] findMatchingBlock (int offset, boolean simpleSearch) throws BadLocationException JavaDoc {
54         try {
55             BaseDocument doc = getDocument ();
56             TokenHierarchy<BaseDocument> tokenHierarchy = TokenHierarchy.<BaseDocument>get (doc);
57             TokenSequence tokens = tokenHierarchy.tokenSequence();
58             tokens.move(offset);
59             tokens.moveNext ();
60             Token<STokenId> token = tokens.token ();
61             String JavaDoc mimeType = (String JavaDoc) doc.getProperty("mimeType"); // NOI18N
62
Language language = ((LanguagesManagerImpl)LanguagesManager.getDefault()).getLanguage(mimeType);
63             Map JavaDoc<String JavaDoc,String JavaDoc>[] bracesValue = getBraces (language);
64             if (bracesValue == null) {
65                 return super.findMatchingBlock(offset, simpleSearch);
66             }
67
68             CharSequence JavaDoc text = token.text().toString();
69             boolean moveToRight = false;
70             String JavaDoc bracket = bracesValue [0].get (text);
71             if (bracket != null) {
72                 moveToRight = true;
73             } else {
74                 bracket = bracesValue [1].get (text);
75                 if (bracket == null) {
76                     return null;
77                 }
78             }
79             String JavaDoc focusedBracket = text.toString();
80             int focusedBracketLength = focusedBracket.length();
81             int length = bracket.length();
82             int depth = 1;
83             while (moveToRight ? tokens.moveNext() : tokens.movePrevious()) {
84                 token = tokens.token();
85                 text = token.text();
86                 if (text.length() == length && bracket.equals(text.toString())) {
87                     depth--;
88                     if (depth == 0) {
89                         int position = token.offset(tokenHierarchy);
90                         return new int[] {position, position + token.length()};
91                     }
92                 } else if (text.length() == focusedBracketLength && focusedBracket.equals(text.toString())) {
93                     depth++;
94                 }
95             }
96         } catch (ConcurrentModificationException JavaDoc e) {
97         } catch (ParseException e) {
98         }
99         return null;
100     }
101     
102     private static Map JavaDoc<Language,Map JavaDoc<String JavaDoc,String JavaDoc>[]> braces = new WeakHashMap JavaDoc<Language,Map JavaDoc<String JavaDoc,String JavaDoc>[]> ();
103     
104     private static Map JavaDoc<String JavaDoc,String JavaDoc>[] getBraces (Language l) {
105         if (!braces.containsKey (l)) {
106             Map JavaDoc<String JavaDoc,String JavaDoc> startToEnd = new HashMap JavaDoc<String JavaDoc,String JavaDoc> ();
107             Map JavaDoc<String JavaDoc,String JavaDoc> endToStart = new HashMap JavaDoc<String JavaDoc,String JavaDoc> ();
108             
109             List JavaDoc<Feature> indents = l.getFeatures ("BRACE");
110             Iterator JavaDoc<Feature> it = indents.iterator ();
111             while (it.hasNext ()) {
112                 Feature indent = it.next ();
113                 String JavaDoc s = (String JavaDoc) indent.getValue ();
114                 int i = s.indexOf (':');
115                 String JavaDoc start = s.substring (0, i);
116                 String JavaDoc end = s.substring (i + 1);
117                 startToEnd.put (start, end);
118                 endToStart.put (end, start);
119             }
120             braces.put (
121                 l,
122                 new Map JavaDoc[] {startToEnd, endToStart}
123             );
124         }
125         return braces.get (l);
126     }
127 }
128
129
Popular Tags