/*
 This file is part of the software framework JStatCom, 
 a library for building applications for data based analysis.

 Copyright (C) 2000-2005  Markus Kraetzig

 This library is free software; you can redistribute it and/or modify
 it under the terms of the GNU Lesser General Public License as
 published by the Free Software Foundation; either version 2.1 of the
 License, or (at your option) any later version.

 This library is distributed in the hope that it will be useful, but
 WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 Lesser General Public License for more details.

 You should have received a copy of the GNU Lesser General Public
 License along with this library; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 USA
 */

package com.jstatcom.component;
import com.jstatcom.model.JSCConstants;

/**
 * This component allows to verify an input string against
 * the rules for valid variable names throughout JStatCom.
 * Variable names must start with a non digit and may contain
 * digits, letters and '_'. the must not be null
 * or empty. Variable names are in general case insensitive.
 * Except for the last rule, the same syntax as
 * for variables in JAVA applies.
 * 
 * @author Markus Kraetzig
 */
public class VarNameSelector extends IValTextField {
	private String varName = null;

	/**
	 * VarNameSelector constructor which sets the default input
	 * to "varname".
	 */
	public VarNameSelector() {
		super();

		setSize(48, getHeight());
		setVarName("varname");
	}

	/**
	 * Gets the currently selected name.
	 * 
	 * @return the validated name
	 */
	public String getVarName() {
		return varName;
	}

	/**
	 * Error handling routine if validation failed. Displays error message
	 * and sets back string value to the last valid one..
	 * 
	 * @param msg an error message
	 */
	public void handleError(String msg) {
		super.handleError(
			msg + "\nThe input is set back to the previous value.");
		setVarName(varName);
	}

	/**
	 * Sets the selected name to newVarName,
	 * validates it and automatically sets it to lower case. 
	 * 
	 * @param  newVarName the new selected name
	 * @throws IllegalArgumentException if newVarName is not valid
	 */
	public void setVarName(String newVarName) {
		String error = JSCConstants.isValidName(newVarName);
		if (error != null)
			throw new IllegalArgumentException(error);
		varName = newVarName.trim().toLowerCase();
		setText(varName);
	}

	/**
	 * @see com.jstatcom.component.IValTextField
	 */
	public String validateInput() {

		String input = getText().trim().toLowerCase();
		String error = JSCConstants.isValidName(input);
		if (error == null)
			varName = input;
		return error;
	}
}
 

Ads help maintain this website.