Skip to content

Commit

Permalink
Added InputComponents.setDefaultValueFor
Browse files Browse the repository at this point in the history
  • Loading branch information
gastaldi committed Jun 12, 2013
1 parent b328428 commit 6da8d6c
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,16 +88,36 @@ public static void setValueFor(final ConverterFactory converterFactory, final In
{
if (component instanceof SingleValued)
{
setSingleInputValue(converterFactory, component, value);
setSingleInputValue(converterFactory, component, value, false);
}
else if (component instanceof ManyValued)
{
setManyInputValue(converterFactory, component, value);
setManyInputValue(converterFactory, component, value, false);
}
}

/**
* Sets the default value in the provided {@link InputComponent}, making any necessary conversions
*
* @param component
* @param value
*/
public static void setDefaultValueFor(final ConverterFactory converterFactory,
final InputComponent<?, Object> component,
final Object value)
{
if (component instanceof SingleValued)
{
setSingleInputValue(converterFactory, component, value, true);
}
else if (component instanceof ManyValued)
{
setManyInputValue(converterFactory, component, value, true);
}
}

private static void setSingleInputValue(final ConverterFactory converterFactory,
final InputComponent<?, Object> input, final Object value)
final InputComponent<?, Object> input, final Object value, boolean defaultValue)
{
final Object convertedType;
if (value != null)
Expand All @@ -108,11 +128,18 @@ private static void setSingleInputValue(final ConverterFactory converterFactory,
{
convertedType = null;
}
((SingleValued) input).setValue(convertedType);
if (defaultValue)
{
((SingleValued) input).setDefaultValue(convertedType);
}
else
{
((SingleValued) input).setValue(convertedType);
}
}

private static void setManyInputValue(final ConverterFactory converterFactory,
final InputComponent<?, Object> input, Object value)
final InputComponent<?, Object> input, Object value, boolean defaultValue)
{
final Iterable<Object> convertedValues;
if (value != null)
Expand All @@ -135,7 +162,14 @@ private static void setManyInputValue(final ConverterFactory converterFactory,
{
convertedValues = null;
}
((ManyValued) input).setValue(convertedValues);
if (defaultValue)
{
((ManyValued) input).setDefaultValue(convertedValues);
}
else
{
((ManyValued) input).setValue(convertedValues);
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,8 @@ private void preconfigureInput(InputComponent<?, ?> input, WithAttributes atts)
ConverterFactory converterFactory = exportedInstance.get();
try
{
InputComponents.setValueFor(converterFactory, (InputComponent<?, Object>) input, atts.defaultValue());
InputComponents.setDefaultValueFor(converterFactory, (InputComponent<?, Object>) input,
atts.defaultValue());
}
finally
{
Expand Down

0 comments on commit 6da8d6c

Please sign in to comment.