JAL-2316 Refactoring of functionality to provide urls to gui
[jalview.git] / src / jalview / gui / Preferences.java
index b43989a..69dc9fe 100755 (executable)
  */
 package jalview.gui;
 
-import static jalview.util.UrlConstants.EMBLEBI_STRING;
-import static jalview.util.UrlConstants.SEQUENCE_ID;
-import static jalview.util.UrlConstants.SRS_STRING;
-
 import jalview.analysis.AnnotationSorter.SequenceAnnotationOrder;
 import jalview.bin.Cache;
 import jalview.gui.Help.HelpId;
@@ -35,6 +31,8 @@ import jalview.jbgui.GSequenceLink;
 import jalview.schemes.ColourSchemeProperty;
 import jalview.util.MessageManager;
 import jalview.util.Platform;
+import jalview.util.UrlProvider;
+import jalview.util.UrlProviderI;
 import jalview.ws.sifts.SiftsSettings;
 
 import java.awt.BorderLayout;
@@ -47,7 +45,6 @@ import java.awt.event.MouseEvent;
 import java.io.File;
 import java.util.ArrayList;
 import java.util.List;
-import java.util.StringTokenizer;
 import java.util.Vector;
 
 import javax.help.HelpSetException;
@@ -101,7 +98,7 @@ public class Preferences extends GPreferences
    * Holds name and link separated with | character. Sequence ID must be
    * $SEQUENCE_ID$ or $SEQUENCE_ID=/.possible | chars ./=$
    */
-  public static Vector<String> sequenceURLLinks;
+  public static UrlProviderI sequenceUrlLinks;
 
   /**
    * Holds name and link separated with | character. Sequence IDS and Sequences
@@ -114,36 +111,9 @@ public class Preferences extends GPreferences
   public static List<String> groupURLLinks;
   static
   {
-    String string = Cache.getDefault("SEQUENCE_LINKS", EMBLEBI_STRING);
-    sequenceURLLinks = new Vector<String>();
-
-    try
-    {
-      StringTokenizer st = new StringTokenizer(string, "|");
-      while (st.hasMoreElements())
-      {
-        String name = st.nextToken();
-        String url = st.nextToken();
-        // check for '|' within a regex
-        int rxstart = url.indexOf("$" + SEQUENCE_ID + "$");
-        while (rxstart == -1 && url.indexOf("/=$") == -1)
-        {
-          url = url + "|" + st.nextToken();
-        }
-        sequenceURLLinks.addElement(name + "|" + url);
-      }
-    } catch (Exception ex)
-    {
-      System.out.println(ex + "\nError parsing sequence links");
-    }
-    {
-      // upgrade old SRS link
-      int srsPos = sequenceURLLinks.indexOf(SRS_STRING);
-      if (srsPos > -1)
-      {
-        sequenceURLLinks.setElementAt(EMBLEBI_STRING, srsPos);
-      }
-    }
+    String string = Cache.getDefault("SEQUENCE_LINKS",
+            UrlProviderI.DEFAULT_STRING);
+    sequenceUrlLinks = new UrlProvider(UrlProviderI.DEFAULT_LABEL, string);
 
     /**
      * TODO: reformulate groupURL encoding so two or more can be stored in the
@@ -340,13 +310,8 @@ public class Preferences extends GPreferences
      */
     nameLinks = new Vector<String>();
     urlLinks = new Vector<String>();
-    for (int i = 0; i < sequenceURLLinks.size(); i++)
-    {
-      String link = sequenceURLLinks.elementAt(i).toString();
-      nameLinks.addElement(link.substring(0, link.indexOf("|")));
-      urlLinks.addElement(link.substring(link.indexOf("|") + 1));
-    }
 
+    resetStoredLinks();
     updateLinkData();
 
     useProxy.setSelected(Cache.getDefault("USE_PROXY", false));
@@ -543,25 +508,16 @@ public class Preferences extends GPreferences
 
     jalview.util.BrowserLauncher.resetBrowser();
 
-    if (nameLinks.size() > 0)
+    // save user-defined and selected links
+    String links = sequenceUrlLinks.writeUrlsAsString();
+    if (links.isEmpty())
     {
-      StringBuffer links = new StringBuffer();
-      sequenceURLLinks = new Vector<String>();
-      for (int i = 0; i < nameLinks.size(); i++)
-      {
-        sequenceURLLinks.addElement(nameLinks.elementAt(i) + "|"
-                + urlLinks.elementAt(i));
-        links.append(sequenceURLLinks.elementAt(i).toString());
-        links.append("|");
-      }
-      // remove last "|"
-      links.setLength(links.length() - 1);
-      Cache.applicationProperties.setProperty("SEQUENCE_LINKS",
-              links.toString());
+      Cache.applicationProperties.remove("SEQUENCE_LINKS");
     }
     else
     {
-      Cache.applicationProperties.remove("SEQUENCE_LINKS");
+      Cache.applicationProperties.setProperty("SEQUENCE_LINKS",
+              links.toString());
     }
 
     Cache.applicationProperties.setProperty("USE_PROXY",
@@ -756,8 +712,14 @@ public class Preferences extends GPreferences
         {
           nameLinks.addElement(link.getName());
           urlLinks.addElement(link.getURL());
-          updateLinkData();
-          valid = true;
+          if (updateLinkData())
+          {
+            valid = true;
+          }
+          else
+          {
+            break;
+          }
         }
       }
       else
@@ -797,8 +759,14 @@ public class Preferences extends GPreferences
         {
           nameLinks.setElementAt(link.getName(), index);
           urlLinks.setElementAt(link.getURL(), index);
-          updateLinkData();
-          valid = true;
+          if (updateLinkData())
+          {
+            valid = true;
+          }
+          else
+          {
+            break;
+          }
         }
       }
 
@@ -826,10 +794,42 @@ public class Preferences extends GPreferences
     updateLinkData();
   }
 
-  void updateLinkData()
+  private boolean updateLinkData()
   {
-    linkNameList.setListData(nameLinks);
-    linkURLList.setListData(urlLinks);
+    try
+    {
+      sequenceUrlLinks.setUrlLinks(nameLinks, urlLinks);
+      linkNameList.setListData(nameLinks);
+      linkURLList.setListData(urlLinks);
+    } catch (IllegalArgumentException e)
+    {
+
+      // put back the old links
+      resetStoredLinks();
+
+      linkNameList.setListData(nameLinks);
+      linkURLList.setListData(urlLinks);
+
+      JOptionPane.showInternalMessageDialog(Desktop.desktop,
+              e.getMessage(), MessageManager.getString("label.link_name"),
+              JOptionPane.WARNING_MESSAGE);
+
+      return false;
+    }
+    return true;
+  }
+
+  private void resetStoredLinks()
+  {
+    Vector<String> nlinks = sequenceUrlLinks.getLinksForDisplay();
+
+    nameLinks.clear();
+    urlLinks.clear();
+    for (String entry : nlinks)
+    {
+      nameLinks.addElement(entry.split("\\|")[0]);
+      urlLinks.addElement(entry.split("\\|")[1]);
+    }
   }
 
   @Override