1 19 20 package org.netbeans.api.languages.support; 21 22 import java.awt.Color ; 23 import java.awt.Font ; 24 import java.awt.Graphics ; 25 import java.awt.Image ; 26 import java.awt.event.KeyEvent ; 27 import java.util.HashMap ; 28 import java.util.Map ; 29 import javax.swing.Icon ; 30 import javax.swing.ImageIcon ; 31 import javax.swing.JLabel ; 32 import javax.swing.text.BadLocationException ; 33 import javax.swing.text.JTextComponent ; 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 54 public class CompletionSupport { 55 56 public static CompletionItem createCompletionItem ( 57 String text 58 ) { 59 return new CompletionItemImpl (text, text, null, 100); 60 } 61 62 public static CompletionItem createCompletionItem ( 63 String text, 64 String description 65 ) { 66 return new CompletionItemImpl (text, description, null, 100); 67 } 68 69 public static CompletionItem createCompletionItem ( 70 String text, 71 String description, 72 String icon 73 ) { 74 return new CompletionItemImpl (text, description, icon, 100); 75 } 76 77 public static CompletionItem createCompletionItem ( 78 String text, 79 String description, 80 String icon, 81 int priority 82 ) { 83 return new CompletionItemImpl (text, description, icon, priority); 84 } 85 86 private static Map <String ,ImageIcon > icons = new HashMap <String ,ImageIcon > (); 87 88 private static Icon getCIcon (String resourceName) { 89 if (resourceName == null) 90 resourceName = "/org/netbeans/modules/languages/resources/node.gif"; 91 if (!icons.containsKey (resourceName)) { 92 Image 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 (image) 100 ); 101 } 102 return icons.get (resourceName); 103 } 104 105 private static class CompletionItemImpl implements CompletionItem { 106 107 private String text; 108 private String description; 109 private String icon; 110 private int priority; 111 private static JLabel label = new JLabel (); 112 113 114 CompletionItemImpl ( 115 String text, 116 String description, 117 String 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 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 mimeType = (String ) doc.getProperty ("mimeType"); 136 Language l = ((LanguagesManagerImpl) LanguagesManager.getDefault ()). 137 getLanguage (mimeType); 138 String 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 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 evt) { 151 } 152 153 public int getPreferredWidth (Graphics g, Font defaultFont) { 154 label.setFont (defaultFont); 155 label.setText (description); 156 return label.getPreferredSize ().width; 157 } 158 159 public void render ( 160 Graphics g, 161 Font defaultFont, 162 Color defaultColor, 163 Color 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 component) { 184 return false; 185 } 186 187 public int getSortPriority () { 188 return priority; 189 } 190 191 public CharSequence getSortText () { 192 return text; 193 } 194 195 public CharSequence getInsertPrefix () { 196 return text; 197 } 198 } 199 } 200 201 202 | Popular Tags |