예제 #1
0
  /**
   * Gets the date pattern to use for putting selected values in the coupled component. If you
   * override this method to support components that would otherwise not be supported, you should
   * override {@link #checkComponentProvidesDateFormat(Component)} and let it return normally.
   *
   * @return The date pattern
   */
  protected String getDatePattern() {

    if (component instanceof ITextFormatProvider) {
      return ((ITextFormatProvider) component).getTextFormat();
    } else {
      // cast from hell, but we checked before whether we could
      IConverter converter = component.getConverter(DateTime.class);
      if (converter == null) {
        converter = component.getConverter(Date.class);
      }
      return ((SimpleDateFormat) ((DateConverter) converter).getDateFormat(component.getLocale()))
          .toPattern();
    }
  }
예제 #2
0
  /**
   * Check that this behavior can get a date format out of the component it is coupled to. if you
   * override this method to allow for other types (such as your own), you should override {@link
   * #getDatePattern()} as well. This method should return normally if the component is accepted or
   * throw a RTE when it is not.
   *
   * @param component the component this behavior is being coupled to
   * @throws WicketRuntimeException if the component is not support.
   */
  protected void checkComponentProvidesDateFormat(Component component) {

    if (component instanceof ITextFormatProvider) {
      // were ok
      return;
    }

    IConverter converter = component.getConverter(DateTime.class);
    if (converter == null) {
      converter = component.getConverter(Date.class);
    }
    if (converter instanceof DateConverter) {

      return; // This is ok
    }
    throw new WicketRuntimeException(
        "this behavior can only be added to components that either implement "
            + ITextFormatProvider.class.getName()
            + " or that use "
            + DateConverter.class.getName()
            + " configured with an instance of "
            + SimpleDateFormat.class.getName()
            + " (like Wicket's default configuration has)");
  }