From: kiramt Date: Mon, 12 Dec 2016 15:43:50 +0000 (+0000) Subject: JAL-2316 Unit test update to UrlLinkTableModel and associated tidies X-Git-Tag: Release_2_10_3b1~346^2~17 X-Git-Url: http://source.jalview.org/gitweb/?a=commitdiff_plain;h=d046470837e799fd02070d30bb3aacb95348f71a;p=jalview.git JAL-2316 Unit test update to UrlLinkTableModel and associated tidies --- diff --git a/src/jalview/gui/Preferences.java b/src/jalview/gui/Preferences.java index 1c40ea1..9ee3077 100755 --- a/src/jalview/gui/Preferences.java +++ b/src/jalview/gui/Preferences.java @@ -377,9 +377,7 @@ public class Preferences extends GPreferences public boolean include( Entry entry) { - int col = ((UrlLinkTableModel) entry.getModel()).getIdColumn(); - String id = entry.getStringValue(col); - return sequenceUrlLinks.isUserEntry(id); + return ((UrlLinkTableModel) entry.getModel()).isUserEntry(entry); } }; @@ -1174,29 +1172,25 @@ public class Preferences extends GPreferences } int modelIndex = linkUrlTable.convertRowIndexToModel(index); - // determine if the new selection is a custom url or not - int col = ((UrlLinkTableModel) linkUrlTable.getModel()) - .getNameColumn(); - if (!sequenceUrlLinks.isUserEntry((String) linkUrlTable.getModel() - .getValueAt(modelIndex, col))) + // enable/disable edit and delete link buttons + if (((UrlLinkTableModel) linkUrlTable.getModel()) + .isRowDeletable(modelIndex)) { - // entry is not a user-defined url and so should not be edited - // disable edit and delete buttons - deleteLink.setEnabled(false); - editLink.setEnabled(false); + deleteLink.setEnabled(true); } else { - deleteLink.setEnabled(true); - editLink.setEnabled(true); + deleteLink.setEnabled(false); } - // BUT if it's the default url, don't allow deletion - col = ((UrlLinkTableModel) linkUrlTable.getModel()) - .getDefaultColumn(); - if ((boolean) linkUrlTable.getValueAt(index, col)) + if (((UrlLinkTableModel) linkUrlTable.getModel()) + .isRowEditable(modelIndex)) { - deleteLink.setEnabled(false); + editLink.setEnabled(true); + } + else + { + editLink.setEnabled(false); } } } diff --git a/src/jalview/urls/UrlLinkTableModel.java b/src/jalview/urls/UrlLinkTableModel.java index bbb227b..6b03667 100644 --- a/src/jalview/urls/UrlLinkTableModel.java +++ b/src/jalview/urls/UrlLinkTableModel.java @@ -28,9 +28,11 @@ import jalview.util.UrlLink; import java.util.Iterator; import java.util.List; +import javax.swing.RowFilter.Entry; import javax.swing.event.TableModelEvent; import javax.swing.event.TableModelListener; import javax.swing.table.AbstractTableModel; +import javax.swing.table.TableModel; /** * TableModel for UrlLinks table @@ -137,6 +139,33 @@ public class UrlLinkTableModel extends AbstractTableModel return data.get(rowIndex).isEditable(columnIndex); } + /** + * Determine if a row is editable indirectly (rather than directly in table as + * in isCellEditable) + * + * @param rowIndex + * @return true if row can be edited indirectly + */ + public boolean isRowEditable(int rowIndex) + { + // to edit, row must be a user entered row + return (dataProvider.isUserEntry(data.get(rowIndex).getId())); + } + + /** + * Determine if a row is deletable + * + * @param rowIndex + * the row to be tested + * @return true if row can be deleted + */ + public boolean isRowDeletable(int rowIndex) + { + // to delete, row must be a user entered row, and not the default row + return (dataProvider.isUserEntry(data.get(rowIndex).getId()) && !data + .get(rowIndex).getIsDefault()); + } + @Override public void setValueAt(Object aValue, int rowIndex, int columnIndex) { @@ -228,4 +257,11 @@ public class UrlLinkTableModel extends AbstractTableModel { return UrlLinkDisplay.SELECTED; } + + public boolean isUserEntry( + Entry entry) + { + return dataProvider + .isUserEntry(entry.getStringValue(UrlLinkDisplay.ID)); + } } diff --git a/test/jalview/urls/UrlLinkTableModelTest.java b/test/jalview/urls/UrlLinkTableModelTest.java index ca4b658..141c8fa 100644 --- a/test/jalview/urls/UrlLinkTableModelTest.java +++ b/test/jalview/urls/UrlLinkTableModelTest.java @@ -287,4 +287,46 @@ public class UrlLinkTableModelTest { } } } + + /* + * Test row 'deletability' + */ + @Test(groups = { "Functional" }) + public void testDeletable() + { + UrlLinkTableModel m = new UrlLinkTableModel(prov); + + for (int row = 0; row < m.getRowCount(); row++) + { + if (row > 4) + { + Assert.assertTrue(m.isRowDeletable(row)); + } + else + { + Assert.assertFalse(m.isRowDeletable(row)); + } + } + } + + /* + * Test indirect row editability + */ + @Test(groups = { "Functional" }) + public void testRowEditable() + { + UrlLinkTableModel m = new UrlLinkTableModel(prov); + + for (int row = 0; row < m.getRowCount(); row++) + { + if (row > 3) + { + Assert.assertTrue(m.isRowEditable(row)); + } + else + { + Assert.assertFalse(m.isRowEditable(row)); + } + } + } }