KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > api > languages > support > CompletionSupport


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.api.languages.support;
21
22 import java.awt.Color JavaDoc;
23 import java.awt.Font JavaDoc;
24 import java.awt.Graphics JavaDoc;
25 import java.awt.Image JavaDoc;
26 import java.awt.event.KeyEvent JavaDoc;
27 import java.util.HashMap JavaDoc;
28 import java.util.Map JavaDoc;
29 import javax.swing.Icon JavaDoc;
30 import javax.swing.ImageIcon JavaDoc;
31 import javax.swing.JLabel JavaDoc;
32 import javax.swing.text.BadLocationException JavaDoc;
33 import javax.swing.text.JTextComponent JavaDoc;
34 import org.netbeans.api.editor.completion.Completion;
35 import org.netbeans.api.languages.LanguagesManager;
36 import org.netbeans.api.languages.ParseException;
37 import org.netbeans.api.lexer.Token;
38 import org.netbeans.api.lexer.TokenHierarchy;
39 import org.netbeans.api.lexer.TokenSequence;
40 import org.netbeans.modules.editor.NbEditorDocument;
41 import org.netbeans.modules.languages.Language;
42 import org.netbeans.modules.languages.LanguagesManagerImpl;
43 import org.netbeans.spi.editor.completion.CompletionItem;
44 import org.netbeans.spi.editor.completion.CompletionTask;
45 import org.openide.ErrorManager;
46 import org.openide.util.Utilities;
47
48
49 /*
50  * CompletionSupport.
51  *
52  * @author Jan Jancura
53  */

54 public class CompletionSupport {
55     
56     public static CompletionItem createCompletionItem (
57         String JavaDoc text
58     ) {
59         return new CompletionItemImpl (text, text, null, 100);
60     }
61     
62     public static CompletionItem createCompletionItem (
63         String JavaDoc text,
64         String JavaDoc description
65     ) {
66         return new CompletionItemImpl (text, description, null, 100);
67     }
68     
69     public static CompletionItem createCompletionItem (
70         String JavaDoc text,
71         String JavaDoc description,
72         String JavaDoc icon
73     ) {
74         return new CompletionItemImpl (text, description, icon, 100);
75     }
76     
77     public static CompletionItem createCompletionItem (
78         String JavaDoc text,
79         String JavaDoc description,
80         String JavaDoc icon,
81         int priority
82     ) {
83         return new CompletionItemImpl (text, description, icon, priority);
84     }
85     
86     private static Map JavaDoc<String JavaDoc,ImageIcon JavaDoc> icons = new HashMap JavaDoc<String JavaDoc,ImageIcon JavaDoc> ();
87     
88     private static Icon JavaDoc getCIcon (String JavaDoc resourceName) {
89         if (resourceName == null)
90             resourceName = "/org/netbeans/modules/languages/resources/node.gif";
91         if (!icons.containsKey (resourceName)) {
92             Image JavaDoc image = Utilities.loadImage (resourceName);
93             if (image == null)
94                 image = Utilities.loadImage (
95                     "/org/netbeans/modules/languages/resources/node.gif"
96                 );
97             icons.put (
98                 resourceName,
99                 new ImageIcon JavaDoc (image)
100             );
101         }
102         return icons.get (resourceName);
103     }
104     
105     private static class CompletionItemImpl implements CompletionItem {
106         
107         private String JavaDoc text;
108         private String JavaDoc description;
109         private String JavaDoc icon;
110         private int priority;
111         private static JLabel JavaDoc label = new JLabel JavaDoc ();
112         
113         
114         CompletionItemImpl (
115             String JavaDoc text,
116             String JavaDoc description,
117             String JavaDoc icon,
118             int priority
119         ) {
120             this.text = text;
121             this.description = description;
122             this.icon = icon;
123             this.priority = priority;
124         }
125         
126         public void defaultAction (JTextComponent JavaDoc component) {
127             NbEditorDocument doc = (NbEditorDocument) component.getDocument ();
128             int offset = component.getCaret ().getDot ();
129             try {
130                 TokenHierarchy tokenHierarchy = TokenHierarchy.get (doc);
131                 TokenSequence sequence = tokenHierarchy.tokenSequence ();
132                 sequence.move (offset - 1);
133                 sequence.moveNext();
134                 Token token = sequence.token ();
135                 String JavaDoc mimeType = (String JavaDoc) doc.getProperty ("mimeType");
136                 Language l = ((LanguagesManagerImpl) LanguagesManager.getDefault ()).
137                     getLanguage (mimeType);
138                 String JavaDoc t = text;
139                 if (!l.getSkipTokenTypes ().contains (token.id ().name ()))
140                     t = text.substring (offset - sequence.offset ());
141                 doc.insertString (offset, t, null);
142             } catch (BadLocationException JavaDoc ex) {
143                 ErrorManager.getDefault ().notify (ex);
144             } catch (ParseException ex) {
145                 ErrorManager.getDefault ().notify (ex);
146             }
147             Completion.get ().hideAll ();
148         }
149
150         public void processKeyEvent (KeyEvent JavaDoc evt) {
151         }
152
153         public int getPreferredWidth (Graphics JavaDoc g, Font JavaDoc defaultFont) {
154             label.setFont (defaultFont);
155             label.setText (description);
156             return label.getPreferredSize ().width;
157         }
158
159         public void render (
160             Graphics JavaDoc g,
161             Font JavaDoc defaultFont,
162             Color JavaDoc defaultColor,
163             Color JavaDoc backgroundColor,
164             int width, int height, boolean selected
165         ) {
166             label.setText (description);
167             label.setForeground (defaultColor);
168             label.setBackground (backgroundColor);
169             label.setFont (defaultFont);
170             label.setIcon (getCIcon (icon));
171             label.setBounds (g.getClipBounds ());
172             label.paint (g);
173         }
174
175         public CompletionTask createDocumentationTask () {
176             return null;
177         }
178
179         public CompletionTask createToolTipTask () {
180             return null;
181         }
182
183         public boolean instantSubstitution (JTextComponent JavaDoc component) {
184             return false;
185         }
186
187         public int getSortPriority () {
188             return priority;
189         }
190
191         public CharSequence JavaDoc getSortText () {
192             return text;
193         }
194
195         public CharSequence JavaDoc getInsertPrefix () {
196             return text;
197         }
198     }
199 }
200
201
202
Popular Tags