JAL-2316 Refactoring of functionality to provide urls to gui
[jalview.git] / src / jalview / gui / Preferences.java
index 718e14f..69dc9fe 100755 (executable)
@@ -20,8 +20,6 @@
  */
 package jalview.gui;
 
-import static jalview.util.UrlConstants.EMBLEBI_STRING;
-
 import jalview.analysis.AnnotationSorter.SequenceAnnotationOrder;
 import jalview.bin.Cache;
 import jalview.gui.Help.HelpId;
@@ -100,7 +98,6 @@ 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;
 
   /**
@@ -114,43 +111,10 @@ public class Preferences extends GPreferences
   public static List<String> groupURLLinks;
   static
   {
-    String string = Cache.getDefault("SEQUENCE_LINKS", EMBLEBI_STRING);
-    sequenceUrlLinks = new UrlProvider(EMBLEBI_STRING.split("\\|")[0],
-            string); // TODO sort out the default string
-    // sequenceURLLinks = new Vector<String>();
+    String string = Cache.getDefault("SEQUENCE_LINKS",
+            UrlProviderI.DEFAULT_STRING);
+    sequenceUrlLinks = new UrlProvider(UrlProviderI.DEFAULT_LABEL, 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("$" + DB_ACCESSION + "$");
-            if (rxstart == -1)
-            {
-              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);
-          }
-        }
-    */
     /**
      * TODO: reformulate groupURL encoding so two or more can be stored in the
      * .properties file as '|' separated strings
@@ -346,14 +310,8 @@ public class Preferences extends GPreferences
      */
     nameLinks = new Vector<String>();
     urlLinks = new Vector<String>();
-    sequenceUrlLinks.getLinksForDisplay(nameLinks, urlLinks);
-    /*    for (HashMap.Entry<String, UrlLink> entry: sequenceUrlLinks.getUrlLinks())
-        {
-          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));
@@ -550,6 +508,7 @@ public class Preferences extends GPreferences
 
     jalview.util.BrowserLauncher.resetBrowser();
 
+    // save user-defined and selected links
     String links = sequenceUrlLinks.writeUrlsAsString();
     if (links.isEmpty())
     {
@@ -561,28 +520,6 @@ public class Preferences extends GPreferences
               links.toString());
     }
 
-    /*    if (nameLinks.size() > 0)
-        {
-          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());
-        }
-        else
-        {
-          Cache.applicationProperties.remove("SEQUENCE_LINKS");
-          sequenceURLLinks.clear();
-        }*/
-
     Cache.applicationProperties.setProperty("USE_PROXY",
             Boolean.toString(useProxy.isSelected()));
 
@@ -775,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
@@ -816,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;
+          }
         }
       }
 
@@ -845,10 +794,42 @@ public class Preferences extends GPreferences
     updateLinkData();
   }
 
-  void updateLinkData()
+  private boolean updateLinkData()
+  {
+    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()
   {
-    linkNameList.setListData(nameLinks);
-    linkURLList.setListData(urlLinks);
+    Vector<String> nlinks = sequenceUrlLinks.getLinksForDisplay();
+
+    nameLinks.clear();
+    urlLinks.clear();
+    for (String entry : nlinks)
+    {
+      nameLinks.addElement(entry.split("\\|")[0]);
+      urlLinks.addElement(entry.split("\\|")[1]);
+    }
   }
 
   @Override