KickJava   Java API By Example, From Geeks To Geeks.

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


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.Iterator JavaDoc;
23 import java.util.List JavaDoc;
24 import javax.swing.text.BadLocationException JavaDoc;
25 import javax.swing.text.Caret JavaDoc;
26 import javax.swing.text.StyledDocument JavaDoc;
27 import org.netbeans.api.languages.Context;
28 import org.netbeans.api.languages.LanguagesManager;
29 import org.netbeans.api.languages.ParseException;
30 import org.netbeans.api.lexer.TokenHierarchy;
31 import org.netbeans.api.lexer.TokenSequence;
32 import org.netbeans.editor.BaseDocument;
33 import org.netbeans.editor.ext.ExtKit.ExtDefaultKeyTypedAction;
34 import org.netbeans.modules.languages.Feature;
35 import org.netbeans.modules.languages.Feature.Type;
36 import org.netbeans.modules.languages.Language;
37 import org.netbeans.modules.languages.LanguagesManagerImpl;
38 import org.openide.ErrorManager;
39 import org.openide.text.NbDocument;
40
41 /**
42  *
43  * @author Jan Jancura
44  */

45 public class BraceCompletionInsertAction extends ExtDefaultKeyTypedAction {
46
47     protected void insertString (
48         BaseDocument doc, int dotPos,
49         Caret JavaDoc caret, String JavaDoc str,
50         boolean overwrite
51     ) throws BadLocationException JavaDoc {
52         try {
53             String JavaDoc mimeType = (String JavaDoc) doc.getProperty ("mimeType");
54             TokenHierarchy th = TokenHierarchy.get (doc);
55             TokenSequence ts = th.tokenSequence ();
56             while (true) {
57                 ts.move (caret.getDot ());
58                 if (!ts.moveNext ()) {
59                     super.insertString (doc, dotPos, caret, str, overwrite);
60                     return;
61                 }
62                 TokenSequence ts2 = ts.embedded ();
63                 if (ts2 == null) break;
64                 ts = ts2;
65             }
66             mimeType = ts.language ().mimeType ();
67             Language l = ((LanguagesManagerImpl) LanguagesManager.getDefault ()).getLanguage (mimeType);
68             List JavaDoc<Feature> completes = l.getFeatures ("COMPLETE");
69             if (completes == null) {
70                 super.insertString (doc, dotPos, caret, str, overwrite);
71                 return;
72             }
73             Feature methodCall = null;
74             Iterator JavaDoc<Feature> it = completes.iterator ();
75             while (it.hasNext ()) {
76                 Feature complete = it.next ();
77                 if (complete.getType () == Type.METHOD_CALL) {
78                     methodCall = complete;
79                     continue;
80                 }
81                 String JavaDoc s = (String JavaDoc) complete.getValue ();
82                 int i = s.indexOf (':');
83                 String JavaDoc ss = doc.getText (
84                     caret.getDot (),
85                     s.length () - i - 1
86                 );
87                 if (s.endsWith (ss) && str.equals (ss)) {
88                     // skip closing bracket / do not write it again
89
caret.setDot (caret.getDot () + 1);
90                     return;
91                 }
92             }
93             boolean beg = ts.offset () == caret.getDot ();
94
95             super.insertString (doc, dotPos, caret, str, overwrite);
96
97             th = TokenHierarchy.get (doc);
98             ts = th.tokenSequence ();
99             while (true) {
100                 ts.move (caret.getDot ());
101                 if (!ts.moveNext ()) {
102                     return;
103                 }
104                 TokenSequence ts2 = ts.embedded ();
105                 if (ts2 == null) break;
106                 ts = ts2;
107             }
108             if (methodCall != null) {
109                 if (caret.getDot () < doc.getLength ())
110                     ts.movePrevious ();
111                 String JavaDoc s = (String JavaDoc) methodCall.getValue (Context.create (doc, ts));
112                 if (s != null) {
113                     int pos = caret.getDot ();
114                     doc.insertString (pos, s, null);
115                     caret.setDot (pos);
116                 }
117             }
118             if (!beg &&
119                 ts.token ().id ().name ().indexOf ("whitespace") < 0
120             ) return;
121             StyledDocument JavaDoc sdoc = (StyledDocument JavaDoc) doc;
122             int ln = NbDocument.findLineNumber (sdoc, caret.getDot ());
123             int ls = NbDocument.findLineOffset (sdoc, ln);
124             String JavaDoc text = sdoc.getText (ls, caret.getDot () - ls);
125             it = completes.iterator ();
126             while (it.hasNext ()) {
127                 Feature complete = it.next ();
128                 if (complete.getType () == Type.METHOD_CALL) continue;
129                 String JavaDoc s = (String JavaDoc) complete.getValue ();
130                 int i = s.indexOf (':');
131                 if (text.endsWith (s.substring (0, i))) {
132                     int pos = caret.getDot ();
133                     doc.insertString (pos, s.substring (i + 1), null);
134                     caret.setDot (pos);
135                     return;
136                 }
137             }
138         } catch (ParseException ex) {
139             ErrorManager.getDefault ().notify (ex);
140         }
141     }
142 /*
143     protected void replaceSelection (
144         JTextComponent target,
145         int dotPos,
146         Caret caret,
147         String str,
148         boolean overwrite
149     ) throws BadLocationException {
150         System.out.println ("replaceSelection " + str);
151         super.replaceSelection (target, dotPos, caret, str, overwrite);
152     }
153 */

154 }
155
Popular Tags