Search in sources :

Example 6 with SelectModel

use of org.apache.tapestry5.SelectModel in project tapestry-5 by apache.

the class LocalizationSetterImplTest method get_locale_model.

@Test
public void get_locale_model() {
    LocalizationSetter setter = new LocalizationSetterImpl(null, null, null, "en,fr");
    SelectModel model = setter.getSupportedLocalesModel();
    assertNull(model.getOptionGroups());
    List<OptionModel> options = model.getOptions();
    assertEquals(options.size(), 2);
    assertEquals(options.get(0).getLabel(), "English");
    // Note that the label is localized to the underlying locale, not the default locale.
    // That's why its "français" (i.e., as a French speaker would say it), not "French"
    // (like an English speaker).
    assertEquals(options.get(1).getLabel(), "fran\u00e7ais");
    assertEquals(options.get(0).getValue(), Locale.ENGLISH);
    assertEquals(options.get(1).getValue(), Locale.FRENCH);
}
Also used : OptionModel(org.apache.tapestry5.OptionModel) LocalizationSetter(org.apache.tapestry5.services.LocalizationSetter) SelectModel(org.apache.tapestry5.SelectModel) Test(org.testng.annotations.Test)

Example 7 with SelectModel

use of org.apache.tapestry5.SelectModel in project tapestry-5 by apache.

the class OptionGroupForm method getModel.

public SelectModel getModel() {
    return new AbstractSelectModel() {

        private List<OptionGroupModel> groupModels = null;

        public List<OptionModel> getOptions() {
            return null;
        }

        public List<OptionGroupModel> getOptionGroups() {
            if (groupModels == null) {
                computeModel();
            }
            return groupModels;
        }

        private void computeModel() {
            groupModels = new ArrayList<OptionGroupModel>();
            for (Entity entity : entityList) {
                List<OptionModel> options = new ArrayList<OptionModel>();
                options.add(new OptionModelImpl(entity.getLabel(), entity));
                OptionGroupModel groupModel = new OptionGroupModelImpl(entity.getLabel(), false, options);
                groupModels.add(groupModel);
            }
        }
    };
}
Also used : Entity(org.apache.tapestry5.integration.app1.data.Entity) OptionGroupModel(org.apache.tapestry5.OptionGroupModel) OptionModelImpl(org.apache.tapestry5.internal.OptionModelImpl) AbstractSelectModel(org.apache.tapestry5.util.AbstractSelectModel) OptionGroupModelImpl(org.apache.tapestry5.internal.OptionGroupModelImpl) OptionModel(org.apache.tapestry5.OptionModel) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List)

Example 8 with SelectModel

use of org.apache.tapestry5.SelectModel in project tapestry-5 by apache.

the class TapestryModule method contributeTypeCoercer.

/**
 * Adds coercions:
 * <ul>
 * <li>String to {@link SelectModel}
 * <li>Map to {@link SelectModel}
 * <li>Collection to {@link GridDataSource}
 * <li>null to {@link GridDataSource}
 * <li>List to {@link SelectModel}
 * <li>{@link ComponentResourcesAware} (typically, a component) to {@link ComponentResources}
 * <li>{@link ComponentResources} to {@link PropertyOverrides}
 * <li>String to {@link Renderable}
 * <li>{@link Renderable} to {@link Block}
 * <li>String to {@link DateFormat}
 * <li>String to {@link Resource} (via {@link AssetSource#resourceForPath(String)})
 * <li>{@link Renderable} to {@link RenderCommand}</li>
 * <li>String to {@link Pattern}</li>
 * <li>String to {@link DateFormat}</li>
 * <li>{@link Resource} to {@link DynamicTemplate}</li>
 * <li>{@link Asset} to {@link Resource}</li>
 * <li>{@link ValueEncoder} to {@link ValueEncoderFactory}</li>
 * </ul>
 */
public static void contributeTypeCoercer(MappedConfiguration<CoercionTuple.Key, CoercionTuple> configuration, final ObjectLocator objectLocator, @Builtin final ThreadLocale threadLocale, @Core final AssetSource assetSource, @Core final DynamicTemplateParser dynamicTemplateParser) {
    CoercionTuple<ComponentResources, PropertyOverrides> componentResourcesToPropertyOverrides = CoercionTuple.create(ComponentResources.class, PropertyOverrides.class, new Coercion<ComponentResources, PropertyOverrides>() {

        public PropertyOverrides coerce(ComponentResources input) {
            return new PropertyOverridesImpl(input);
        }
    });
    configuration.add(componentResourcesToPropertyOverrides.getKey(), componentResourcesToPropertyOverrides);
    // See TAP5-2184 for why this causes some trouble!
    CoercionTuple<String, SelectModel> stringToSelectModel = CoercionTuple.create(String.class, SelectModel.class, new Coercion<String, SelectModel>() {

        public SelectModel coerce(String input) {
            return TapestryInternalUtils.toSelectModel(input);
        }
    });
    configuration.add(stringToSelectModel.getKey(), stringToSelectModel);
    CoercionTuple<Map, SelectModel> mapToSelectModel = CoercionTuple.create(Map.class, SelectModel.class, new Coercion<Map, SelectModel>() {

        @SuppressWarnings("unchecked")
        public SelectModel coerce(Map input) {
            return TapestryInternalUtils.toSelectModel(input);
        }
    });
    configuration.add(mapToSelectModel.getKey(), mapToSelectModel);
    CoercionTuple<Collection, GridDataSource> collectionToGridDataSource = CoercionTuple.create(Collection.class, GridDataSource.class, new Coercion<Collection, GridDataSource>() {

        public GridDataSource coerce(Collection input) {
            return new CollectionGridDataSource(input);
        }
    });
    configuration.add(collectionToGridDataSource.getKey(), collectionToGridDataSource);
    CoercionTuple<Void, GridDataSource> voidToGridDataSource = CoercionTuple.create(void.class, GridDataSource.class, new Coercion<Void, GridDataSource>() {

        private final GridDataSource source = new NullDataSource();

        public GridDataSource coerce(Void input) {
            return source;
        }
    });
    configuration.add(voidToGridDataSource.getKey(), voidToGridDataSource);
    CoercionTuple<List, SelectModel> listToSelectModel = CoercionTuple.create(List.class, SelectModel.class, new Coercion<List, SelectModel>() {

        private SelectModelFactory selectModelFactory;

        @SuppressWarnings("unchecked")
        public SelectModel coerce(List input) {
            // to another value, and a race condition is harmless.
            if (selectModelFactory == null) {
                selectModelFactory = objectLocator.getService(SelectModelFactory.class);
            }
            return selectModelFactory.create(input);
        }
    });
    configuration.add(listToSelectModel.getKey(), listToSelectModel);
    CoercionTuple<String, Pattern> stringToPattern = CoercionTuple.create(String.class, Pattern.class, new Coercion<String, Pattern>() {

        public Pattern coerce(String input) {
            return Pattern.compile(input);
        }
    });
    configuration.add(stringToPattern.getKey(), stringToPattern);
    CoercionTuple<ComponentResourcesAware, ComponentResources> componentResourcesAwareToComponentResources = CoercionTuple.create(ComponentResourcesAware.class, ComponentResources.class, new Coercion<ComponentResourcesAware, ComponentResources>() {

        public ComponentResources coerce(ComponentResourcesAware input) {
            return input.getComponentResources();
        }
    });
    configuration.add(componentResourcesAwareToComponentResources.getKey(), componentResourcesAwareToComponentResources);
    CoercionTuple<String, Renderable> stringToRenderable = CoercionTuple.create(String.class, Renderable.class, new Coercion<String, Renderable>() {

        public Renderable coerce(String input) {
            return new StringRenderable(input);
        }
    });
    configuration.add(stringToRenderable.getKey(), stringToRenderable);
    CoercionTuple<Renderable, Block> renderableToBlock = CoercionTuple.create(Renderable.class, Block.class, new Coercion<Renderable, Block>() {

        public Block coerce(Renderable input) {
            return new RenderableAsBlock(input);
        }
    });
    configuration.add(renderableToBlock.getKey(), renderableToBlock);
    CoercionTuple<String, DateFormat> stringToDateFormat = CoercionTuple.create(String.class, DateFormat.class, new Coercion<String, DateFormat>() {

        public DateFormat coerce(String input) {
            final SimpleDateFormat dateFormat = new SimpleDateFormat(input, threadLocale.getLocale());
            final String lenient = objectLocator.getService(SymbolSource.class).valueForSymbol(SymbolConstants.LENIENT_DATE_FORMAT);
            dateFormat.setLenient(Boolean.parseBoolean(lenient));
            return dateFormat;
        }
    });
    configuration.add(stringToDateFormat.getKey(), stringToDateFormat);
    CoercionTuple<String, Resource> stringToResource = CoercionTuple.create(String.class, Resource.class, new Coercion<String, Resource>() {

        public Resource coerce(String input) {
            return assetSource.resourceForPath(input);
        }
    });
    configuration.add(stringToResource.getKey(), stringToResource);
    CoercionTuple<Renderable, RenderCommand> renderableToRenderCommand = CoercionTuple.create(Renderable.class, RenderCommand.class, new Coercion<Renderable, RenderCommand>() {

        public RenderCommand coerce(final Renderable input) {
            return new RenderCommand() {

                public void render(MarkupWriter writer, RenderQueue queue) {
                    input.render(writer);
                }
            };
        }
    });
    configuration.add(renderableToRenderCommand.getKey(), renderableToRenderCommand);
    CoercionTuple<Date, Calendar> dateToCalendar = CoercionTuple.create(Date.class, Calendar.class, new Coercion<Date, Calendar>() {

        public Calendar coerce(Date input) {
            Calendar calendar = Calendar.getInstance(threadLocale.getLocale());
            calendar.setTime(input);
            return calendar;
        }
    });
    configuration.add(dateToCalendar.getKey(), dateToCalendar);
    CoercionTuple<Resource, DynamicTemplate> resourceToDynamicTemplate = CoercionTuple.create(Resource.class, DynamicTemplate.class, new Coercion<Resource, DynamicTemplate>() {

        public DynamicTemplate coerce(Resource input) {
            return dynamicTemplateParser.parseTemplate(input);
        }
    });
    configuration.add(resourceToDynamicTemplate.getKey(), resourceToDynamicTemplate);
    CoercionTuple<Asset, Resource> assetToResource = CoercionTuple.create(Asset.class, Resource.class, new Coercion<Asset, Resource>() {

        public Resource coerce(Asset input) {
            return input.getResource();
        }
    });
    configuration.add(assetToResource.getKey(), assetToResource);
    CoercionTuple<ValueEncoder, ValueEncoderFactory> valueEncoderToValueEncoderFactory = CoercionTuple.create(ValueEncoder.class, ValueEncoderFactory.class, new Coercion<ValueEncoder, ValueEncoderFactory>() {

        public ValueEncoderFactory coerce(ValueEncoder input) {
            return new GenericValueEncoderFactory(input);
        }
    });
    configuration.add(valueEncoderToValueEncoderFactory.getKey(), valueEncoderToValueEncoderFactory);
}
Also used : ValueEncoderFactory(org.apache.tapestry5.services.ValueEncoderFactory) ComponentResources(org.apache.tapestry5.ComponentResources) RenderCommand(org.apache.tapestry5.runtime.RenderCommand) Renderable(org.apache.tapestry5.Renderable) StringRenderable(org.apache.tapestry5.internal.util.StringRenderable) StringRenderable(org.apache.tapestry5.internal.util.StringRenderable) Asset(org.apache.tapestry5.Asset) List(java.util.List) SelectModelFactory(org.apache.tapestry5.services.SelectModelFactory) Resource(org.apache.tapestry5.commons.Resource) PropertyOverrides(org.apache.tapestry5.PropertyOverrides) SelectModel(org.apache.tapestry5.SelectModel) MarkupWriter(org.apache.tapestry5.MarkupWriter) ComponentResourcesAware(org.apache.tapestry5.runtime.ComponentResourcesAware) CollectionGridDataSource(org.apache.tapestry5.internal.grid.CollectionGridDataSource) Collection(java.util.Collection) RenderableAsBlock(org.apache.tapestry5.internal.util.RenderableAsBlock) Block(org.apache.tapestry5.Block) Map(java.util.Map) PropertyOverridesImpl(org.apache.tapestry5.internal.PropertyOverridesImpl) CollectionGridDataSource(org.apache.tapestry5.internal.grid.CollectionGridDataSource) GridDataSource(org.apache.tapestry5.grid.GridDataSource) ValueEncoder(org.apache.tapestry5.ValueEncoder) ContextValueEncoder(org.apache.tapestry5.services.ContextValueEncoder) RenderQueue(org.apache.tapestry5.runtime.RenderQueue) NullDataSource(org.apache.tapestry5.internal.grid.NullDataSource) Pattern(java.util.regex.Pattern) RenderableAsBlock(org.apache.tapestry5.internal.util.RenderableAsBlock) Calendar(java.util.Calendar) Date(java.util.Date) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) SimpleDateFormat(java.text.SimpleDateFormat) DynamicTemplate(org.apache.tapestry5.services.dynamic.DynamicTemplate)

Example 9 with SelectModel

use of org.apache.tapestry5.SelectModel in project tapestry-5 by apache.

the class SelectTest method checkSubmittedOption.

/**
 * Utility for testing the "secure" option with various values and model
 * states. This avoids a lot of redundant test setup code.
 *
 * @param withModel whether there should be a model to test against
 * @param secureOption which "secure" option to test
 * @param expectedError the expected error message, nor null if no error
 * @throws ValidationException
 */
private void checkSubmittedOption(boolean withModel, SecureOption secureOption, String expectedError) throws ValidationException {
    ValueEncoder<Platform> encoder = getService(ValueEncoderSource.class).getValueEncoder(Platform.class);
    ValidationTracker tracker = mockValidationTracker();
    Request request = mockRequest();
    Messages messages = mockMessages();
    FieldValidationSupport fvs = mockFieldValidationSupport();
    TypeCoercer typeCoercer = mockTypeCoercer();
    InternalComponentResources resources = mockInternalComponentResources();
    Binding selectModelBinding = mockBinding();
    expect(request.getParameter("xyz")).andReturn("MAC");
    expect(messages.contains(EasyMock.anyObject(String.class))).andReturn(false).anyTimes();
    expect(resources.getBinding("model")).andReturn(selectModelBinding);
    final Holder<SelectModel> modelHolder = Holder.create();
    expect(typeCoercer.coerce(EasyMock.or(EasyMock.isA(SelectModel.class), EasyMock.isNull()), EasyMock.eq(SelectModel.class))).andAnswer(new IAnswer<SelectModel>() {

        @Override
        public SelectModel answer() throws Throwable {
            return modelHolder.get();
        }
    });
    expect(selectModelBinding.get()).andAnswer(new IAnswer<SelectModel>() {

        @Override
        public SelectModel answer() throws Throwable {
            return modelHolder.get();
        }
    });
    Select select = new Select();
    tracker.recordInput(select, "MAC");
    // when not failing we will expect to call the fvs.validate method
    if (expectedError == null) {
        fvs.validate(Platform.MAC, resources, null);
    } else {
        tracker.recordError(EasyMock.eq(select), EasyMock.contains(expectedError));
    }
    replay();
    if (withModel) {
        modelHolder.put(new EnumSelectModel(Platform.class, messages));
    }
    set(select, "encoder", encoder);
    set(select, "model", modelHolder.get());
    set(select, "request", request);
    set(select, "secure", secureOption);
    // Disable BeanValidationContextSupport
    set(select, "beanValidationDisabled", true);
    set(select, "tracker", tracker);
    set(select, "fieldValidationSupport", fvs);
    set(select, "typeCoercer", typeCoercer);
    set(select, "resources", resources);
    select.processSubmission("xyz");
    if (expectedError == null) {
        assertEquals(get(select, "value"), Platform.MAC);
    }
    verify();
}
Also used : Messages(org.apache.tapestry5.commons.Messages) Platform(org.apache.tapestry5.corelib.components.SelectTest.Platform) InternalComponentResources(org.apache.tapestry5.internal.InternalComponentResources) TypeCoercer(org.apache.tapestry5.commons.services.TypeCoercer) Request(org.apache.tapestry5.http.services.Request) ValueEncoderSource(org.apache.tapestry5.services.ValueEncoderSource) EnumSelectModel(org.apache.tapestry5.util.EnumSelectModel) EnumSelectModel(org.apache.tapestry5.util.EnumSelectModel)

Example 10 with SelectModel

use of org.apache.tapestry5.SelectModel in project tapestry-5 by apache.

the class EnumSelectModelTest method prefixed_name_in_message_catalog.

@Test
public void prefixed_name_in_message_catalog() {
    Messages messages = mockMessages();
    stub_contains(messages, false);
    train_contains(messages, "Stooge.LARRY", true);
    train_get(messages, "Stooge.LARRY", "Mr. Larry Fine");
    replay();
    SelectModel model = new EnumSelectModel(Stooge.class, messages);
    List<OptionModel> options = model.getOptions();
    assertEquals(options.size(), 3);
    checkOption(options, 0, "Moe", Stooge.MOE);
    checkOption(options, 1, "Mr. Larry Fine", Stooge.LARRY);
    checkOption(options, 2, "Curly Joe", Stooge.CURLY_JOE);
    verify();
}
Also used : Messages(org.apache.tapestry5.commons.Messages) OptionModel(org.apache.tapestry5.OptionModel) SelectModel(org.apache.tapestry5.SelectModel) Test(org.testng.annotations.Test)

Aggregations

OptionModel (org.apache.tapestry5.OptionModel)7 Test (org.testng.annotations.Test)7 SelectModel (org.apache.tapestry5.SelectModel)6 Messages (org.apache.tapestry5.commons.Messages)6 TypeCoercer (org.apache.tapestry5.commons.services.TypeCoercer)3 OptionModelImpl (org.apache.tapestry5.internal.OptionModelImpl)3 List (java.util.List)2 Request (org.apache.tapestry5.http.services.Request)2 InternalComponentResources (org.apache.tapestry5.internal.InternalComponentResources)2 SelectModelImpl (org.apache.tapestry5.internal.SelectModelImpl)2 DateFormat (java.text.DateFormat)1 SimpleDateFormat (java.text.SimpleDateFormat)1 ArrayList (java.util.ArrayList)1 Calendar (java.util.Calendar)1 Collection (java.util.Collection)1 Date (java.util.Date)1 Map (java.util.Map)1 Pattern (java.util.regex.Pattern)1 Asset (org.apache.tapestry5.Asset)1 Block (org.apache.tapestry5.Block)1