/** * Implement the interface for validating and converting to internal object. Null is a valid * successful return, so errors are indicated only by existance or not of a message in the * messageBuffer. */ public Object validateAndConvert(String value, Object originalValue, StringBuffer messageBuffer) { // handle null, which is shown as the special string "<null>" if (value.equals("<null>") || value.equals("")) return null; // Do the conversion into the object in a safe manner try { if (useJavaDefaultFormat) { // allow the user to enter just the hour or just hour and minute // and assume the un-entered values are 0 int firstColon = value.indexOf(":"); if (firstColon == -1) { // user just entered the hour, so append min & sec value = value + ":0:0"; } else { // user entered hour an min. See if they also entered secs if (value.indexOf(":", firstColon + 1) == -1) { // user did not enter seconds value = value + ":0"; } } Object obj = Time.valueOf(value); return obj; } else { // use the DateFormat to parse java.util.Date javaDate = dateFormat.parse(value); return new Time(javaDate.getTime()); } } catch (Exception e) { messageBuffer.append(e.toString() + "\n"); // ?? do we need the message also, or is it automatically part of the toString()? // messageBuffer.append(e.getMessage()); return null; } }
/** User has clicked OK in the surrounding JPanel, so save the current state of all variables */ public void ok() { // get the values from the controls and set them in the static properties useJavaDefaultFormat = useJavaDefaultFormatChk.isSelected(); DTProperties.put( thisClassName, "useJavaDefaultFormat", Boolean.valueOf(useJavaDefaultFormat).toString()); localeFormat = dateFormatTypeDrop.getValue(); dateFormat = new ThreadSafeDateFormat(localeFormat, true); // lenient is set next DTProperties.put(thisClassName, "localeFormat", Integer.toString(localeFormat)); lenient = lenientChk.isSelected(); dateFormat.setLenient(lenient); DTProperties.put(thisClassName, "lenient", Boolean.valueOf(lenient).toString()); initDateFormat(localeFormat, lenient); }
/** Render a value into text for this DataType. */ public String renderObject(Object value) { // use the Java default date-to-string if (useJavaDefaultFormat == true || value == null) return (String) _renderer.renderObject(value); // use a date formatter try { return (String) _renderer.renderObject(dateFormat.format(value)); } catch (Exception e) { if (false == _renderExceptionHasBeenLogged) { _renderExceptionHasBeenLogged = true; s_log.error("Could not format \"" + value + "\" as date type", e); } return (String) _renderer.renderObject(value); } }
/** Defines the dateFormat with the specific format and lenient options */ private static void initDateFormat(int format, boolean lenient) { dateFormat = new ThreadSafeDateFormat(format, true); // lenient is set next dateFormat.setLenient(lenient); }