01: /*
02: * $Header: /cvs/j3dfly/J3dFly/src/org/jdesktop/j3dfly/utils/gui/FloatDocument.java,v 1.1 2005/04/20 21:04:57 paulby Exp $
03: *
04: * Sun Public License Notice
05: *
06: * The contents of this file are subject to the Sun Public License Version
07: * 1.0 (the "License"). You may not use this file except in compliance with
08: * the License. A copy of the License is available at http://www.sun.com/
09: *
10: * The Original Code is Java 3D(tm) Fly Through.
11: * The Initial Developer of the Original Code is Paul Byrne.
12: * Portions created by Paul Byrne are Copyright (C) 2002.
13: * All Rights Reserved.
14: *
15: * Contributor(s): Paul Byrne.
16: *
17: **/
18: package org.jdesktop.j3dfly.utils.gui;
19:
20: import javax.swing.text.AttributeSet;
21: import javax.swing.text.BadLocationException;
22: import java.awt.Toolkit;
23:
24: /**
25: * Class to ensure that a Textfield contains a valid Float
26: */
27: public class FloatDocument extends javax.swing.text.PlainDocument {
28:
29: public void insertString(int offset, String s,
30: AttributeSet attributeSet) throws BadLocationException {
31: try {
32: StringBuffer buf = new StringBuffer(getText(0, getLength()));
33: buf.insert(offset, s);
34: if (offset == 0 && s.equals("-")) {
35: super .insertString(offset, s, attributeSet);
36: return;
37: }
38: Float.parseFloat(buf.toString());
39: } catch (NumberFormatException e) { // Only allow Float values
40: Toolkit.getDefaultToolkit().beep();
41: return;
42: }
43: super.insertString(offset, s, attributeSet);
44: }
45: }
|