xref: /titanic_41/usr/src/cmd/krb5/kadmin/gui/dchanger/DCCircularTextField.java (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
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 java.awt.TextField;
30 
31     /**
32      * This class is a DCTextField that allows values to wrap around over
33      * a maximum and a minimum value.
34      */
35     public class DCCircularTextField extends DCTextField {
36     private int maximum = 59;
37     private int minimum = 0;
38 
39     /**
40      * Constructor for DCCircularTextField.
41      * @param text the text to initilize the text field with
42      * @param columns the width of the text field in number of columns
43      */
DCCircularTextField(String text, int columns)44     public DCCircularTextField(String text, int columns) {
45 	super(text, columns);
46     }
47 
48     /**
49      * Sets the maximum allowable value for this field. If the current
50      * value is greater than this maximum value, then the current value is
51      * set to the maximum value.
52      * @param maxValue the maximum integer value for this text field.
53      */
setMaximum(int maxValue)54     public void setMaximum(int maxValue) {
55 	maximum = maxValue;
56 	if (getValue() > maxValue)
57 	    super.setValue(maxValue);
58     }
59 
60     /**
61      * Sets the minimum allowable value for this field. If the current
62      * value is less than this minimum value, then the current value is
63      * set to the minimum value.
64      * @param minValue the minimum integer value for this text field.
65      */
setMinimum(int minValue)66     public void setMinimum(int minValue) {
67 	minimum = minValue;
68 	if (getValue() < minValue)
69 	    super.setValue(minValue);
70     }
71 
72     /**
73      * Increments the value of the textfield. It does a wrap around on
74      * the max value and min value.
75      * @param value how much to increment by. It can be negative if one
76      * desires to decrement.
77      */
increment(int value)78     protected final void increment(int value) {
79 
80     int current = getValue();
81     int next = (current + value);
82 
83     /*
84      * Now wrap it around this way:
85      */
86 
87     /*
88      *
89      * (1) Translate coordinates by 'minimum' to get the minimum to 0
90      *     eg. the legal range -1..5   to   0..6
91      *                      or  1..5   to   0..4
92      */
93     int transMax   = maximum - minimum;
94     int transValue = next    - minimum;
95 
96     /*
97      * (2) Now do circular math
98      */
99     transValue %= (transMax + 1); // modulo max+1 since max is permissible
100     transValue = (transValue < 0)? (transValue + (transMax+1)) : transValue;
101 
102     /*
103      * (3) Translate back to old coordinates
104      */
105     next = transValue + minimum;
106 
107     setValue(next);
108     }
109 
110     /**
111      * Checks to see if the given value would be
112      * valid for this text
113      * field. The check looks to see if the value is less than the
114      * minimum value or greater than the maximum value.
115      * @param newValue
116      * @return true if it will be valid, false otherwise.
117      */
checkValue(int value)118     public boolean checkValue(int value) {
119     if (value > maximum || value < minimum)
120         return false;
121     else
122         return true;
123     }
124 }
125