1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * ident	"%Z%%M%	%I%	%E% SMI"
24*0Sstevel@tonic-gate  *
25*0Sstevel@tonic-gate  * Copyright (c) 1999-2000 by Sun Microsystems, Inc.
26*0Sstevel@tonic-gate  * All rights reserved.
27*0Sstevel@tonic-gate  */
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate     import java.awt.TextField;
30*0Sstevel@tonic-gate 
31*0Sstevel@tonic-gate     /**
32*0Sstevel@tonic-gate      * This class is a DCTextField that allows values to wrap around over
33*0Sstevel@tonic-gate      * a maximum and a minimum value.
34*0Sstevel@tonic-gate      */
35*0Sstevel@tonic-gate     public class DCCircularTextField extends DCTextField {
36*0Sstevel@tonic-gate     private int maximum = 59;
37*0Sstevel@tonic-gate     private int minimum = 0;
38*0Sstevel@tonic-gate 
39*0Sstevel@tonic-gate     /**
40*0Sstevel@tonic-gate      * Constructor for DCCircularTextField.
41*0Sstevel@tonic-gate      * @param text the text to initilize the text field with
42*0Sstevel@tonic-gate      * @param columns the width of the text field in number of columns
43*0Sstevel@tonic-gate      */
DCCircularTextField(String text, int columns)44*0Sstevel@tonic-gate     public DCCircularTextField(String text, int columns) {
45*0Sstevel@tonic-gate 	super(text, columns);
46*0Sstevel@tonic-gate     }
47*0Sstevel@tonic-gate 
48*0Sstevel@tonic-gate     /**
49*0Sstevel@tonic-gate      * Sets the maximum allowable value for this field. If the current
50*0Sstevel@tonic-gate      * value is greater than this maximum value, then the current value is
51*0Sstevel@tonic-gate      * set to the maximum value.
52*0Sstevel@tonic-gate      * @param maxValue the maximum integer value for this text field.
53*0Sstevel@tonic-gate      */
setMaximum(int maxValue)54*0Sstevel@tonic-gate     public void setMaximum(int maxValue) {
55*0Sstevel@tonic-gate 	maximum = maxValue;
56*0Sstevel@tonic-gate 	if (getValue() > maxValue)
57*0Sstevel@tonic-gate 	    super.setValue(maxValue);
58*0Sstevel@tonic-gate     }
59*0Sstevel@tonic-gate 
60*0Sstevel@tonic-gate     /**
61*0Sstevel@tonic-gate      * Sets the minimum allowable value for this field. If the current
62*0Sstevel@tonic-gate      * value is less than this minimum value, then the current value is
63*0Sstevel@tonic-gate      * set to the minimum value.
64*0Sstevel@tonic-gate      * @param minValue the minimum integer value for this text field.
65*0Sstevel@tonic-gate      */
setMinimum(int minValue)66*0Sstevel@tonic-gate     public void setMinimum(int minValue) {
67*0Sstevel@tonic-gate 	minimum = minValue;
68*0Sstevel@tonic-gate 	if (getValue() < minValue)
69*0Sstevel@tonic-gate 	    super.setValue(minValue);
70*0Sstevel@tonic-gate     }
71*0Sstevel@tonic-gate 
72*0Sstevel@tonic-gate     /**
73*0Sstevel@tonic-gate      * Increments the value of the textfield. It does a wrap around on
74*0Sstevel@tonic-gate      * the max value and min value.
75*0Sstevel@tonic-gate      * @param value how much to increment by. It can be negative if one
76*0Sstevel@tonic-gate      * desires to decrement.
77*0Sstevel@tonic-gate      */
increment(int value)78*0Sstevel@tonic-gate     protected final void increment(int value) {
79*0Sstevel@tonic-gate 
80*0Sstevel@tonic-gate     int current = getValue();
81*0Sstevel@tonic-gate     int next = (current + value);
82*0Sstevel@tonic-gate 
83*0Sstevel@tonic-gate     /*
84*0Sstevel@tonic-gate      * Now wrap it around this way:
85*0Sstevel@tonic-gate      */
86*0Sstevel@tonic-gate 
87*0Sstevel@tonic-gate     /*
88*0Sstevel@tonic-gate      *
89*0Sstevel@tonic-gate      * (1) Translate coordinates by 'minimum' to get the minimum to 0
90*0Sstevel@tonic-gate      *     eg. the legal range -1..5   to   0..6
91*0Sstevel@tonic-gate      *                      or  1..5   to   0..4
92*0Sstevel@tonic-gate      */
93*0Sstevel@tonic-gate     int transMax   = maximum - minimum;
94*0Sstevel@tonic-gate     int transValue = next    - minimum;
95*0Sstevel@tonic-gate 
96*0Sstevel@tonic-gate     /*
97*0Sstevel@tonic-gate      * (2) Now do circular math
98*0Sstevel@tonic-gate      */
99*0Sstevel@tonic-gate     transValue %= (transMax + 1); // modulo max+1 since max is permissible
100*0Sstevel@tonic-gate     transValue = (transValue < 0)? (transValue + (transMax+1)) : transValue;
101*0Sstevel@tonic-gate 
102*0Sstevel@tonic-gate     /*
103*0Sstevel@tonic-gate      * (3) Translate back to old coordinates
104*0Sstevel@tonic-gate      */
105*0Sstevel@tonic-gate     next = transValue + minimum;
106*0Sstevel@tonic-gate 
107*0Sstevel@tonic-gate     setValue(next);
108*0Sstevel@tonic-gate     }
109*0Sstevel@tonic-gate 
110*0Sstevel@tonic-gate     /**
111*0Sstevel@tonic-gate      * Checks to see if the given value would be
112*0Sstevel@tonic-gate      * valid for this text
113*0Sstevel@tonic-gate      * field. The check looks to see if the value is less than the
114*0Sstevel@tonic-gate      * minimum value or greater than the maximum value.
115*0Sstevel@tonic-gate      * @param newValue
116*0Sstevel@tonic-gate      * @return true if it will be valid, false otherwise.
117*0Sstevel@tonic-gate      */
checkValue(int value)118*0Sstevel@tonic-gate     public boolean checkValue(int value) {
119*0Sstevel@tonic-gate     if (value > maximum || value < minimum)
120*0Sstevel@tonic-gate         return false;
121*0Sstevel@tonic-gate     else
122*0Sstevel@tonic-gate         return true;
123*0Sstevel@tonic-gate     }
124*0Sstevel@tonic-gate }
125