UI: fixed row selecting in tables list

This commit is contained in:
Oleg Agafonov 2019-01-22 23:00:23 +04:00
parent 4349ec881e
commit dd09de7a09
3 changed files with 38 additions and 17 deletions

View file

@ -27,16 +27,15 @@ public class MageTable extends JTable {
@Override
public String getToolTipText(MouseEvent e) {
// default tooltip for cells
String tip = null;
java.awt.Point p = e.getPoint();
int rowIndex = rowAtPoint(p);
int colIndex = columnAtPoint(p);
try {
tip = this.getModel().getValueAt(this.convertRowIndexToModel(rowIndex), this.convertColumnIndexToModel(colIndex)).toString();
} catch (RuntimeException e1) {
//catch null pointer exception if mouse is over an empty line
int viewRow = rowAtPoint(p);
int viewCol = columnAtPoint(p);
int modelRow = TablesUtil.getModelRowFromView(this, viewRow);
int modelCol = this.convertColumnIndexToModel(viewCol);
String tip = null;
if (modelRow != -1 && modelCol != -1) {
tip = this.getModel().getValueAt(modelRow, modelCol).toString();
}
return GUISizeHelper.textToHtmlWithSize(tip, GUISizeHelper.tableFont);
}

View file

@ -387,9 +387,12 @@ public class TablesPanel extends javax.swing.JPanel {
table.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent e) {
int row = e.getFirstIndex();
String rowId = TablesUtil.getSearchIdFromTable(table, row);
tablesLastSelection.put(table, rowId);
int modelRow = TablesUtil.getSelectedModelRow(table);
if (modelRow != -1) {
// needs only selected
String rowId = TablesUtil.getSearchIdFromTable(table, modelRow);
tablesLastSelection.put(table, rowId);
}
}
});
@ -401,9 +404,11 @@ public class TablesPanel extends javax.swing.JPanel {
@Override
public void run() {
String lastRowID = tablesLastSelection.get(table);
int needRow = TablesUtil.findTableRowFromSearchId(table.getModel(), lastRowID);
if (needRow != -1) {
table.addRowSelectionInterval(needRow, needRow);
int needModelRow = TablesUtil.findTableRowFromSearchId(table.getModel(), lastRowID);
int needViewRow = TablesUtil.getViewRowFromModel(table, needModelRow);
if (needViewRow != -1) {
table.clearSelection();
table.addRowSelectionInterval(needViewRow, needViewRow);
}
}
});
@ -415,9 +420,9 @@ public class TablesPanel extends javax.swing.JPanel {
table.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
int row = table.getSelectedRow();
if (e.getClickCount() == 2 && row != -1) {
action.actionPerformed(new ActionEvent(table, ActionEvent.ACTION_PERFORMED, TablesUtil.getSearchIdFromTable(table, row)));
int modelRow = TablesUtil.getSelectedModelRow(table);
if (e.getClickCount() == 2 && modelRow != -1) {
action.actionPerformed(new ActionEvent(table, ActionEvent.ACTION_PERFORMED, TablesUtil.getSearchIdFromTable(table, modelRow)));
}
}
});

View file

@ -43,4 +43,21 @@ public class TablesUtil {
return row;
}
public static int getSelectedModelRow(JTable table) {
return getModelRowFromView(table, table.getSelectedRow());
}
public static int getModelRowFromView(JTable table, int viewRow) {
if (viewRow != -1 && viewRow < table.getModel().getRowCount()) {
return table.convertRowIndexToModel(viewRow);
}
return -1;
}
public static int getViewRowFromModel(JTable table, int modelRow) {
if (modelRow != -1 && modelRow < table.getModel().getRowCount()) {
return table.convertRowIndexToView(modelRow);
}
return -1;
}
}