/*
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.ts;
import org.apache.log4j.Logger;
import com.jstatcom.component.IValTextField;
/**
* Component for display and selection of a single TSDate. It
* takes an input string and validates it against a parser and a defined range.
* This component can be costumized by setting an overall range that must
* enclose the date that is selected. This is ensured by the validation
* mechanism.
*
* This class fires a "TSDate" property change, if a new date was set.
*
* @see com.jstatcom.ts.TSDate
* @see com.jstatcom.parser.TSDateParser
* @author Markus Kraetzig
*/
public final class TSDateSelector extends IValTextField {
private static final Logger log = Logger.getLogger(TSDateSelector.class);
/**
* Range that the input must be in with.
*/
private TSDateRange range = null;
/**
* Currently displayed date.
*/
private TSDate tSDate = null;
/**
* TSDateSelector default constructor sets range to
* [1960 q1,1982 q4] and displayed date to
* 1960 q1.
*/
public TSDateSelector() {
super();
TSDate t1 = TSDate.valueOf("1960 q1");
TSDate t2 = TSDate.valueOf("1982 q4");
range = new TSDateRange(t1, t2);
setTSDate(t1);
setSize(39, getHeight());
}
/**
* Returns the currently displayed date as an index relative to the lower
* bound of the defined range, starting from 0.
*
* @return index that is >= 0 and < than the number of observations within
* the allowed range
*/
public int getDateAsIndex() {
return range.indexForDate(tSDate);
}
/**
* Gets the range that the selected date must be in with.
*
* @return the bounding range
*/
public TSDateRange getRange() {
return range;
}
/**
* Gets the currently displayed date.
*
* @return the selected date
*/
public TSDate getTSDate() {
return tSDate;
}
/**
* Error handling routine if validation failed. Displays error message and
* sets back date value to the last valid one..
*
* @param msg
* an error message
*/
public void handleError(String msg) {
super.handleError(msg + "\nThe previous value {" + tSDate
+ "} is restored.");
setTSDate(tSDate);
}
/**
* Sets the range that the selected date must be in with by setting an
* initial date and the desired number of available periods within the
* range.
*
* @param newStart
* the new start for the bounding range
* @param numberOfPeriods >
* 0 to define the end of the bounding range
*/
public void setEnclosingRange(TSDate newStart, int numberOfPeriods) {
setEnclosingRange(new TSDateRange(newStart, newStart
.addPeriods(numberOfPeriods - 1)));
}
/**
* Sets the range that the selected date must be in with. The selected date
* is not changed unless it violates the new range setting. In that case it
* is set to the start date of the new range.
*
* @param newOverallRange
* the new bounding range that the date has to be in with
* @throws IllegalArgumentException
* if (newOverallRange == null)
*/
public void setEnclosingRange(TSDateRange newOverallRange) {
if (newOverallRange == null)
throw new IllegalArgumentException("Range cannot be null.");
range = newOverallRange;
if (range.encloses(tSDate) != null)
setTSDate(range.lowerBound());
}
/**
* Sets the currently displayed date if it conforms to the specified range.
* This method fires a "TSDate" property change with the old value and the
* changed value if a new date was set.
*
* @param newTSDate
* the date to be displayed
* @throws IllegalArgumentException
* if newTSDate is not enclosed in range
*/
public void setTSDate(TSDate newTSDate) {
String msg;
if ((msg = range.encloses(newTSDate)) != null)
throw new IllegalArgumentException(msg);
TSDate oldDate = tSDate;
tSDate = newTSDate;
setTextCarefully(tSDate.toString());
// fire property change
if (oldDate != null && !oldDate.equals(tSDate))
firePropertyChange("TSDate", oldDate, tSDate);
}
/**
* Validates input against the TSDateParser and the specified
* TSDateRange.
*
* @return error message if failure, null if ok
*/
public String validateInput() {
TSDate tmpDate = null;
String inputString = getText();
try {
tmpDate = TSDate.valueOf(inputString);
} catch (Throwable ex) {
String msg = "Input \"" + inputString + "\" is not a valid date.";
log.warn(msg, ex);
return msg;
}
String error = range.encloses(tmpDate);
if (error == null)
setTSDate(tmpDate);
return error;
}
}
Ads help maintain this website.