JAL-2316 GUI updates to Connections tab in Preferences dialog
[jalview.git] / src / jalview / urls / UrlLinkTableModel.java
diff --git a/src/jalview/urls/UrlLinkTableModel.java b/src/jalview/urls/UrlLinkTableModel.java
new file mode 100644 (file)
index 0000000..103454b
--- /dev/null
@@ -0,0 +1,204 @@
+/*
+ * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
+ * Copyright (C) $$Year-Rel$$ The Jalview Authors
+ * 
+ * This file is part of Jalview.
+ * 
+ * Jalview is free software: you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License 
+ * as published by the Free Software Foundation, either version 3
+ * of the License, or (at your option) any later version.
+ *  
+ * Jalview is distributed in the hope that it will be useful, but 
+ * WITHOUT ANY WARRANTY; without even the implied warranty 
+ * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
+ * PURPOSE.  See the GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License
+ * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
+ * The Jalview Authors are detailed in the 'AUTHORS' file.
+ */
+
+package jalview.urls;
+
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+
+import javax.swing.table.AbstractTableModel;
+
+/**
+ * TableModel for UrlLinks table
+ * 
+ * @author $author$
+ * @version $Revision$
+ */
+
+public class UrlLinkTableModel extends AbstractTableModel
+{
+  // local storage of data
+  // use LinkedHashMap to guarantee ordering remains the same, as
+  // we need to maintain a row/col mapping into the HashMap
+  private List<UrlLinkDisplay> data;
+
+  private UrlProviderI dataProvider;
+
+  private List<String> displayColumns;
+
+  // row in table which is currently the default
+  private int defaultRow;
+
+  /**
+   * UrlLinkTableModel constructor
+   * 
+   * @param baseData
+   *          base data set to be presented in table
+   * @param entryNames
+   *          keys of entries in baseData's nested hashmap. Should match order
+   *          in displayColNames
+   * @param displayColNames
+   *          names of columns to display in order.
+   * @param keyColName
+   *          name of column corresponding to keys in baseData
+   */
+  public UrlLinkTableModel(UrlProviderI baseData,
+          List<String> displayColNames,
+          String keyColName)
+  {
+    dataProvider = baseData;
+    data = baseData.getLinksForTable();
+    displayColumns = new ArrayList<String>();
+    displayColumns.add(keyColName);
+    displayColumns.addAll(displayColNames);
+
+    // find the default row
+    defaultRow = 0;
+    Iterator<UrlLinkDisplay> it = data.iterator();
+    while (it.hasNext())
+    {
+      if (it.next().getIsDefault())
+      {
+        break;
+      }
+      else
+      {
+        defaultRow++;
+      }
+    }
+  }
+
+  @Override
+  public int getRowCount()
+  {
+    if (data == null)
+    {
+      return 0;
+    }
+    else
+    {
+      return data.size();
+    }
+  }
+
+  @Override
+  public int getColumnCount()
+  {
+    return displayColumns.size();
+  }
+
+  @Override
+  public Object getValueAt(int rowIndex, int columnIndex)
+  {
+    return data.get(rowIndex).getValue(columnIndex);
+  }
+
+  @Override
+  public boolean isCellEditable(int rowIndex, int columnIndex)
+  {
+    return data.get(rowIndex).isEditable(columnIndex);
+  }
+
+  @Override
+  public void setValueAt(Object aValue, int rowIndex, int columnIndex)
+  {
+    if ((columnIndex == UrlLinkDisplay.SELECTED)
+            && (rowIndex == defaultRow))
+    {
+      // Selected urls column: can't deselect default URL
+      // refuse to edit: TODO show message box here
+
+    }
+    else if (columnIndex == UrlLinkDisplay.DEFAULT)
+    {
+      // Default url column: exactly one row must always be true
+      if (rowIndex != defaultRow)
+      {
+        // selected row is not currently the default
+        // set the current default to false
+        data.get(defaultRow).setValue(columnIndex, false);
+        fireTableRowsUpdated(defaultRow, defaultRow);
+
+        // set the default to be the selected row
+        defaultRow = rowIndex;
+        data.get(rowIndex).setValue(columnIndex, aValue);
+
+        // default row must also be selected
+        if (!data.get(rowIndex).getIsSelected())
+        {
+          data.get(rowIndex).setValue(UrlLinkDisplay.SELECTED, true);
+        }
+        fireTableRowsUpdated(rowIndex, rowIndex);
+      }
+    }
+    else
+    {
+      data.get(rowIndex).setValue(columnIndex, aValue);
+      fireTableRowsUpdated(rowIndex, rowIndex);
+    }
+  }
+
+  @Override
+  public Class<?> getColumnClass(int columnIndex)
+  {
+    return getValueAt(0, columnIndex).getClass();
+  }
+
+  @Override
+  public String getColumnName(int columnIndex)
+  {
+    return displayColumns.get(columnIndex);
+  }
+
+  @Override
+  public String toString()
+  {
+    // update the UrlProvider from data list
+    dataProvider.setUrlData(data);
+
+    return dataProvider.writeUrlsAsString();
+  }
+
+  public void removeRow(int rowIndex)
+  {
+    // remove the row from data
+    data.remove(rowIndex);
+
+    // update default row
+    if (defaultRow > rowIndex)
+    {
+      defaultRow--;
+    }
+
+    fireTableRowsDeleted(rowIndex, rowIndex);
+  }
+
+  public int insertRow(String name, String url)
+  {
+    // add a row to the data
+    UrlLinkDisplay u = new UrlLinkDisplay(name, name, url, true, false);
+    int index = data.size();
+    data.add(u);
+    fireTableRowsInserted(index, index);
+    return index;
+  }
+}