xref: /titanic_44/usr/src/cmd/krb5/kadmin/gui/dchanger/DCTextField.java (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * ident	"%Z%%M%	%I%	%E% SMI"
24*7c478bd9Sstevel@tonic-gate  *
25*7c478bd9Sstevel@tonic-gate  * Copyright (c) 1999-2000 by Sun Microsystems, Inc.
26*7c478bd9Sstevel@tonic-gate  * All rights reserved.
27*7c478bd9Sstevel@tonic-gate  */
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate import sunsoft.jws.visual.rt.shadow.java.awt.*;
30*7c478bd9Sstevel@tonic-gate import java.awt.*;
31*7c478bd9Sstevel@tonic-gate 
32*7c478bd9Sstevel@tonic-gate /**
33*7c478bd9Sstevel@tonic-gate  * This creates a text field for storing integers that implements the
34*7c478bd9Sstevel@tonic-gate  *  DCListener interface so that it can be notified to
35*7c478bd9Sstevel@tonic-gate  * increment/decrement its value.
36*7c478bd9Sstevel@tonic-gate  */
37*7c478bd9Sstevel@tonic-gate public class DCTextField extends TextField implements DCListener {
38*7c478bd9Sstevel@tonic-gate 
39*7c478bd9Sstevel@tonic-gate 	private int value;
40*7c478bd9Sstevel@tonic-gate 
41*7c478bd9Sstevel@tonic-gate 	private int bigIncrementValue = 1;
42*7c478bd9Sstevel@tonic-gate 
43*7c478bd9Sstevel@tonic-gate     /**
44*7c478bd9Sstevel@tonic-gate      * Constructor for DCTextField.
45*7c478bd9Sstevel@tonic-gate      * @param text the text to initialize the text field with
46*7c478bd9Sstevel@tonic-gate      * @param columns the width of the text field in number of columns
47*7c478bd9Sstevel@tonic-gate      */
DCTextField(String text, int columns)48*7c478bd9Sstevel@tonic-gate     public DCTextField(String text, int columns) {
49*7c478bd9Sstevel@tonic-gate 	super(columns);
50*7c478bd9Sstevel@tonic-gate 	setValueFromText(text);
51*7c478bd9Sstevel@tonic-gate     }
52*7c478bd9Sstevel@tonic-gate 
53*7c478bd9Sstevel@tonic-gate     /**
54*7c478bd9Sstevel@tonic-gate      * Sets the value of the big increment for this text field.
55*7c478bd9Sstevel@tonic-gate      */
setBigIncrement(int value)56*7c478bd9Sstevel@tonic-gate     public void setBigIncrement(int value) {
57*7c478bd9Sstevel@tonic-gate 	bigIncrementValue = value;
58*7c478bd9Sstevel@tonic-gate     }
59*7c478bd9Sstevel@tonic-gate 
60*7c478bd9Sstevel@tonic-gate     /**
61*7c478bd9Sstevel@tonic-gate      * Method from interface DCListener.
62*7c478bd9Sstevel@tonic-gate      */
increment()63*7c478bd9Sstevel@tonic-gate     public void increment() {
64*7c478bd9Sstevel@tonic-gate    	increment(1);
65*7c478bd9Sstevel@tonic-gate     }
66*7c478bd9Sstevel@tonic-gate 
67*7c478bd9Sstevel@tonic-gate     /**
68*7c478bd9Sstevel@tonic-gate      * Method from interface DCListener.
69*7c478bd9Sstevel@tonic-gate      */
decrement()70*7c478bd9Sstevel@tonic-gate     public void decrement() {
71*7c478bd9Sstevel@tonic-gate 	increment(-1);
72*7c478bd9Sstevel@tonic-gate     }
73*7c478bd9Sstevel@tonic-gate 
74*7c478bd9Sstevel@tonic-gate     /**
75*7c478bd9Sstevel@tonic-gate      * Method from interface DCListener.
76*7c478bd9Sstevel@tonic-gate      */
bigIncrement()77*7c478bd9Sstevel@tonic-gate     public void bigIncrement() {
78*7c478bd9Sstevel@tonic-gate 	increment(bigIncrementValue);
79*7c478bd9Sstevel@tonic-gate     }
80*7c478bd9Sstevel@tonic-gate 
81*7c478bd9Sstevel@tonic-gate     /**
82*7c478bd9Sstevel@tonic-gate      * Method from interface DCListener.
83*7c478bd9Sstevel@tonic-gate      */
bigDecrement()84*7c478bd9Sstevel@tonic-gate     public void bigDecrement() {
85*7c478bd9Sstevel@tonic-gate 	increment(-1*bigIncrementValue);
86*7c478bd9Sstevel@tonic-gate     }
87*7c478bd9Sstevel@tonic-gate 
88*7c478bd9Sstevel@tonic-gate     /**
89*7c478bd9Sstevel@tonic-gate      * Increments the value of the textfield. It does not increment it
90*7c478bd9Sstevel@tonic-gate      * if this will lead to an invalid value.
91*7c478bd9Sstevel@tonic-gate      * @param value how much to increment by.  It can be negative if one
92*7c478bd9Sstevel@tonic-gate      * desires to decrement.
93*7c478bd9Sstevel@tonic-gate      */
increment(int value)94*7c478bd9Sstevel@tonic-gate     protected void increment(int value) {
95*7c478bd9Sstevel@tonic-gate 	setValue(getValue() + value);
96*7c478bd9Sstevel@tonic-gate     }
97*7c478bd9Sstevel@tonic-gate 
98*7c478bd9Sstevel@tonic-gate     /**
99*7c478bd9Sstevel@tonic-gate      * The current integer value associated with this text field.
100*7c478bd9Sstevel@tonic-gate      * @return the int value.
101*7c478bd9Sstevel@tonic-gate      */
getValue()102*7c478bd9Sstevel@tonic-gate     public int getValue() {
103*7c478bd9Sstevel@tonic-gate 	return value;
104*7c478bd9Sstevel@tonic-gate     }
105*7c478bd9Sstevel@tonic-gate 
106*7c478bd9Sstevel@tonic-gate     /**
107*7c478bd9Sstevel@tonic-gate      * Sets the current integer value associated with this text
108*7c478bd9Sstevel@tonic-gate      * field. The text field will display this value. If the value is not
109*7c478bd9Sstevel@tonic-gate      * valid then the old value will remain in effect.
110*7c478bd9Sstevel@tonic-gate      */
setValue(int newValue)111*7c478bd9Sstevel@tonic-gate 	public void setValue(int newValue) {
112*7c478bd9Sstevel@tonic-gate 		if (checkValue(newValue)) {
113*7c478bd9Sstevel@tonic-gate 			value = newValue;
114*7c478bd9Sstevel@tonic-gate 			setText(Integer.toString(newValue));
115*7c478bd9Sstevel@tonic-gate 		}
116*7c478bd9Sstevel@tonic-gate 	}
117*7c478bd9Sstevel@tonic-gate 
118*7c478bd9Sstevel@tonic-gate     /**
119*7c478bd9Sstevel@tonic-gate      * Sets the value for this text field from the given text.
120*7c478bd9Sstevel@tonic-gate      * @param text the text that this text field shoudl contain.
121*7c478bd9Sstevel@tonic-gate      * @exception NumberFormatException Thrown when the supplied text
122*7c478bd9Sstevel@tonic-gate      * cannot be parsed in to an interger value.
123*7c478bd9Sstevel@tonic-gate      */
setValueFromText(String text)124*7c478bd9Sstevel@tonic-gate 	public void setValueFromText(String text) throws NumberFormatException {
125*7c478bd9Sstevel@tonic-gate 		Integer i = Integer.valueOf(text);
126*7c478bd9Sstevel@tonic-gate 		setValue(i.intValue());
127*7c478bd9Sstevel@tonic-gate 	}
128*7c478bd9Sstevel@tonic-gate 
129*7c478bd9Sstevel@tonic-gate     /**
130*7c478bd9Sstevel@tonic-gate      * Checks to see if the given value
131*7c478bd9Sstevel@tonic-gate      * would be valid for this text
132*7c478bd9Sstevel@tonic-gate      * field. Classes deriving form this class should override this to
133*7c478bd9Sstevel@tonic-gate      * provide whatever checks they desire.
134*7c478bd9Sstevel@tonic-gate      * @param newValue
135*7c478bd9Sstevel@tonic-gate      * @return true if it will be valid,
136*7c478bd9Sstevel@tonic-gate      * false otherwise. This class
137*7c478bd9Sstevel@tonic-gate      * returns true always for all integer values.
138*7c478bd9Sstevel@tonic-gate      */
checkValue(int newValue)139*7c478bd9Sstevel@tonic-gate     public boolean checkValue(int newValue) {
140*7c478bd9Sstevel@tonic-gate 	return true;
141*7c478bd9Sstevel@tonic-gate     }
142*7c478bd9Sstevel@tonic-gate 
143*7c478bd9Sstevel@tonic-gate }
144