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 java.util.Iterator;
25 import java.util.List;
27 import javax.swing.table.AbstractTableModel;
30 * TableModel for UrlLinks table
36 public class UrlLinkTableModel extends AbstractTableModel
38 // local storage of data
39 // use LinkedHashMap to guarantee ordering remains the same, as
40 // we need to maintain a row/col mapping into the HashMap
41 private List<UrlLinkDisplay> data;
43 private UrlProviderI dataProvider;
45 private List<String> displayColumns;
47 // row in table which is currently the default
48 private int defaultRow;
51 * UrlLinkTableModel constructor
54 * base data set to be presented in table
56 * keys of entries in baseData's nested hashmap. Should match order
58 * @param displayColNames
59 * names of columns to display in order.
61 * name of column corresponding to keys in baseData
63 public UrlLinkTableModel(UrlProviderI baseData)
65 dataProvider = baseData;
66 data = baseData.getLinksForTable();
67 displayColumns = UrlLinkDisplay.getDisplayColumnNames();
69 // find the default row
71 Iterator<UrlLinkDisplay> it = data.iterator();
74 if (it.next().getIsDefault())
86 public int getRowCount()
99 public int getColumnCount()
101 return displayColumns.size();
105 public Object getValueAt(int rowIndex, int columnIndex)
107 return data.get(rowIndex).getValue(columnIndex);
111 public boolean isCellEditable(int rowIndex, int columnIndex)
113 return data.get(rowIndex).isEditable(columnIndex);
117 public void setValueAt(Object aValue, int rowIndex, int columnIndex)
119 if ((columnIndex == UrlLinkDisplay.SELECTED)
120 && (rowIndex == defaultRow))
122 // Selected urls column: can't deselect default URL
123 // refuse to edit: TODO show message box here
126 else if (columnIndex == UrlLinkDisplay.DEFAULT)
128 // Default url column: exactly one row must always be true
129 if (rowIndex != defaultRow)
131 // selected row is not currently the default
132 // set the current default to false
133 data.get(defaultRow).setValue(columnIndex, false);
134 fireTableRowsUpdated(defaultRow, defaultRow);
136 // set the default to be the selected row
137 defaultRow = rowIndex;
138 data.get(rowIndex).setValue(columnIndex, aValue);
140 // default row must also be selected
141 if (!data.get(rowIndex).getIsSelected())
143 data.get(rowIndex).setValue(UrlLinkDisplay.SELECTED, true);
145 fireTableRowsUpdated(rowIndex, rowIndex);
150 data.get(rowIndex).setValue(columnIndex, aValue);
151 fireTableRowsUpdated(rowIndex, rowIndex);
156 public Class<?> getColumnClass(int columnIndex)
158 return getValueAt(0, columnIndex).getClass();
162 public String getColumnName(int columnIndex)
164 return displayColumns.get(columnIndex);
168 public String toString()
170 // update the UrlProvider from data list
171 dataProvider.setUrlData(data);
173 return dataProvider.writeUrlsAsString();
176 public void removeRow(int rowIndex)
178 // remove the row from data
179 data.remove(rowIndex);
181 // update default row
182 if (defaultRow > rowIndex)
187 fireTableRowsDeleted(rowIndex, rowIndex);
190 public int insertRow(String name, String url)
192 // add a row to the data
193 UrlLinkDisplay u = new UrlLinkDisplay(name, name, url, true, false);
194 int index = data.size();
196 fireTableRowsInserted(index, index);