001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: /*
042: * AutoCompleteDocument.java
043: *
044: * Created on September 5, 2006, 4:37 PM
045: *
046: * To change this template, choose Tools | Template Manager
047: * and open the template in the editor.
048: */
049:
050: package org.netbeans.modules.db.sql.visualeditor.querybuilder;
051:
052: import java.awt.Font;
053: import java.util.ArrayList;
054: import java.util.Arrays;
055: import java.util.Iterator;
056: import java.util.List;
057: import javax.swing.BoxLayout;
058: import javax.swing.JTextField;
059: import javax.swing.text.AttributeSet;
060: import javax.swing.text.BadLocationException;
061: import javax.swing.text.DefaultStyledDocument;
062: import javax.swing.text.JTextComponent;
063:
064: /**
065: *
066: * @author John Baker
067: */
068: public class QueryBuilderSqlCompletion extends DefaultStyledDocument {
069:
070: private List dictionary = new ArrayList();
071: private JTextComponent comp;
072: private int charCount = -1;
073: private int lastOffset = 0;
074:
075: public QueryBuilderSqlCompletion(JTextComponent field,
076: String[] aDictionary) {
077: comp = field;
078: dictionary.addAll(Arrays.asList(aDictionary));
079: }
080:
081: public void addDictionaryEntry(String item) {
082: dictionary.add(item);
083: }
084:
085: /**
086: * Insert text that is matched by the sequence of keys typed in the QueryBuilderSqlTextArea
087: **/
088: public void insertString(int offs, String str, AttributeSet a)
089: throws BadLocationException {
090: super .insertString(offs, str, a);
091:
092: if ((offs + charCount) <= lastOffset) // caret currently precedes or equals position of the last offset
093: charCount = 0; // if cursor moved reset character count (may not be needed)
094: else
095: charCount++; // contiguous chars
096:
097: String charTyped = getText(offs - charCount, this .comp
098: .getCaretPosition()
099: - (offs - charCount));
100: String word = completeText(charTyped);
101:
102: // do the completion if the keys typed form the sequence of a matching word
103: if (word != null) {
104: super .insertString(offs + str.length(), word, a);
105: comp.setCaretPosition(offs + str.length());
106: comp.moveCaretPosition(offs + word.length() + 1);
107: } else {
108: comp.setCaretPosition(offs + str.length());
109:
110: if (charCount >= 0)
111: charCount--; // if no matching character, reset
112: }
113:
114: // save the starting position in case the caret location is moved behind the previous starting cursor position
115: lastOffset = offs;
116: }
117:
118: // Compare prefix of chars typed with sqlReservedWords from QueryBuilderSqlTextArea
119: public String completeText(String text) {
120: for (Iterator i = dictionary.iterator(); i.hasNext();) {
121: String word = (String) i.next();
122: if (word.startsWith(text)) {
123: return word.substring(text.length());
124: } else if (word.startsWith(text.toUpperCase()))
125: return word.substring(text.length()).toLowerCase();
126: }
127: return null;
128: }
129:
130: }
|