KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > editor > java > JavaCodeTemplateFilter


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.java;
21
22 import com.sun.source.tree.Tree;
23 import com.sun.source.util.TreePath;
24 import java.io.IOException JavaDoc;
25 import java.util.EnumSet JavaDoc;
26 import javax.swing.text.JTextComponent JavaDoc;
27 import org.netbeans.api.java.source.CancellableTask;
28 import org.netbeans.api.java.source.CompilationController;
29 import org.netbeans.api.java.source.JavaSource;
30 import org.netbeans.api.java.source.JavaSource.Phase;
31 import org.netbeans.lib.editor.codetemplates.api.CodeTemplate;
32 import org.netbeans.lib.editor.codetemplates.spi.CodeTemplateFilter;
33 import org.openide.util.Exceptions;
34
35 /**
36  *
37  * @author Dusan Balek
38  */

39 public class JavaCodeTemplateFilter implements CodeTemplateFilter, CancellableTask<CompilationController> {
40     
41     private int startOffset;
42     private int endOffset;
43     private Tree.Kind ctx = null;
44     
45     private JavaCodeTemplateFilter(JTextComponent JavaDoc component, int offset) {
46         this.startOffset = offset;
47         this.endOffset = component.getSelectionStart() == offset ? component.getSelectionEnd() : -1;
48         JavaSource js = JavaSource.forDocument(component.getDocument());
49         if (js != null) {
50             try {
51                 js.runUserActionTask(this, true);
52             } catch (IOException JavaDoc ex) {
53                 Exceptions.printStackTrace(ex);
54             }
55         }
56     }
57
58     public synchronized boolean accept(CodeTemplate template) {
59         return ctx != null && getTemplateContexts(template).contains(ctx);
60     }
61     
62     public void cancel() {
63     }
64
65     public synchronized void run(CompilationController controller) throws IOException JavaDoc {
66         controller.toPhase(Phase.PARSED);
67         TreePath startPath = controller.getTreeUtilities().pathFor(startOffset);
68         ctx = startPath.getLeaf().getKind();
69         if (endOffset >= 0 && startOffset != endOffset) {
70             TreePath endPath = controller.getTreeUtilities().pathFor(endOffset);
71             if (endPath.getLeaf().getKind() != ctx)
72                 ctx = null;
73         }
74     }
75
76     private EnumSet JavaDoc<Tree.Kind> getTemplateContexts(CodeTemplate template) {
77         //TODO: rewrite this method when contexts are provided by templates
78
String JavaDoc abbrev = template.getAbbreviation().toLowerCase();
79         if (abbrev.equals("runn") || abbrev.startsWith("for") || abbrev.startsWith("while") || abbrev.equals("inst") || abbrev.startsWith("if") || abbrev.startsWith("do") || abbrev.startsWith("try"))
80             return EnumSet.of(Tree.Kind.BLOCK);
81         return EnumSet.noneOf(Tree.Kind.class);
82     }
83
84     public static final class Factory implements CodeTemplateFilter.Factory {
85         
86         public CodeTemplateFilter createFilter(JTextComponent JavaDoc component, int offset) {
87             return new JavaCodeTemplateFilter(component, offset);
88         }
89     }
90 }
91
Popular Tags