JAL-2316 Refactoring of functionality to provide urls to gui
[jalview.git] / src / jalview / appletgui / IdPanel.java
index 182f20e..f8ea522 100755 (executable)
  */
 package jalview.appletgui;
 
-import static jalview.util.UrlConstants.EMBLEBI_STRING;
-import static jalview.util.UrlConstants.SRS_STRING;
-
 import jalview.datamodel.Sequence;
 import jalview.datamodel.SequenceFeature;
 import jalview.datamodel.SequenceGroup;
 import jalview.datamodel.SequenceI;
-import jalview.util.UrlLink;
+import jalview.util.UrlProvider;
+import jalview.util.UrlProviderI;
 import jalview.viewmodel.AlignmentViewport;
 
 import java.awt.BorderLayout;
@@ -36,6 +34,7 @@ import java.awt.event.InputEvent;
 import java.awt.event.MouseEvent;
 import java.awt.event.MouseListener;
 import java.awt.event.MouseMotionListener;
+import java.util.HashMap;
 import java.util.List;
 import java.util.Vector;
 
@@ -55,7 +54,7 @@ public class IdPanel extends Panel implements MouseListener,
 
   boolean mouseDragging = false;
 
-  java.util.Vector links = new java.util.Vector();
+  UrlProviderI urlProvider = null;
 
   public IdPanel(AlignViewport av, AlignmentPanel parent)
   {
@@ -69,6 +68,9 @@ public class IdPanel extends Panel implements MouseListener,
 
     String label, url;
     // TODO: add in group link parameter
+
+    // make a list of label,url pairs
+    HashMap<String, String> urlList = new HashMap<String, String>();
     if (av.applet != null)
     {
       for (int i = 1; i < 10; i++)
@@ -76,26 +78,16 @@ public class IdPanel extends Panel implements MouseListener,
         label = av.applet.getParameter("linkLabel_" + i);
         url = av.applet.getParameter("linkURL_" + i);
 
-        if (label != null && url != null)
-        {
-          links.addElement(label + "|" + url);
-        }
-
+        urlList.put(label, url);
       }
-    }
-    {
-      // upgrade old SRS link
-      int srsPos = links.indexOf(SRS_STRING);
-      if (srsPos > -1)
+
+      if (!urlList.isEmpty())
       {
-        links.setElementAt(EMBLEBI_STRING, srsPos);
+        // set default as first entry in list
+        String defaultUrl = av.applet.getParameter("linkLabel_1");
+        urlProvider = new UrlProvider(defaultUrl, urlList);
       }
     }
-    if (links.size() < 1)
-    {
-      links = new java.util.Vector();
-      links.addElement(EMBLEBI_STRING);
-    }
   }
 
   Tooltip tooltip;
@@ -225,54 +217,55 @@ public class IdPanel extends Panel implements MouseListener,
       return;
     }
     String id = sq.getName();
+    String url = urlProvider.getDefaultUrl(id);
+    String target = urlProvider.getDefaultTarget(id);
 
-    String target = null;
-    String url = null;
-    int i = 0;
-    while (url == null && i < links.size())
-    {
-      // DEFAULT LINK IS FIRST IN THE LINK LIST
-      // BUT IF ITS A REGEX AND DOES NOT MATCH THE NEXT ONE WILL BE TRIED
-      url = links.elementAt(i++).toString();
-      jalview.util.UrlLink urlLink = null;
-      try
-      {
-        urlLink = new UrlLink(url);
-        target = urlLink.getTarget();
-      } catch (Exception foo)
-      {
-        System.err.println("Exception for URLLink '" + url + "'");
-        foo.printStackTrace();
-        url = null;
-        continue;
-      }
+    /*    String target = null;
+        String url = null;
+        int i = 0;
+        while (url == null && i < links.size())
+        {
+          // DEFAULT LINK IS FIRST IN THE LINK LIST
+          // BUT IF ITS A REGEX AND DOES NOT MATCH THE NEXT ONE WILL BE TRIED
+          url = links.elementAt(i++).toString();
+          jalview.util.UrlLink urlLink = null;
+          try
+          {
+            urlLink = new UrlLink(url);
+            target = urlLink.getTarget();
+          } catch (Exception foo)
+          {
+            System.err.println("Exception for URLLink '" + url + "'");
+            foo.printStackTrace();
+            url = null;
+            continue;
+          }
 
-      if (urlLink.usesDBAccession())
-      {
-        // this URL requires an accession id, not the name of a sequence
-        url = null;
-        continue;
-      }
+          if (urlLink.usesDBAccession())
+          {
+            // this URL requires an accession id, not the name of a sequence
+            url = null;
+            continue;
+          }
 
-      if (!urlLink.isValid())
-      {
-        System.err.println(urlLink.getInvalidMessage());
-        url = null;
-        continue;
-      }
+          if (!urlLink.isValid())
+          {
+            System.err.println(urlLink.getInvalidMessage());
+            url = null;
+            continue;
+          }
 
-      String urls[] = urlLink.makeUrls(id, true);
-      if (urls == null || urls[0] == null || urls[0].length() < 1)
-      {
-        url = null;
-        continue;
-      }
-      // just take first URL made from regex
-      url = urls[1];
-    }
+          String urls[] = urlLink.makeUrls(id, true);
+          if (urls == null || urls[0] == null || urls[0].length() < 1)
+          {
+            url = null;
+            continue;
+          }
+          // just take first URL made from regex
+          url = urls[1];
+        }*/
     try
     {
-
       alignPanel.alignFrame.showURL(url, target);
     } catch (Exception ex)
     {
@@ -331,11 +324,8 @@ public class IdPanel extends Panel implements MouseListener,
 
       // build a new links menu based on the current links + any non-positional
       // features
-      Vector nlinks = new Vector();
-      for (int l = 0, lSize = links.size(); l < lSize; l++)
-      {
-        nlinks.addElement(links.elementAt(l));
-      }
+      Vector<String> nlinks = urlProvider.getLinksForDisplay();
+
       SequenceFeature sf[] = sq == null ? null : sq.getSequenceFeatures();
       for (int sl = 0; sf != null && sl < sf.length; sl++)
       {