Search in sources :

Example 31 with TableModel

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

the class UserActivityWatcher method processComponent.

protected void processComponent(final Component parentComponent) {
    if (parentComponent instanceof JTextComponent) {
        ((JTextComponent) parentComponent).getDocument().addDocumentListener(myDocumentListener);
    } else if (parentComponent instanceof ItemSelectable) {
        ((ItemSelectable) parentComponent).addItemListener(myItemListener);
    } else if (parentComponent instanceof JList) {
        ((JList) parentComponent).getModel().addListDataListener(myListDataListener);
        ((JList) parentComponent).addListSelectionListener(myListSelectionListener);
    } else if (parentComponent instanceof JTree) {
        ((JTree) parentComponent).getModel().addTreeModelListener(myTreeModelListener);
    } else if (parentComponent instanceof DocumentBasedComponent) {
        ((DocumentBasedComponent) parentComponent).getDocument().addDocumentListener(myIdeaDocumentListener);
    }
    if (parentComponent instanceof JComboBox) {
        ComboBoxEditor editor = ((JComboBox) parentComponent).getEditor();
        if (editor != null) {
            register(editor.getEditorComponent());
        }
    }
    if (parentComponent instanceof JTable) {
        JTable table = (JTable) parentComponent;
        table.addPropertyChangeListener("model", myTableListener);
        TableModel model = table.getModel();
        if (model != null) {
            model.addTableModelListener(myTableModelListener);
        }
        table.addPropertyChangeListener(ComboBox.TABLE_CELL_EDITOR_PROPERTY, myCellEditorChangeListener);
    }
    if (parentComponent instanceof JSlider) {
        ((JSlider) parentComponent).addChangeListener(myChangeListener);
    }
    if (parentComponent instanceof UserActivityProviderComponent) {
        ((UserActivityProviderComponent) parentComponent).addChangeListener(myChangeListener);
    }
}
Also used : JTextComponent(javax.swing.text.JTextComponent) TableModel(javax.swing.table.TableModel)

Example 32 with TableModel

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

the class TableUtil method doRemoveSelectedItems.

public static boolean doRemoveSelectedItems(@NotNull JTable table, @NotNull ItemRemovable itemRemovable, @Nullable ItemChecker applyable) {
    if (table.isEditing()) {
        table.getCellEditor().stopCellEditing();
    }
    ListSelectionModel selectionModel = table.getSelectionModel();
    int minSelectionIndex = selectionModel.getMinSelectionIndex();
    int maxSelectionIndex = selectionModel.getMaxSelectionIndex();
    if (minSelectionIndex == -1 || maxSelectionIndex == -1) {
        return false;
    }
    TableModel model = table.getModel();
    boolean removed = false;
    for (int index = maxSelectionIndex; index >= 0; index--) {
        int modelIndex = table.convertRowIndexToModel(index);
        if (selectionModel.isSelectedIndex(index) && (applyable == null || applyable.isOperationApplyable(model, modelIndex))) {
            itemRemovable.removeRow(modelIndex);
            removed = true;
        }
    }
    if (!removed) {
        return false;
    }
    int count = model.getRowCount();
    if (count == 0) {
        table.clearSelection();
    } else if (selectionModel.getMinSelectionIndex() == -1) {
        if (minSelectionIndex >= model.getRowCount()) {
            selectionModel.setSelectionInterval(model.getRowCount() - 1, model.getRowCount() - 1);
        } else {
            selectionModel.setSelectionInterval(minSelectionIndex, minSelectionIndex);
        }
    }
    return true;
}
Also used : TableModel(javax.swing.table.TableModel)

Example 33 with TableModel

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

the class TableUtil method moveSelectedItemsDown.

public static int moveSelectedItemsDown(@NotNull JTable table) {
    if (table.isEditing()) {
        table.getCellEditor().stopCellEditing();
    }
    TableModel model = table.getModel();
    ListSelectionModel selectionModel = table.getSelectionModel();
    int counter = 0;
    for (int row = model.getRowCount() - 1; row >= 0; row--) {
        if (selectionModel.isSelectedIndex(row)) {
            counter++;
            for (int column = 0; column < model.getColumnCount(); column++) {
                Object temp = model.getValueAt(row, column);
                model.setValueAt(model.getValueAt(row + 1, column), row, column);
                model.setValueAt(temp, row + 1, column);
            }
            selectionModel.removeSelectionInterval(row, row);
            selectionModel.addSelectionInterval(row + 1, row + 1);
        }
    }
    Rectangle cellRect = table.getCellRect(selectionModel.getMaxSelectionIndex(), 0, true);
    if (cellRect != null) {
        table.scrollRectToVisible(cellRect);
    }
    table.repaint();
    return counter;
}
Also used : TableModel(javax.swing.table.TableModel)

Example 34 with TableModel

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

the class TableUtil method removeSelectedItems.

@NotNull
public static List<Object[]> removeSelectedItems(@NotNull JTable table, @Nullable ItemChecker applyable) {
    final TableModel model = table.getModel();
    if (!(model instanceof ItemRemovable)) {
        throw new RuntimeException("model must be instance of ItemRemovable");
    }
    if (table.getSelectionModel().isSelectionEmpty()) {
        return new ArrayList<Object[]>(0);
    }
    final List<Object[]> removedItems = new SmartList<Object[]>();
    final ItemRemovable itemRemovable = (ItemRemovable) model;
    final int columnCount = model.getColumnCount();
    doRemoveSelectedItems(table, new ItemRemovable() {

        @Override
        public void removeRow(int index) {
            Object[] row = new Object[columnCount];
            for (int column = 0; column < columnCount; column++) {
                row[column] = model.getValueAt(index, column);
            }
            removedItems.add(row);
            itemRemovable.removeRow(index);
        }
    }, applyable);
    return ContainerUtil.reverse(removedItems);
}
Also used : ItemRemovable(com.intellij.util.ui.ItemRemovable) ArrayList(java.util.ArrayList) SmartList(com.intellij.util.SmartList) TableModel(javax.swing.table.TableModel) NotNull(org.jetbrains.annotations.NotNull)

Example 35 with TableModel

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

the class TableRowsTransferHandler method importData.

@Override
public boolean importData(TransferHandler.TransferSupport support) {
    TableModel tableModel = myTable.getModel();
    if (!(tableModel instanceof MultiReorderedModel) || !((MultiReorderedModel) tableModel).canMoveRows())
        return false;
    JTable.DropLocation dl = (JTable.DropLocation) support.getDropLocation();
    int index = dl.getRow();
    int max = tableModel.getRowCount();
    if (index < 0 || index > max) {
        index = max;
    }
    myTable.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
    try {
        int[] rows = ((RowsDragInfo) support.getTransferable().getTransferData(myDataFlavor)).myRows;
        if (rows != null && rows.length > 0) {
            int dist = 0;
            for (int row : rows) {
                if (index > row) {
                    dist++;
                }
            }
            index -= dist;
            int[] newSelection = ((MultiReorderedModel) tableModel).moveRows(rows, index);
            TableUtil.selectRows(myTable, newSelection);
            return true;
        }
    } catch (Exception e) {
        LOG.error("Couldn't move transferred data.");
    }
    return false;
}
Also used : 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