xref: /titanic_44/usr/src/cmd/krb5/kadmin/gui/dchanger/DCTextField.java (revision 49b225e1cfa7bbf7738d4df0a03f18e3283426eb)
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) 1999-2000 by Sun Microsystems, Inc.
26  * All rights reserved.
27  */
28 
29 import sunsoft.jws.visual.rt.shadow.java.awt.*;
30 import java.awt.*;
31 
32 /**
33  * This creates a text field for storing integers that implements the
34  *  DCListener interface so that it can be notified to
35  * increment/decrement its value.
36  */
37 public class DCTextField extends TextField implements DCListener {
38 
39 	private int value;
40 
41 	private int bigIncrementValue = 1;
42 
43     /**
44      * Constructor for DCTextField.
45      * @param text the text to initialize the text field with
46      * @param columns the width of the text field in number of columns
47      */
48     public DCTextField(String text, int columns) {
49 	super(columns);
50 	setValueFromText(text);
51     }
52 
53     /**
54      * Sets the value of the big increment for this text field.
55      */
56     public void setBigIncrement(int value) {
57 	bigIncrementValue = value;
58     }
59 
60     /**
61      * Method from interface DCListener.
62      */
63     public void increment() {
64    	increment(1);
65     }
66 
67     /**
68      * Method from interface DCListener.
69      */
70     public void decrement() {
71 	increment(-1);
72     }
73 
74     /**
75      * Method from interface DCListener.
76      */
77     public void bigIncrement() {
78 	increment(bigIncrementValue);
79     }
80 
81     /**
82      * Method from interface DCListener.
83      */
84     public void bigDecrement() {
85 	increment(-1*bigIncrementValue);
86     }
87 
88     /**
89      * Increments the value of the textfield. It does not increment it
90      * if this will lead to an invalid value.
91      * @param value how much to increment by.  It can be negative if one
92      * desires to decrement.
93      */
94     protected void increment(int value) {
95 	setValue(getValue() + value);
96     }
97 
98     /**
99      * The current integer value associated with this text field.
100      * @return the int value.
101      */
102     public int getValue() {
103 	return value;
104     }
105 
106     /**
107      * Sets the current integer value associated with this text
108      * field. The text field will display this value. If the value is not
109      * valid then the old value will remain in effect.
110      */
111 	public void setValue(int newValue) {
112 		if (checkValue(newValue)) {
113 			value = newValue;
114 			setText(Integer.toString(newValue));
115 		}
116 	}
117 
118     /**
119      * Sets the value for this text field from the given text.
120      * @param text the text that this text field shoudl contain.
121      * @exception NumberFormatException Thrown when the supplied text
122      * cannot be parsed in to an interger value.
123      */
124 	public void setValueFromText(String text) throws NumberFormatException {
125 		Integer i = Integer.valueOf(text);
126 		setValue(i.intValue());
127 	}
128 
129     /**
130      * Checks to see if the given value
131      * would be valid for this text
132      * field. Classes deriving form this class should override this to
133      * provide whatever checks they desire.
134      * @param newValue
135      * @return true if it will be valid,
136      * false otherwise. This class
137      * returns true always for all integer values.
138      */
139     public boolean checkValue(int newValue) {
140 	return true;
141     }
142 
143 }
144