Search in sources :

Example 11 with TableModel

use of javax.swing.table.TableModel in project adempiere by adempiere.

the class CTable method autoSize.

//  stopEditor
/**************************************************************************
	 *	Size Columns.
	 *	@param useColumnIdentifier if false uses plain content -
	 *  otherwise uses Column Identifier to indicate displayed columns
	 */
public void autoSize(boolean useColumnIdentifier) {
    TableModel model = this.getModel();
    int size = model.getColumnCount();
    //	for all columns
    for (int c = 0; c < size; c++) {
        TableColumn column = getColumnModel().getColumn(c);
        //	Not displayed columns
        if (useColumnIdentifier && (column.getIdentifier() == null || column.getMaxWidth() == 0 || column.getIdentifier().toString().length() == 0))
            continue;
        int width = 0;
        //	Header
        TableCellRenderer renderer = column.getHeaderRenderer();
        if (renderer == null)
            renderer = new DefaultTableCellRenderer();
        Component comp = null;
        if (renderer != null)
            comp = renderer.getTableCellRendererComponent(this, column.getHeaderValue(), false, false, 0, 0);
        //
        if (comp != null) {
            width = comp.getPreferredSize().width;
            width = Math.max(width, comp.getWidth());
            //	Cells
            int col = column.getModelIndex();
            int maxRow = Math.min(20, getRowCount());
            try {
                for (int row = 0; row < maxRow; row++) {
                    renderer = getCellRenderer(row, col);
                    comp = renderer.getTableCellRendererComponent(this, getValueAt(row, col), false, false, row, col);
                    if (comp != null) {
                        int rowWidth = comp.getPreferredSize().width;
                        width = Math.max(width, rowWidth);
                    }
                }
            } catch (Exception e) {
                log.log(Level.SEVERE, column.getIdentifier().toString(), e);
            }
            //	Width not greater than 250
            width = Math.min(MAXSIZE, width + SLACK);
        }
        //
        column.setPreferredWidth(width);
    }
//	for all columns
}
Also used : TableCellRenderer(javax.swing.table.TableCellRenderer) DefaultTableCellRenderer(javax.swing.table.DefaultTableCellRenderer) JComponent(javax.swing.JComponent) Component(java.awt.Component) TableColumn(javax.swing.table.TableColumn) TableModel(javax.swing.table.TableModel) DefaultTableModel(javax.swing.table.DefaultTableModel) DefaultTableCellRenderer(javax.swing.table.DefaultTableCellRenderer)

Example 12 with TableModel

use of javax.swing.table.TableModel in project adempiere by adempiere.

the class VBrowserTable method autoSize.

/**
     * Size Columns.
     * Uses Mimimun Column Size
     */
public void autoSize() {
    if (!autoResize)
        return;
    long start = System.currentTimeMillis();
    //
    //	making sure it fits in a column
    final int SLACK = 8;
    //	max size of a column
    final int MAXSIZE = 300;
    //
    TableModel model = this.getModel();
    int size = model.getColumnCount();
    //	for all columns
    for (int col = 0; col < size; col++) {
        //  Column & minimum width
        TableColumn tc = this.getColumnModel().getColumn(col);
        int width = 0;
        if (m_minWidth.size() > col)
            width = ((Integer) m_minWidth.get(col)).intValue();
        //  log.config( "Column=" + col + " " + column.getHeaderValue());
        //	Header
        TableCellRenderer renderer = tc.getHeaderRenderer();
        if (renderer == null)
            renderer = new DefaultTableCellRenderer();
        Component comp = renderer.getTableCellRendererComponent(this, tc.getHeaderValue(), false, false, 0, 0);
        //	log.fine( "Hdr - preferred=" + comp.getPreferredSize().width + ", width=" + comp.getWidth());
        width = Math.max(width, comp.getPreferredSize().width + SLACK);
        //	Cells
        //  first 30 rows
        int maxRow = Math.min(30, getRowCount());
        for (int row = 0; row < maxRow; row++) {
            renderer = getCellRenderer(row, col);
            comp = renderer.getTableCellRendererComponent(this, getValueAt(row, col), false, false, row, col);
            if (comp != null) {
                int rowWidth = comp.getPreferredSize().width + SLACK;
                width = Math.max(width, rowWidth);
            }
        }
        //	Width not greater ..
        width = Math.min(MAXSIZE, width);
        tc.setPreferredWidth(width);
    //	log.fine( "width=" + width);
    }
    //	for all columns
    log.finer("Cols=" + size + " - " + (System.currentTimeMillis() - start) + "ms");
}
Also used : TableCellRenderer(javax.swing.table.TableCellRenderer) DefaultTableCellRenderer(javax.swing.table.DefaultTableCellRenderer) Component(java.awt.Component) TableColumn(javax.swing.table.TableColumn) TableModel(javax.swing.table.TableModel) DefaultTableModel(javax.swing.table.DefaultTableModel) DefaultTableCellRenderer(javax.swing.table.DefaultTableCellRenderer)

Example 13 with TableModel

use of javax.swing.table.TableModel in project adempiere by adempiere.

the class VCreateFromDialog method save.

public boolean save(String trxName) {
    dataTable.stopEditor(true);
    TableModel model = dataTable.getModel();
    int rows = model.getRowCount();
    if (rows == 0)
        return false;
    return createFrom.save(dataTable, trxName);
}
Also used : TableModel(javax.swing.table.TableModel)

Example 14 with TableModel

use of javax.swing.table.TableModel in project intellij-community by JetBrains.

the class ImportLayoutPanel method createTableForPackageEntries.

public static JBTable createTableForPackageEntries(final PackageEntryTable packageTable, final ImportLayoutPanel panel) {
    final String[] names = { ApplicationBundle.message("listbox.import.package"), ApplicationBundle.message("listbox.import.with.subpackages") };
    // Create a model of the data.
    TableModel dataModel = new AbstractTableModel() {

        public int getColumnCount() {
            return names.length + (panel.areStaticImportsEnabled() ? 1 : 0);
        }

        public int getRowCount() {
            return packageTable.getEntryCount();
        }

        @Nullable
        public Object getValueAt(int row, int col) {
            PackageEntry entry = packageTable.getEntryAt(row);
            if (entry == null || !isCellEditable(row, col))
                return null;
            col += panel.areStaticImportsEnabled() ? 0 : 1;
            if (col == 0) {
                return entry.isStatic();
            }
            if (col == 1) {
                return entry.getPackageName();
            }
            if (col == 2) {
                return entry.isWithSubpackages();
            }
            throw new IllegalArgumentException(String.valueOf(col));
        }

        public String getColumnName(int column) {
            if (panel.areStaticImportsEnabled() && column == 0)
                return "Static";
            column -= panel.areStaticImportsEnabled() ? 1 : 0;
            return names[column];
        }

        public Class getColumnClass(int col) {
            col += panel.areStaticImportsEnabled() ? 0 : 1;
            if (col == 0) {
                return Boolean.class;
            }
            if (col == 1) {
                return String.class;
            }
            if (col == 2) {
                return Boolean.class;
            }
            throw new IllegalArgumentException(String.valueOf(col));
        }

        public boolean isCellEditable(int row, int col) {
            PackageEntry packageEntry = packageTable.getEntryAt(row);
            return !packageEntry.isSpecial();
        }

        public void setValueAt(Object aValue, int row, int col) {
            PackageEntry packageEntry = packageTable.getEntryAt(row);
            col += panel.areStaticImportsEnabled() ? 0 : 1;
            if (col == 0) {
                PackageEntry newPackageEntry = new PackageEntry((Boolean) aValue, packageEntry.getPackageName(), packageEntry.isWithSubpackages());
                packageTable.setEntryAt(newPackageEntry, row);
            } else if (col == 1) {
                PackageEntry newPackageEntry = new PackageEntry(packageEntry.isStatic(), ((String) aValue).trim(), packageEntry.isWithSubpackages());
                packageTable.setEntryAt(newPackageEntry, row);
            } else if (col == 2) {
                PackageEntry newPackageEntry = new PackageEntry(packageEntry.isStatic(), packageEntry.getPackageName(), ((Boolean) aValue).booleanValue());
                packageTable.setEntryAt(newPackageEntry, row);
            } else {
                throw new IllegalArgumentException(String.valueOf(col));
            }
        }
    };
    // Create the table
    final JBTable result = new JBTable(dataModel);
    result.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    resizeColumns(packageTable, result, panel.areStaticImportsEnabled());
    TableCellEditor editor = result.getDefaultEditor(String.class);
    if (editor instanceof DefaultCellEditor) {
        ((DefaultCellEditor) editor).setClickCountToStart(1);
    }
    TableCellEditor beditor = result.getDefaultEditor(Boolean.class);
    beditor.addCellEditorListener(new CellEditorListener() {

        public void editingStopped(ChangeEvent e) {
            if (panel.areStaticImportsEnabled()) {
                // add/remove static keyword
                result.repaint();
            }
        }

        public void editingCanceled(ChangeEvent e) {
        }
    });
    return result;
}
Also used : CellEditorListener(javax.swing.event.CellEditorListener) JBTable(com.intellij.ui.table.JBTable) ChangeEvent(javax.swing.event.ChangeEvent) PackageEntry(com.intellij.psi.codeStyle.PackageEntry) AbstractTableModel(javax.swing.table.AbstractTableModel) TableCellEditor(javax.swing.table.TableCellEditor) AbstractTableModel(javax.swing.table.AbstractTableModel) TableModel(javax.swing.table.TableModel)

Example 15 with TableModel

use of javax.swing.table.TableModel in project intellij-community by JetBrains.

the class EditMigrationDialog method createTable.

private JBTable createTable() {
    final String[] names = { RefactoringBundle.message("migration.type.column.header"), RefactoringBundle.message("migration.old.name.column.header"), RefactoringBundle.message("migration.new.name.column.header") };
    // Create a model of the data.
    TableModel dataModel = new AbstractTableModel() {

        public int getColumnCount() {
            return 3;
        }

        public int getRowCount() {
            return myMigrationMap.getEntryCount();
        }

        public Object getValueAt(int row, int col) {
            MigrationMapEntry entry = myMigrationMap.getEntryAt(row);
            if (col == 0) {
                if (entry.getType() == MigrationMapEntry.PACKAGE && entry.isRecursive()) {
                    return RefactoringBundle.message("migration.package.with.subpackages");
                } else if (entry.getType() == MigrationMapEntry.PACKAGE && !entry.isRecursive()) {
                    return RefactoringBundle.message("migration.package");
                } else {
                    return RefactoringBundle.message("migration.class");
                }
            }
            String suffix = (entry.getType() == MigrationMapEntry.PACKAGE ? ".*" : "");
            if (col == 1) {
                return entry.getOldName() + suffix;
            } else {
                return entry.getNewName() + suffix;
            }
        }

        public String getColumnName(int column) {
            return names[column];
        }

        public Class getColumnClass(int c) {
            return String.class;
        }

        public boolean isCellEditable(int row, int col) {
            return false;
        }

        public void setValueAt(Object aValue, int row, int column) {
        }
    };
    // Create the table
    myTable = new JBTable(dataModel);
    myTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    myTable.setPreferredScrollableViewportSize(new Dimension(300, myTable.getRowHeight() * 10));
    return myTable;
}
Also used : AbstractTableModel(javax.swing.table.AbstractTableModel) JBTable(com.intellij.ui.table.JBTable) AbstractTableModel(javax.swing.table.AbstractTableModel) TableModel(javax.swing.table.TableModel)

Aggregations

TableModel (javax.swing.table.TableModel)53 AbstractTableModel (javax.swing.table.AbstractTableModel)11 JTable (javax.swing.JTable)9 DefaultTableModel (javax.swing.table.DefaultTableModel)9 TableColumn (javax.swing.table.TableColumn)9 ArrayList (java.util.ArrayList)8 TableRowSorter (javax.swing.table.TableRowSorter)8 TableCellRenderer (javax.swing.table.TableCellRenderer)6 ActionEvent (java.awt.event.ActionEvent)5 JButton (javax.swing.JButton)5 JScrollPane (javax.swing.JScrollPane)5 TableCellEditor (javax.swing.table.TableCellEditor)5 Component (java.awt.Component)4 ActionListener (java.awt.event.ActionListener)4 List (java.util.List)4 Set (java.util.Set)4 TableColumnModel (javax.swing.table.TableColumnModel)4 IOException (java.io.IOException)3 HashSet (java.util.HashSet)3 AttributesTableModel (com.android.tools.idea.editors.theme.attributes.AttributesTableModel)2