KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.netbeans.api.languages.LanguagesManager;
27 import org.netbeans.api.languages.ParseException;
28 import org.netbeans.api.lexer.Token;
29 import org.netbeans.api.lexer.TokenHierarchy;
30 import org.netbeans.api.lexer.TokenSequence;
31 import org.netbeans.editor.BaseDocument;
32 import org.netbeans.editor.ext.ExtKit.ExtDeleteCharAction;
33 import org.netbeans.modules.languages.Feature;
34 import org.netbeans.modules.languages.Language;
35 import org.netbeans.modules.languages.LanguagesManagerImpl;
36 import org.openide.ErrorManager;
37
38 /**
39  *
40  * @author Jan Jancura
41  */

42 public class BraceCompletionDeleteAction extends ExtDeleteCharAction {
43
44     public BraceCompletionDeleteAction () {
45         super ("delete-previous", false);
46     }
47
48     protected void charBackspaced (
49         BaseDocument doc, int dotPos, Caret JavaDoc caret, char ch
50     ) throws BadLocationException JavaDoc {
51         try {
52             String JavaDoc mimeType = (String JavaDoc) doc.getProperty ("mimeType");
53             TokenHierarchy th = TokenHierarchy.get (doc);
54             TokenSequence ts = th.tokenSequence ();
55             while (true) {
56                 ts.move (caret.getDot ());
57                 if (!ts.moveNext ()) return;
58                 TokenSequence ts2 = ts.embedded ();
59                 if (ts2 == null) break;
60                 ts = ts2;
61             }
62             mimeType = ts.language ().mimeType ();
63             Language l = ((LanguagesManagerImpl) LanguagesManager.getDefault ()).getLanguage (mimeType);
64             List JavaDoc<Feature> completes = l.getFeatures ("COMPLETE");
65             Iterator JavaDoc<Feature> it = completes.iterator ();
66             while (it.hasNext ()) {
67                 Feature complete = it.next ();
68                 if (complete.getType () != Feature.Type.STRING)
69                     continue;
70                 String JavaDoc s = (String JavaDoc) complete.getValue ();
71                 int i = s.indexOf (':');
72                 if (i != 1) continue;
73                 String JavaDoc ss = doc.getText (
74                     caret.getDot (),
75                     s.length () - i - 1
76                 );
77                 if (s.endsWith (ss) &&
78                     s.charAt (0) == ch
79                 ) {
80                     doc.remove (caret.getDot (), s.length () - i - 1);
81                     return;
82                 }
83             }
84         } catch (ParseException ex) {
85             ErrorManager.getDefault ().notify (ex);
86         }
87     }
88 }
89
Popular Tags