Example usage for com.vaadin.ui AbstractComponent setLocale

List of usage examples for com.vaadin.ui AbstractComponent setLocale

Introduction

In this page you can find the example usage for com.vaadin.ui AbstractComponent setLocale.

Prototype

public void setLocale(Locale locale) 

Source Link

Document

Sets the locale of this component.

Usage

From source file:uk.co.q3c.v7.i18n.AnnotationI18NInterpreter.java

License:Apache License

private void processComponent(I18NListener listener, Field field) {
    if (field.isAnnotationPresent(I18N.class)) {
        I18N annotation = field.getAnnotation(I18N.class);
        LabelKeys captionKey = annotation.caption();
        DescriptionKeys descriptionKey = annotation.description();
        DescriptionKeys valueKey = annotation.value();

        String captionValue = captionKey.equals(LabelKeys._notdefined_) ? null : captionKey.getValue(locale);
        String descriptionValue = descriptionKey.equals(DescriptionKeys._notdefined_) ? null
                : descriptionKey.getValue(locale);

        field.setAccessible(true);/* w  ww  .j av  a2s.  c  om*/
        try {
            AbstractComponent c = (AbstractComponent) field.get(listener);
            if (captionValue != null) {
                c.setCaption(captionValue);
            }
            if (descriptionValue != null) {
                c.setDescription(descriptionValue);
            }
            c.setLocale(locale);
        } catch (IllegalArgumentException | IllegalAccessException e) {
            log.error("Unable to set I18N caption or description for " + field.getName(), e);
        }

        // These components have a value. Usually I18N would only be used for Label values. If no key is provided
        // the component value is left unchanged
        if (valueKey != null) {
            if (Property.class.isAssignableFrom(field.getType())) {
                try {
                    @SuppressWarnings("unchecked")
                    Property<String> c = (Property<String>) field.get(listener);
                    String valueValue = valueKey.equals(DescriptionKeys._notdefined_) ? null
                            : valueKey.getValue(locale);
                    if (valueValue != null) {
                        c.setValue(valueValue);
                    }
                } catch (Exception e) {
                    log.error("Unable to set I18N value for " + field.getName(), e);

                }
            }
        }

    }

}

From source file:uk.co.q3c.v7.i18n.AnnotationI18NTranslator.java

License:Apache License

private void decodeAnnotation(I18NListener listener, Field field, Annotation annotation,
        Provider<? extends I18NAnnotationReader> provider) {

    // get a reader
    I18NAnnotationReader reader = provider.get();

    // get the keys from the reader
    I18NKey<?> captionKey = reader.caption(annotation);
    I18NKey<?> descriptionKey = reader.description(annotation);
    I18NKey<?> valueKey = reader.value(annotation);

    // check for nulls. Nulls are used for caption and description so that content can be cleared.
    // for value, this is not the case, as it may be a bad idea
    String captionValue = captionKey.isNullKey() ? null : captionKey.getValue(currentLocale.getLocale());
    String descriptionValue = descriptionKey.isNullKey() ? null
            : descriptionKey.getValue(currentLocale.getLocale());

    // set caption and description
    field.setAccessible(true);//from   w w w .j a  va  2 s.co  m
    try {
        AbstractComponent c = (AbstractComponent) field.get(listener);
        if (captionValue != null) {
            c.setCaption(captionValue);
        }
        if (descriptionValue != null) {
            c.setDescription(descriptionValue);
        }
        c.setLocale(currentLocale.getLocale());
    } catch (IllegalArgumentException | IllegalAccessException e) {
        log.error("Unable to set I18N caption or description for " + field.getName(), e);
    }

    // These components have a value. Usually I18N would only be used for Label values. If no key is provided
    // the component value is left unchanged
    if (valueKey != null) {
        if (Property.class.isAssignableFrom(field.getType())) {
            try {
                @SuppressWarnings("unchecked")
                Property<String> c = (Property<String>) field.get(listener);
                String valueValue = valueKey.isNullKey() ? null : valueKey.getValue(currentLocale.getLocale());
                if (valueValue != null) {
                    c.setValue(valueValue);
                }
            } catch (Exception e) {
                log.error("Unable to set I18N value for " + field.getName(), e);

            }
        }
    }

    // Table columns need special treatment
    if (Table.class.isAssignableFrom(field.getType())) {
        try {
            Table table = (Table) field.get(listener);
            Object[] columns = table.getVisibleColumns();
            List<String> headers = new ArrayList<>();
            for (Object column : columns) {
                if (column instanceof LabelKey) {
                    LabelKey columnid = (LabelKey) column;
                    String header = columnid.getValue(currentLocale.getLocale());
                    headers.add(header);
                } else {
                    headers.add(column.toString());
                }
            }
            String headerArray[] = headers.toArray(new String[] {});
            table.setColumnHeaders(headerArray);

        } catch (Exception e) {
            log.error("Unable to set I18N table columns headers for " + field.getName(), e);
        }

    }
}

From source file:uk.q3c.krail.core.i18n.DefaultI18NProcessor.java

License:Apache License

/**
 * Applies annotation values to {@code component}
 *
 * @param component        the component to be updated
 * @param annotationValues the annotation values to apply
 * @param annotationInfo   used primarily to identify the Field, and therefore its name
 *//*  www  .ja v  a2s .  co m*/
private void applyAnnotationValues(AbstractComponent component, AnnotationValues annotationValues,
        AnnotationInfo annotationInfo) {
    // set locale first
    Locale locale = annotationValues.locale.isPresent() ? annotationValues.locale.get()
            : currentLocale.getLocale();
    component.setLocale(locale);

    // set caption, description & value if available
    if (annotationValues.captionKey.isPresent()) {
        component.setCaption(translate.from(annotationValues.captionKey.get(), locale));
    }
    if (annotationValues.descriptionKey.isPresent()) {
        component.setDescription(translate.from(annotationValues.descriptionKey.get(), locale));
    }
    if (annotationValues.valueKey.isPresent()) {
        if (component instanceof HasValue) {
            ((HasValue) component).setValue(translate.from(annotationValues.valueKey.get(), locale));
            return;
        } else {
            log.warn("Field {} has a value annotation but does not implement HasValue.  Annotation ignored",
                    annotationInfo.getField().getName());
        }
    }

}

From source file:uk.q3c.krail.i18n.DefaultI18NProcessor.java

License:Apache License

/**
 * Applies annotation values to {@code component}
 *
 * @param component/*w w  w .  ja  v a2s  .c  om*/
 *         the component to be updated
 * @param annotationValues
 *         the annotation values to apply
 * @param annotationInfo
 *         used primarily to identify the Field, and therefore its name
 */
private void applyAnnotationValues(AbstractComponent component, AnnotationValues annotationValues,
        AnnotationInfo annotationInfo) {
    // set locale first
    Locale locale = annotationValues.locale.isPresent() ? annotationValues.locale.get()
            : currentLocale.getLocale();
    component.setLocale(locale);

    // set caption, description & value if available
    if (annotationValues.captionKey.isPresent()) {
        component.setCaption(translate.from(annotationValues.captionKey.get(), locale));
    }
    if (annotationValues.descriptionKey.isPresent()) {
        component.setDescription(translate.from(annotationValues.descriptionKey.get(), locale));
    }
    if (annotationValues.valueKey.isPresent()) {
        if (component instanceof Property) {
            //noinspection unchecked
            ((Property) component).setValue(translate.from(annotationValues.valueKey.get(), locale));
        } else {
            log.warn("Field {} has a value annotation but does not implement Property.  Annotation ignored",
                    annotationInfo.getField().getName());
        }
    }

}