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 // use LinkedHashMap to guarantee ordering remains the same, as
42 // we need to maintain a row/col mapping into the HashMap
43 private List<UrlLinkDisplay> data;
45 private UrlProviderI dataProvider;
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 this.addTableModelListener(new TableModelListener()
89 public void tableChanged(TableModelEvent e)
91 // update the UrlProvider from data list
92 dataProvider.setUrlData(data);
99 public int getRowCount()
112 public int getColumnCount()
114 return displayColumns.size();
118 public Object getValueAt(int rowIndex, int columnIndex)
120 return data.get(rowIndex).getValue(columnIndex);
124 public boolean isCellEditable(int rowIndex, int columnIndex)
126 return data.get(rowIndex).isEditable(columnIndex);
130 public void setValueAt(Object aValue, int rowIndex, int columnIndex)
132 if ((columnIndex == UrlLinkDisplay.SELECTED)
133 && (rowIndex == defaultRow))
135 // Selected urls column: can't deselect default URL
136 // refuse to edit: TODO show message box here
139 else if (columnIndex == UrlLinkDisplay.DEFAULT)
141 // Default url column: exactly one row must always be true
142 if (rowIndex != defaultRow)
144 // selected row is not currently the default
145 // set the current default to false
146 data.get(defaultRow).setValue(columnIndex, false);
147 fireTableRowsUpdated(defaultRow, defaultRow);
149 // set the default to be the selected row
150 defaultRow = rowIndex;
151 data.get(rowIndex).setValue(columnIndex, aValue);
153 // default row must also be selected
154 if (!data.get(rowIndex).getIsSelected())
156 data.get(rowIndex).setValue(UrlLinkDisplay.SELECTED, true);
158 fireTableRowsUpdated(rowIndex, rowIndex);
163 data.get(rowIndex).setValue(columnIndex, aValue);
164 fireTableRowsUpdated(rowIndex, rowIndex);
169 public Class<?> getColumnClass(int columnIndex)
171 return getValueAt(0, columnIndex).getClass();
175 public String getColumnName(int columnIndex)
177 return displayColumns.get(columnIndex);
180 public void removeRow(int rowIndex)
182 // remove the row from data
183 data.remove(rowIndex);
185 // update default row
186 if (defaultRow > rowIndex)
191 fireTableRowsDeleted(rowIndex, rowIndex);
194 public int insertRow(String name, String url)
196 // add a row to the data
197 UrlLinkDisplay u = new UrlLinkDisplay(name, name, url, true, false);
198 int index = data.size();
200 fireTableRowsInserted(index, index);