1 19 27 28 package org.netbeans.modules.db.sql.visualeditor.querybuilder; 29 30 import java.awt.Font ; 31 import java.util.ArrayList ; 32 import java.util.Arrays ; 33 import java.util.Iterator ; 34 import java.util.List ; 35 import javax.swing.BoxLayout ; 36 import javax.swing.JTextField ; 37 import javax.swing.text.AttributeSet ; 38 import javax.swing.text.BadLocationException ; 39 import javax.swing.text.DefaultStyledDocument ; 40 import javax.swing.text.JTextComponent ; 41 42 46 public class QueryBuilderSqlCompletion extends DefaultStyledDocument { 47 48 private List dictionary = new ArrayList (); 49 private JTextComponent comp; 50 private int charCount = -1; 51 private int lastOffset = 0; 52 53 public QueryBuilderSqlCompletion( JTextComponent field, String [] aDictionary ) { 54 comp = field; 55 dictionary.addAll( Arrays.asList( aDictionary ) ); 56 } 57 58 public void addDictionaryEntry( String item ) { 59 dictionary.add( item ); 60 } 61 62 65 public void insertString(int offs, String str, AttributeSet a) throws BadLocationException { 66 super.insertString( offs, str, a ); 67 68 if ((offs + charCount) <= lastOffset) charCount = 0; else 71 charCount ++; 73 String charTyped = getText(offs - charCount , this.comp.getCaretPosition() - (offs - charCount) ); 74 String word = completeText( charTyped ); 75 76 if( word != null ) { 78 super.insertString( offs + str.length(), word, a ); 79 comp.setCaretPosition( offs + str.length() ); 80 comp.moveCaretPosition( offs + word.length()+1 ); 81 } else { 82 comp.setCaretPosition( offs + str.length() ); 83 84 if (charCount >= 0) 85 charCount--; } 87 88 lastOffset = offs; 90 } 91 92 public String completeText( String text ) { 94 for( Iterator i = dictionary.iterator(); i.hasNext(); ) { 95 String word = (String ) i.next(); 96 if( word.startsWith( text ) ) { 97 return word.substring( text.length() ); 98 } else if (word.startsWith(text.toUpperCase())) 99 return word.substring( text.length() ).toLowerCase(); 100 } 101 return null; 102 } 103 104 } 105 106 107 | Popular Tags |