2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
24 import jalview.bin.Console;
25 import jalview.urls.api.UrlProviderI;
26 import jalview.util.UrlLink;
28 import java.util.Iterator;
29 import java.util.List;
31 import javax.swing.RowFilter.Entry;
32 import javax.swing.event.TableModelEvent;
33 import javax.swing.event.TableModelListener;
34 import javax.swing.table.AbstractTableModel;
35 import javax.swing.table.TableModel;
38 * TableModel for UrlLinks table
44 public class UrlLinkTableModel extends AbstractTableModel
46 // local storage of data
47 private List<UrlLinkDisplay> data;
49 // supplier of url data
50 private UrlProviderI dataProvider;
52 // list of columns to display in table in correct order
53 private List<String> displayColumns;
55 // row in table which is currently the primary
56 private int primaryRow;
59 * UrlLinkTableModel constructor
62 * base data set to be presented in table
64 * keys of entries in baseData's nested hashmap. Should match order
66 * @param displayColNames
67 * names of columns to display in order.
69 * name of column corresponding to keys in baseData
71 public UrlLinkTableModel(UrlProviderI baseData)
73 dataProvider = baseData;
74 data = baseData.getLinksForTable();
75 displayColumns = UrlLinkDisplay.getDisplayColumnNames();
77 // find the primary row
79 Iterator<UrlLinkDisplay> it = data.iterator();
82 if (it.next().getIsPrimary())
92 // set up listener which updates data source when table changes
93 this.addTableModelListener(new TableModelListener()
96 public void tableChanged(TableModelEvent e)
100 // update the UrlProvider from data list
101 dataProvider.setUrlData(data);
102 } catch (IllegalArgumentException ex)
104 Console.error(ex.getMessage());
112 public int getRowCount()
125 public int getColumnCount()
127 return displayColumns.size();
131 public Object getValueAt(int rowIndex, int columnIndex)
133 return data.get(rowIndex).getValue(columnIndex);
137 public boolean isCellEditable(int rowIndex, int columnIndex)
139 return data.get(rowIndex).isEditable(columnIndex);
143 * Determine if a row is editable indirectly (rather than directly in table as
147 * @return true if row can be edited indirectly
149 public boolean isRowEditable(int rowIndex)
151 // to edit, row must be a user entered row
152 return (dataProvider.isUserEntry(data.get(rowIndex).getId()));
156 * Determine if a row is deletable
159 * the row to be tested
160 * @return true if row can be deleted
162 public boolean isRowDeletable(int rowIndex)
164 // to delete, row must be a user entered row, and not the default row
165 return (dataProvider.isUserEntry(data.get(rowIndex).getId())
166 && !data.get(rowIndex).getIsPrimary());
170 public void setValueAt(Object aValue, int rowIndex, int columnIndex)
172 if (columnIndex == UrlLinkDisplay.PRIMARY)
174 // Default url column: exactly one row must always be true
175 if (rowIndex != primaryRow)
177 // selected row is not currently the default
178 // set the current default to false
179 data.get(primaryRow).setValue(columnIndex, false);
180 fireTableRowsUpdated(primaryRow, primaryRow);
182 // set the default to be the selected row
183 primaryRow = rowIndex;
184 data.get(rowIndex).setValue(columnIndex, aValue);
186 fireTableRowsUpdated(rowIndex, rowIndex);
191 data.get(rowIndex).setValue(columnIndex, aValue);
192 fireTableRowsUpdated(rowIndex, rowIndex);
197 public Class<?> getColumnClass(int columnIndex)
199 return getValueAt(0, columnIndex).getClass();
203 public String getColumnName(int columnIndex)
205 return displayColumns.get(columnIndex);
208 public void removeRow(int rowIndex)
210 // remove the row from data
211 data.remove(rowIndex);
213 // update default row
214 if (primaryRow > rowIndex)
219 // fire update which will update data source
220 fireTableRowsDeleted(rowIndex, rowIndex);
223 public int insertRow(String name, String url)
225 // add a row to the data
226 UrlLink link = new UrlLink(name, url, name);
227 UrlLinkDisplay u = new UrlLinkDisplay(name, link, true, false);
228 int index = data.size();
231 // fire update which will update data source
232 fireTableRowsInserted(index, index);
236 public int getPrimaryColumn()
238 return UrlLinkDisplay.PRIMARY;
241 public int getNameColumn()
243 return UrlLinkDisplay.NAME;
246 public int getDatabaseColumn()
248 return UrlLinkDisplay.DATABASE;
251 public int getIdColumn()
253 return UrlLinkDisplay.ID;
256 public int getUrlColumn()
258 return UrlLinkDisplay.URL;
261 public int getSelectedColumn()
263 return UrlLinkDisplay.SELECTED;
266 public boolean isUserEntry(
267 Entry<? extends TableModel, ? extends Object> entry)
270 .isUserEntry(entry.getStringValue(UrlLinkDisplay.ID));
273 public boolean isUniqueName(String name)
275 return !dataProvider.contains(name);