use OOMwarning to warn user when out of Memory occurs
[jalview.git] / src / jalview / gui / PopupMenu.java
index 546ce65..ce71034 100755 (executable)
@@ -1,17 +1,17 @@
 /*
- * Jalview - A Sequence Alignment Editor and Viewer
- * Copyright (C) 2007 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
- *
+ * Jalview - A Sequence Alignment Editor and Viewer (Version 2.4)
+ * Copyright (C) 2008 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
+ * 
  * This program 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 2
  * of the License, or (at your option) any later version.
- *
+ * 
  * This program 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 this program; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
@@ -30,6 +30,7 @@ import jalview.commands.*;
 import jalview.datamodel.*;
 import jalview.io.*;
 import jalview.schemes.*;
+import jalview.util.UrlLink;
 
 /**
  * DOCUMENT ME!
@@ -365,49 +366,62 @@ public class PopupMenu
       for (int i = 0; i < links.size(); i++)
       {
         String link = links.elementAt(i).toString();
-        final String label = link.substring(0, link.indexOf("|"));
-        
-        
-        if (link.indexOf("$SEQUENCE_ID$") > -1)
+        UrlLink urlLink = null;
+        try {
+          urlLink = new UrlLink(link);
+        } catch (Exception foo) {
+          jalview.bin.Cache.log.error("Exception for URLLink '"+link+"'",foo);
+          continue;
+        };
+        if (!urlLink.isValid())
+        {
+          jalview.bin.Cache.log.error(urlLink.getInvalidMessage());
+          continue;
+        }
+        final String label = urlLink.getLabel();
+        if (urlLink.isDynamic())
         {
-          // Substitute SEQUENCE_ID string and any matching database reference accessions
-          String url_pref = link.substring(link.indexOf("|") + 1,
-                  link.indexOf("$SEQUENCE_ID$"));
-          
-          String url_suff = link.substring(link.indexOf("$SEQUENCE_ID$") + 13); 
 
           // collect matching db-refs
-          DBRefEntry[] dbr = jalview.util.DBRefUtils.selectRefs(seq.getDBRef(), new String[]{label});
+          DBRefEntry[] dbr = jalview.util.DBRefUtils.selectRefs(seq.getDBRef(), new String[]{urlLink.getTarget()});
           // collect id string too
           String id = seq.getName();
-          if (id.indexOf("|") > -1)
-          {
-            id = id.substring(id.lastIndexOf("|") + 1);
-          }
-
           if (dbr!=null)
           {
             for (int r=0;r<dbr.length; r++)
             {
-              if (dbr[r].getAccessionId().equals(id))
+              if (id!=null && dbr[r].getAccessionId().equals(id))
               {
                 // suppress duplicate link creation for the bare sequence ID string with this link
                 id = null;
               }
-              addshowLink(linkMenu, dbr[r].getSource()+"|"+dbr[r].getAccessionId(), 
-                      url_pref+dbr[r].getAccessionId()+url_suff);
+              // create Bare ID link for this RUL
+              String[] urls = urlLink.makeUrls(dbr[r].getAccessionId(), true);
+              if (urls!=null)
+              {
+                for (int u=0; u<urls.length; u+=2)
+                {
+                  addshowLink(linkMenu, label+"|"+urls[u],urls[u+1]); 
+                }
+              }
             }
           }
           if (id!=null)
           {
             // create Bare ID link for this RUL
-            addshowLink(linkMenu, label,  url_pref + id + url_suff);
+            String[] urls = urlLink.makeUrls(id, true);
+            if (urls!=null)
+            {
+              for (int u=0; u<urls.length; u+=2)
+              {
+                addshowLink(linkMenu, label,  urls[u+1]);
+              }
+            }
           }
-        }
-        else
+        } else
         {
           // Add a non-dynamic link
-          addshowLink(linkMenu, label, link.substring(link.lastIndexOf("|") + 1));
+          addshowLink(linkMenu, label, urlLink.getUrl_prefix());
         }
       }
       if (sequence != null)
@@ -430,6 +444,7 @@ public class PopupMenu
   private void addshowLink(JMenu linkMenu, String label, final String url)
   {
     JMenuItem item = new JMenuItem(label);
+    item.setToolTipText("open URL: "+url);
     item.addActionListener(new java.awt.event.ActionListener()
     {
       public void actionPerformed(ActionEvent e)
@@ -1428,13 +1443,21 @@ public class PopupMenu
 
   public void discoverPDB_actionPerformed()
   {
-    SequenceI[] sequences =
-         ap.av.selectionGroup == null ?
-           new Sequence[]{sequence}
-         : ap.av.selectionGroup.getSequencesInOrder(ap.av.alignment);
-
-    new jalview.ws.DBRefFetcher(sequences,
-        ap.alignFrame).fetchDBRefs(false);
+    
+    final SequenceI[] sequences = 
+      ((ap.av.selectionGroup == null) 
+              ?  new Sequence[]{sequence}
+      : ap.av.selectionGroup.getSequencesInOrder(ap.av.alignment));
+    Thread discpdb = new Thread(new Runnable() {
+      public void run()
+      {
+        
+        new jalview.ws.DBRefFetcher(sequences,
+                ap.alignFrame).fetchDBRefs(false);
+      }
+      
+    });
+    discpdb.start();
   }
 
   public void sequenceFeature_actionPerformed()