KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > lib > editor > codetemplates > SurroundWithFix


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.lib.editor.codetemplates;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.Collection JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.List JavaDoc;
26 import javax.swing.text.Document JavaDoc;
27 import javax.swing.text.JTextComponent JavaDoc;
28 import org.netbeans.lib.editor.codetemplates.api.CodeTemplate;
29 import org.netbeans.lib.editor.codetemplates.spi.CodeTemplateFilter;
30 import org.netbeans.spi.editor.hints.ChangeInfo;
31 import org.netbeans.spi.editor.hints.Fix;
32 import org.openide.util.NbBundle;
33
34 /**
35  *
36  * @author Dusan Balek
37  */

38 public class SurroundWithFix implements Fix {
39     
40     private static String JavaDoc SURROUND_WITH = NbBundle.getMessage(SurroundWithFix.class, "TXT_SurroundWithHint_Prefix"); //NOI18N
41

42     public static List JavaDoc getFixes(JTextComponent JavaDoc component) {
43         List JavaDoc fixes = new ArrayList JavaDoc();
44         Document JavaDoc doc = component.getDocument();
45         CodeTemplateManagerOperation op = CodeTemplateManagerOperation.get(doc);
46         op.waitLoaded();
47         Collection JavaDoc/*<CodeTemplateFilter>*/ filters = op.getTemplateFilters(component, component.getSelectionStart());
48         for (Iterator JavaDoc it = op.findSelectionTemplates().iterator(); it.hasNext();) {
49             CodeTemplate template = (CodeTemplate)it.next();
50             if (accept(template, filters))
51                 fixes.add(new SurroundWithFix(template, component));
52         }
53         return fixes;
54     }
55     
56     private CodeTemplate template;
57     private JTextComponent JavaDoc component;
58     
59     /** Creates a new instance of SurroundWithFix */
60     private SurroundWithFix(CodeTemplate template, JTextComponent JavaDoc component) {
61         this.template = template;
62         this.component = component;
63     }
64
65     public String JavaDoc getText() {
66         return SURROUND_WITH + template.getDescription();
67     }
68
69     public ChangeInfo implement() {
70         template.insert(component);
71         return null;
72     }
73     
74     private static boolean accept(CodeTemplate template, Collection JavaDoc/*<CodeTemplateFilter>*/ filters) {
75         for(Iterator JavaDoc it = filters.iterator(); it.hasNext();) {
76             CodeTemplateFilter filter = (CodeTemplateFilter)it.next();
77             if (!filter.accept(template))
78                 return false;
79         }
80         return true;
81     }
82 }
83
Popular Tags