1 19 20 package org.netbeans.modules.xml.text.completion; 21 22 import java.util.Collections ; 23 import java.util.Iterator ; 24 import java.util.List ; 25 import javax.swing.text.BadLocationException ; 26 import javax.swing.text.Document ; 27 import javax.swing.text.JTextComponent ; 28 import org.netbeans.api.editor.completion.Completion; 29 import org.netbeans.editor.BaseDocument; 30 import org.netbeans.editor.TokenItem; 31 import org.netbeans.modules.xml.text.api.XMLDefaultTokenContext; 32 import org.netbeans.modules.xml.text.syntax.XMLKit; 33 import org.netbeans.modules.xml.text.syntax.XMLSyntaxSupport; 34 import org.netbeans.editor.Utilities; 35 import org.netbeans.editor.ext.CompletionQuery; 36 import org.netbeans.editor.ext.CompletionQuery.ResultItem; 37 import org.netbeans.editor.ext.ExtEditorUI; 38 import org.netbeans.editor.ext.ExtSyntaxSupport; 39 import org.netbeans.modules.xml.text.syntax.XMLTokenContext; 40 import org.netbeans.spi.editor.completion.CompletionItem; 41 import org.netbeans.spi.editor.completion.CompletionResultSet; 42 import org.netbeans.spi.editor.completion.support.AsyncCompletionQuery; 43 import org.netbeans.spi.editor.completion.support.AsyncCompletionTask; 44 import org.netbeans.spi.editor.completion.CompletionProvider; 45 import org.netbeans.spi.editor.completion.CompletionTask; 46 47 51 public class XMLCompletionProvider implements CompletionProvider { 52 53 private static final boolean ENABLED = true; 54 55 58 public XMLCompletionProvider() { 59 } 60 61 public int getAutoQueryTypes(JTextComponent component, String typedText) { 62 int type = ((XMLSyntaxSupport)Utilities.getDocument(component). 63 getSyntaxSupport()).checkCompletion(component, typedText, false); 64 65 if(type == ExtSyntaxSupport.COMPLETION_POPUP) 66 return COMPLETION_QUERY_TYPE; 67 else return 0; 68 } 69 70 public CompletionTask createTask(int queryType, JTextComponent component) { 71 if (queryType == COMPLETION_QUERY_TYPE) { 72 return new AsyncCompletionTask(new Query(), component); 73 } 74 75 return null; 76 } 77 78 static class Query extends AbstractQuery { 79 80 private JTextComponent component; 81 82 protected void prepareQuery(JTextComponent component) { 83 this.component = component; 84 } 85 86 protected void doQuery(CompletionResultSet resultSet, Document doc, int caretOffset) { 87 XMLCompletionQuery.XMLCompletionResult res = queryImpl(component, caretOffset); 88 if(res != null) { 89 List results = res.getData(); 90 resultSet.addAllItems(results); 91 resultSet.setTitle(res.getTitle()); 92 resultSet.setAnchorOffset(res.getSubstituteOffset()); 93 } 94 } 95 } 96 97 private static XMLCompletionQuery.XMLCompletionResult queryImpl(JTextComponent component, int offset) { 98 if (!ENABLED) return null; 99 100 Class kitClass = Utilities.getKitClass(component); 101 if (kitClass != null) { 102 ExtEditorUI eeui = (ExtEditorUI)Utilities.getEditorUI(component); 103 org.netbeans.editor.ext.Completion compl = ((XMLKit)Utilities.getKit(component)).createCompletionForProvider(eeui); 104 XMLSyntaxSupport support = (XMLSyntaxSupport)Utilities.getSyntaxSupport(component); 105 return (XMLCompletionQuery.XMLCompletionResult)compl.getQuery().query(component, offset, support); 106 } 107 108 return null; 109 } 110 111 private static abstract class AbstractQuery extends AsyncCompletionQuery { 112 113 protected void preQueryUpdate(JTextComponent component) { 114 int caretOffset = component.getCaretPosition(); 115 Document doc = component.getDocument(); 116 checkHideCompletion((BaseDocument)doc, caretOffset); 117 } 118 119 protected void query(CompletionResultSet resultSet, Document doc, int caretOffset) { 120 if(doc != null) { 121 checkHideCompletion((BaseDocument)doc, caretOffset); 122 } 123 doQuery(resultSet, doc, caretOffset); 124 resultSet.finish(); 125 } 126 127 abstract void doQuery(CompletionResultSet resultSet, Document doc, int caretOffset); 128 129 } 130 131 private static void checkHideCompletion(BaseDocument doc, int caretOffset) { 132 XMLSyntaxSupport sup = (XMLSyntaxSupport)doc.getSyntaxSupport().get(XMLSyntaxSupport.class); 136 try { 137 TokenItem ti = sup.getTokenChain(caretOffset <= 0 ? 0 : caretOffset - 1, caretOffset); 138 if(ti != null && ti.getTokenID() == XMLDefaultTokenContext.TEXT && !ti.getImage().startsWith("<") && !ti.getImage().startsWith("&")) { 139 hideCompletion(); 140 } 141 }catch(BadLocationException e) { 142 } 144 } 145 146 private static void hideCompletion() { 147 Completion.get().hideCompletion(); 148 } 149 150 } 151 | Popular Tags |