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