Example usage for com.vaadin.ui Component setId

List of usage examples for com.vaadin.ui Component setId

Introduction

In this page you can find the example usage for com.vaadin.ui Component setId.

Prototype

public void setId(String id);

Source Link

Document

Adds an unique id for component that is used in the client-side for testing purposes.

Usage

From source file:annis.libgui.IDGenerator.java

License:Apache License

public static String assignID(Component c, String fieldName) {
    String id = null;/*from   ww  w. j  a v  a2s  . c  o m*/
    if (c != null && fieldName != null && !fieldName.isEmpty()) {
        Preconditions.checkArgument(c.isAttached(),
                "Component " + c.getConnectorId() + " must be attached before it can get an automatic ID.");
        id = c.getId();
        if (id == null) {
            // try to get the parent ID
            HasComponents parent = c.getParent();
            if (parent == null || parent instanceof UI) {
                // use class name as ID
                id = fieldName;
            } else {
                String parentID = parent.getId();
                if (parentID == null) {
                    parentID = assignID(parent);
                }
                String idBase = parentID + ":" + fieldName;
                // check that no other child has the same ID
                int counter = 1;
                id = idBase;
                while (childHasID(parent, id)) {
                    id = idBase + "." + counter++;
                }
            }
            c.setId(id);
        }
    }
    return id;
}

From source file:com.google.code.vaadin.internal.preconfigured.VaadinComponentsInjector.java

License:Apache License

private void configureComponentApi(Component component, Preconfigured preconfigured) {
    component.setEnabled(preconfigured.enabled());
    component.setVisible(preconfigured.visible());
    component.setReadOnly(preconfigured.readOnly());

    String[] styleName = preconfigured.styleName();
    if (styleName.length > 0) {
        for (String style : styleName) {
            component.addStyleName(style);
        }/*from w w w . ja va2  s. c o m*/
    }

    configureLocalization(component, preconfigured);

    String id = preconfigured.id();
    if (!id.isEmpty()) {
        component.setId(id);
    }

    if (preconfigured.sizeFull()) {
        component.setSizeFull();
    } else if (preconfigured.sizeUndefined()) {
        component.setSizeUndefined();
    } else {
        float width = preconfigured.width();
        if (width > -1.0f) {
            Sizeable.Unit widthUnits = preconfigured.widthUnits();
            component.setWidth(width, widthUnits);
        }
        float height = preconfigured.height();
        if (height > -1.0f) {
            Sizeable.Unit heightUnits = preconfigured.heightUnits();
            component.setHeight(height, heightUnits);
        }
    }
}

From source file:org.azrul.langkuik.framework.webgui.BeanView.java

private void createForm(EntityRight entityRight, final Set<String> currentUserRoles,
        final Map<String, Map<Integer, FieldContainer>> groups, String group, final BeanFieldGroup fieldGroup,
        final List<DataAccessObject<?>> customTypeDaos, final Navigator nav, final FormLayout form)
        throws FieldGroup.BindException, UnsupportedOperationException {
    //create bean utils
    final BeanUtils beanUtils = new BeanUtils();

    //select which group we want
    Map<Integer, FieldContainer> fieldContainerMap = null;
    if (group == null) {
        fieldContainerMap = groups.entrySet().iterator().next().getValue();
    } else {/* w  w  w. j a va  2  s . c  o m*/
        fieldContainerMap = groups.get(group);
    }

    //collect all activechoices
    Map<com.vaadin.ui.ComboBox, ActiveChoiceTarget> activeChoicesWithFieldAsKey = new HashMap<>();
    Map<String, com.vaadin.ui.ComboBox> activeChoicesFieldWithHierarchyAsKey = new HashMap<>();

    //collect all cutsom types
    List<Class> customTypes = new ArrayList<>();
    for (DataAccessObject<?> ctDao : customTypeDaos) {
        customTypes.add(ctDao.getType());
    }
    //deal with every field
    everyField: for (Map.Entry<Integer, FieldContainer> entry : fieldContainerMap.entrySet()) {
        final FieldContainer fieldContainer = entry.getValue();
        final ComponentState effectiveFieldState = beanUtils
                .calculateEffectiveComponentState(fieldContainer.getPojoField(), currentUserRoles, entityRight);

        //Create form
        if (ComponentState.INVISIBLE.equals(effectiveFieldState)) {
            continue everyField; //Continue with next field
        }

        //deal with normal form element
        com.vaadin.ui.Field uifield = null;
        //deal with plain choices
        if (fieldContainer.getWebField().choices().length > 0) {
            //deal with choices
            com.vaadin.ui.ComboBox formComboBox = new com.vaadin.ui.ComboBox(
                    fieldContainer.getWebField().name());
            formComboBox.setImmediate(true);
            fieldGroup.bind(formComboBox, fieldContainer.getPojoField().getName());
            for (Choice choice : fieldContainer.getWebField().choices()) {
                if (choice.value() == -1) {
                    formComboBox.addItem(choice.textValue());
                    formComboBox.setItemCaption(choice.textValue(), choice.display());
                } else {
                    formComboBox.addItem(choice.value());
                    formComboBox.setItemCaption(choice.value(), choice.display());
                }
            }
            form.addComponent(formComboBox);
            uifield = formComboBox;
            //deal with active choices
        } else if (fieldContainer.getWebField().activeChoice().enumTree() != EmptyEnum.class) {
            //collect active choices - 
            com.vaadin.ui.ComboBox formComboBox = new com.vaadin.ui.ComboBox(
                    fieldContainer.getWebField().name());
            formComboBox.setImmediate(true);
            fieldGroup.bind(formComboBox, fieldContainer.getPojoField().getName());
            String hierarchy = fieldContainer.getWebField().activeChoice().hierarchy();
            Class<ActiveChoiceEnum> enumTree = (Class<ActiveChoiceEnum>) fieldContainer.getWebField()
                    .activeChoice().enumTree();
            ActiveChoiceTarget activeChoiceTarget = ActiveChoiceUtils.build(enumTree, hierarchy);
            for (String choice : activeChoiceTarget.getSourceChoices()) {
                formComboBox.addItem(choice);
                activeChoicesWithFieldAsKey.put(formComboBox, activeChoiceTarget);
                activeChoicesFieldWithHierarchyAsKey.put(hierarchy, formComboBox);
            }

            form.addComponent(formComboBox);
            uifield = formComboBox;

            //deal with relationship
        } else if (fieldContainer.getPojoField().isAnnotationPresent(OneToMany.class)
                || fieldContainer.getPojoField().isAnnotationPresent(ManyToOne.class)
                || fieldContainer.getPojoField().isAnnotationPresent(ManyToMany.class)) {

            //special relationship: deal with custom type form element
            int state = 0;
            Class classOfField = null;
            while (true) {
                if (state == 0) {
                    if (Collection.class.isAssignableFrom(fieldContainer.getPojoField().getType())) {
                        classOfField = (Class) ((ParameterizedType) fieldContainer.getPojoField()
                                .getGenericType()).getActualTypeArguments()[0];
                        state = 1;
                    } else {
                        state = 3;
                        break;
                    }
                }
                if (state == 1) {
                    if (CustomType.class.isAssignableFrom(classOfField)) {
                        state = 2;
                        break;
                    } else {
                        state = 3;
                        break;
                    }
                }
            }

            if (state == 2) { //Custom type
                Button openCustom = new Button("Manage " + fieldContainer.getWebField().name(),
                        new Button.ClickListener() {
                            @Override
                            public void buttonClick(ClickEvent event) {
                                try {
                                    fieldGroup.commit();
                                    currentBean = (C) fieldGroup.getItemDataSource().getBean();
                                    currentBean = saveBean(currentBean, parentBean, beanUtils,
                                            currentUserRoles);
                                    fieldGroup.setItemDataSource(currentBean);
                                    //field class
                                    Class iclassOfField = (Class) ((ParameterizedType) fieldContainer
                                            .getPojoField().getGenericType()).getActualTypeArguments()[0];

                                    //find a custom type dao
                                    DataAccessObject<? extends CustomType> chosenCTDao = null;
                                    for (DataAccessObject cdao : customTypeDaos) {
                                        if (cdao.getType().isAssignableFrom(iclassOfField)) {
                                            chosenCTDao = cdao;
                                            break;
                                        }
                                    }

                                    //deal with windows
                                    final Window window = new Window();
                                    final AttachmentCustomTypeUICreator<C> attachmentCustomTypeUICreator = new AttachmentCustomTypeUICreator();
                                    Component customTypeComponent = attachmentCustomTypeUICreator
                                            .createUIForForm(currentBean, iclassOfField,
                                                    fieldContainer.getPojoField().getName(), BeanView.this, dao,
                                                    chosenCTDao, pageParameter.getRelationManagerFactory(),
                                                    pageParameter.getConfig(), effectiveFieldState, window);
                                    customTypeComponent
                                            .setCaption("Manage " + fieldContainer.getWebField().name());
                                    customTypeComponent.setId(customTypeComponent.getCaption());
                                    window.setCaption(customTypeComponent.getCaption());
                                    window.setId(window.getCaption());
                                    window.setContent(customTypeComponent);
                                    window.setModal(true);

                                    BeanView.this.getUI().addWindow(window);
                                } catch (CommitException ex) {
                                    handleFieldsError(fieldGroup);
                                }
                            }
                        });
                openCustom.setId(openCustom.getCaption());
                form.addComponent(openCustom);

            } else { //relationship
                try {
                    fieldContainer.getPojoField().setAccessible(true);
                    final WebField webField = fieldContainer.getPojoField().getAnnotation(WebField.class);
                    Button relationshipButton = null;
                    if (fieldContainer.getPojoField().isAnnotationPresent(OneToMany.class)) {
                        relationshipButton = new Button("Manage " + webField.name(),
                                new Button.ClickListener() {
                                    @Override
                                    public void buttonClick(Button.ClickEvent event) {
                                        try {
                                            fieldGroup.commit();
                                            currentBean = (C) fieldGroup.getItemDataSource().getBean();
                                            currentBean = saveBean(currentBean, parentBean, beanUtils,
                                                    currentUserRoles);

                                            Class classOfField = (Class) ((ParameterizedType) fieldContainer
                                                    .getPojoField().getGenericType())
                                                            .getActualTypeArguments()[0];
                                            EditorTableView view = new EditorTableView(currentBean,
                                                    fieldContainer.getPojoField().getName(),
                                                    effectiveFieldState, classOfField, ChoiceType.CHOOSE_MANY,
                                                    pageParameter);
                                            String targetView = "BEAN_VIEW_" + UUID.randomUUID().toString();
                                            History his = new History(targetView, "Manage " + webField.name());
                                            pageParameter.getHistory().push(his);
                                            nav.addView(targetView, view);
                                            nav.navigateTo(targetView);
                                        } catch (CommitException ex) {
                                            handleFieldsError(fieldGroup);
                                        }
                                    }
                                });

                    } else if (fieldContainer.getPojoField().isAnnotationPresent(ManyToOne.class)) {
                        relationshipButton = new Button("Manage " + webField.name(),
                                new Button.ClickListener() {
                                    @Override
                                    public void buttonClick(Button.ClickEvent event) {
                                        try {
                                            fieldGroup.commit();
                                            currentBean = (C) fieldGroup.getItemDataSource().getBean();
                                            fieldGroup.commit();
                                            currentBean = (C) fieldGroup.getItemDataSource().getBean();
                                            currentBean = saveBean(currentBean, parentBean, beanUtils,
                                                    currentUserRoles);

                                            EditorTableView view = new EditorTableView(currentBean,
                                                    fieldContainer.getPojoField().getName(),
                                                    effectiveFieldState,
                                                    fieldContainer.getPojoField().getType(),
                                                    ChoiceType.CHOOSE_ONE, pageParameter);
                                            String targetView = "BEAN_VIEW_" + UUID.randomUUID().toString();
                                            History his = new History(targetView, "Manage " + webField.name());
                                            pageParameter.getHistory().push(his);
                                            nav.addView(targetView, view);
                                            nav.navigateTo(targetView);
                                        } catch (CommitException ex) {
                                            handleFieldsError(fieldGroup);
                                        }
                                    }
                                });
                    } else if (fieldContainer.getPojoField().isAnnotationPresent(ManyToMany.class)) {
                        relationshipButton = new Button("Manage " + webField.name(),
                                new Button.ClickListener() {
                                    @Override
                                    public void buttonClick(Button.ClickEvent event) {
                                        try {
                                            fieldGroup.commit();
                                            currentBean = (C) fieldGroup.getItemDataSource().getBean();
                                            fieldGroup.commit();
                                            currentBean = (C) fieldGroup.getItemDataSource().getBean();
                                            currentBean = saveBean(currentBean, parentBean, beanUtils,
                                                    currentUserRoles);

                                            Class classOfField = (Class) ((ParameterizedType) fieldContainer
                                                    .getPojoField().getGenericType())
                                                            .getActualTypeArguments()[0];
                                            EditorTableView view = new EditorTableView(currentBean,
                                                    fieldContainer.getPojoField().getName(),
                                                    effectiveFieldState, classOfField, ChoiceType.CHOOSE_MANY,
                                                    pageParameter);

                                            String targetView = "BEAN_VIEW_" + UUID.randomUUID().toString();
                                            History his = new History(targetView, "Manage " + webField.name());
                                            pageParameter.getHistory().push(his);
                                            nav.addView(targetView, view);
                                            nav.navigateTo(targetView);
                                        } catch (CommitException ex) {
                                            handleFieldsError(fieldGroup);
                                        }
                                    }
                                });
                    }
                    relationshipButton.setId(relationshipButton.getCaption());
                    form.addComponent(relationshipButton);
                } catch (IllegalArgumentException ex) {
                    Logger.getLogger(BeanView.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            //deal with id
        } else if (fieldContainer.getPojoField().isAnnotationPresent(Id.class)) {
            com.vaadin.ui.Field formField = fieldGroup.buildAndBind(fieldContainer.getWebField().name(),
                    fieldContainer.getPojoField().getName());
            if (Number.class.isAssignableFrom(fieldContainer.getPojoField().getType())) {
                ((TextField) formField).setConverter(
                        new NumberBasedIDConverter((Class<Number>) fieldContainer.getPojoField().getType()));
            }

            form.addComponent(formField);
            uifield = formField;

            //deal with nominal form element
        } else {
            com.vaadin.ui.Field formField = fieldGroup.buildAndBind(fieldContainer.getWebField().name(),
                    fieldContainer.getPojoField().getName());
            if (fieldContainer.getPojoField().getType().equals(Date.class)) {
                //deal with date
                DateField dateField = (DateField) formField;
                dateField.setDateFormat(pageParameter.getConfig().get("dateFormat"));
                dateField.setWidth(100f, Unit.PIXELS);
            } else if (fieldContainer.getPojoField().getType().equals(BigDecimal.class)) {
                TextField bdField = (TextField) formField;
                bdField.setConverter(new StringToBigDecimalConverter());
            }

            form.addComponent(formField);
            uifield = formField;
        }

        if (uifield != null) {
            //deal with read only
            if (ComponentState.READ_ONLY.equals(effectiveFieldState)) {
                uifield.setReadOnly(true);
            } else {
                uifield.setReadOnly(false);
                if (fieldContainer.getWebField().required() == true) {
                    uifield.setRequired(true);
                }
            }

            //set null presentation
            if (uifield instanceof AbstractTextField) {
                AbstractTextField textField = (AbstractTextField) uifield;
                textField.setNullRepresentation("");
            }

            //set debug id
            uifield.setId(uifield.getCaption());
        }
    }

    //deal with active choice
    for (final com.vaadin.ui.ComboBox sourceField : activeChoicesWithFieldAsKey.keySet()) {
        final ActiveChoiceTarget target = activeChoicesWithFieldAsKey.get(sourceField);
        final com.vaadin.ui.ComboBox targetField = activeChoicesFieldWithHierarchyAsKey
                .get(target.getTargetHierarchy());
        sourceField.addValueChangeListener(new ValueChangeListener() {
            @Override
            public void valueChange(Property.ValueChangeEvent event) {
                List<String> targetValues = target.getTargets().get(sourceField.getValue());
                if (targetValues != null && !targetValues.isEmpty() && targetField != null) {
                    targetField.removeAllItems();
                    for (String targetValue : targetValues) {
                        targetField.addItem(targetValue);
                    }
                }
            }
        });

    }
}

From source file:org.eclipse.hawkbit.ui.menu.DashboardMenu.java

License:Open Source License

/**
 * Creates the wrapper which contains the menu item and the adjacent label
 * for displaying the occurred events/*www.j  av  a2 s. c om*/
 * 
 * @param menuItemButton
 *            the menu item
 * @param notificationLabel
 *            the label for displaying the occurred events
 * @return Component of type CssLayout
 */
private static Component buildLabelWrapper(final ValoMenuItemButton menuItemButton,
        final Component notificationLabel) {
    final CssLayout dashboardWrapper = new CssLayout(menuItemButton);
    dashboardWrapper.addStyleName("labelwrapper");
    dashboardWrapper.addStyleName(ValoTheme.MENU_ITEM);
    notificationLabel.addStyleName(ValoTheme.MENU_BADGE);
    notificationLabel.setWidthUndefined();
    notificationLabel.setVisible(false);
    notificationLabel
            .setId(UIComponentIdProvider.NOTIFICATION_MENU_ID + menuItemButton.getCaption().toLowerCase());
    dashboardWrapper.addComponent(notificationLabel);
    return dashboardWrapper;
}

From source file:org.vaadin.tori.view.thread.PostsLayout.java

License:Apache License

private void renderUntil(final int untilIndex) {
    boolean postsAdded = false;
    while (renderedIndex <= untilIndex) {
        renderedIndex++;//  w  w w  . j  a  v  a2  s.c  om
        if (posts != null) {
            if (renderedIndex < posts.size()) {
                postsAdded = true;
                final Component component = new PostComponent(posts.get(renderedIndex), presenter, prettyTime);
                addComponent(component);
                if (scrollToIndex != null && renderedIndex == scrollToIndex) {
                    // The component should be scrolled to
                    UI.getCurrent().scrollIntoView(component);
                    component.setId("scrollpostid");
                    JavaScript.eval(
                            "window.setTimeout(\"document.getElementById('scrollpostid').scrollIntoView(true)\",10)");
                    scrollToIndex = null;
                }
            } else {
                break;
            }
        }
    }

    if (!postsAdded) {
        List<String> styles = getState(false).styles;
        if ((styles == null || !styles.contains(STYLE_READY))) {
            ToriScheduler.get().executeManualCommands();
            addStyleName(STYLE_READY);
        }
    }
}