103454bbb954d520c8dda85aa6506ba86b2e7f5a
[jalview.git] / src / jalview / urls / UrlLinkTableModel.java
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 java.util.ArrayList;
25 import java.util.Iterator;
26 import java.util.List;
27
28 import javax.swing.table.AbstractTableModel;
29
30 /**
31  * TableModel for UrlLinks table
32  * 
33  * @author $author$
34  * @version $Revision$
35  */
36
37 public class UrlLinkTableModel extends AbstractTableModel
38 {
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;
43
44   private UrlProviderI dataProvider;
45
46   private List<String> displayColumns;
47
48   // row in table which is currently the default
49   private int defaultRow;
50
51   /**
52    * UrlLinkTableModel constructor
53    * 
54    * @param baseData
55    *          base data set to be presented in table
56    * @param entryNames
57    *          keys of entries in baseData's nested hashmap. Should match order
58    *          in displayColNames
59    * @param displayColNames
60    *          names of columns to display in order.
61    * @param keyColName
62    *          name of column corresponding to keys in baseData
63    */
64   public UrlLinkTableModel(UrlProviderI baseData,
65           List<String> displayColNames,
66           String keyColName)
67   {
68     dataProvider = baseData;
69     data = baseData.getLinksForTable();
70     displayColumns = new ArrayList<String>();
71     displayColumns.add(keyColName);
72     displayColumns.addAll(displayColNames);
73
74     // find the default row
75     defaultRow = 0;
76     Iterator<UrlLinkDisplay> it = data.iterator();
77     while (it.hasNext())
78     {
79       if (it.next().getIsDefault())
80       {
81         break;
82       }
83       else
84       {
85         defaultRow++;
86       }
87     }
88   }
89
90   @Override
91   public int getRowCount()
92   {
93     if (data == null)
94     {
95       return 0;
96     }
97     else
98     {
99       return data.size();
100     }
101   }
102
103   @Override
104   public int getColumnCount()
105   {
106     return displayColumns.size();
107   }
108
109   @Override
110   public Object getValueAt(int rowIndex, int columnIndex)
111   {
112     return data.get(rowIndex).getValue(columnIndex);
113   }
114
115   @Override
116   public boolean isCellEditable(int rowIndex, int columnIndex)
117   {
118     return data.get(rowIndex).isEditable(columnIndex);
119   }
120
121   @Override
122   public void setValueAt(Object aValue, int rowIndex, int columnIndex)
123   {
124     if ((columnIndex == UrlLinkDisplay.SELECTED)
125             && (rowIndex == defaultRow))
126     {
127       // Selected urls column: can't deselect default URL
128       // refuse to edit: TODO show message box here
129
130     }
131     else if (columnIndex == UrlLinkDisplay.DEFAULT)
132     {
133       // Default url column: exactly one row must always be true
134       if (rowIndex != defaultRow)
135       {
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);
140
141         // set the default to be the selected row
142         defaultRow = rowIndex;
143         data.get(rowIndex).setValue(columnIndex, aValue);
144
145         // default row must also be selected
146         if (!data.get(rowIndex).getIsSelected())
147         {
148           data.get(rowIndex).setValue(UrlLinkDisplay.SELECTED, true);
149         }
150         fireTableRowsUpdated(rowIndex, rowIndex);
151       }
152     }
153     else
154     {
155       data.get(rowIndex).setValue(columnIndex, aValue);
156       fireTableRowsUpdated(rowIndex, rowIndex);
157     }
158   }
159
160   @Override
161   public Class<?> getColumnClass(int columnIndex)
162   {
163     return getValueAt(0, columnIndex).getClass();
164   }
165
166   @Override
167   public String getColumnName(int columnIndex)
168   {
169     return displayColumns.get(columnIndex);
170   }
171
172   @Override
173   public String toString()
174   {
175     // update the UrlProvider from data list
176     dataProvider.setUrlData(data);
177
178     return dataProvider.writeUrlsAsString();
179   }
180
181   public void removeRow(int rowIndex)
182   {
183     // remove the row from data
184     data.remove(rowIndex);
185
186     // update default row
187     if (defaultRow > rowIndex)
188     {
189       defaultRow--;
190     }
191
192     fireTableRowsDeleted(rowIndex, rowIndex);
193   }
194
195   public int insertRow(String name, String url)
196   {
197     // add a row to the data
198     UrlLinkDisplay u = new UrlLinkDisplay(name, name, url, true, false);
199     int index = data.size();
200     data.add(u);
201     fireTableRowsInserted(index, index);
202     return index;
203   }
204 }