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: import java.text.NumberFormat;
045: import java.text.ParseException;
046: import java.util.Locale;
047: import net.java.hulp.i18n.Logger;
048: import javax.swing.JTextField;
049: import javax.swing.SwingConstants;
050: import javax.swing.text.AttributeSet;
051: import javax.swing.text.BadLocationException;
052: import javax.swing.text.Document;
053: import javax.swing.text.PlainDocument;
054: import org.netbeans.modules.etl.logger.Localizer;
055: import org.netbeans.modules.etl.logger.LogUtil;
056:
057: /**
058: * Used in ColumnMetadata table to render scale and precision
059: *
060: * @author Jonathan Giron
061: * @version $Revision$
062: */
063: public class ColumnSizeTextField extends JTextField {
064:
065: private static transient final Logger mLogger = LogUtil
066: .getLogger(ColumnSizeTextField.class.getName());
067: private static transient final Localizer mLoc = Localizer.get();
068:
069: /**
070: * class IntegerDocument extends a plain document.
071: */
072: protected class IntegerDocument extends PlainDocument {
073:
074: /**
075: * method insertString inserts a string into the text field.
076: *
077: * @param offs is the offset to insert
078: * @param str is the string to insert
079: * @param a is the attribute.
080: * @throws BadLocationException if the string cannot be inserted.
081: */
082: public void insertString(int offs, String str, AttributeSet a)
083: throws BadLocationException {
084: char[] source = str.toCharArray();
085: char[] result = new char[source.length];
086: int j = 0;
087:
088: for (int i = 0; i < result.length; i++) {
089: if (Character.isDigit(source[i])) {
090: result[j++] = source[i];
091: } else {
092: toolkit.beep();
093: mLogger.infoNoloc(mLoc.t(
094: "PRSR073: insertString:{0}" + source[i],
095: getClass().getName()));
096: }
097: }
098: super .insertString(offs, new String(result, 0, j), a);
099: }
100: }
101:
102: private NumberFormat integerFormatter;
103: private Toolkit toolkit;
104:
105: /**
106: * Creates a new instance of IntegerField.
107: *
108: * @param columns number of columns used to calculate preferred width
109: */
110: public ColumnSizeTextField(int columns) {
111: super (columns);
112: setHorizontalAlignment(SwingConstants.RIGHT);
113:
114: toolkit = Toolkit.getDefaultToolkit();
115: integerFormatter = NumberFormat.getNumberInstance(Locale.US);
116: integerFormatter.setParseIntegerOnly(true);
117: }
118:
119: /**
120: * Creates a new instance of IntegerField.
121: *
122: * @param value is the initial value to display
123: * @param columns number of columns used to calculate preferred width
124: */
125: public ColumnSizeTextField(int value, int columns) {
126: this (columns);
127: setValue(value);
128: }
129:
130: /**
131: * Gets an integer value from the text field.
132: *
133: * @return int value retrieved
134: */
135: public int getValue() {
136: int retVal = 0;
137: try {
138: retVal = integerFormatter.parse(getText()).intValue();
139: } catch (ParseException e) {
140: toolkit.beep();
141: }
142: return retVal;
143: }
144:
145: /**
146: * Sets the given integer value into the text field.
147: *
148: * @param value is the value to use.
149: */
150: public void setValue(int value) {
151: setText(integerFormatter.format(value));
152: }
153:
154: /**
155: * Creates an IntegerDocument as the default model.
156: *
157: * @return Document that is created.
158: */
159: protected Document createDefaultModel() {
160: return new IntegerDocument();
161: }
162: }
|