1 19 20 package org.netbeans.lib.editor.codetemplates; 21 22 import java.util.ArrayList ; 23 import java.util.Collection ; 24 import java.util.Iterator ; 25 import java.util.List ; 26 import javax.swing.text.Document ; 27 import javax.swing.text.JTextComponent ; 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 38 public class SurroundWithFix implements Fix { 39 40 private static String SURROUND_WITH = NbBundle.getMessage(SurroundWithFix.class, "TXT_SurroundWithHint_Prefix"); 42 public static List getFixes(JTextComponent component) { 43 List fixes = new ArrayList (); 44 Document doc = component.getDocument(); 45 CodeTemplateManagerOperation op = CodeTemplateManagerOperation.get(doc); 46 op.waitLoaded(); 47 Collection filters = op.getTemplateFilters(component, component.getSelectionStart()); 48 for (Iterator 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 component; 58 59 60 private SurroundWithFix(CodeTemplate template, JTextComponent component) { 61 this.template = template; 62 this.component = component; 63 } 64 65 public String 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 filters) { 75 for(Iterator 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 |