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.ArrayList;
25 import java.util.Iterator;
26 import java.util.List;
28 import javax.swing.table.AbstractTableModel;
31 * TableModel for UrlLinks table
37 public class UrlLinkTableModel extends AbstractTableModel
39 // local storage of data
40 // use LinkedHashMap to guarantee ordering remains the same, as
41 // we need to maintain a row/col mapping into the HashMap
42 private List<UrlLinkDisplay> data;
44 private UrlProviderI dataProvider;
46 private List<String> displayColumns;
48 // row in table which is currently the default
49 private int defaultRow;
52 * UrlLinkTableModel constructor
55 * base data set to be presented in table
57 * keys of entries in baseData's nested hashmap. Should match order
59 * @param displayColNames
60 * names of columns to display in order.
62 * name of column corresponding to keys in baseData
64 public UrlLinkTableModel(UrlProviderI baseData,
65 List<String> displayColNames,
68 dataProvider = baseData;
69 data = baseData.getLinksForTable();
70 displayColumns = new ArrayList<String>();
71 displayColumns.add(keyColName);
72 displayColumns.addAll(displayColNames);
74 // find the default row
76 Iterator<UrlLinkDisplay> it = data.iterator();
79 if (it.next().getIsDefault())
91 public int getRowCount()
104 public int getColumnCount()
106 return displayColumns.size();
110 public Object getValueAt(int rowIndex, int columnIndex)
112 return data.get(rowIndex).getValue(columnIndex);
116 public boolean isCellEditable(int rowIndex, int columnIndex)
118 return data.get(rowIndex).isEditable(columnIndex);
122 public void setValueAt(Object aValue, int rowIndex, int columnIndex)
124 if ((columnIndex == UrlLinkDisplay.SELECTED)
125 && (rowIndex == defaultRow))
127 // Selected urls column: can't deselect default URL
128 // refuse to edit: TODO show message box here
131 else if (columnIndex == UrlLinkDisplay.DEFAULT)
133 // Default url column: exactly one row must always be true
134 if (rowIndex != defaultRow)
136 // selected row is not currently the default
137 // set the current default to false
138 data.get(defaultRow).setValue(columnIndex, false);
139 fireTableRowsUpdated(defaultRow, defaultRow);
141 // set the default to be the selected row
142 defaultRow = rowIndex;
143 data.get(rowIndex).setValue(columnIndex, aValue);
145 // default row must also be selected
146 if (!data.get(rowIndex).getIsSelected())
148 data.get(rowIndex).setValue(UrlLinkDisplay.SELECTED, true);
150 fireTableRowsUpdated(rowIndex, rowIndex);
155 data.get(rowIndex).setValue(columnIndex, aValue);
156 fireTableRowsUpdated(rowIndex, rowIndex);
161 public Class<?> getColumnClass(int columnIndex)
163 return getValueAt(0, columnIndex).getClass();
167 public String getColumnName(int columnIndex)
169 return displayColumns.get(columnIndex);
173 public String toString()
175 // update the UrlProvider from data list
176 dataProvider.setUrlData(data);
178 return dataProvider.writeUrlsAsString();
181 public void removeRow(int rowIndex)
183 // remove the row from data
184 data.remove(rowIndex);
186 // update default row
187 if (defaultRow > rowIndex)
192 fireTableRowsDeleted(rowIndex, rowIndex);
195 public int insertRow(String name, String url)
197 // add a row to the data
198 UrlLinkDisplay u = new UrlLinkDisplay(name, name, url, true, false);
199 int index = data.size();
201 fireTableRowsInserted(index, index);