KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > editor > html > HTMLAutoCompletion


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.editor.html;
21
22
23 import javax.swing.text.BadLocationException JavaDoc;
24 import javax.swing.text.Caret JavaDoc;
25
26 import org.netbeans.api.html.lexer.HTMLTokenId;
27
28 import org.netbeans.api.lexer.Token;
29 import org.netbeans.api.lexer.TokenHierarchy;
30 import org.netbeans.api.lexer.TokenSequence;
31
32 import org.netbeans.editor.BaseDocument;
33 import org.netbeans.editor.ext.ExtSyntaxSupport;
34
35 /**
36  * This static class groups the whole aspect of bracket
37  * completion. It is defined to clearly separate the functionality
38  * and keep actions clean.
39  * The methods of the class are called from different actions as
40  * KeyTyped, DeletePreviousChar.
41  */

42 class HTMLAutoCompletion {
43     
44     //an index of lastly completed equals sign
45
private static int equalsSignInsertedOffset = -1;
46     
47     /**
48      * A hook method called after a character was inserted into the
49      * document. The function checks for special characters for
50      * completion ()[]'"{} and other conditions and optionally performs
51      * changes to the doc and or caret (complets braces, moves caret,
52      * etc.)
53      * @param doc the document where the change occurred
54      * @param dotPos position of the character insertion
55      * @param caret caret
56      * @param ch the character that was inserted
57      * @throws BadLocationException
58      */

59     static void charInserted(BaseDocument doc,
60             int dotPos,
61             Caret JavaDoc caret,
62             char ch) throws BadLocationException JavaDoc {
63         if (doc.getSyntaxSupport() instanceof ExtSyntaxSupport) {
64             if (ch == '=') {
65                 completeQuotes(doc, dotPos, caret);
66             } else if(ch == '"') {
67                 //user has pressed quotation mark
68
handleQuotationMark(doc, dotPos, caret);
69             } else {
70                 //user has pressed a key so I need to cancel the "quotation consuming mode"
71
equalsSignInsertedOffset = -1;
72             }
73         }
74     }
75     
76     //called when user deleted something in the document
77
static void charDeleted(BaseDocument doc, int dotPos, Caret JavaDoc caret, char ch) {
78         equalsSignInsertedOffset = -1;
79     }
80     
81     private static void handleQuotationMark(BaseDocument doc, int dotPos, Caret JavaDoc caret) throws BadLocationException JavaDoc {
82         if(equalsSignInsertedOffset != -1) {
83             //test whether the cursor is between completed quotations: attrname="|"
84
//this situation can happen when user autocompletes ="|",
85
//moves cursor somewhere else and type "
86
if(dotPos == (equalsSignInsertedOffset + ("=\"".length()))) {
87                 //remove the quotation mark
88
doc.remove(dotPos,1);
89                 caret.setDot(dotPos);
90             }
91             
92         } else {
93             //test whether the user typed an ending quotation in the attribute value
94
doc.readLock();
95             try {
96                 TokenHierarchy hi = TokenHierarchy.get(doc);
97                 TokenSequence ts = hi.tokenSequence();
98                 
99                 ts.move(dotPos);
100                 if(!ts.moveNext()) {
101                     return ; //no token
102
}
103                 
104                 Token token = ts.token();
105                 if(token.id() == HTMLTokenId.VALUE) {
106                     //test if the user inserted the qutation in an attribute value and before
107
//an already existing end quotation
108
//the text looks following in such a situation:
109
//
110
// atrname="abcd|"", where offset of the | == dotPos
111
if("\"\"".equals(doc.getText(dotPos , 2))) {
112                         doc.remove(dotPos,1);
113                         caret.setDot(dotPos+1);
114                     }
115                 }
116             }finally {
117                 doc.readUnlock();
118             }
119         }
120         //reset the semaphore
121
equalsSignInsertedOffset = -1;
122     }
123     
124     private static void completeQuotes(BaseDocument doc, int dotPos, Caret JavaDoc caret) throws BadLocationException JavaDoc{
125         doc.readLock();
126         try {
127             TokenHierarchy hi = TokenHierarchy.get(doc);
128             TokenSequence ts = hi.tokenSequence();
129             
130             ts.move(dotPos);
131             if(!ts.moveNext()) {
132                 return ; //no token
133
}
134             
135             Token token = ts.token();
136             
137             int dotPosAfterTypedChar = dotPos + 1;
138             if(token != null &&
139                     token.id() == HTMLTokenId.ARGUMENT) {
140                 doc.insertString( dotPosAfterTypedChar, "\"\"" , null);
141                 caret.setDot(dotPosAfterTypedChar + 1);
142                 //mark the last autocomplete position
143
equalsSignInsertedOffset = dotPos;
144             }
145             
146         }finally {
147             doc.readUnlock();
148         }
149         
150     }
151     
152     
153 }
154
Popular Tags