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);
}
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;
}
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();
}
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();
}
Aggregations