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.event.TableModelEvent;
28 import javax.swing.event.TableModelListener;
29 import javax.swing.table.AbstractTableModel;
32 * TableModel for UrlLinks table
38 public class UrlLinkTableModel extends AbstractTableModel
40 // local storage of data
41 private List<UrlLinkDisplay> data;
43 // supplier of url data
44 private UrlProviderI dataProvider;
46 // list of columns to display in table in correct order
47 private List<String> displayColumns;
49 // row in table which is currently the default
50 private int defaultRow;
53 * UrlLinkTableModel constructor
56 * base data set to be presented in table
58 * keys of entries in baseData's nested hashmap. Should match order
60 * @param displayColNames
61 * names of columns to display in order.
63 * name of column corresponding to keys in baseData
65 public UrlLinkTableModel(UrlProviderI baseData)
67 dataProvider = baseData;
68 data = baseData.getLinksForTable();
69 displayColumns = UrlLinkDisplay.getDisplayColumnNames();
71 // find the default row
73 Iterator<UrlLinkDisplay> it = data.iterator();
76 if (it.next().getIsDefault())
86 // set up listener which updates data source when table changes
87 this.addTableModelListener(new TableModelListener()
90 public void tableChanged(TableModelEvent e)
92 // update the UrlProvider from data list
93 dataProvider.setUrlData(data);
100 public int getRowCount()
113 public int getColumnCount()
115 return displayColumns.size();
119 public Object getValueAt(int rowIndex, int columnIndex)
121 return data.get(rowIndex).getValue(columnIndex);
125 public boolean isCellEditable(int rowIndex, int columnIndex)
127 return data.get(rowIndex).isEditable(columnIndex);
131 public void setValueAt(Object aValue, int rowIndex, int columnIndex)
133 if (columnIndex == UrlLinkDisplay.DEFAULT)
135 // Default url column: exactly one row must always be true
136 if (rowIndex != defaultRow)
138 // selected row is not currently the default
139 // set the current default to false
140 data.get(defaultRow).setValue(columnIndex, false);
141 fireTableRowsUpdated(defaultRow, defaultRow);
143 // set the default to be the selected row
144 defaultRow = rowIndex;
145 data.get(rowIndex).setValue(columnIndex, aValue);
147 fireTableRowsUpdated(rowIndex, rowIndex);
152 data.get(rowIndex).setValue(columnIndex, aValue);
153 fireTableRowsUpdated(rowIndex, rowIndex);
158 public Class<?> getColumnClass(int columnIndex)
160 return getValueAt(0, columnIndex).getClass();
164 public String getColumnName(int columnIndex)
166 return displayColumns.get(columnIndex);
169 public void removeRow(int rowIndex)
171 // remove the row from data
172 data.remove(rowIndex);
174 // update default row
175 if (defaultRow > rowIndex)
180 // fire update which will update data source
181 fireTableRowsDeleted(rowIndex, rowIndex);
184 public int insertRow(String name, String url)
186 // add a row to the data
187 UrlLinkDisplay u = new UrlLinkDisplay(name, name, url, true, false);
188 int index = data.size();
191 // fire update which will update data source
192 fireTableRowsInserted(index, index);