Search in sources :

Example 6 with RecordTableCellEditor

use of com.revolsys.swing.table.record.editor.RecordTableCellEditor in project com.revolsys.open by revolsys.

the class AbstractSingleRecordTableModel method newTable.

public static BaseJTable newTable(final AbstractSingleRecordTableModel model) {
    final BaseJTable table = new BaseJTable(model);
    table.setModel(model);
    table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    table.setAutoCreateColumnsFromModel(false);
    table.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);
    final SingleRecordTableCellRenderer cellRenderer = new SingleRecordTableCellRenderer();
    final RecordTableCellEditor cellEditor = new RecordTableCellEditor(table);
    cellEditor.setPopupMenu(model.getMenu()::newJPopupMenu);
    final RecordDefinition recordDefinition = model.getRecordDefinition();
    final List<String> allFieldNames = recordDefinition.getFieldNames();
    int maxTitleWidth = 100;
    for (final String fieldName : allFieldNames) {
        final String title = model.getFieldTitle(fieldName);
        final int titleWidth = Math.max(title.length(), fieldName.length()) * 8;
        if (titleWidth > maxTitleWidth) {
            maxTitleWidth = titleWidth;
        }
    }
    final int columnCount = model.getColumnCount();
    int columnWidth;
    if (columnCount > 3) {
        columnWidth = (740 - maxTitleWidth) / 2;
    } else {
        columnWidth = (740 - maxTitleWidth) / 2;
    }
    for (int i = 0; i < columnCount; i++) {
        final TableColumnExt column = table.getColumnExt(i);
        column.setCellRenderer(cellRenderer);
        if (i == 0) {
            column.setMinWidth(40);
            column.setPreferredWidth(40);
            column.setMaxWidth(40);
            column.setComparator(new NumericComparator());
        } else if (i == 1) {
            column.setMinWidth(maxTitleWidth);
            column.setPreferredWidth(maxTitleWidth);
            column.setMaxWidth(maxTitleWidth);
        } else {
            column.setPreferredWidth(columnWidth);
            if (i == 2) {
                column.setCellEditor(cellEditor);
            }
        }
    }
    return table;
}
Also used : TableColumnExt(org.jdesktop.swingx.table.TableColumnExt) SingleRecordTableCellRenderer(com.revolsys.swing.table.record.renderer.SingleRecordTableCellRenderer) BaseJTable(com.revolsys.swing.table.BaseJTable) NumericComparator(com.revolsys.comparator.NumericComparator) RecordTableCellEditor(com.revolsys.swing.table.record.editor.RecordTableCellEditor) RecordDefinition(com.revolsys.record.schema.RecordDefinition)

Aggregations

RecordTableCellEditor (com.revolsys.swing.table.record.editor.RecordTableCellEditor)6 RecordRowTableModel (com.revolsys.swing.table.record.model.RecordRowTableModel)3 JComponent (javax.swing.JComponent)3 EventObject (java.util.EventObject)2 NumericComparator (com.revolsys.comparator.NumericComparator)1 Geometry (com.revolsys.geometry.model.Geometry)1 RecordDefinition (com.revolsys.record.schema.RecordDefinition)1 BaseJTable (com.revolsys.swing.table.BaseJTable)1 SingleRecordTableCellRenderer (com.revolsys.swing.table.record.renderer.SingleRecordTableCellRenderer)1 StringSelection (java.awt.datatransfer.StringSelection)1 Transferable (java.awt.datatransfer.Transferable)1 TableColumnExt (org.jdesktop.swingx.table.TableColumnExt)1