Search in sources :

Example 31 with MetaPropertyPath

use of com.haulmont.chile.core.model.MetaPropertyPath in project cuba by cuba-platform.

the class DesktopAbstractField method getResolvedMetaPropertyPath.

protected MetaPropertyPath getResolvedMetaPropertyPath(MetaClass metaClass, String property) {
    MetaPropertyPath metaPropertyPath = AppBeans.get(MetadataTools.NAME, MetadataTools.class).resolveMetaPropertyPath(metaClass, property);
    Preconditions.checkNotNullArgument(metaPropertyPath, "Could not resolve property path '%s' in '%s'", property, metaClass);
    return metaPropertyPath;
}
Also used : MetadataTools(com.haulmont.cuba.core.global.MetadataTools) MetaPropertyPath(com.haulmont.chile.core.model.MetaPropertyPath)

Example 32 with MetaPropertyPath

use of com.haulmont.chile.core.model.MetaPropertyPath in project cuba by cuba-platform.

the class DesktopAbstractField method getAlternativeDebugId.

@Override
protected String getAlternativeDebugId() {
    if (id != null) {
        return id;
    }
    Datasource datasource = getDatasource();
    MetaPropertyPath metaPropertyPath = getMetaPropertyPath();
    if (datasource != null && StringUtils.isNotEmpty(datasource.getId()) && metaPropertyPath != null) {
        return getClass().getSimpleName() + "_" + datasource.getId() + "_" + metaPropertyPath.toString();
    }
    return getClass().getSimpleName();
}
Also used : Datasource(com.haulmont.cuba.gui.data.Datasource) MetaPropertyPath(com.haulmont.chile.core.model.MetaPropertyPath)

Example 33 with MetaPropertyPath

use of com.haulmont.chile.core.model.MetaPropertyPath in project cuba by cuba-platform.

the class DesktopAbstractTable method removeColumn.

@Override
public void removeColumn(Column column) {
    if (column == null) {
        return;
    }
    String name;
    if (column.getId() instanceof MetaPropertyPath) {
        MetaPropertyPath metaPropertyPath = (MetaPropertyPath) column.getId();
        name = metaPropertyPath.getMetaProperty().getName();
        editableColumns.remove(metaPropertyPath);
    } else {
        name = column.getId().toString();
    }
    TableColumn tableColumn = null;
    Iterator<TableColumn> columnIterator = getAllColumns().iterator();
    while (columnIterator.hasNext() && (tableColumn == null)) {
        TableColumn xColumn = columnIterator.next();
        Object identifier = xColumn.getIdentifier();
        if (identifier instanceof String && identifier.equals(name)) {
            tableColumn = xColumn;
        } else if (column.equals(identifier)) {
            tableColumn = xColumn;
        }
    }
    if (tableColumn != null) {
        // store old cell editors / renderers
        Map<Object, TableCellEditor> cellEditors = new HashMap<>();
        Map<Object, TableCellRenderer> cellRenderers = new HashMap<>();
        for (int i = 0; i < tableModel.getColumnCount(); i++) {
            Column tableModelColumn = tableModel.getColumn(i);
            if (tableModel.isGeneratedColumn(tableModelColumn)) {
                TableColumn oldColumn = getColumn(tableModelColumn);
                cellEditors.put(tableModelColumn.getId(), oldColumn.getCellEditor());
                cellRenderers.put(tableModelColumn.getId(), oldColumn.getCellRenderer());
            }
        }
        impl.getColumnModel().removeColumn(tableColumn);
        impl.removeColumn(tableColumn);
        columns.remove(column.getId());
        columnsOrder.remove(column);
        if (tableModel != null) {
            tableModel.removeColumn(column);
        }
        // reassign column identifiers
        setColumnIdentifiers();
        // reattach old generated columns
        for (int i = 0; i < tableModel.getColumnCount(); i++) {
            Column tableModelColumn = tableModel.getColumn(i);
            if (tableModel.isGeneratedColumn(tableModelColumn)) {
                TableColumn oldColumn = getColumn(tableModelColumn);
                if (cellEditors.containsKey(tableModelColumn.getId())) {
                    oldColumn.setCellEditor(cellEditors.get(tableModelColumn.getId()));
                }
                if (cellRenderers.containsKey(tableModelColumn.getId())) {
                    oldColumn.setCellRenderer(cellRenderers.get(tableModelColumn.getId()));
                }
            }
        }
        packRows();
        repaintImplIfNeeded();
    }
    column.setOwner(null);
}
Also used : TableCellRenderer(javax.swing.table.TableCellRenderer) TableColumn(javax.swing.table.TableColumn) MetaPropertyPath(com.haulmont.chile.core.model.MetaPropertyPath) TableCellEditor(javax.swing.table.TableCellEditor) TableColumn(javax.swing.table.TableColumn)

Example 34 with MetaPropertyPath

use of com.haulmont.chile.core.model.MetaPropertyPath in project cuba by cuba-platform.

the class DesktopAbstractTable method getColumnRenderer.

protected TableCellRenderer getColumnRenderer(int column) {
    TableColumn tableColumn = impl.getColumnModel().getColumn(column);
    if (tableColumn.getIdentifier() instanceof Table.Column) {
        Table.Column columnConf = (Table.Column) tableColumn.getIdentifier();
        if (columnConf.getId() instanceof MetaPropertyPath && !(isEditable() && columnConf.isEditable()) && !getTableModel().isGeneratedColumn(columnConf)) {
            MetaPropertyPath propertyPath = (MetaPropertyPath) columnConf.getId();
            final CellProvider cellViewProvider = getCustomCellView(propertyPath);
            if (cellViewProvider != null) {
                return new CellProviderRenderer(cellViewProvider);
            } else if (multiLineCells && String.class == columnConf.getType()) {
                return new MultiLineTableCellRenderer();
            }
        }
    }
    return null;
}
Also used : JXTable(org.jdesktop.swingx.JXTable) FocusableTable(com.haulmont.cuba.desktop.sys.vcl.FocusableTable) TableColumn(javax.swing.table.TableColumn) MetaPropertyPath(com.haulmont.chile.core.model.MetaPropertyPath) TableColumn(javax.swing.table.TableColumn)

Example 35 with MetaPropertyPath

use of com.haulmont.chile.core.model.MetaPropertyPath in project cuba by cuba-platform.

the class DesktopAbstractTable method setDatasource.

@Override
public void setDatasource(final CollectionDatasource datasource) {
    Preconditions.checkNotNullArgument(datasource, "datasource is null");
    final Collection<Object> properties;
    if (this.columns.isEmpty()) {
        MetadataTools metadataTools = AppBeans.get(MetadataTools.NAME);
        MessageTools messageTools = AppBeans.get(MessageTools.NAME);
        Collection<MetaPropertyPath> paths = datasource.getView() != null ? // if a view is specified - use view properties
        metadataTools.getViewPropertyPaths(datasource.getView(), datasource.getMetaClass()) : // otherwise use only string properties from meta-class - the temporary solution for KeyValue datasources
        metadataTools.getPropertyPaths(datasource.getMetaClass()).stream().filter(mpp -> mpp.getRangeJavaClass().equals(String.class)).collect(Collectors.toList());
        for (MetaPropertyPath metaPropertyPath : paths) {
            MetaProperty property = metaPropertyPath.getMetaProperty();
            if (!property.getRange().getCardinality().isMany() && !metadataTools.isSystem(property)) {
                Table.Column column = new Table.Column(metaPropertyPath);
                String propertyName = property.getName();
                MetaClass propertyMetaClass = metadataTools.getPropertyEnclosingMetaClass(metaPropertyPath);
                column.setCaption(messageTools.getPropertyCaption(propertyMetaClass, propertyName));
                column.setType(metaPropertyPath.getRangeJavaClass());
                Element element = DocumentHelper.createElement("column");
                column.setXmlDescriptor(element);
                addColumn(column);
            }
        }
    }
    properties = this.columns.keySet();
    this.datasource = datasource;
    collectionChangeListener = e -> {
        switch(e.getOperation()) {
            case CLEAR:
            case REFRESH:
                fieldDatasources.clear();
                break;
            case UPDATE:
            case REMOVE:
                for (Object entity : e.getItems()) {
                    fieldDatasources.remove(entity);
                }
                break;
        }
    };
    // noinspection unchecked
    datasource.addCollectionChangeListener(new WeakCollectionChangeListener(datasource, collectionChangeListener));
    initTableModel(datasource);
    initChangeListener();
    setColumnIdentifiers();
    if (isSortable()) {
        impl.setRowSorter(new RowSorterImpl(tableModel));
    }
    initSelectionListener(datasource);
    List<MetaPropertyPath> editableColumns = null;
    if (isEditable()) {
        editableColumns = new LinkedList<>();
    }
    MetaClass metaClass = datasource.getMetaClass();
    for (final Object property : properties) {
        final Table.Column column = this.columns.get(property);
        if (column != null) {
            if (column.isCollapsed() && getColumnControlVisible()) {
                TableColumn tableColumn = getColumn(column);
                if (tableColumn instanceof TableColumnExt) {
                    ((TableColumnExt) tableColumn).setVisible(false);
                }
            }
            if (editableColumns != null && column.isEditable() && (property instanceof MetaPropertyPath)) {
                MetaPropertyPath propertyPath = (MetaPropertyPath) property;
                if (security.isEntityAttrUpdatePermitted(metaClass, property.toString())) {
                    editableColumns.add(propertyPath);
                }
            }
        }
    }
    if (editableColumns != null && !editableColumns.isEmpty()) {
        setEditableColumns(editableColumns);
    }
    List<Object> columnsOrder = new ArrayList<>();
    for (Table.Column column : this.columnsOrder) {
        if (column.getId() instanceof MetaPropertyPath) {
            MetaPropertyPath metaPropertyPath = (MetaPropertyPath) column.getId();
            if (security.isEntityAttrReadPermitted(metaClass, metaPropertyPath.toString())) {
                columnsOrder.add(column.getId());
            }
        } else {
            columnsOrder.add(column.getId());
        }
    }
    setVisibleColumns(columnsOrder);
    if (security.isSpecificPermitted(ShowInfoAction.ACTION_PERMISSION)) {
        ShowInfoAction action = (ShowInfoAction) getAction(ShowInfoAction.ACTION_ID);
        if (action == null) {
            action = new ShowInfoAction();
            addAction(action);
        }
        action.setDatasource(datasource);
    }
    securityCollectionChangeListener = e -> {
        onDataChange();
        packRows();
        // #PL-2035, reload selection from ds
        Set<E> selectedItems1 = getSelected();
        if (selectedItems1 == null) {
            selectedItems1 = Collections.emptySet();
        }
        Set<E> newSelection = new HashSet<>();
        for (E entity : selectedItems1) {
            if (e.getDs().containsItem(entity.getId())) {
                newSelection.add(entity);
            }
        }
        if (e.getDs().getState() == Datasource.State.VALID && e.getDs().getItem() != null) {
            if (e.getDs().containsItem(e.getDs().getItem().getId())) {
                newSelection.add((E) e.getDs().getItem());
            }
        }
        if (newSelection.isEmpty()) {
            setSelected((E) null);
        } else {
            setSelected(newSelection);
        }
    };
    // noinspection unchecked
    datasource.addCollectionChangeListener(new WeakCollectionChangeListener(datasource, securityCollectionChangeListener));
    itemPropertyChangeListener = e -> {
        List<Column> columns1 = getColumns();
        boolean find = false;
        int i = 0;
        while ((i < columns1.size()) & !find) {
            Object columnId = columns1.get(i).getId();
            if (columnId instanceof MetaPropertyPath) {
                String propertyName = ((MetaPropertyPath) columnId).getMetaProperty().getName();
                if (propertyName.equals(e.getProperty())) {
                    find = true;
                }
            }
            i++;
        }
        if (find) {
            onDataChange();
        }
        packRows();
    };
    // noinspection unchecked
    datasource.addItemPropertyChangeListener(new WeakItemPropertyChangeListener(datasource, itemPropertyChangeListener));
    if (rowsCount != null) {
        rowsCount.setDatasource(datasource);
    }
    collectionDsActionsNotifier = new CollectionDsActionsNotifier(this);
    collectionDsActionsNotifier.bind(datasource);
    for (Action action : getActions()) {
        action.refreshState();
    }
    if (!canBeSorted(datasource))
        setSortable(false);
}
Also used : RowSorterImpl(com.haulmont.cuba.desktop.gui.data.RowSorterImpl) AbstractAction(javax.swing.AbstractAction) BoundAction(org.jdesktop.swingx.action.BoundAction) Action(com.haulmont.cuba.gui.components.Action) Element(org.dom4j.Element) MetaPropertyPath(com.haulmont.chile.core.model.MetaPropertyPath) WeakCollectionChangeListener(com.haulmont.cuba.gui.data.impl.WeakCollectionChangeListener) TableColumn(javax.swing.table.TableColumn) WeakItemPropertyChangeListener(com.haulmont.cuba.gui.data.impl.WeakItemPropertyChangeListener) MetaProperty(com.haulmont.chile.core.model.MetaProperty) JXTable(org.jdesktop.swingx.JXTable) FocusableTable(com.haulmont.cuba.desktop.sys.vcl.FocusableTable) TableColumn(javax.swing.table.TableColumn) MetaClass(com.haulmont.chile.core.model.MetaClass) TableColumnExt(org.jdesktop.swingx.table.TableColumnExt) CollectionDsActionsNotifier(com.haulmont.cuba.gui.data.impl.CollectionDsActionsNotifier)

Aggregations

MetaPropertyPath (com.haulmont.chile.core.model.MetaPropertyPath)84 MetaClass (com.haulmont.chile.core.model.MetaClass)34 MetaProperty (com.haulmont.chile.core.model.MetaProperty)27 Element (org.dom4j.Element)16 CategoryAttribute (com.haulmont.cuba.core.entity.CategoryAttribute)11 Entity (com.haulmont.cuba.core.entity.Entity)9 MetadataTools (com.haulmont.cuba.core.global.MetadataTools)9 Datasource (com.haulmont.cuba.gui.data.Datasource)6 Table (com.haulmont.cuba.gui.components.Table)5 CollectionDatasource (com.haulmont.cuba.gui.data.CollectionDatasource)5 MessageTools (com.haulmont.cuba.core.global.MessageTools)4 Instance (com.haulmont.chile.core.model.Instance)3 Op (com.haulmont.cuba.core.global.filter.Op)3 FocusableTable (com.haulmont.cuba.desktop.sys.vcl.FocusableTable)3 GuiDevelopmentException (com.haulmont.cuba.gui.GuiDevelopmentException)3 Formatter (com.haulmont.cuba.gui.components.Formatter)3 Window (com.haulmont.cuba.gui.components.Window)3 CollectionFormatter (com.haulmont.cuba.gui.components.formatters.CollectionFormatter)3 GroupInfo (com.haulmont.cuba.gui.data.GroupInfo)3 java.util (java.util)3