123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- package com.vaadin.tests.components;
-
- import java.lang.reflect.ParameterizedType;
- import java.lang.reflect.Type;
- import java.util.ArrayList;
- import java.util.List;
-
- import com.googlecode.gentyref.GenericTypeReflector;
- import com.vaadin.data.HasValue;
- import com.vaadin.server.VaadinRequest;
- import com.vaadin.ui.AbsoluteLayout;
- import com.vaadin.ui.AbsoluteLayout.ComponentPosition;
- import com.vaadin.ui.Component;
- import com.vaadin.ui.CssLayout;
- import com.vaadin.ui.FormLayout;
- import com.vaadin.ui.GridLayout;
- import com.vaadin.ui.HorizontalLayout;
- import com.vaadin.ui.Layout;
- import com.vaadin.ui.VerticalLayout;
-
- /**
- * @author Vaadin Ltd
- *
- */
- public abstract class HasValueRequiredIndicator<C extends HasValue & Component>
- extends AbstractTestUI {
-
- private static final List<Class<? extends Layout>> LAYOUTS = getVaadinLayouts();
-
- @Override
- protected void setup(VaadinRequest request) {
- getContent().setSizeFull();
- getVaadinLayouts().stream().map(this::createLayout).forEach(layout -> {
- addComponent(layout, createComponent());
- addComponent(layout);
- });
- }
-
- protected void addComponent(Layout layout, C component) {
- layout.addComponent(component);
- if (layout instanceof AbsoluteLayout) {
- AbsoluteLayout absLayout = (AbsoluteLayout) layout;
- ComponentPosition position = absLayout.new ComponentPosition();
- position.setTop(30f, Unit.PIXELS);
- absLayout.setPosition(component, position);
- }
- }
-
- protected Layout createLayout(Class<? extends Layout> clazz) {
- try {
- Layout layout = clazz.newInstance();
- if (clazz.equals(AbsoluteLayout.class)) {
- layout.setWidth("100px");
- layout.setHeight("150px");
- }
- layout.addStyleName("vaadin-layout");
- return layout;
- } catch (InstantiationException | IllegalAccessException e) {
- throw new RuntimeException(e);
- }
- }
-
- protected C createComponent() {
- Type type = GenericTypeReflector.getTypeParameter(getClass(),
- HasValueRequiredIndicator.class.getTypeParameters()[0]);
- if (type instanceof ParameterizedType) {
- type = ((ParameterizedType) type).getRawType();
- }
-
- if (type instanceof Class<?>) {
- Class<?> clazz = (Class<?>) type;
- try {
- C component = (C) clazz.newInstance();
- initValue(component);
- component.setRequiredIndicatorVisible(true);
- component.addStyleName("test-component");
- return component;
- } catch (InstantiationException | IllegalAccessException e) {
- throw new RuntimeException(e);
- }
- } else {
- throw new IllegalStateException(
- "Cannot infer component type " + type.getTypeName());
- }
- }
-
- /**
- * Initialize value for the {@code component}.
- *
- * @param component
- * a UI component
- */
- protected abstract void initValue(C component);
-
- private static List<Class<? extends Layout>> getVaadinLayouts() {
- List<Class<? extends Layout>> layouts = new ArrayList<>();
- layouts.add(AbsoluteLayout.class);
- layouts.add(VerticalLayout.class);
- layouts.add(HorizontalLayout.class);
- layouts.add(FormLayout.class);
- layouts.add(CssLayout.class);
- layouts.add(GridLayout.class);
- return layouts;
- }
-
- }
|