Url links changes - incomplete checkin as battery low
authorkiramt <k.mourao@dundee.ac.uk>
Mon, 14 Nov 2016 07:39:44 +0000 (07:39 +0000)
committerkiramt <k.mourao@dundee.ac.uk>
Mon, 14 Nov 2016 07:39:44 +0000 (07:39 +0000)
src/jalview/gui/Preferences.java
src/jalview/util/CustomUrlProvider.java [new file with mode: 0644]
src/jalview/util/IdentifiersUrlProvider.java [new file with mode: 0644]
src/jalview/util/UrlProvider.java [new file with mode: 0644]
src/jalview/util/UrlProviderI.java [new file with mode: 0644]

index 3bffd3a..718e14f 100755 (executable)
  */
 package jalview.gui;
 
-import static jalview.util.UrlConstants.DB_ACCESSION;
 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;
@@ -36,6 +33,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;
@@ -48,7 +47,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;
@@ -102,7 +100,8 @@ 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 Vector<String> sequenceURLLinks;
+  public static UrlProviderI sequenceUrlLinks;
 
   /**
    * Holds name and link separated with | character. Sequence IDS and Sequences
@@ -116,40 +115,42 @@ public class Preferences extends GPreferences
   static
   {
     String string = Cache.getDefault("SEQUENCE_LINKS", EMBLEBI_STRING);
-    sequenceURLLinks = new Vector<String>();
+    sequenceUrlLinks = new UrlProvider(EMBLEBI_STRING.split("\\|")[0],
+            string); // TODO sort out the default 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("$" + DB_ACCESSION + "$");
-        if (rxstart == -1)
+    /*    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)
         {
-          rxstart = url.indexOf("$" + SEQUENCE_ID + "$");
+          System.out.println(ex + "\nError parsing sequence links");
         }
-        while (rxstart == -1 && url.indexOf("/=$") == -1)
         {
-          url = url + "|" + st.nextToken();
+          // upgrade old SRS link
+          int srsPos = sequenceURLLinks.indexOf(SRS_STRING);
+          if (srsPos > -1)
+          {
+            sequenceURLLinks.setElementAt(EMBLEBI_STRING, srsPos);
+          }
         }
-        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
@@ -345,13 +346,14 @@ 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));
-    }
-
+    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));
+        }
+    */
     updateLinkData();
 
     useProxy.setSelected(Cache.getDefault("USE_PROXY", false));
@@ -548,28 +550,39 @@ public class Preferences extends GPreferences
 
     jalview.util.BrowserLauncher.resetBrowser();
 
-    if (nameLinks.size() > 0)
+    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");
-      sequenceURLLinks.clear();
+      Cache.applicationProperties.setProperty("SEQUENCE_LINKS",
+              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()));
 
diff --git a/src/jalview/util/CustomUrlProvider.java b/src/jalview/util/CustomUrlProvider.java
new file mode 100644 (file)
index 0000000..f679e36
--- /dev/null
@@ -0,0 +1,157 @@
+/*
+ * 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.util;
+
+import static jalview.util.UrlConstants.DB_ACCESSION;
+import static jalview.util.UrlConstants.EMBLEBI_STRING;
+import static jalview.util.UrlConstants.SEQUENCE_ID;
+import static jalview.util.UrlConstants.SRS_STRING;
+
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.StringTokenizer;
+import java.util.Vector;
+
+/**
+ * 
+ * Implements the UrlProviderI interface for a UrlProvider object which serves
+ * custom URLs defined by the user
+ * 
+ * @author $author$
+ * @version $Revision$
+ */
+public class CustomUrlProvider implements UrlProviderI
+{
+
+  private HashMap<String, UrlLink> urls;
+
+  public CustomUrlProvider(String cachedUrlList)
+  {
+    try
+    {
+      StringTokenizer st = new StringTokenizer(cachedUrlList, "|");
+      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();
+        }
+        urls.put(name, new UrlLink(url));
+        // sequenceURLLinks.addElement(name + "|" + url);
+      }
+    } catch (Exception ex)
+    {
+      System.out.println(ex + "\nError parsing sequence links");
+    }
+
+    // upgrade old SRS link
+    String srs_key = SRS_STRING.split("\\|")[0];
+    if (urls.containsKey(srs_key))
+    {
+      urls.remove(srs_key);
+      urls.put(EMBLEBI_STRING.split("//|")[0],
+              new UrlLink(EMBLEBI_STRING.split("//|")[1]));
+    }
+  }
+
+  @Override
+  public HashMap<String, UrlLink> getUrlLinks()
+  {
+    // TODO Auto-generated method stub
+    return null;
+  }
+
+  @Override
+  public void getLinksForDisplay(Vector<String> nameLinks,
+          Vector<String> urlLinks)
+  {
+    Iterator<Map.Entry<String, UrlLink>> it = urls.entrySet().iterator();
+    while (it.hasNext())
+    {
+      Map.Entry<String, UrlLink> pair = it.next();
+      nameLinks.addElement(pair.getKey());
+      urlLinks.addElement(pair.getValue().toString()); // TODO check this is
+                                                       // right
+    }
+  }
+
+  @Override
+  public String getDefaultUrl()
+  {
+    // TODO Auto-generated method stub
+    return null;
+  }
+
+  @Override
+  public void setDefaultUrl(String id)
+  {
+    // TODO Auto-generated method stub
+
+  }
+
+  @Override
+  public String writeUrlsAsString()
+  {
+    StringBuffer links = new StringBuffer();
+    if (urls.size() > 0)
+    {
+      for (UrlLink link : urls.values())
+      {
+        links.append(link.toString());
+        links.append("|");
+      }
+
+      // remove last "|"
+      links.setLength(links.length() - 1);
+    }
+    else
+    {
+      urls.clear();
+    }
+    return links.toString();
+  }
+
+  @Override
+  public void setUrlLinks(String cachedUrl)
+  {
+    // TODO Auto-generated method stub, maybe don't need this? watch out for
+    // superclass interactions
+
+  }
+
+  @Override
+  public void setUrlLinks(HashMap<String, UrlLink> links)
+  {
+    // TODO Auto-generated method stub
+
+  }
+
+}
diff --git a/src/jalview/util/IdentifiersUrlProvider.java b/src/jalview/util/IdentifiersUrlProvider.java
new file mode 100644 (file)
index 0000000..e13eab5
--- /dev/null
@@ -0,0 +1,93 @@
+/*
+ * 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.util;
+
+import java.util.HashMap;
+import java.util.Vector;
+
+/**
+ * 
+ * Implements the UrlProviderI interface for a UrlProvider object which serves
+ * URLs from identifiers.org
+ * 
+ * @author $author$
+ * @version $Revision$
+ */
+public class IdentifiersUrlProvider implements UrlProviderI
+{
+
+  public IdentifiersUrlProvider(String cachedUrlList)
+  {
+
+  }
+
+  @Override
+  public HashMap<String, UrlLink> getUrlLinks()
+  {
+    // TODO Auto-generated method stub
+    return null;
+  }
+
+  @Override
+  public String getDefaultUrl()
+  {
+    // TODO Auto-generated method stub
+    return null;
+  }
+
+  @Override
+  public void setDefaultUrl(String id)
+  {
+    // TODO Auto-generated method stub
+
+  }
+
+  @Override
+  public String writeUrlsAsString()
+  {
+    // TODO Auto-generated method stub
+    return null;
+  }
+
+  @Override
+  public void setUrlLinks(String cachedUrl)
+  {
+    // TODO Auto-generated method stub
+
+  }
+
+  @Override
+  public void setUrlLinks(HashMap<String, UrlLink> links)
+  {
+    // TODO Auto-generated method stub
+
+  }
+
+  @Override
+  public void getLinksForDisplay(Vector<String> nameLinks,
+          Vector<String> urlLinks)
+  {
+    // TODO Auto-generated method stub
+
+  }
+
+}
diff --git a/src/jalview/util/UrlProvider.java b/src/jalview/util/UrlProvider.java
new file mode 100644 (file)
index 0000000..3cd033b
--- /dev/null
@@ -0,0 +1,159 @@
+/*
+ * 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.util;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Vector;
+
+/**
+ * 
+ * Implements the UrlProviderI interface for a composite UrlProvider object
+ * 
+ * @author $author$
+ * @version $Revision$
+ */
+public class UrlProvider implements UrlProviderI
+{
+  // List of actual URL link providers
+  private List<UrlProviderI> providers;
+
+  // Record of default URL
+  private String defaultUrl;
+  
+  /**
+   * Construct URL provider from string of cached URLs, and set default URL
+   * 
+   * @param defaultUrl
+   *          id of the current default URL
+   * @param cachedUrlList
+   *          string listing current active URLs, expected to be custom URLs
+   *          separated by |, or ids of URLs
+   */
+  public UrlProvider(String defaultUrl, String cachedUrlList)
+  {
+    // create all the UrlProviders we need
+    providers = new ArrayList<UrlProviderI>();
+    
+    UrlProviderI idProvider = new IdentifiersUrlProvider(cachedUrlList);
+    UrlProviderI customProvider = new CustomUrlProvider(cachedUrlList);
+    providers.add(idProvider);
+    providers.add(customProvider);
+
+    // check that the defaultUrl still exists
+  }
+  
+  /**
+   * Get all the URL links
+   * @return all URL links
+   */
+  @Override
+  public HashMap<String, UrlLink> getUrlLinks()
+  {
+    HashMap<String, UrlLink> urls = new HashMap<String, UrlLink>();
+    for (UrlProviderI p : providers)
+    {
+      urls.putAll(p.getUrlLinks());
+    }
+    return urls;
+  }
+  
+  /**
+   * Get the default URL
+   * @return id of the default URL
+   */
+  @Override
+  public String getDefaultUrl()
+  {
+    return defaultUrl;
+  }
+  
+  /**
+   * Set the default URL
+   * @param id the id of the URL to set as default
+   */
+  @Override
+  public void setDefaultUrl(String id)
+  {
+    defaultUrl = id;
+  }
+  
+  /**
+   * Write out all URLs as a string suitable for serialising
+   * @return string representation of available URLs
+   */
+  @Override
+  public String writeUrlsAsString()
+  {
+    String result = "";
+    for (UrlProviderI p : providers)
+    {
+      result += p.writeUrlsAsString();
+    }
+    return result;
+  }
+  
+  /**
+   * Set URL links from string
+   * 
+   * @param cachedUrl
+   *          string representation of URL links, i.e. | separated
+   */
+  @Override
+  public void setUrlLinks(String cachedUrl)
+  {
+    for (UrlProviderI p : providers)
+    {
+      p.setUrlLinks(cachedUrl);
+    }
+  }
+  
+  /**
+   * Set URL links from collection
+   * 
+   * @param links
+   *          hashmap containing string indexed UrlLinks
+   */
+  @Override
+  public void setUrlLinks(HashMap<String, UrlLink> links)
+  {
+    for (UrlProviderI p : providers)
+    {
+      p.setUrlLinks(links);
+    }
+  }
+
+  @Override
+  public void getLinksForDisplay(Vector<String> nameLinks,
+          Vector<String> urlLinks)
+  {
+    Vector<String> names = new Vector<String>();
+    Vector<String> links = new Vector<String>();
+    for (UrlProviderI p : providers)
+    {
+      p.getLinksForDisplay(names, links);
+      nameLinks.addAll(names);
+      urlLinks.addAll(links);
+    }
+  }
+  
+}
diff --git a/src/jalview/util/UrlProviderI.java b/src/jalview/util/UrlProviderI.java
new file mode 100644 (file)
index 0000000..2118f7e
--- /dev/null
@@ -0,0 +1,87 @@
+/*
+ * 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.util;
+
+import java.util.HashMap;
+import java.util.Vector;
+
+/**
+ * Methods for providing consistent access to up-to-date URLs
+ * 
+ * @author $author$
+ * @version $Revision$
+ */
+public interface UrlProviderI
+{
+  /**
+   * Get all the URL links
+   * 
+   * @return all URL links
+   */
+  public HashMap<String, UrlLink> getUrlLinks();
+
+  /**
+   * Get names and urls in as strings for display
+   * 
+   * @param nameLinks
+   * @param urlLinks
+   */
+  public void getLinksForDisplay(Vector<String> nameLinks,
+          Vector<String> urlLinks);
+
+  /**
+   * Get the default URL
+   * 
+   * @return id of the default URL
+   */
+  public String getDefaultUrl();
+
+  /**
+   * Set the default URL
+   * 
+   * @param id
+   *          the id of the URL to set as default
+   */
+  public void setDefaultUrl(String id);
+
+  /**
+   * Write out all URLs as a string suitable for serialising
+   * 
+   * @return string representation of available URLs
+   */
+  public String writeUrlsAsString();
+
+  /**
+   * Set URL links from string
+   * 
+   * @param cachedUrl
+   *          string representation of URL links, i.e. | separated
+   */
+  public void setUrlLinks(String cachedUrl);
+
+  /**
+   * Set URL links from collection
+   * 
+   * @param links
+   *          hashmap containing string indexed UrlLinks
+   */
+  public void setUrlLinks(HashMap<String, UrlLink> links);
+}