KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > retouche > editor > completion > GsfCompletionItem


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.modules.retouche.editor.completion;
21
22
23 import java.awt.Color JavaDoc;
24 import java.awt.Font JavaDoc;
25 import java.awt.Graphics JavaDoc;
26 import java.awt.event.KeyEvent JavaDoc;
27 import java.util.*;
28 import javax.swing.ImageIcon JavaDoc;
29 import javax.swing.text.BadLocationException JavaDoc;
30 import javax.swing.text.JTextComponent JavaDoc;
31 import org.netbeans.api.editor.completion.Completion;
32 import org.netbeans.api.gsf.CompletionProposal;
33 import org.netbeans.api.gsf.Modifier;
34 import org.netbeans.api.retouche.source.CompilationInfo;
35 import org.netbeans.editor.BaseDocument;
36 import org.netbeans.spi.editor.completion.CompletionItem;
37 import org.netbeans.spi.editor.completion.CompletionTask;
38 import org.netbeans.spi.editor.completion.support.CompletionUtilities;
39
40 /**
41  * Code completion items originating from the language plugin.
42  *
43  * Based on JavaCompletionItem by Dusan Balek.
44  *
45  * @author Tor Norbye
46  */

47 public abstract class GsfCompletionItem implements CompletionItem {
48     protected CompilationInfo info;
49         
50     private static class DelegatedItem extends GsfCompletionItem {
51         private org.netbeans.api.gsf.CompletionProposal item;
52         private static final String JavaDoc METHOD_PUBLIC = "org/netbeans/modules/editor/resources/completion/method_16.png"; //NOI18N
53
private static final String JavaDoc METHOD_PROTECTED = "org/netbeans/modules/editor/resources/completion/method_protected_16.png"; //NOI18N
54
private static final String JavaDoc METHOD_PACKAGE = "org/netbeans/modules/editor/resources/completion/method_package_private_16.png"; //NOI18N
55
private static final String JavaDoc METHOD_PRIVATE = "org/netbeans/modules/editor/resources/completion/method_private_16.png"; //NOI18N
56
private static final String JavaDoc METHOD_ST_PUBLIC = "org/netbeans/modules/editor/resources/completion/method_static_16.png"; //NOI18N
57
private static final String JavaDoc METHOD_ST_PROTECTED = "org/netbeans/modules/editor/resources/completion/method_static_protected_16.png"; //NOI18N
58
private static final String JavaDoc METHOD_ST_PRIVATE = "org/netbeans/modules/editor/resources/completion/method_static_private_16.png"; //NOI18N
59
private static final String JavaDoc METHOD_ST_PACKAGE = "org/netbeans/modules/editor/resources/completion/method_static_package_private_16.png"; //NOI18N
60
private static final String JavaDoc METHOD_COLOR = "<font color=#000000>"; //NOI18N
61
private static final String JavaDoc PARAMETER_NAME_COLOR = "<font color=#a06001>"; //NOI18N
62
private static ImageIcon JavaDoc icon[][] = new ImageIcon JavaDoc[2][4];
63         
64         private DelegatedItem(CompilationInfo info, org.netbeans.api.gsf.CompletionProposal item) {
65             super(item.getAnchorOffset());
66             this.item = item;
67             this.info = info;
68         }
69         
70         public int getSortPriority() {
71             switch (item.getKind()) {
72             case CONSTRUCTOR: return 400;
73             case MODULE: return 900;
74             case CLASS: return 800;
75             case METHOD: return 500;
76             case ATTRIBUTE:
77             case FIELD: return 300;
78             case CONSTANT:
79             case VARIABLE: return 200;
80             case KEYWORD: return 600;
81             case OTHER:
82             default:
83                 return 999;
84             }
85         }
86
87         public CharSequence JavaDoc getSortText() {
88             return item.getSortText();
89         }
90
91         public CharSequence JavaDoc getInsertPrefix() {
92             return item.getInsertPrefix();
93         }
94
95         protected String JavaDoc getLeftHtmlText() {
96             return item.getLhsHtml();
97         }
98         
99         public String JavaDoc toString() {
100             return item.getName();
101         }
102         
103         protected String JavaDoc getRightHtmlText() {
104             String JavaDoc rhs = item.getRhsHtml();
105
106             // Count text length on LHS
107
String JavaDoc lhs = item.getLhsHtml();
108             boolean inTag = false;
109             int length = 0;
110             for (int i = 0, n = lhs.length(); i < n; i++) {
111                 char c = lhs.charAt(i);
112                 if (inTag) {
113                     if (c == '>') {
114                         inTag = false;
115                     }
116                 } else if (c == '<') {
117                     inTag = true;
118                 } else {
119                     length++;
120                 }
121             }
122             
123             return truncateRhs(rhs, length);
124         }
125         
126         public CompletionTask createDocumentationTask() {
127             if (item.getElement() != null) {
128                 return GsfCompletionProvider.createDocTask(item.getElement(), info);
129             }
130             
131             return null;
132         }
133         
134         protected ImageIcon JavaDoc getIcon() {
135             ImageIcon JavaDoc ic = item.getIcon();
136             if (ic != null) {
137                 return ic;
138             }
139             
140             ImageIcon JavaDoc icon = org.netbeans.modules.retouche.navigation.Icons.getElementIcon(item.getKind(), item.getModifiers());
141             // TODO - cache!
142
return icon;
143 //
144
//
145
// ElementKind kind = item.getKind();
146
// switch (kind) {
147
// case CONSTRUCTOR:
148
// case METHOD:
149
// return getMethodIcon();
150
// case ATTRIBUTE:
151
// case FIELD:
152
// return getFieldIcon();
153
// case CLASS:
154
// return getClassIcon();
155
// case MODULE:
156
// return getModuleIcon();
157
// case CONSTANT:
158
// return getConstantIcon();
159
// case VARIABLE:
160
// return getVariableIcon();
161
// case KEYWORD:
162
// return getKeywordIcon();
163
// case OTHER:
164
// }
165
//
166
// return null;
167
}
168             
169 // protected ImageIcon getMethodIcon() {
170
// Set<Modifier> modifiers = item.getModifiers();
171
//
172
// boolean isStatic = modifiers.contains(Modifier.STATIC);
173
//// int level = getProtectionLevel(elem.getModifiers());
174
//
175
// ImageIcon cachedIcon = icon[isStatic?1:0][level];
176
// if (cachedIcon != null) {
177
// return cachedIcon;
178
// }
179
//
180
// String iconPath = METHOD_PUBLIC;
181
// if (isStatic) {
182
// switch (level) {
183
// case PRIVATE_LEVEL:
184
// iconPath = METHOD_ST_PRIVATE;
185
// break;
186
//
187
// case PACKAGE_LEVEL:
188
// iconPath = METHOD_ST_PACKAGE;
189
// break;
190
//
191
// case PROTECTED_LEVEL:
192
// iconPath = METHOD_ST_PROTECTED;
193
// break;
194
//
195
// case PUBLIC_LEVEL:
196
// iconPath = METHOD_ST_PUBLIC;
197
// break;
198
// }
199
// }else{
200
// switch (level) {
201
// case PRIVATE_LEVEL:
202
// iconPath = METHOD_PRIVATE;
203
// break;
204
//
205
// case PACKAGE_LEVEL:
206
// iconPath = METHOD_PACKAGE;
207
// break;
208
//
209
// case PROTECTED_LEVEL:
210
// iconPath = METHOD_PROTECTED;
211
// break;
212
//
213
// case PUBLIC_LEVEL:
214
// iconPath = METHOD_PUBLIC;
215
// break;
216
// }
217
// }
218
// ImageIcon newIcon = new ImageIcon(org.openide.util.Utilities.loadImage(iconPath));
219
// icon[isStatic?1:0][level] = newIcon;
220
// return newIcon;
221
// }
222
}
223     
224
225     public static final GsfCompletionItem createItem(CompletionProposal proposal, CompilationInfo info) {
226         return new DelegatedItem(info, proposal);
227     }
228
229     public static final String JavaDoc COLOR_END = "</font>"; //NOI18N
230
public static final String JavaDoc STRIKE = "<s>"; //NOI18N
231
public static final String JavaDoc STRIKE_END = "</s>"; //NOI18N
232
public static final String JavaDoc BOLD = "<b>"; //NOI18N
233
public static final String JavaDoc BOLD_END = "</b>"; //NOI18N
234

235     protected int substitutionOffset;
236     
237     private GsfCompletionItem(int substitutionOffset) {
238         this.substitutionOffset = substitutionOffset;
239     }
240     
241     public void defaultAction(JTextComponent JavaDoc component) {
242         if (component != null) {
243             Completion.get().hideDocumentation();
244             Completion.get().hideCompletion();
245             int caretOffset = component.getSelectionEnd();
246             substituteText(component, substitutionOffset, caretOffset - substitutionOffset, null);
247         }
248     }
249
250     public void processKeyEvent(KeyEvent JavaDoc evt) {
251         if (evt.getID() == KeyEvent.KEY_TYPED) {
252             switch (evt.getKeyChar()) {
253                 case ';':
254                 case ',':
255                 case '(':
256                 case '.':
257                     Completion.get().hideDocumentation();
258                     Completion.get().hideCompletion();
259 // case '.':
260
// JTextComponent component = (JTextComponent)evt.getSource();
261
// int caretOffset = component.getSelectionEnd();
262
// substituteText(component, substitutionOffset, caretOffset - substitutionOffset, Character.toString(evt.getKeyChar()));
263
// evt.consume();
264
// break;
265
}
266         }
267     }
268
269     public boolean instantSubstitution(JTextComponent JavaDoc component) {
270         defaultAction(component);
271         return true;
272     }
273     
274     public CompletionTask createDocumentationTask() {
275         return null;
276     }
277     
278     public CompletionTask createToolTipTask() {
279         return null;
280     }
281     
282     public int getPreferredWidth(Graphics JavaDoc g, Font JavaDoc defaultFont) {
283         return CompletionUtilities.getPreferredWidth(getLeftHtmlText(), getRightHtmlText(), g, defaultFont);
284     }
285     
286     public void render(Graphics JavaDoc g, Font JavaDoc defaultFont, Color JavaDoc defaultColor, Color JavaDoc backgroundColor, int width, int height, boolean selected) {
287         CompletionUtilities.renderHtml(getIcon(), getLeftHtmlText(), getRightHtmlText(), g, defaultFont, defaultColor, width, height, selected);
288     }
289
290     protected abstract ImageIcon JavaDoc getIcon();
291     
292     protected String JavaDoc getLeftHtmlText() {
293         return null;
294     }
295     
296     protected String JavaDoc getRightHtmlText() {
297         return null;
298     }
299
300     protected void substituteText(JTextComponent JavaDoc c, int offset, int len, String JavaDoc toAdd) {
301         BaseDocument doc = (BaseDocument)c.getDocument();
302         String JavaDoc text = getInsertPrefix().toString();
303         if (text != null) {
304 // if (toAdd != null && !toAdd.equals("\n")) {//NOI18N
305
// TokenSequence<JavaTokenId> sequence = TokenHierarchy.get(doc).tokenSequence(JavaTokenId.language());
306
// if (!sequence.moveNext()) {
307
// text += toAdd;
308
// toAdd = null;
309
// }
310
// sequence.move(offset + len);
311
// boolean added = false;
312
// while(toAdd != null && toAdd.length() > 0) {
313
// String tokenText = sequence.token().text().toString();
314
// if (tokenText.startsWith(toAdd)) {
315
// len = sequence.offset() - offset + toAdd.length();
316
// text += toAdd;
317
// toAdd = null;
318
// } else if (toAdd.startsWith(tokenText)) {
319
// sequence.moveNext();
320
// len = sequence.offset() - offset;
321
// text += toAdd.substring(0, tokenText.length());
322
// toAdd = toAdd.substring(tokenText.length());
323
// added = true;
324
// } else if (sequence.token().id() == GsfTokenId.WHITESPACE && sequence.token().text().toString().indexOf('\n') < 0) {//NOI18N
325
// if (!sequence.moveNext()) {
326
// text += toAdd;
327
// toAdd = null;
328
// }
329
// } else {
330
// if (!added)
331
// text += toAdd;
332
// toAdd = null;
333
// }
334
// }
335
// }
336
// Update the text
337
doc.atomicLock();
338             try {
339                 String JavaDoc textToReplace = doc.getText(offset, len);
340                 if (text.equals(textToReplace)) return;
341                 
342                 doc.remove(offset, len);
343                 doc.insertString(offset, text, null);
344             } catch (BadLocationException JavaDoc e) {
345                 // Can't update
346
} finally {
347                 doc.atomicUnlock();
348             }
349         }
350     }
351
352     private static String JavaDoc truncateRhs(String JavaDoc rhs, int left) {
353         if (rhs != null) {
354             final int MAX_SIZE = 40;
355             int size = MAX_SIZE-left;
356             if (size < 10) {
357                 size = 10;
358             }
359             if (rhs != null && rhs.length() > size) {
360                 rhs = rhs.substring(0,size-3) + "<b>&gt;</b>"; // Add a ">" to indicate truncation
361
}
362         }
363         return rhs;
364     }
365
366     
367     // TODO: KeywordItem has a postfix:
368
// private static class KeywordItem extends GsfCompletionItem {
369
// private String postfix;
370
// private KeywordItem(ComKeyword keyword, String postfix, int substitutionOffset) {
371
// this.postfix = postfix;
372
// }
373
// protected void substituteText(JTextComponent c, int offset, int len, String toAdd) {
374
// super.substituteText(c, offset, len, toAdd != null ? toAdd : postfix);
375
// }
376

377     
378
379     private static final int PUBLIC_LEVEL = 3;
380     private static final int PROTECTED_LEVEL = 2;
381     private static final int PACKAGE_LEVEL = 1;
382     private static final int PRIVATE_LEVEL = 0;
383     
384     private static int getProtectionLevel(Set<Modifier> modifiers) {
385         if(modifiers.contains(Modifier.PUBLIC))
386             return PUBLIC_LEVEL;
387         if(modifiers.contains(Modifier.PROTECTED))
388             return PROTECTED_LEVEL;
389         if(modifiers.contains(Modifier.PRIVATE))
390             return PRIVATE_LEVEL;
391         return PACKAGE_LEVEL;
392     }
393 }
394
Popular Tags