1 19 20 package org.netbeans.lib.editor.codetemplates; 21 22 import java.awt.Color ; 23 import java.awt.Font ; 24 import java.awt.Graphics ; 25 import java.awt.event.KeyEvent ; 26 import javax.swing.ImageIcon ; 27 import javax.swing.text.BadLocationException ; 28 import javax.swing.text.Document ; 29 import javax.swing.text.JTextComponent ; 30 import org.netbeans.api.editor.completion.Completion; 31 import org.netbeans.lib.editor.codetemplates.api.CodeTemplate; 32 import org.netbeans.lib.editor.util.swing.DocumentUtilities; 33 import org.netbeans.spi.editor.completion.CompletionDocumentation; 34 import org.netbeans.spi.editor.completion.CompletionItem; 35 import org.netbeans.spi.editor.completion.CompletionResultSet; 36 import org.netbeans.spi.editor.completion.CompletionTask; 37 import org.netbeans.spi.editor.completion.support.AsyncCompletionQuery; 38 import org.netbeans.spi.editor.completion.support.AsyncCompletionTask; 39 import org.netbeans.spi.editor.completion.support.CompletionUtilities; 40 import org.openide.util.Utilities; 41 42 47 public final class CodeTemplateCompletionItem implements CompletionItem { 48 49 private static ImageIcon icon; 50 51 private final CodeTemplate codeTemplate; 52 53 private String leftText; 54 55 public static String toHtmlText(String text) { 56 StringBuffer htmlText = null; 57 for (int i = 0; i < text.length(); i++) { 58 String rep; char ch = text.charAt(i); 60 switch (ch) { 61 case '<': 62 rep = "<"; break; 64 case '>': 65 rep = ">"; break; 67 case '\n': 68 rep = "<br>"; break; 70 default: 71 rep = null; 72 break; 73 } 74 75 if (rep != null) { 76 if (htmlText == null) { 77 htmlText = new StringBuffer (120 * text.length() / 100); 79 if (i > 0) { 80 htmlText.append(text.substring(0, i)); 81 } 82 } 83 htmlText.append(rep); 84 85 } else { if (htmlText != null) { 87 htmlText.append(ch); 88 } 89 } 90 } 91 return (htmlText != null) ? htmlText.toString() : text; 92 } 93 94 public CodeTemplateCompletionItem(CodeTemplate codeTemplate) { 95 this.codeTemplate = codeTemplate; 96 } 97 98 private String getLeftText() { 99 return codeTemplate.getDescription(); 102 } 103 104 private String getRightText() { 105 if (leftText == null) { 106 leftText = toHtmlText(codeTemplate.getAbbreviation()); 107 } 108 return leftText; 109 } 110 111 public int getPreferredWidth(Graphics g, Font defaultFont) { 112 return CompletionUtilities.getPreferredWidth(getLeftText(), getRightText(), 113 g, defaultFont); 114 } 115 116 public void render(Graphics g, Font defaultFont, Color defaultColor, 117 Color backgroundColor, int width, int height, boolean selected) { 118 119 if (icon == null) { 120 icon = new ImageIcon (Utilities.loadImage( 121 "org/netbeans/lib/editor/codetemplates/resources/code_template.png")); } 123 CompletionUtilities.renderHtml(icon, getLeftText(), getRightText(), 124 g, defaultFont, defaultColor, width, height, selected); 125 } 126 127 public void defaultAction(JTextComponent component) { 128 Completion.get().hideAll(); 129 Document doc = component.getDocument(); 131 int caretOffset = component.getSelectionStart(); 132 int initMatchLen = getInitialMatchLength(doc, caretOffset, codeTemplate.getParametrizedText()); 133 if (initMatchLen > 0) { 134 try { 135 doc.remove(caretOffset - initMatchLen, initMatchLen); 137 } catch (BadLocationException ble) { 138 } 139 } 140 codeTemplate.insert(component); 141 } 142 143 public void processKeyEvent(KeyEvent evt) { 144 } 145 146 public boolean instantSubstitution(JTextComponent component) { 147 return false; 149 } 150 151 public static int getInitialMatchLength(Document doc, int caretOffset, String text) { 152 int matchLength = Math.min(text.length(), caretOffset); 153 CharSequence docText = DocumentUtilities.getText((org.netbeans.editor.BaseDocument)doc); 154 while (matchLength > 0) { 155 int i; 156 for (i = 1; i < matchLength; i++) { 157 if (docText.charAt(caretOffset - i) != text.charAt(matchLength - i)) { 158 break; 159 } 160 } 161 if (i == matchLength) { 162 break; 163 } 164 matchLength--; 165 } 166 return matchLength; 167 } 168 169 public CompletionTask createDocumentationTask() { 170 return new AsyncCompletionTask(new DocQuery(codeTemplate)); 171 } 172 173 public CompletionTask createToolTipTask() { 174 return null; 175 } 176 177 public int getSortPriority() { 178 return 650; 179 } 180 181 public CharSequence getSortText() { 182 return ""; 183 } 184 185 public CharSequence getInsertPrefix() { 186 String insertPrefix = codeTemplate.getParametrizedText(); 187 int dollarIndex = insertPrefix.indexOf("${"); if (dollarIndex >= 0) { 189 insertPrefix = insertPrefix.substring(0, dollarIndex); 190 } 191 return insertPrefix; 192 } 193 194 private static final class DocQuery extends AsyncCompletionQuery { 195 196 private CodeTemplate codeTemplate; 197 198 DocQuery(CodeTemplate codeTemplate) { 199 this.codeTemplate = codeTemplate; 200 } 201 202 protected void query(CompletionResultSet resultSet, Document doc, int caretOffset) { 203 resultSet.setDocumentation(new DocItem(codeTemplate)); 204 resultSet.finish(); 205 } 206 207 } 208 209 private static final class DocItem implements CompletionDocumentation { 210 211 private CodeTemplate codeTemplate; 212 213 private String text; 214 215 DocItem(CodeTemplate codeTemplate) { 216 this.codeTemplate = codeTemplate; 217 text = createText(); 218 } 219 220 public String getText() { 221 return text; 222 } 223 224 private String createText() { 225 StringBuffer htmlText = new StringBuffer ("<html><pre>"); ParametrizedTextParser parser = new ParametrizedTextParser(null, codeTemplate.getParametrizedText()); 228 parser.parse(); 229 parser.appendHtmlText(htmlText); 230 htmlText.append("</pre>"); 232 htmlText.append("<br>Abbreviation: "); htmlText.append(toHtmlText(codeTemplate.getAbbreviation())); 235 htmlText.append(" ["); String mimeType = CodeTemplateApiPackageAccessor.get().getOperation(codeTemplate).getMimeType(); 238 htmlText.append(AbbrevSettings.get(mimeType).getExpandKeyStrokeText()); 239 htmlText.append(" for expansion]</html>"); return htmlText.toString(); 241 } 242 243 public CompletionDocumentation resolveLink(String link) { 244 return null; 245 } 246 247 public java.net.URL getURL() { 248 return null; 249 } 250 251 252 public javax.swing.Action getGotoSourceAction() { 253 return null; 254 } 255 256 } 257 258 } 259 | Popular Tags |