Class |
Line # |
Actions |
|||
---|---|---|---|---|---|
UrlLinkTableModel | 44 | 50 | 28 |
1 | /* | |
2 | * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$) | |
3 | * Copyright (C) $$Year-Rel$$ The Jalview Authors | |
4 | * | |
5 | * This file is part of Jalview. | |
6 | * | |
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. | |
11 | * | |
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. | |
16 | * | |
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. | |
20 | */ | |
21 | ||
22 | package jalview.urls; | |
23 | ||
24 | import jalview.bin.Console; | |
25 | import jalview.urls.api.UrlProviderI; | |
26 | import jalview.util.UrlLink; | |
27 | ||
28 | import java.util.Iterator; | |
29 | import java.util.List; | |
30 | ||
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; | |
36 | ||
37 | /** | |
38 | * TableModel for UrlLinks table | |
39 | * | |
40 | * @author $author$ | |
41 | * @version $Revision$ | |
42 | */ | |
43 | ||
44 | public class UrlLinkTableModel extends AbstractTableModel | |
45 | { | |
46 | // local storage of data | |
47 | private List<UrlLinkDisplay> data; | |
48 | ||
49 | // supplier of url data | |
50 | private UrlProviderI dataProvider; | |
51 | ||
52 | // list of columns to display in table in correct order | |
53 | private List<String> displayColumns; | |
54 | ||
55 | // row in table which is currently the primary | |
56 | private int primaryRow; | |
57 | ||
58 | /** | |
59 | * UrlLinkTableModel constructor | |
60 | * | |
61 | * @param baseData | |
62 | * base data set to be presented in table | |
63 | * @param entryNames | |
64 | * keys of entries in baseData's nested hashmap. Should match order | |
65 | * in displayColNames | |
66 | * @param displayColNames | |
67 | * names of columns to display in order. | |
68 | * @param keyColName | |
69 | * name of column corresponding to keys in baseData | |
70 | */ | |
71 | 19 | public UrlLinkTableModel(UrlProviderI baseData) |
72 | { | |
73 | 19 | dataProvider = baseData; |
74 | 19 | data = baseData.getLinksForTable(); |
75 | 19 | displayColumns = UrlLinkDisplay.getDisplayColumnNames(); |
76 | ||
77 | // find the primary row | |
78 | 19 | primaryRow = 0; |
79 | 19 | Iterator<UrlLinkDisplay> it = data.iterator(); |
80 | 6285 | while (it.hasNext()) |
81 | { | |
82 | 6285 | if (it.next().getIsPrimary()) |
83 | { | |
84 | 19 | break; |
85 | } | |
86 | else | |
87 | { | |
88 | 6266 | primaryRow++; |
89 | } | |
90 | } | |
91 | ||
92 | // set up listener which updates data source when table changes | |
93 | 19 | this.addTableModelListener(new TableModelListener() |
94 | { | |
95 | 9 | @Override |
96 | public void tableChanged(TableModelEvent e) | |
97 | { | |
98 | 9 | try |
99 | { | |
100 | // update the UrlProvider from data list | |
101 | 9 | dataProvider.setUrlData(data); |
102 | } catch (IllegalArgumentException ex) | |
103 | { | |
104 | 0 | Console.error(ex.getMessage()); |
105 | } | |
106 | } | |
107 | }); | |
108 | ||
109 | } | |
110 | ||
111 | 68 | @Override |
112 | public int getRowCount() | |
113 | { | |
114 | 68 | if (data == null) |
115 | { | |
116 | 0 | return 0; |
117 | } | |
118 | else | |
119 | { | |
120 | 68 | return data.size(); |
121 | } | |
122 | } | |
123 | ||
124 | 1 | @Override |
125 | public int getColumnCount() | |
126 | { | |
127 | 1 | return displayColumns.size(); |
128 | } | |
129 | ||
130 | 55 | @Override |
131 | public Object getValueAt(int rowIndex, int columnIndex) | |
132 | { | |
133 | 55 | return data.get(rowIndex).getValue(columnIndex); |
134 | } | |
135 | ||
136 | 50 | @Override |
137 | public boolean isCellEditable(int rowIndex, int columnIndex) | |
138 | { | |
139 | 50 | return data.get(rowIndex).isEditable(columnIndex); |
140 | } | |
141 | ||
142 | /** | |
143 | * Determine if a row is editable indirectly (rather than directly in table as | |
144 | * in isCellEditable) | |
145 | * | |
146 | * @param rowIndex | |
147 | * @return true if row can be edited indirectly | |
148 | */ | |
149 | 10 | public boolean isRowEditable(int rowIndex) |
150 | { | |
151 | // to edit, row must be a user entered row | |
152 | 10 | return (dataProvider.isUserEntry(data.get(rowIndex).getId())); |
153 | } | |
154 | ||
155 | /** | |
156 | * Determine if a row is deletable | |
157 | * | |
158 | * @param rowIndex | |
159 | * the row to be tested | |
160 | * @return true if row can be deleted | |
161 | */ | |
162 | 10 | public boolean isRowDeletable(int rowIndex) |
163 | { | |
164 | // to delete, row must be a user entered row, and not the default row | |
165 | 10 | return (dataProvider.isUserEntry(data.get(rowIndex).getId()) |
166 | && !data.get(rowIndex).getIsPrimary()); | |
167 | } | |
168 | ||
169 | 6 | @Override |
170 | public void setValueAt(Object aValue, int rowIndex, int columnIndex) | |
171 | { | |
172 | 6 | if (columnIndex == UrlLinkDisplay.PRIMARY) |
173 | { | |
174 | // Default url column: exactly one row must always be true | |
175 | 1 | if (rowIndex != primaryRow) |
176 | { | |
177 | // selected row is not currently the default | |
178 | // set the current default to false | |
179 | 1 | data.get(primaryRow).setValue(columnIndex, false); |
180 | 1 | fireTableRowsUpdated(primaryRow, primaryRow); |
181 | ||
182 | // set the default to be the selected row | |
183 | 1 | primaryRow = rowIndex; |
184 | 1 | data.get(rowIndex).setValue(columnIndex, aValue); |
185 | ||
186 | 1 | fireTableRowsUpdated(rowIndex, rowIndex); |
187 | } | |
188 | } | |
189 | else | |
190 | { | |
191 | 5 | data.get(rowIndex).setValue(columnIndex, aValue); |
192 | 5 | fireTableRowsUpdated(rowIndex, rowIndex); |
193 | } | |
194 | } | |
195 | ||
196 | 5 | @Override |
197 | public Class<?> getColumnClass(int columnIndex) | |
198 | { | |
199 | 5 | return getValueAt(0, columnIndex).getClass(); |
200 | } | |
201 | ||
202 | 5 | @Override |
203 | public String getColumnName(int columnIndex) | |
204 | { | |
205 | 5 | return displayColumns.get(columnIndex); |
206 | } | |
207 | ||
208 | 1 | public void removeRow(int rowIndex) |
209 | { | |
210 | // remove the row from data | |
211 | 1 | data.remove(rowIndex); |
212 | ||
213 | // update default row | |
214 | 1 | if (primaryRow > rowIndex) |
215 | { | |
216 | 1 | primaryRow--; |
217 | } | |
218 | ||
219 | // fire update which will update data source | |
220 | 1 | fireTableRowsDeleted(rowIndex, rowIndex); |
221 | } | |
222 | ||
223 | 1 | public int insertRow(String name, String url) |
224 | { | |
225 | // add a row to the data | |
226 | 1 | UrlLink link = new UrlLink(name, url, name); |
227 | 1 | UrlLinkDisplay u = new UrlLinkDisplay(name, link, true, false); |
228 | 1 | int index = data.size(); |
229 | 1 | data.add(u); |
230 | ||
231 | // fire update which will update data source | |
232 | 1 | fireTableRowsInserted(index, index); |
233 | 1 | return index; |
234 | } | |
235 | ||
236 | 0 | public int getPrimaryColumn() |
237 | { | |
238 | 0 | return UrlLinkDisplay.PRIMARY; |
239 | } | |
240 | ||
241 | 0 | public int getNameColumn() |
242 | { | |
243 | 0 | return UrlLinkDisplay.NAME; |
244 | } | |
245 | ||
246 | 0 | public int getDatabaseColumn() |
247 | { | |
248 | 0 | return UrlLinkDisplay.DATABASE; |
249 | } | |
250 | ||
251 | 0 | public int getIdColumn() |
252 | { | |
253 | 0 | return UrlLinkDisplay.ID; |
254 | } | |
255 | ||
256 | 0 | public int getUrlColumn() |
257 | { | |
258 | 0 | return UrlLinkDisplay.URL; |
259 | } | |
260 | ||
261 | 0 | public int getSelectedColumn() |
262 | { | |
263 | 0 | return UrlLinkDisplay.SELECTED; |
264 | } | |
265 | ||
266 | 0 | public boolean isUserEntry( |
267 | Entry<? extends TableModel, ? extends Object> entry) | |
268 | { | |
269 | 0 | return dataProvider |
270 | .isUserEntry(entry.getStringValue(UrlLinkDisplay.ID)); | |
271 | } | |
272 | ||
273 | 0 | public boolean isUniqueName(String name) |
274 | { | |
275 | 0 | return !dataProvider.contains(name); |
276 | } | |
277 | } |