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