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