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: package org.netbeans.modules.mashup.db.ui.wizard;
042:
043: import java.awt.Toolkit;
044:
045: import javax.swing.JTextField;
046: import javax.swing.text.AttributeSet;
047: import javax.swing.text.BadLocationException;
048: import javax.swing.text.Document;
049: import javax.swing.text.PlainDocument;
050:
051: import net.java.hulp.i18n.Logger;
052: import org.netbeans.modules.etl.logger.Localizer;
053: import org.netbeans.modules.etl.logger.LogUtil;
054: import org.openide.DialogDisplayer;
055: import org.openide.NotifyDescriptor;
056:
057: /**
058: * @author Jonathan Giron
059: * @version $Revision$
060: */
061: public class ColumnNameTextField extends JTextField {
062:
063: private static transient final Logger mLogger = LogUtil
064: .getLogger(ColumnNameTextField.class.getName());
065: private static transient final Localizer mLoc = Localizer.get();
066:
067: /**
068: * Extends a plain document to enforce character limitataions for a field name
069: * textfield.
070: */
071: protected class FieldNameDocument extends PlainDocument {
072: /**
073: * Inserts a string into the text field.
074: *
075: * @param offs is the offset to insert
076: * @param str is the string to insert
077: * @param a is the attribute.
078: * @throws BadLocationException if the string cannot be inserted.
079: */
080: public void insertString(int offs, String str, AttributeSet a)
081: throws BadLocationException {
082: char[] source = str.toCharArray();
083:
084: if (offs == 0 && !Character.isLetter(source[0])) {
085: // First character of field name must be a letter.
086: toolkit.beep();
087: return;
088: } else if (str.length() == 1) {
089: // Check individual char if illegal, beep and refuse if true.
090: if (!isValidChar(source[0])) {
091: toolkit.beep();
092: return;
093: }
094: } else {
095: // Must be a pasted string, check all characters and display error message
096: // if it contains illegal chars.
097: boolean isBadString = false;
098: for (int i = 0; i < source.length; i++) {
099: if (!isValidChar(source[i])) {
100: isBadString = true;
101: break;
102: }
103: }
104:
105: if (isBadString) {
106: toolkit.beep();
107: String nbBundle1 = mLoc
108: .t(
109: "PRSR001: String({0})contains invalid characters.\nLegal column name characters include letters, numbers, '$' and '#'.",
110: str);
111: DialogDisplayer.getDefault().notify(
112: new NotifyDescriptor.Message(Localizer
113: .parse(nbBundle1)));
114: return;
115: }
116: }
117:
118: super .insertString(offs, str.toUpperCase(), a);
119: }
120:
121: private boolean isValidChar(final char c) {
122: return Character.isDigit(c) || Character.isLetter(c)
123: || ('_' == c) || ('$' == c) || ('#' == c);
124: }
125: }
126:
127: private Toolkit toolkit;
128:
129: /**
130: * Creates a new instance of ColumnNameTextField.
131: *
132: * @param value is the value to create with.
133: * @param columns is used to construct this object's subclass
134: */
135: public ColumnNameTextField() {
136: toolkit = Toolkit.getDefaultToolkit();
137: }
138:
139: /**
140: * Creates a FieldNameDocument as the default model.
141: *
142: * @return Document that is created.
143: */
144: protected Document createDefaultModel() {
145: return new FieldNameDocument();
146: }
147: }
|