1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License, Version 1.0 only 6 * (the "License"). You may not use this file except in compliance 7 * with the License. 8 * 9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10 * or http://www.opensolaris.org/os/licensing. 11 * See the License for the specific language governing permissions 12 * and limitations under the License. 13 * 14 * When distributing Covered Code, include this CDDL HEADER in each 15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16 * If applicable, add the following below this CDDL HEADER, with the 17 * fields enclosed by brackets "[]" replaced with your own identifying 18 * information: Portions Copyright [yyyy] [name of copyright owner] 19 * 20 * CDDL HEADER END 21 */ 22 /* 23 * ident "%Z%%M% %I% %E% SMI" 24 * 25 * Copyright (c) 1998-1999 by Sun Microsystems, Inc. 26 * All rights reserved. 27 */ 28 package com.sun.dhcpmgr.ui; 29 30 import java.awt.*; 31 import java.awt.event.*; 32 import javax.swing.*; 33 import javax.swing.text.*; 34 35 /** 36 * A text field which limits input to only digits. 37 */ 38 public class IntegerField extends JTextField { 39 40 /** 41 * Construct an empty field 42 */ 43 public IntegerField() { 44 this(""); 45 } 46 47 /** 48 * Construct a field initialized to the specified text. Defaults to a width 49 * of 5 characters. 50 * @param text The initial text to display 51 */ 52 public IntegerField(String text) { 53 this(text, 5); 54 } 55 56 /** 57 * Construct a field initialized to the specified value. Defaults to a 58 * width of 5 characters. 59 * @param value The initial value to display 60 */ 61 public IntegerField(int value) { 62 this(); 63 setValue(value); 64 } 65 66 /** 67 * Construct a field initialized to the specified Integer object. 68 * Defaults to a width of 5 characters. 69 * @param value The initial value to display 70 */ 71 public IntegerField(Integer value) { 72 this(); 73 setValue(value); 74 } 75 76 /** 77 * Construct a field initialized to the specified text and width. 78 * @param text The initial text to display 79 * @param width The width of the field in characters 80 */ 81 public IntegerField(String text, int width) { 82 super(text, width); 83 setHorizontalAlignment(RIGHT); 84 } 85 86 /** 87 * Set the value of the field 88 * @param value An <code>int</code> value to set 89 */ 90 public void setValue(int value) { 91 setText(String.valueOf(value)); 92 } 93 94 /** 95 * Set the value of the field 96 * @param value The value to set as an <code>Integer</code> 97 */ 98 public void setValue(Integer value) { 99 if (value != null) { 100 setValue(value.intValue()); 101 } 102 } 103 104 /** 105 * Retrieve the entered value 106 * @return The value stored in the field as an <code>int</code> 107 */ 108 public int getValue() { 109 String s = getText(); 110 if ((s == null) || (s.length() == 0)) { 111 return 0; 112 } else { 113 return Integer.parseInt(getText()); 114 } 115 } 116 117 protected Document createDefaultModel() { 118 return new IntegerDocument(); 119 } 120 121 /* 122 * This is the recommended way to validate input, as opposed to trapping 123 * KeyEvents because this will actually catch paste operations as well. 124 */ 125 static class IntegerDocument extends PlainDocument { 126 public void insertString(int offs, String str, AttributeSet a) 127 throws BadLocationException { 128 if (str != null) { 129 char [] chars = str.toCharArray(); 130 for (int i = 0; i < chars.length; ++i) { 131 if (!Character.isDigit(chars[i])) { 132 throw new BadLocationException("", offs); 133 } 134 } 135 } 136 super.insertString(offs, str, a); 137 } 138 } 139 }