Search in sources :

Example 1 with TableColumnExt

use of org.jdesktop.swingx.table.TableColumnExt in project com.revolsys.open by revolsys.

the class Log4jTableModel method newLogTable.

private static BaseJTable newLogTable() {
    final Log4jTableModel model = new Log4jTableModel();
    final BaseJTable table = new BaseJTable(model);
    table.setAutoResizeMode(JTable.AUTO_RESIZE_LAST_COLUMN);
    for (int i = 0; i < model.getColumnCount(); i++) {
        final TableColumnExt column = table.getColumnExt(i);
        if (i == 0) {
            column.setMinWidth(180);
            column.setPreferredWidth(180);
            column.setMaxWidth(180);
        } else if (i == 1) {
            column.setMinWidth(80);
            column.setPreferredWidth(80);
            column.setMaxWidth(80);
        } else if (i == 2) {
            column.setMinWidth(200);
            column.setPreferredWidth(400);
        } else if (i == 3) {
            column.setMinWidth(200);
            column.setPreferredWidth(800);
        }
    }
    table.setSortOrder(0, SortOrder.DESCENDING);
    table.addMouseListener(new MouseAdapter() {

        @Override
        public void mouseClicked(final MouseEvent e) {
            if (SwingUtilities.isLeftMouseButton(e) && e.getClickCount() == 2) {
                int row = table.rowAtPoint(e.getPoint());
                row = table.convertRowIndexToModel(row);
                if (row != -1) {
                    final LoggingEvent loggingEvent = model.getLoggingEvent(row);
                    LoggingEventPanel.showDialog(table, loggingEvent);
                }
            }
        }
    });
    return table;
}
Also used : LoggingEvent(org.apache.log4j.spi.LoggingEvent) MouseEvent(java.awt.event.MouseEvent) TableColumnExt(org.jdesktop.swingx.table.TableColumnExt) BaseJTable(com.revolsys.swing.table.BaseJTable) MouseAdapter(java.awt.event.MouseAdapter)

Example 2 with TableColumnExt

use of org.jdesktop.swingx.table.TableColumnExt in project com.revolsys.open by revolsys.

the class BaseJTable method setColumnWidth.

public void setColumnWidth(final int i, final int width) {
    final TableColumnExt column = getColumnExt(i);
    column.setMinWidth(width);
    column.setWidth(width);
    column.setMaxWidth(width);
}
Also used : TableColumnExt(org.jdesktop.swingx.table.TableColumnExt)

Example 3 with TableColumnExt

use of org.jdesktop.swingx.table.TableColumnExt in project com.revolsys.open by revolsys.

the class BaseJTable method resizeColumnsToContent.

public void resizeColumnsToContent() {
    final TableModel model = getModel();
    final int columnCount = getColumnCount();
    for (int columnIndex = 0; columnIndex < columnCount; columnIndex++) {
        final TableColumnExt column = getColumnExt(columnIndex);
        final TableCellRenderer headerRenderer = column.getHeaderRenderer();
        final String columnName = model.getColumnName(columnIndex);
        int maxPreferedWidth = getPreferedSize(headerRenderer, String.class, columnName);
        for (int rowIndex = 0; rowIndex < model.getRowCount(); rowIndex++) {
            final Object value = model.getValueAt(rowIndex, columnIndex);
            if (value != null) {
                final TableCellRenderer renderer = column.getCellRenderer();
                final Class<?> columnClass = model.getColumnClass(columnIndex);
                final int width = getPreferedSize(renderer, columnClass, value);
                if (width > maxPreferedWidth) {
                    maxPreferedWidth = width;
                }
            }
        }
        column.setMinWidth(maxPreferedWidth + 25);
        column.setPreferredWidth(maxPreferedWidth + 25);
    }
}
Also used : TableCellRenderer(javax.swing.table.TableCellRenderer) TableColumnExt(org.jdesktop.swingx.table.TableColumnExt) TableModel(javax.swing.table.TableModel)

Example 4 with TableColumnExt

use of org.jdesktop.swingx.table.TableColumnExt 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;
            case ADD:
                // no action
                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) ShowInfoAction(com.haulmont.cuba.gui.components.sys.ShowInfoAction) 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) ShowInfoAction(com.haulmont.cuba.gui.components.sys.ShowInfoAction) MetaClass(com.haulmont.chile.core.model.MetaClass) TableColumnExt(org.jdesktop.swingx.table.TableColumnExt) CollectionDsActionsNotifier(com.haulmont.cuba.gui.data.impl.CollectionDsActionsNotifier)

Example 5 with TableColumnExt

use of org.jdesktop.swingx.table.TableColumnExt in project cuba by cuba-platform.

the class SwingXTableSettings method saveSettings.

@Override
public boolean saveSettings(Element element) {
    element.addAttribute("horizontalScroll", String.valueOf(table.isHorizontalScrollEnabled()));
    saveFontPreferences(element);
    Element columnsElem = element.element("columns");
    if (columnsElem != null) {
        element.remove(columnsElem);
    }
    columnsElem = element.addElement("columns");
    final List<TableColumn> visibleTableColumns = table.getColumns();
    final List<Table.Column> visibleColumns = new ArrayList<>();
    for (TableColumn tableColumn : visibleTableColumns) {
        visibleColumns.add((Table.Column) tableColumn.getIdentifier());
    }
    List<TableColumn> columns = table.getColumns(true);
    Collections.sort(columns, new Comparator<TableColumn>() {

        @SuppressWarnings("SuspiciousMethodCalls")
        @Override
        public int compare(TableColumn col1, TableColumn col2) {
            if (col1 instanceof TableColumnExt && !((TableColumnExt) col1).isVisible()) {
                return 1;
            }
            if (col2 instanceof TableColumnExt && !((TableColumnExt) col2).isVisible()) {
                return -1;
            }
            int i1 = visibleColumns.indexOf(col1.getIdentifier());
            int i2 = visibleColumns.indexOf(col2.getIdentifier());
            return Integer.compare(i1, i2);
        }
    });
    for (TableColumn column : columns) {
        Element colElem = columnsElem.addElement("column");
        colElem.addAttribute("id", column.getIdentifier().toString());
        int width = column.getWidth();
        colElem.addAttribute("width", String.valueOf(width));
        if (column instanceof TableColumnExt) {
            Boolean visible = ((TableColumnExt) column).isVisible();
            colElem.addAttribute("visible", visible.toString());
        }
    }
    if (table.getRowSorter() != null) {
        TableColumn sortedColumn = table.getSortedColumn();
        List<? extends RowSorter.SortKey> sortKeys = table.getRowSorter().getSortKeys();
        if (sortedColumn != null && !sortKeys.isEmpty()) {
            columnsElem.addAttribute("sortColumn", String.valueOf(sortedColumn.getIdentifier()));
            columnsElem.addAttribute("sortOrder", sortKeys.get(0).getSortOrder().toString());
        }
    }
    return true;
}
Also used : Table(com.haulmont.cuba.gui.components.Table) JXTable(org.jdesktop.swingx.JXTable) Element(org.dom4j.Element) TableColumn(javax.swing.table.TableColumn) TableColumn(javax.swing.table.TableColumn) TableColumnExt(org.jdesktop.swingx.table.TableColumnExt)

Aggregations

TableColumnExt (org.jdesktop.swingx.table.TableColumnExt)11 TableColumn (javax.swing.table.TableColumn)5 BaseJTable (com.revolsys.swing.table.BaseJTable)4 Element (org.dom4j.Element)3 JXTable (org.jdesktop.swingx.JXTable)3 Table (com.haulmont.cuba.gui.components.Table)2 MetaClass (com.haulmont.chile.core.model.MetaClass)1 MetaProperty (com.haulmont.chile.core.model.MetaProperty)1 MetaPropertyPath (com.haulmont.chile.core.model.MetaPropertyPath)1 RowSorterImpl (com.haulmont.cuba.desktop.gui.data.RowSorterImpl)1 FocusableTable (com.haulmont.cuba.desktop.sys.vcl.FocusableTable)1 Action (com.haulmont.cuba.gui.components.Action)1 ShowInfoAction (com.haulmont.cuba.gui.components.sys.ShowInfoAction)1 CollectionDsActionsNotifier (com.haulmont.cuba.gui.data.impl.CollectionDsActionsNotifier)1 WeakCollectionChangeListener (com.haulmont.cuba.gui.data.impl.WeakCollectionChangeListener)1 WeakItemPropertyChangeListener (com.haulmont.cuba.gui.data.impl.WeakItemPropertyChangeListener)1 NumericComparator (com.revolsys.comparator.NumericComparator)1 RecordDefinition (com.revolsys.record.schema.RecordDefinition)1 RecordTableCellEditor (com.revolsys.swing.table.record.editor.RecordTableCellEditor)1 SingleRecordTableCellRenderer (com.revolsys.swing.table.record.renderer.SingleRecordTableCellRenderer)1