Search in sources :

Example 1 with Formatter

use of com.haulmont.cuba.gui.components.Formatter in project cuba by cuba-platform.

the class EntityInspectorBrowse method createEntitiesTable.

protected void createEntitiesTable(MetaClass meta) {
    if (entitiesTable != null)
        tableBox.remove(entitiesTable);
    if (filter != null) {
        filterBox.remove(filter);
    }
    entitiesTable = componentsFactory.createComponent(Table.class);
    entitiesTable.setFrame(frame);
    if (companion != null) {
        companion.setHorizontalScrollEnabled(entitiesTable, true);
    }
    ClientType clientType = AppConfig.getClientType();
    if (clientType == ClientType.WEB) {
        textSelection.setVisible(true);
        textSelection.addValueChangeListener(e -> changeTableTextSelectionEnabled());
    }
    final SimpleDateFormat dateTimeFormat = new SimpleDateFormat(getMessage("dateTimeFormat"));
    Formatter dateTimeFormatter = value -> {
        if (value == null) {
            return StringUtils.EMPTY;
        }
        return dateTimeFormat.format(value);
    };
    // collect properties in order to add non-system columns first
    List<Table.Column> nonSystemPropertyColumns = new ArrayList<>(10);
    List<Table.Column> systemPropertyColumns = new ArrayList<>(10);
    for (MetaProperty metaProperty : meta.getProperties()) {
        // don't show embedded, transient & multiple referred entities
        if (isEmbedded(metaProperty) || metadata.getTools().isNotPersistent(metaProperty))
            continue;
        Range range = metaProperty.getRange();
        if (range.getCardinality().isMany())
            continue;
        Table.Column column = new Table.Column(meta.getPropertyPath(metaProperty.getName()));
        if (range.isDatatype() && range.asDatatype().getJavaClass().equals(Date.class)) {
            column.setFormatter(dateTimeFormatter);
        }
        if (metaProperty.getJavaType().equals(String.class)) {
            column.setMaxTextLength(MAX_TEXT_LENGTH);
        }
        if (!metadata.getTools().isSystem(metaProperty)) {
            column.setCaption(getPropertyCaption(meta, metaProperty));
            nonSystemPropertyColumns.add(column);
        } else {
            column.setCaption(metaProperty.getName());
            systemPropertyColumns.add(column);
        }
    }
    for (Table.Column column : nonSystemPropertyColumns) {
        entitiesTable.addColumn(column);
    }
    for (Table.Column column : systemPropertyColumns) {
        entitiesTable.addColumn(column);
    }
    if (entitiesDs != null) {
        ((DsContextImplementation) getDsContext()).unregister(entitiesDs);
    }
    entitiesDs = DsBuilder.create(getDsContext()).setId("entitiesDs").setMetaClass(meta).setView(createView(meta)).buildCollectionDatasource();
    entitiesDs.setLoadDynamicAttributes(true);
    entitiesDs.setSoftDeletion(!removedRecords.isChecked());
    entitiesDs.setQuery(String.format("select e from %s e", meta.getName()));
    entitiesTable.setDatasource(entitiesDs);
    tableBox.add(entitiesTable);
    entitiesTable.setSizeFull();
    createButtonsPanel(entitiesTable);
    RowsCount rowsCount = componentsFactory.createComponent(RowsCount.class);
    rowsCount.setDatasource(entitiesDs);
    entitiesTable.setRowsCount(rowsCount);
    entitiesTable.setEnterPressAction(entitiesTable.getAction("edit"));
    entitiesTable.setItemClickAction(entitiesTable.getAction("edit"));
    entitiesTable.setMultiSelect(true);
    createFilter();
}
Also used : StringUtils(org.apache.commons.lang.StringUtils) java.util(java.util) ReferenceImportBehaviour(com.haulmont.cuba.core.app.importexport.ReferenceImportBehaviour) DsBuilder(com.haulmont.cuba.gui.data.DsBuilder) LoggerFactory(org.slf4j.LoggerFactory) ParamsMap(com.haulmont.bali.util.ParamsMap) CubaIcon(com.haulmont.cuba.gui.icons.CubaIcon) SimpleDateFormat(java.text.SimpleDateFormat) Icons(com.haulmont.cuba.gui.icons.Icons) MetaClass(com.haulmont.chile.core.model.MetaClass) com.haulmont.cuba.core.global(com.haulmont.cuba.core.global) Inject(javax.inject.Inject) JSON(com.haulmont.cuba.gui.export.ExportFormat.JSON) ComponentsFactory(com.haulmont.cuba.gui.xml.layout.ComponentsFactory) Files(com.google.common.io.Files) DsContextImplementation(com.haulmont.cuba.gui.data.impl.DsContextImplementation) ExportFormat(com.haulmont.cuba.gui.export.ExportFormat) com.haulmont.cuba.gui.components(com.haulmont.cuba.gui.components) com.haulmont.cuba.gui.components.actions(com.haulmont.cuba.gui.components.actions) OpenType(com.haulmont.cuba.gui.WindowManager.OpenType) Logger(org.slf4j.Logger) Range(com.haulmont.chile.core.model.Range) Formatter(com.haulmont.cuba.gui.components.Formatter) MetaProperty(com.haulmont.chile.core.model.MetaProperty) EntityImportExportService(com.haulmont.cuba.core.app.importexport.EntityImportExportService) ExportDisplay(com.haulmont.cuba.gui.export.ExportDisplay) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) File(java.io.File) StandardCharsets(java.nio.charset.StandardCharsets) EntityImportView(com.haulmont.cuba.core.app.importexport.EntityImportView) ZIP(com.haulmont.cuba.gui.export.ExportFormat.ZIP) IOUtils(org.apache.commons.io.IOUtils) EntityOp(com.haulmont.cuba.security.entity.EntityOp) ByteArrayDataProvider(com.haulmont.cuba.gui.export.ByteArrayDataProvider) AppConfig(com.haulmont.cuba.gui.AppConfig) FileUploadingAPI(com.haulmont.cuba.gui.upload.FileUploadingAPI) ClientConfig(com.haulmont.cuba.client.ClientConfig) CollectionDatasource(com.haulmont.cuba.gui.data.CollectionDatasource) Session(com.haulmont.chile.core.model.Session) Entity(com.haulmont.cuba.core.entity.Entity) InputStream(java.io.InputStream) DsContextImplementation(com.haulmont.cuba.gui.data.impl.DsContextImplementation) Formatter(com.haulmont.cuba.gui.components.Formatter) Range(com.haulmont.chile.core.model.Range) MetaProperty(com.haulmont.chile.core.model.MetaProperty) SimpleDateFormat(java.text.SimpleDateFormat)

Example 2 with Formatter

use of com.haulmont.cuba.gui.components.Formatter in project cuba by cuba-platform.

the class EntityRestore method buildLayout.

protected void buildLayout() {
    Object value = entities.getValue();
    if (value != null) {
        MetaClass metaClass = (MetaClass) value;
        MetaProperty deleteTsMetaProperty = metaClass.getProperty("deleteTs");
        if (deleteTsMetaProperty != null) {
            if (entitiesTable != null) {
                tablePanel.remove(entitiesTable);
            }
            if (filter != null) {
                tablePanel.remove(filter);
            }
            ComponentsFactory componentsFactory = AppConfig.getFactory();
            entitiesTable = componentsFactory.createComponent(Table.class);
            entitiesTable.setFrame(frame);
            restoreButton = componentsFactory.createComponent(Button.class);
            restoreButton.setId("restore");
            restoreButton.setCaption(getMessage("entityRestore.restore"));
            ButtonsPanel buttonsPanel = componentsFactory.createComponent(ButtonsPanel.class);
            buttonsPanel.add(restoreButton);
            entitiesTable.setButtonsPanel(buttonsPanel);
            RowsCount rowsCount = componentsFactory.createComponent(RowsCount.class);
            entitiesTable.setRowsCount(rowsCount);
            final SimpleDateFormat dateTimeFormat = new SimpleDateFormat(getMessage("dateTimeFormat"));
            Formatter dateTimeFormatter = propertyValue -> {
                if (propertyValue == null) {
                    return StringUtils.EMPTY;
                }
                return dateTimeFormat.format(propertyValue);
            };
            // collect properties in order to add non-system columns first
            LinkedList<Table.Column> nonSystemPropertyColumns = new LinkedList<>();
            LinkedList<Table.Column> systemPropertyColumns = new LinkedList<>();
            List<MetaProperty> metaProperties = new ArrayList<>();
            for (MetaProperty metaProperty : metaClass.getProperties()) {
                // don't show embedded & multiple referred entities
                Range range = metaProperty.getRange();
                if (isEmbedded(metaProperty) || range.getCardinality().isMany() || metadataTools.isSystemLevel(metaProperty) || (range.isClass() && metadataTools.isSystemLevel(range.asClass()))) {
                    continue;
                }
                metaProperties.add(metaProperty);
                Table.Column column = new Table.Column(metaClass.getPropertyPath(metaProperty.getName()));
                if (!metadataTools.isSystem(metaProperty)) {
                    column.setCaption(getPropertyCaption(metaClass, metaProperty));
                    nonSystemPropertyColumns.add(column);
                } else {
                    column.setCaption(metaProperty.getName());
                    systemPropertyColumns.add(column);
                }
                if (range.isDatatype() && range.asDatatype().getJavaClass().equals(Date.class)) {
                    column.setFormatter(dateTimeFormatter);
                }
            }
            for (Table.Column column : nonSystemPropertyColumns) {
                entitiesTable.addColumn(column);
            }
            for (Table.Column column : systemPropertyColumns) {
                entitiesTable.addColumn(column);
            }
            DsContext dsContext = getDsContext();
            if (entitiesDs != null) {
                ((DsContextImplementation) dsContext).unregister(entitiesDs);
            }
            entitiesDs = DsBuilder.create(dsContext).setId("entitiesDs").setMetaClass(metaClass).setView(buildView(metaClass, metaProperties)).buildGroupDatasource();
            entitiesDs.setQuery("select e from " + metaClass.getName() + " e " + "where e.deleteTs is not null order by e.deleteTs");
            entitiesDs.setSoftDeletion(false);
            entitiesDs.refresh();
            entitiesTable.setDatasource(entitiesDs);
            String filterId = metaClass.getName().replace("$", "") + "GenericFilter";
            filter = componentsFactory.createComponent(Filter.class);
            filter.setId(filterId);
            filter.setFrame(getFrame());
            StringBuilder sb = new StringBuilder("");
            for (MetaProperty property : metaClass.getProperties()) {
                AnnotatedElement annotatedElement = property.getAnnotatedElement();
                if (annotatedElement.getAnnotation(com.haulmont.chile.core.annotations.MetaProperty.class) != null) {
                    sb.append(property.getName()).append("|");
                }
            }
            Element filterElement = Dom4j.readDocument(String.format("<filter id=\"%s\">\n" + "    <properties include=\".*\" exclude=\"\"/>\n" + "</filter>", filterId)).getRootElement();
            String excludedProperties = sb.toString();
            if (StringUtils.isNotEmpty(excludedProperties)) {
                Element properties = filterElement.element("properties");
                properties.attribute("exclude").setValue(excludedProperties.substring(0, excludedProperties.lastIndexOf("|")));
            }
            filter.setXmlDescriptor(filterElement);
            filter.setUseMaxResults(true);
            filter.setDatasource(entitiesDs);
            entitiesTable.setSizeFull();
            entitiesTable.setMultiSelect(true);
            Action restoreAction = new ItemTrackingAction("restore").withCaption(getMessage("entityRestore.restore")).withHandler(event -> showRestoreDialog());
            entitiesTable.addAction(restoreAction);
            restoreButton.setAction(restoreAction);
            tablePanel.add(filter);
            tablePanel.add(entitiesTable);
            tablePanel.expand(entitiesTable, "100%", "100%");
            entitiesTable.refresh();
            ((FilterImplementation) filter).loadFiltersAndApplyDefault();
        }
    }
}
Also used : SoftDelete(com.haulmont.cuba.core.entity.SoftDelete) StringUtils(org.apache.commons.lang.StringUtils) java.util(java.util) Dom4j(com.haulmont.bali.util.Dom4j) DsBuilder(com.haulmont.cuba.gui.data.DsBuilder) EntityRestoreService(com.haulmont.cuba.core.app.EntityRestoreService) SimpleDateFormat(java.text.SimpleDateFormat) EnableRestore(com.haulmont.cuba.core.entity.annotation.EnableRestore) MetaClass(com.haulmont.chile.core.model.MetaClass) Inject(javax.inject.Inject) MetadataTools(com.haulmont.cuba.core.global.MetadataTools) ComponentsFactory(com.haulmont.cuba.gui.xml.layout.ComponentsFactory) DsContextImplementation(com.haulmont.cuba.gui.data.impl.DsContextImplementation) GroupDatasource(com.haulmont.cuba.gui.data.GroupDatasource) ViewRepository(com.haulmont.cuba.core.global.ViewRepository) com.haulmont.cuba.gui.components(com.haulmont.cuba.gui.components) ItemTrackingAction(com.haulmont.cuba.gui.components.actions.ItemTrackingAction) DsContext(com.haulmont.cuba.gui.data.DsContext) Range(com.haulmont.chile.core.model.Range) Formatter(com.haulmont.cuba.gui.components.Formatter) MetaProperty(com.haulmont.chile.core.model.MetaProperty) Status(com.haulmont.cuba.gui.components.Action.Status) Type(com.haulmont.cuba.gui.components.DialogAction.Type) View(com.haulmont.cuba.core.global.View) AppConfig(com.haulmont.cuba.gui.AppConfig) Element(org.dom4j.Element) Entity(com.haulmont.cuba.core.entity.Entity) MessageTools(com.haulmont.cuba.core.global.MessageTools) AnnotatedElement(java.lang.reflect.AnnotatedElement) ItemTrackingAction(com.haulmont.cuba.gui.components.actions.ItemTrackingAction) DsContext(com.haulmont.cuba.gui.data.DsContext) Formatter(com.haulmont.cuba.gui.components.Formatter) ItemTrackingAction(com.haulmont.cuba.gui.components.actions.ItemTrackingAction) Element(org.dom4j.Element) AnnotatedElement(java.lang.reflect.AnnotatedElement) AnnotatedElement(java.lang.reflect.AnnotatedElement) ComponentsFactory(com.haulmont.cuba.gui.xml.layout.ComponentsFactory) MetaProperty(com.haulmont.chile.core.model.MetaProperty) DsContextImplementation(com.haulmont.cuba.gui.data.impl.DsContextImplementation) Range(com.haulmont.chile.core.model.Range) MetaClass(com.haulmont.chile.core.model.MetaClass) SimpleDateFormat(java.text.SimpleDateFormat)

Example 3 with Formatter

use of com.haulmont.cuba.gui.components.Formatter in project cuba by cuba-platform.

the class WebAbstractTable method getFormattedValue.

protected String getFormattedValue(Column column, Object value) {
    String cellText;
    if (value == null) {
        cellText = "";
    } else {
        if (value instanceof String) {
            cellText = (String) value;
        } else {
            Formatter formatter = column.getFormatter();
            if (formatter != null) {
                // noinspection unchecked
                cellText = formatter.format(value);
            } else {
                Datatype datatype = Datatypes.get(value.getClass());
                if (datatype != null) {
                    UserSessionSource sessionSource = AppBeans.get(UserSessionSource.NAME);
                    cellText = datatype.format(value, sessionSource.getLocale());
                } else {
                    cellText = value.toString();
                }
            }
        }
    }
    return cellText;
}
Also used : CollectionFormatter(com.haulmont.cuba.gui.components.formatters.CollectionFormatter) Formatter(com.haulmont.cuba.gui.components.Formatter) Datatype(com.haulmont.chile.core.datatypes.Datatype)

Example 4 with Formatter

use of com.haulmont.cuba.gui.components.Formatter in project cuba by cuba-platform.

the class WebAbstractTable method addColumn.

@Override
public void addColumn(Table.Column column) {
    checkNotNullArgument(column, "Column must be non null");
    Object columnId = column.getId();
    component.addContainerProperty(columnId, column.getType(), null);
    if (StringUtils.isNotBlank(column.getDescription())) {
        component.setColumnDescription(columnId, column.getDescription());
    }
    if (StringUtils.isNotBlank(column.getValueDescription())) {
        component.setAggregationDescription(columnId, column.getValueDescription());
    } else if (column.getAggregation() != null && column.getAggregation().getType() != AggregationInfo.Type.CUSTOM) {
        Messages messages = AppBeans.get(Messages.NAME);
        String aggregationTypeLabel;
        switch(column.getAggregation().getType()) {
            case AVG:
                aggregationTypeLabel = "aggreagtion.avg";
                break;
            case COUNT:
                aggregationTypeLabel = "aggreagtion.count";
                break;
            case SUM:
                aggregationTypeLabel = "aggreagtion.sum";
                break;
            case MIN:
                aggregationTypeLabel = "aggreagtion.min";
                break;
            case MAX:
                aggregationTypeLabel = "aggreagtion.max";
                break;
            default:
                throw new IllegalArgumentException(String.format("AggregationType %s is not supported", column.getAggregation().getType().toString()));
        }
        component.setAggregationDescription(columnId, messages.getMainMessage(aggregationTypeLabel));
    }
    if (!column.isSortable()) {
        component.setColumnSortable(columnId, column.isSortable());
    }
    columns.put(columnId, column);
    columnsOrder.add(column);
    if (column.getWidth() != null) {
        component.setColumnWidth(columnId, column.getWidth());
    }
    if (column.getAlignment() != null) {
        component.setColumnAlignment(columnId, WebComponentsHelper.convertColumnAlignment(column.getAlignment()));
    }
    final String caption = getColumnCaption(columnId, column);
    setColumnHeader(columnId, caption);
    column.setOwner(this);
    if (column.getFormatter() == null && columnId instanceof MetaPropertyPath) {
        MetaProperty metaProperty = ((MetaPropertyPath) columnId).getMetaProperty();
        if (Collection.class.isAssignableFrom(metaProperty.getJavaType())) {
            final Formatter collectionFormatter = new CollectionFormatter();
            column.setFormatter(collectionFormatter);
        }
    }
    if (columnId instanceof MetaPropertyPath) {
        PersistenceManagerService persistenceManagerService = AppBeans.get(PersistenceManagerClient.NAME);
        MetaPropertyPath propertyPath = (MetaPropertyPath) columnId;
        MetaProperty metaProperty = propertyPath.getMetaProperty();
        MetaClass propertyMetaClass = metadataTools.getPropertyEnclosingMetaClass(propertyPath);
        String storeName = metadataTools.getStoreName(propertyMetaClass);
        if (metadataTools.isLob(metaProperty) && !persistenceManagerService.supportsLobSortingAndFiltering(storeName)) {
            component.setColumnSortable(columnId, false);
        }
    }
}
Also used : MetaClass(com.haulmont.chile.core.model.MetaClass) CollectionFormatter(com.haulmont.cuba.gui.components.formatters.CollectionFormatter) Formatter(com.haulmont.cuba.gui.components.Formatter) MetaPropertyPath(com.haulmont.chile.core.model.MetaPropertyPath) PersistenceManagerService(com.haulmont.cuba.core.app.PersistenceManagerService) MetaProperty(com.haulmont.chile.core.model.MetaProperty) CollectionFormatter(com.haulmont.cuba.gui.components.formatters.CollectionFormatter)

Example 5 with Formatter

use of com.haulmont.cuba.gui.components.Formatter in project cuba by cuba-platform.

the class WebLabel method setDatasource.

@Override
public void setDatasource(Datasource datasource, String property) {
    if ((datasource == null && property != null) || (datasource != null && property == null))
        throw new IllegalArgumentException("Datasource and property should be either null or not null at the same time");
    if (datasource == this.datasource && metaPropertyPath != null && metaPropertyPath.toString().equals(property))
        return;
    if (this.datasource != null)
        unsubscribeDatasource();
    if (datasource != null) {
        // noinspection unchecked
        this.datasource = datasource;
        this.metaPropertyPath = resolveMetaPropertyPath(datasource.getMetaClass(), property);
        this.metaProperty = metaPropertyPath.getMetaProperty();
        switch(metaProperty.getType()) {
            case ASSOCIATION:
                component.setConverter(new StringToEntityConverter() {

                    @Override
                    public Formatter getFormatter() {
                        return WebLabel.this.formatter;
                    }
                });
                break;
            case DATATYPE:
                component.setConverter(new StringToDatatypeConverter(metaProperty.getRange().asDatatype()) {

                    @Override
                    public Formatter getFormatter() {
                        return WebLabel.this.formatter;
                    }
                });
                break;
            case ENUM:
                // noinspection unchecked
                component.setConverter(new StringToEnumConverter((Class<Enum>) metaProperty.getJavaType()) {

                    @Override
                    public Formatter getFormatter() {
                        return WebLabel.this.formatter;
                    }
                });
                break;
            default:
                component.setConverter(new StringToDatatypeConverter(Datatypes.getNN(String.class)) {

                    @Override
                    public Formatter getFormatter() {
                        return WebLabel.this.formatter;
                    }
                });
                break;
        }
        itemWrapper = createDatasourceWrapper(datasource, Collections.singleton(this.metaPropertyPath));
        component.setPropertyDataSource(itemWrapper.getItemProperty(this.metaPropertyPath));
    }
}
Also used : StringToEnumConverter(com.haulmont.cuba.web.toolkit.ui.converters.StringToEnumConverter) Formatter(com.haulmont.cuba.gui.components.Formatter) MetaClass(com.haulmont.chile.core.model.MetaClass) StringToEntityConverter(com.haulmont.cuba.web.toolkit.ui.converters.StringToEntityConverter) StringToDatatypeConverter(com.haulmont.cuba.web.toolkit.ui.converters.StringToDatatypeConverter)

Aggregations

Formatter (com.haulmont.cuba.gui.components.Formatter)6 MetaClass (com.haulmont.chile.core.model.MetaClass)4 MetaProperty (com.haulmont.chile.core.model.MetaProperty)4 CollectionFormatter (com.haulmont.cuba.gui.components.formatters.CollectionFormatter)3 MetaPropertyPath (com.haulmont.chile.core.model.MetaPropertyPath)2 Range (com.haulmont.chile.core.model.Range)2 Entity (com.haulmont.cuba.core.entity.Entity)2 AppConfig (com.haulmont.cuba.gui.AppConfig)2 com.haulmont.cuba.gui.components (com.haulmont.cuba.gui.components)2 DsBuilder (com.haulmont.cuba.gui.data.DsBuilder)2 DsContextImplementation (com.haulmont.cuba.gui.data.impl.DsContextImplementation)2 ComponentsFactory (com.haulmont.cuba.gui.xml.layout.ComponentsFactory)2 SimpleDateFormat (java.text.SimpleDateFormat)2 java.util (java.util)2 Inject (javax.inject.Inject)2 StringUtils (org.apache.commons.lang.StringUtils)2 Files (com.google.common.io.Files)1 Dom4j (com.haulmont.bali.util.Dom4j)1 ParamsMap (com.haulmont.bali.util.ParamsMap)1 Datatype (com.haulmont.chile.core.datatypes.Datatype)1