KickJava   Java API By Example, From Geeks To Geeks.

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


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.awt.Color JavaDoc;
23 import java.awt.Font JavaDoc;
24 import java.awt.Graphics JavaDoc;
25 import java.awt.event.KeyEvent JavaDoc;
26 import javax.swing.ImageIcon JavaDoc;
27 import javax.swing.text.BadLocationException JavaDoc;
28 import javax.swing.text.Document JavaDoc;
29 import javax.swing.text.JTextComponent JavaDoc;
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 /**
43  * Code template completion result item.
44  *
45  * @author Miloslav Metelka
46  */

47 public final class CodeTemplateCompletionItem implements CompletionItem {
48     
49     private static ImageIcon JavaDoc icon;
50     
51     private final CodeTemplate codeTemplate;
52     
53     private String JavaDoc leftText;
54     
55     public static String JavaDoc toHtmlText(String JavaDoc text) {
56         StringBuffer JavaDoc htmlText = null;
57         for (int i = 0; i < text.length(); i++) {
58             String JavaDoc rep; // replacement string
59
char ch = text.charAt(i);
60             switch (ch) {
61                 case '<':
62                     rep = "&lt;"; // NOI18N
63
break;
64                 case '>':
65                     rep = "&gt;"; // NOI18N
66
break;
67                 case '\n':
68                     rep = "<br>"; // NOI18N
69
break;
70                 default:
71                     rep = null;
72                     break;
73             }
74
75             if (rep != null) {
76                 if (htmlText == null) {
77                     // Expect 20% of text to be html tags text
78
htmlText = new StringBuffer JavaDoc(120 * text.length() / 100);
79                     if (i > 0) {
80                         htmlText.append(text.substring(0, i));
81                     }
82                 }
83                 htmlText.append(rep);
84
85             } else { // no replacement
86
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 JavaDoc getLeftText() {
99         // Temporarily - return just the description - already in html
100
// return toHtmlText(codeTemplate.getDescription());
101
return codeTemplate.getDescription();
102     }
103     
104     private String JavaDoc getRightText() {
105         if (leftText == null) {
106             leftText = toHtmlText(codeTemplate.getAbbreviation());
107         }
108         return leftText;
109     }
110     
111     public int getPreferredWidth(Graphics JavaDoc g, Font JavaDoc defaultFont) {
112         return CompletionUtilities.getPreferredWidth(getLeftText(), getRightText(),
113                 g, defaultFont);
114     }
115
116     public void render(Graphics JavaDoc g, Font JavaDoc defaultFont, Color JavaDoc defaultColor,
117     Color JavaDoc backgroundColor, int width, int height, boolean selected) {
118         
119         if (icon == null) {
120             icon = new ImageIcon JavaDoc(Utilities.loadImage(
121                 "org/netbeans/lib/editor/codetemplates/resources/code_template.png")); // NOI18N
122
}
123         CompletionUtilities.renderHtml(icon, getLeftText(), getRightText(),
124                 g, defaultFont, defaultColor, width, height, selected);
125     }
126
127     public void defaultAction(JTextComponent JavaDoc component) {
128         Completion.get().hideAll();
129         // Remove the typed part
130
Document JavaDoc doc = component.getDocument();
131         int caretOffset = component.getSelectionStart();
132         int initMatchLen = getInitialMatchLength(doc, caretOffset, codeTemplate.getParametrizedText());
133         if (initMatchLen > 0) {
134             try {
135                 // Remove the typed prefix
136
doc.remove(caretOffset - initMatchLen, initMatchLen);
137             } catch (BadLocationException JavaDoc ble) {
138             }
139         }
140         codeTemplate.insert(component);
141     }
142     
143     public void processKeyEvent(KeyEvent JavaDoc evt) {
144     }
145     
146     public boolean instantSubstitution(JTextComponent JavaDoc component) {
147         // defaultAction(component);
148
return false;
149     }
150     
151     public static int getInitialMatchLength(Document JavaDoc doc, int caretOffset, String JavaDoc text) {
152         int matchLength = Math.min(text.length(), caretOffset);
153         CharSequence JavaDoc 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 JavaDoc getSortText() {
182         return "";
183     }
184
185     public CharSequence JavaDoc getInsertPrefix() {
186         String JavaDoc insertPrefix = codeTemplate.getParametrizedText();
187         int dollarIndex = insertPrefix.indexOf("${"); // NOI18N
188
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 JavaDoc 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 JavaDoc text;
214         
215         DocItem(CodeTemplate codeTemplate) {
216             this.codeTemplate = codeTemplate;
217             text = createText();
218         }
219         
220         public String JavaDoc getText() {
221             return text;
222         }
223         
224         private String JavaDoc createText() {
225             // Parametrized text - parsed; parameters in bold
226
StringBuffer JavaDoc htmlText = new StringBuffer JavaDoc("<html><pre>"); // NOI18N
227
ParametrizedTextParser parser = new ParametrizedTextParser(null, codeTemplate.getParametrizedText());
228             parser.parse();
229             parser.appendHtmlText(htmlText);
230             htmlText.append("</pre>"); // NOI18N
231

232             // Append abbreviation
233
htmlText.append("<br>Abbreviation: &nbsp;"); // NOI18N
234
htmlText.append(toHtmlText(codeTemplate.getAbbreviation()));
235             htmlText.append("&nbsp; ["); // NOI18N
236
// Append expansion keystroke
237
String JavaDoc mimeType = CodeTemplateApiPackageAccessor.get().getOperation(codeTemplate).getMimeType();
238             htmlText.append(AbbrevSettings.get(mimeType).getExpandKeyStrokeText());
239             htmlText.append(" for expansion]</html>"); // NOI18N
240
return htmlText.toString();
241         }
242         
243         public CompletionDocumentation resolveLink(String JavaDoc link) {
244             return null;
245         }
246
247         public java.net.URL JavaDoc getURL() {
248             return null;
249         }
250
251
252         public javax.swing.Action JavaDoc getGotoSourceAction() {
253             return null;
254         }
255         
256     }
257
258 }
259
Popular Tags