KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > db > sql > visualeditor > querybuilder > QueryBuilderSqlCompletion


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-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 /*
20  * AutoCompleteDocument.java
21  *
22  * Created on September 5, 2006, 4:37 PM
23  *
24  * To change this template, choose Tools | Template Manager
25  * and open the template in the editor.
26  */

27
28 package org.netbeans.modules.db.sql.visualeditor.querybuilder;
29
30 import java.awt.Font JavaDoc;
31 import java.util.ArrayList JavaDoc;
32 import java.util.Arrays JavaDoc;
33 import java.util.Iterator JavaDoc;
34 import java.util.List JavaDoc;
35 import javax.swing.BoxLayout JavaDoc;
36 import javax.swing.JTextField JavaDoc;
37 import javax.swing.text.AttributeSet JavaDoc;
38 import javax.swing.text.BadLocationException JavaDoc;
39 import javax.swing.text.DefaultStyledDocument JavaDoc;
40 import javax.swing.text.JTextComponent JavaDoc;
41
42 /**
43  *
44  * @author John Baker
45  */

46 public class QueryBuilderSqlCompletion extends DefaultStyledDocument JavaDoc {
47
48     private List JavaDoc dictionary = new ArrayList JavaDoc();
49     private JTextComponent JavaDoc comp;
50     private int charCount = -1;
51     private int lastOffset = 0;
52
53     public QueryBuilderSqlCompletion( JTextComponent JavaDoc field, String JavaDoc[] aDictionary ) {
54         comp = field;
55         dictionary.addAll( Arrays.asList( aDictionary ) );
56     }
57
58     public void addDictionaryEntry( String JavaDoc item ) {
59         dictionary.add( item );
60     }
61
62     /**
63      * Insert text that is matched by the sequence of keys typed in the QueryBuilderSqlTextArea
64      **/

65     public void insertString(int offs, String JavaDoc str, AttributeSet JavaDoc a) throws BadLocationException JavaDoc {
66         super.insertString( offs, str, a );
67
68         if ((offs + charCount) <= lastOffset) // caret currently precedes or equals position of the last offset
69
charCount = 0; // if cursor moved reset character count (may not be needed)
70
else
71             charCount ++; // contiguous chars
72

73         String JavaDoc charTyped = getText(offs - charCount , this.comp.getCaretPosition() - (offs - charCount) );
74         String JavaDoc word = completeText( charTyped );
75
76         // do the completion if the keys typed form the sequence of a matching word
77
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--; // if no matching character, reset
86
}
87
88         // save the starting position in case the caret location is moved behind the previous starting cursor position
89
lastOffset = offs;
90     }
91
92     // Compare prefix of chars typed with sqlReservedWords from QueryBuilderSqlTextArea
93
public String JavaDoc completeText( String JavaDoc text ) {
94         for( Iterator JavaDoc i = dictionary.iterator(); i.hasNext(); ) {
95             String JavaDoc word = (String JavaDoc) 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