Code example for SimpleDateFormat

Methods: setLenient, setTimeZone, toLocalizedPattern

0
  { 
    if (parameterFormatString != null)
    { 
      try 
      { 
        final SimpleDateFormat dateFormat = new SimpleDateFormat(parameterFormatString, new DateFormatSymbols(locale));
        dateFormat.setTimeZone(timeZone);
        dateFormat.setLenient(true);
        return dateFormat;
      } 
      catch (Exception e)
      { 
        // boo! Not a valid pattern ... 
        // its not a show-stopper either, as the pattern is a mere hint, not a mandatory thing 
        logger.warn("Parameter format-string for date-parameter was not a valid date-format-string", e);
      } 
    } 
 
    final SimpleDateFormat dateFormat = new SimpleDateFormat(DEFAULT_FORMAT, new DateFormatSymbols(locale));
    dateFormat.setTimeZone(timeZone);
    dateFormat.setLenient(true);
    return dateFormat;
  } 
 
  private void setDate(final Object value)
  { 
    if (value == null)
    { 
      return; 
    } 
    if (value instanceof String)
    { 
      // if its a string, then it must be in the normalized parameter format. 
      try 
      { 
        final Date date = convertValue((String) value);
        this.date = (date);
        dateField.setText(sdf.format(date));
      } 
      catch (Exception e)
      { 
        logger.debug("Unparsable date-string", e);
      } 
    } 
    else if (value instanceof Date)
    { 
      final Date date = (Date) value;
      this.date = (date);
      dateField.setText(sdf.format(date));
    } 
    else 
    { 
      logger.debug("Date-parameter must be set either as normalized date-string or as date-object: " +
          value + " [" + value.getClass() + "]");
    } 
  } 
 
  protected Date convertValue(final String text)
  { 
    if (StringUtils.isEmpty(text))
    { 
      return null; 
    } 
 
    try 
    { 
      final Date o = (Date) sdf.parseObject(text);
      // this magic converts the date or number value to the real type. 
      // the formatter always returns doubles/bigdecimals or java.util.Dates 
      // but we may need sql-dates, long-objects etc .. 
      return DateConverter.convertToDateType(o, dateType);
    } 
    catch (ParseException e)
    { 
      throw new RuntimeException("Failed to format object", e);
    } 
  } 
 
  /** 
   * Sets an initial <code>value</code> for the editor.  This will cause 
   * the editor to <code>stopEditing</code> and lose any partially 
   * edited value if the editor is editing when this method is called. <p> 
   * <p/> 
   * Returns the component that should be added to the client's 
   * <code>Component</code> hierarchy.  Once installed in the client's 
   * hierarchy this component will then be able to draw and receive 
   * user input. 
   * 
   * @param table      the <code>JTable</code> that is asking the 
   *                   editor to edit; can be <code>null</code> 
   * @param value      the value of the cell to be edited; it is 
   *                   up to the specific editor to interpret 
   *                   and draw the value.  For example, if value is 
   *                   the string "true", it could be rendered as a 
   *                   string or it could be rendered as a check 
   *                   box that is checked.  <code>null</code> 
   *                   is a valid value 
   * @param isSelected true if the cell is to be rendered with 
   *                   highlighting 
   * @param row        the row of the cell being edited 
   * @param column     the column of the cell being edited 
   * @return the component for editing 
   */ 
  public Component getTableCellEditorComponent(final JTable table,
                                               final Object value,
                                               final boolean isSelected,
                                               final int row,
                                               final int column)
  { 
    setDate(value);
    return this;
  } 
 
  /** 
   * Returns the value contained in the editor. 
   * 
   * @return the value contained in the editor 
   */ 
  public Object getCellEditorValue()
  { 
    return date;
  } 
 
  /** 
   * Asks the editor if it can start editing using <code>anEvent</code>. <code>anEvent</code> is in the invoking 
   * component coordinate system. The editor can not assume the Component returned by 
   * <code>getCellEditorComponent</code> is installed.  This method is intended for the use of client to avoid the cost 
   * of setting up and installing the editor component if editing is not possible. If editing can be started this method 
   * returns true. 
   * 
   * @param anEvent the event the editor should use to consider whether to begin editing or not 
   * @return true if editing can be started 
   * @see #shouldSelectCell 
   */ 
  public boolean isCellEditable(final EventObject anEvent)
  { 
    if (anEvent instanceof MouseEvent)
    { 
      final MouseEvent mouseEvent = (MouseEvent) anEvent;
      return mouseEvent.getClickCount() >= 2 && mouseEvent.getButton() == MouseEvent.BUTTON1;
    } 
    return true; 
  } 
 
  /** 
   * Returns true if the editing cell should be selected, false otherwise. Typically, the return value is true, because 
   * is most cases the editing cell should be selected.  However, it is useful to return false to keep the selection 
   * from changing for some types of edits. eg. A table that contains a column of check boxes, the user might want to be 
   * able to change those checkboxes without altering the selection.  (See Netscape Communicator for just such an 
   * example) Of course, it is up to the client of the editor to use the return value, but it doesn't need to if it 
   * doesn't want to. 
   * 
   * @param anEvent the event the editor should use to start editing 
   * @return true if the editor would like the editing cell to be selected; otherwise returns false 
   * @see #isCellEditable 
   */ 
  public boolean shouldSelectCell(final EventObject anEvent)
  { 
    return true; 
  } 
 
  /** 
   * Tells the editor to stop editing and accept any partially edited value as the value of the editor.  The editor 
   * returns false if editing was not stopped; this is useful for editors that validate and can not accept invalid 
   * entries. 
   * 
   * @return true if editing was stopped; false otherwise 
   */ 
  public boolean stopCellEditing() 
  { 
    fireEditingStopped(); 
    return true; 
  } 
 
  /** 
   * Tells the editor to cancel editing and not accept any partially edited value. 
   */ 
  public void cancelCellEditing() 
  { 
    fireEditingCanceled(); 
  } 
 
 
  protected void fireEditingCanceled() 
  { 
    final CellEditorListener[] listeners = this.listeners.getListeners(CellEditorListener.class);
    final ChangeEvent event = new ChangeEvent(this);
    for (int i = 0; i < listeners.length; i++)
    { 
      final CellEditorListener listener = listeners[i];
      listener.editingCanceled(event);
    } 
  } 
 
 
  protected void fireEditingStopped() 
  { 
    final CellEditorListener[] listeners = this.listeners.getListeners(CellEditorListener.class);
    final ChangeEvent event = new ChangeEvent(this);
    for (int i = 0; i < listeners.length; i++)
    { 
      final CellEditorListener listener = listeners[i];
      listener.editingStopped(event);
    } 
  } 
 
  /** 
   * Adds a listener to the list that's notified when the editor 
   * stops, or cancels editing. 
   * 
   * @param l the CellEditorListener 
   */ 
  public void addCellEditorListener(final CellEditorListener l)
  { 
    listeners.add(CellEditorListener.class, l);
  } 
 
  /** 
   * Removes a listener from the list that's notified 
   * 
   * @param l the CellEditorListener 
   */ 
  public void removeCellEditorListener(final CellEditorListener l)
  { 
    listeners.remove(CellEditorListener.class, l);
  } 
 
  public void setDateFormat(final DateFormat timeFormat)
  { 
    if (timeFormat == null)
    { 
      throw new NullPointerException();
    } 
    sdf = timeFormat;
    setDate(getCellEditorValue()); 
    if (timeFormat instanceof SimpleDateFormat)
    { 
      final SimpleDateFormat dateFormat = (SimpleDateFormat) timeFormat;
      setToolTipText(dateFormat.toLocalizedPattern());
    } 
    else 
    { 
      setToolTipText(null); 
    }