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