JAL-2316 Added jalview.urls.* packages
[jalview.git] / src / jalview / gui / Preferences.java
index 7a24aeb..1c40ea1 100755 (executable)
@@ -30,10 +30,12 @@ import jalview.jbgui.GPreferences;
 import jalview.jbgui.GSequenceLink;
 import jalview.schemes.ColourSchemeProperty;
 import jalview.urls.UrlLinkTableModel;
-import jalview.urls.UrlProvider;
-import jalview.urls.UrlProviderI;
+import jalview.urls.api.UrlProviderFactoryI;
+import jalview.urls.api.UrlProviderI;
+import jalview.urls.desktop.DesktopUrlProviderFactory;
 import jalview.util.MessageManager;
 import jalview.util.Platform;
+import jalview.util.UrlConstants;
 import jalview.ws.sifts.SiftsSettings;
 
 import java.awt.BorderLayout;
@@ -126,17 +128,23 @@ public class Preferences extends GPreferences
   public static List<String> groupURLLinks;
   static
   {
-    String string = Cache.getDefault("SEQUENCE_LINKS",
-            UrlProviderI.DEFAULT_STRING);
-    sequenceUrlLinks = new UrlProvider(UrlProviderI.DEFAULT_LABEL, string);
-
-    List<String> colNames = new ArrayList<String>();
-    // colNames.add("ID");
-    // TODO KM add to properties file
-    colNames.add("URL");
-    colNames.add("In Menu");
-    colNames.add("Default");
-    dataModel = new UrlLinkTableModel(sequenceUrlLinks, colNames, "ID");
+    // get links selected to be in the menu (SEQUENCE_LINKS)
+    // and links entered by the user but not selected (STORED_LINKS)
+    String inMenuString = Cache.getDefault("SEQUENCE_LINKS", "");
+    String notInMenuString = Cache.getDefault("STORED_LINKS", "");
+    String defaultUrl = Cache.getDefault("DEFAULT_URL",
+            UrlConstants.DEFAULT_LABEL);
+
+    // if both links lists are empty, add the DEFAULT_URL link
+    // otherwise we assume the default link is in one of the lists
+    if (inMenuString.isEmpty() && notInMenuString.isEmpty())
+    {
+      inMenuString = UrlConstants.DEFAULT_STRING;
+    }
+    UrlProviderFactoryI factory = new DesktopUrlProviderFactory(defaultUrl,
+            inMenuString, notInMenuString);
+    sequenceUrlLinks = factory.createUrlProvider();
+    dataModel = new UrlLinkTableModel(sequenceUrlLinks);
 
     /**
      * TODO: reformulate groupURL encoding so two or more can be stored in the
@@ -337,10 +345,13 @@ public class Preferences extends GPreferences
     linkUrlTable.setRowSorter(sorter);
     List<RowSorter.SortKey> sortKeys = new ArrayList<>();
 
-    sortKeys.add(new RowSorter.SortKey(3,
+    UrlLinkTableModel m = (UrlLinkTableModel) linkUrlTable.getModel();
+    sortKeys.add(new RowSorter.SortKey(m.getDefaultColumn(),
+            SortOrder.DESCENDING));
+    sortKeys.add(new RowSorter.SortKey(m.getSelectedColumn(),
             SortOrder.DESCENDING));
-    sortKeys.add(new RowSorter.SortKey(2, SortOrder.DESCENDING));
-    sortKeys.add(new RowSorter.SortKey(0, SortOrder.ASCENDING));
+    sortKeys.add(new RowSorter.SortKey(m.getNameColumn(),
+            SortOrder.ASCENDING));
 
     sorter.setSortKeys(sortKeys);
     sorter.sort();
@@ -366,7 +377,8 @@ public class Preferences extends GPreferences
       public boolean include(
               Entry<? extends TableModel, ? extends Object> entry)
       {
-        String id = entry.getStringValue(4);
+        int col = ((UrlLinkTableModel) entry.getModel()).getIdColumn();
+        String id = entry.getStringValue(col);
         return sequenceUrlLinks.isUserEntry(id);
       }
     };
@@ -389,24 +401,26 @@ public class Preferences extends GPreferences
 
     filterTB.getDocument().addDocumentListener(new DocumentListener()
     {
+      String caseInsensitiveFlag = "(?i)";
+
       @Override
       public void changedUpdate(DocumentEvent e)
       {
-        sorter.setRowFilter(RowFilter.regexFilter("(?i)"
+        sorter.setRowFilter(RowFilter.regexFilter(caseInsensitiveFlag
                 + filterTB.getText()));
       }
 
       @Override
       public void removeUpdate(DocumentEvent e)
       {
-        sorter.setRowFilter(RowFilter.regexFilter("(?i)"
+        sorter.setRowFilter(RowFilter.regexFilter(caseInsensitiveFlag
                 + filterTB.getText()));
       }
 
       @Override
       public void insertUpdate(DocumentEvent e)
       {
-        sorter.setRowFilter(RowFilter.regexFilter("(?i)"
+        sorter.setRowFilter(RowFilter.regexFilter(caseInsensitiveFlag
                 + filterTB.getText()));
       }
     });
@@ -416,9 +430,12 @@ public class Preferences extends GPreferences
             new UrlListSelectionHandler());
 
     // set up radio buttons
-    linkUrlTable.getColumn("Default").setCellRenderer(
-            new RadioButtonRenderer());
-    linkUrlTable.getColumn("Default")
+    int onClickCol = ((UrlLinkTableModel) linkUrlTable.getModel())
+            .getDefaultColumn();
+    String onClickName = linkUrlTable.getColumnName(onClickCol);
+    linkUrlTable.getColumn(onClickName).setCellRenderer(
+               new RadioButtonRenderer());
+    linkUrlTable.getColumn(onClickName)
             .setCellEditor(new RadioButtonEditor());
 
     // get boolean columns and resize those to min possible
@@ -638,17 +655,31 @@ public class Preferences extends GPreferences
     jalview.util.BrowserLauncher.resetBrowser();
 
     // save user-defined and selected links
-    String links = linkUrlTable.getModel().toString();
-    if (links.isEmpty())
+    String menuLinks = sequenceUrlLinks.writeUrlsAsString(true);
+    if (menuLinks.isEmpty())
     {
       Cache.applicationProperties.remove("SEQUENCE_LINKS");
     }
     else
     {
       Cache.applicationProperties.setProperty("SEQUENCE_LINKS",
-              links.toString());
+              menuLinks.toString());
     }
 
+    String nonMenuLinks = sequenceUrlLinks.writeUrlsAsString(false);
+    if (nonMenuLinks.isEmpty())
+    {
+      Cache.applicationProperties.remove("STORED_LINKS");
+    }
+    else
+    {
+      Cache.applicationProperties.setProperty("STORED_LINKS",
+              nonMenuLinks.toString());
+    }
+
+    Cache.applicationProperties.setProperty("DEFAULT_URL",
+            sequenceUrlLinks.getDefaultUrlId());
+
     Cache.applicationProperties.setProperty("USE_PROXY",
             Boolean.toString(useProxy.isSelected()));
 
@@ -828,7 +859,6 @@ public class Preferences extends GPreferences
   @Override
   public void newLink_actionPerformed(ActionEvent e)
   {
-
     GSequenceLink link = new GSequenceLink();
     boolean valid = false;
     while (!valid)
@@ -859,11 +889,8 @@ public class Preferences extends GPreferences
     int index = linkUrlTable.getSelectedRow();
     if (index == -1)
     {
-      // no row was selected
-      JOptionPane.showInternalMessageDialog(Desktop.desktop,
-              MessageManager.getString("label.no_link_selected"),
-              MessageManager.getString("label.no_link_selected"),
-              JOptionPane.WARNING_MESSAGE);
+      // button no longer enabled if row is not selected
+      Cache.log.debug("Edit with no row selected in linkUrlTable");
       return;
     }
 
@@ -873,14 +900,12 @@ public class Preferences extends GPreferences
     boolean valid = false;
     while (!valid)
     {
-
       if (JOptionPane.showInternalConfirmDialog(Desktop.desktop, link,
-              MessageManager.getString("label.new_sequence_url_link"),
+              MessageManager.getString("label.edit_sequence_url_link"),
               JOptionPane.OK_CANCEL_OPTION, -1, null) == JOptionPane.OK_OPTION)
       {
         if (link.checkValid())
         {
-
           linkUrlTable.setValueAt(link.getName(), index, 0);
           linkUrlTable.setValueAt(link.getURL(), index, 1);
           valid = true;
@@ -900,11 +925,8 @@ public class Preferences extends GPreferences
     int modelIndex = -1;
     if (index == -1)
     {
-      // no row is selected
-      JOptionPane.showInternalMessageDialog(Desktop.desktop,
-              MessageManager.getString("label.no_link_selected"),
-              MessageManager.getString("label.no_link_selected"),
-              JOptionPane.WARNING_MESSAGE);
+      // button no longer enabled if row is not selected
+      Cache.log.debug("Delete with no row selected in linkUrlTable");
       return;
     }
     else
@@ -1153,9 +1175,10 @@ public class Preferences extends GPreferences
       int modelIndex = linkUrlTable.convertRowIndexToModel(index);
 
       // determine if the new selection is a custom url or not
-      if (!sequenceUrlLinks.isUserEntry((String) linkUrlTable
-.getModel()
-              .getValueAt(modelIndex, 4))) // KM TODO do this better
+      int col = ((UrlLinkTableModel) linkUrlTable.getModel())
+              .getNameColumn();
+      if (!sequenceUrlLinks.isUserEntry((String) linkUrlTable.getModel()
+              .getValueAt(modelIndex, col)))
       {
         // entry is not a user-defined url and so should not be edited
         // disable edit and delete buttons
@@ -1168,8 +1191,10 @@ public class Preferences extends GPreferences
         editLink.setEnabled(true);
       }
 
-      // BUT it's the default url, don't allow deletion
-      if ((boolean) linkUrlTable.getValueAt(index, 3))
+      // BUT if it's the default url, don't allow deletion
+      col = ((UrlLinkTableModel) linkUrlTable.getModel())
+              .getDefaultColumn();
+      if ((boolean) linkUrlTable.getValueAt(index, col))
       {
         deleteLink.setEnabled(false);
       }