Search in sources :

Example 1 with EditableModel

use of com.intellij.util.ui.EditableModel in project intellij-community by JetBrains.

the class TableToolbarDecorator method createDefaultTableActions.

private void createDefaultTableActions(@Nullable final ElementProducer<?> producer) {
    final JTable table = myTable;
    final EditableModel tableModel = (EditableModel) table.getModel();
    myAddAction = new AnActionButtonRunnable() {

        @Override
        public void run(AnActionButton button) {
            TableUtil.stopEditing(table);
            final int rowCount = table.getRowCount();
            if (tableModel instanceof ListTableModel && producer != null) {
                //noinspection unchecked
                ((ListTableModel) tableModel).addRow(producer.createElement());
            } else {
                tableModel.addRow();
            }
            if (rowCount == table.getRowCount())
                return;
            final int index = table.getModel().getRowCount() - 1;
            table.setRowSelectionInterval(index, index);
            table.setColumnSelectionInterval(0, 0);
            table.editCellAt(index, 0);
            TableUtil.updateScroller(table);
            //noinspection SSBasedInspection
            SwingUtilities.invokeLater(() -> {
                final Component editorComponent = table.getEditorComponent();
                if (editorComponent != null) {
                    final Rectangle bounds = editorComponent.getBounds();
                    table.scrollRectToVisible(bounds);
                    IdeFocusManager.getGlobalInstance().doWhenFocusSettlesDown(() -> {
                        IdeFocusManager.getGlobalInstance().requestFocus(editorComponent, true);
                    });
                }
            });
        }
    };
    myRemoveAction = new AnActionButtonRunnable() {

        @Override
        public void run(AnActionButton button) {
            if (TableUtil.doRemoveSelectedItems(table, tableModel, null)) {
                updateButtons();
                IdeFocusManager.getGlobalInstance().doWhenFocusSettlesDown(() -> {
                    IdeFocusManager.getGlobalInstance().requestFocus(table, true);
                });
                TableUtil.updateScroller(table);
            }
        }
    };
    class MoveRunnable implements AnActionButtonRunnable {

        final int delta;

        MoveRunnable(int delta) {
            this.delta = delta;
        }

        @Override
        public void run(AnActionButton button) {
            int row = table.getEditingRow();
            int col = table.getEditingColumn();
            TableUtil.stopEditing(table);
            int[] idx = table.getSelectedRows();
            Arrays.sort(idx);
            if (delta > 0) {
                idx = ArrayUtil.reverseArray(idx);
            }
            if (idx.length == 0)
                return;
            if (idx[0] + delta < 0)
                return;
            if (idx[idx.length - 1] + delta > table.getModel().getRowCount())
                return;
            for (int i = 0; i < idx.length; i++) {
                tableModel.exchangeRows(idx[i], idx[i] + delta);
                idx[i] += delta;
            }
            TableUtil.selectRows(table, idx);
            IdeFocusManager.getGlobalInstance().doWhenFocusSettlesDown(() -> {
                IdeFocusManager.getGlobalInstance().requestFocus(table, true);
            });
            if (row > 0 && col != -1) {
                table.editCellAt(row - 1, col);
            }
        }
    }
    myUpAction = new MoveRunnable(-1);
    myDownAction = new MoveRunnable(1);
}
Also used : ListTableModel(com.intellij.util.ui.ListTableModel) EditableModel(com.intellij.util.ui.EditableModel)

Example 2 with EditableModel

use of com.intellij.util.ui.EditableModel in project sonarlint-intellij by SonarSource.

the class SonarLintProjectPropertiesPanel method create.

public JPanel create() {
    tableModel = new PropertiesTableModel();
    // Unfortunately TableModel's listener does not work properly, it doesn't receive events related to changed cells.
    final JBTable table = new JBTable(tableModel);
    table.getEmptyText().setText("No SonarLint properties configured for this project");
    JPanel tablePanel = ToolbarDecorator.createDecorator(table).disableUpAction().disableDownAction().setAddAction(anActionButton -> {
        final TableCellEditor cellEditor = table.getCellEditor();
        if (cellEditor != null) {
            cellEditor.stopCellEditing();
        }
        final TableModel model = table.getModel();
        ((EditableModel) model).addRow();
        TableUtil.editCellAt(table, model.getRowCount() - 1, 0);
    }).createPanel();
    tablePanel.setBorder(BorderFactory.createTitledBorder("Analysis parameters"));
    return tablePanel;
}
Also used : BorderFactory(javax.swing.BorderFactory) ToolbarDecorator(com.intellij.ui.ToolbarDecorator) ArrayList(java.util.ArrayList) JBTable(com.intellij.ui.table.JBTable) TableCellEditor(javax.swing.table.TableCellEditor) EditableModel(com.intellij.util.ui.EditableModel) Map(java.util.Map) TableUtil(com.intellij.ui.TableUtil) AbstractTableModel(javax.swing.table.AbstractTableModel) Comparator(java.util.Comparator) Nullable(javax.annotation.Nullable) JPanel(javax.swing.JPanel) TableModel(javax.swing.table.TableModel) JPanel(javax.swing.JPanel) TableCellEditor(javax.swing.table.TableCellEditor) JBTable(com.intellij.ui.table.JBTable) AbstractTableModel(javax.swing.table.AbstractTableModel) TableModel(javax.swing.table.TableModel)

Example 3 with EditableModel

use of com.intellij.util.ui.EditableModel in project intellij-community by JetBrains.

the class ProcessorProfilePanel method createTablePanel.

private static JPanel createTablePanel(final JBTable table) {
    return ToolbarDecorator.createDecorator(table).disableUpAction().disableDownAction().setAddAction(new AnActionButtonRunnable() {

        @Override
        public void run(AnActionButton anActionButton) {
            final TableCellEditor cellEditor = table.getCellEditor();
            if (cellEditor != null) {
                cellEditor.stopCellEditing();
            }
            final TableModel model = table.getModel();
            ((EditableModel) model).addRow();
            TableUtil.editCellAt(table, model.getRowCount() - 1, 0);
        }
    }).createPanel();
}
Also used : EditableModel(com.intellij.util.ui.EditableModel) TableCellEditor(javax.swing.table.TableCellEditor) AbstractTableModel(javax.swing.table.AbstractTableModel) TableModel(javax.swing.table.TableModel)

Example 4 with EditableModel

use of com.intellij.util.ui.EditableModel in project intellij-community by JetBrains.

the class RowsDnDSupport method installImpl.

private static void installImpl(@NotNull final JComponent component, @NotNull final EditableModel model) {
    component.setTransferHandler(new TransferHandler(null));
    DnDSupport.createBuilder(component).setBeanProvider(info -> {
        final Point p = info.getPoint();
        return new DnDDragStartBean(new RowDragInfo(component, Integer.valueOf(getRow(component, p))));
    }).setTargetChecker(new DnDTargetChecker() {

        @Override
        public boolean update(DnDEvent event) {
            final Object o = event.getAttachedObject();
            if (!(o instanceof RowDragInfo) || ((RowDragInfo) o).component != component) {
                event.setDropPossible(false, "");
                return true;
            }
            event.setDropPossible(true);
            int oldIndex = ((RowDragInfo) o).row;
            int newIndex = getRow(component, event.getPoint());
            if (newIndex == -1) {
                event.setDropPossible(false, "");
                return true;
            }
            Rectangle cellBounds = getCellBounds(component, newIndex);
            if (model instanceof RefinedDropSupport) {
                RefinedDropSupport.Position position = ((RefinedDropSupport) model).isDropInto(component, oldIndex, newIndex) ? INTO : (event.getPoint().y < cellBounds.y + cellBounds.height / 2) ? ABOVE : BELOW;
                boolean canDrop = ((RefinedDropSupport) model).canDrop(oldIndex, newIndex, position);
                event.setDropPossible(canDrop);
                if (canDrop && oldIndex != newIndex) {
                    if (position == BELOW) {
                        cellBounds.y += cellBounds.height - 2;
                    }
                    RelativeRectangle rectangle = new RelativeRectangle(component, cellBounds);
                    switch(position) {
                        case INTO:
                            event.setHighlighting(rectangle, DnDEvent.DropTargetHighlightingType.RECTANGLE);
                            break;
                        case ABOVE:
                        case BELOW:
                            rectangle.getDimension().height = 2;
                            event.setHighlighting(rectangle, DnDEvent.DropTargetHighlightingType.FILLED_RECTANGLE);
                            break;
                    }
                    return true;
                } else {
                    event.hideHighlighter();
                    return true;
                }
            } else {
                if (oldIndex == newIndex) {
                    // Drag&Drop always starts with new==old and we shouldn't display 'rejecting' cursor in this case
                    return true;
                }
                boolean canExchange = model.canExchangeRows(oldIndex, newIndex);
                if (canExchange) {
                    if (oldIndex < newIndex) {
                        cellBounds.y += cellBounds.height - 2;
                    }
                    RelativeRectangle rectangle = new RelativeRectangle(component, cellBounds);
                    rectangle.getDimension().height = 2;
                    event.setDropPossible(true);
                    event.setHighlighting(rectangle, DnDEvent.DropTargetHighlightingType.FILLED_RECTANGLE);
                } else {
                    event.setDropPossible(false);
                }
                return true;
            }
        }
    }).setDropHandler(new DnDDropHandler() {

        @Override
        public void drop(DnDEvent event) {
            final Object o = event.getAttachedObject();
            final Point p = event.getPoint();
            if (o instanceof RowDragInfo && ((RowDragInfo) o).component == component) {
                int oldIndex = ((RowDragInfo) o).row;
                if (oldIndex == -1)
                    return;
                int newIndex = getRow(component, p);
                if (newIndex == -1) {
                    newIndex = getRowCount(component) - 1;
                }
                if (oldIndex != newIndex) {
                    if (model instanceof RefinedDropSupport) {
                        Rectangle cellBounds = getCellBounds(component, newIndex);
                        RefinedDropSupport.Position position = ((RefinedDropSupport) model).isDropInto(component, oldIndex, newIndex) ? INTO : (event.getPoint().y < cellBounds.y + cellBounds.height / 2) ? ABOVE : BELOW;
                        if (((RefinedDropSupport) model).canDrop(oldIndex, newIndex, position)) {
                            ((RefinedDropSupport) model).drop(oldIndex, newIndex, position);
                        }
                    } else {
                        if (model.canExchangeRows(oldIndex, newIndex)) {
                            model.exchangeRows(oldIndex, newIndex);
                            setSelectedRow(component, newIndex);
                        }
                    }
                }
            }
            event.hideHighlighter();
        }
    }).install();
}
Also used : java.awt(java.awt) RelativeRectangle(com.intellij.ui.awt.RelativeRectangle) Function(com.intellij.util.Function) EditableModel(com.intellij.util.ui.EditableModel) com.intellij.ide.dnd(com.intellij.ide.dnd) NotNull(org.jetbrains.annotations.NotNull) javax.swing(javax.swing) Position(com.intellij.ui.RowsDnDSupport.RefinedDropSupport.Position) Position(com.intellij.ui.RowsDnDSupport.RefinedDropSupport.Position) RelativeRectangle(com.intellij.ui.awt.RelativeRectangle) Position(com.intellij.ui.RowsDnDSupport.RefinedDropSupport.Position) RelativeRectangle(com.intellij.ui.awt.RelativeRectangle)

Aggregations

EditableModel (com.intellij.util.ui.EditableModel)4 AbstractTableModel (javax.swing.table.AbstractTableModel)2 TableCellEditor (javax.swing.table.TableCellEditor)2 TableModel (javax.swing.table.TableModel)2 com.intellij.ide.dnd (com.intellij.ide.dnd)1 Position (com.intellij.ui.RowsDnDSupport.RefinedDropSupport.Position)1 TableUtil (com.intellij.ui.TableUtil)1 ToolbarDecorator (com.intellij.ui.ToolbarDecorator)1 RelativeRectangle (com.intellij.ui.awt.RelativeRectangle)1 JBTable (com.intellij.ui.table.JBTable)1 Function (com.intellij.util.Function)1 ListTableModel (com.intellij.util.ui.ListTableModel)1 java.awt (java.awt)1 ArrayList (java.util.ArrayList)1 Comparator (java.util.Comparator)1 Map (java.util.Map)1 Nullable (javax.annotation.Nullable)1 javax.swing (javax.swing)1 BorderFactory (javax.swing.BorderFactory)1 JPanel (javax.swing.JPanel)1