wrap tooltips and revise layout (JAL-644)
[jalview.git] / src / jalview / ws / EnfinEnvision2OneWay.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.5)
3  * Copyright (C) 2010 J Procter, AM Waterhouse, G Barton, M Clamp, S Searle
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
10  * 
11  * Jalview is distributed in the hope that it will be useful, but 
12  * WITHOUT ANY WARRANTY; without even the implied warranty 
13  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
14  * PURPOSE.  See the GNU General Public License for more details.
15  * 
16  * You should have received a copy of the GNU General Public License along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 package jalview.ws;
19
20 import jalview.bin.Cache;
21 import jalview.datamodel.DBRefEntry;
22 import jalview.datamodel.SequenceGroup;
23 import jalview.datamodel.SequenceI;
24 import jalview.gui.AlignFrame;
25 import jalview.gui.Desktop;
26 import jalview.gui.JvSwingUtils;
27 import jalview.util.GroupUrlLink;
28
29 import java.awt.Component;
30 import java.awt.Cursor;
31 import java.awt.event.ActionEvent;
32 import java.awt.event.ActionListener;
33 import java.awt.event.ItemEvent;
34 import java.awt.event.ItemListener;
35 import java.io.BufferedReader;
36 import java.io.InputStreamReader;
37 import java.net.URL;
38 import java.util.Hashtable;
39 import java.util.Map;
40 import java.util.Vector;
41
42 import javax.swing.JMenu;
43 import javax.swing.JMenuItem;
44 import javax.swing.JOptionPane;
45 import javax.swing.event.MenuEvent;
46 import javax.swing.event.MenuListener;
47 import javax.xml.parsers.SAXParser;
48 import javax.xml.parsers.SAXParserFactory;
49
50 import org.xml.sax.Attributes;
51 import org.xml.sax.SAXException;
52 import org.xml.sax.helpers.DefaultHandler;
53
54 /**
55  * Lightweight runnable to discover dynamic 'one way' group URL services
56  * 
57  * @author JimP
58  * 
59  */
60 public class EnfinEnvision2OneWay extends DefaultHandler implements
61         Runnable, WSMenuEntryProviderI
62 {
63   private static EnfinEnvision2OneWay groupURLLinksGatherer = null;
64
65   public static EnfinEnvision2OneWay getInstance()
66   {
67     if (groupURLLinksGatherer == null)
68     {
69       groupURLLinksGatherer = new EnfinEnvision2OneWay();
70     }
71     return groupURLLinksGatherer;
72   }
73
74   private void waitForCompletion()
75   {
76     if (groupURLLinksGatherer.isRunning())
77     {
78       // wait around and show a visual delay indicator
79       Cursor oldCursor = Desktop.instance.getCursor();
80       Desktop.instance.setCursor(new Cursor(Cursor.WAIT_CURSOR));
81       while (groupURLLinksGatherer.isRunning())
82       {
83         try
84         {
85           Thread.sleep(100);
86         } catch (InterruptedException e)
87         {
88         }
89         ;
90       }
91       Desktop.instance.setCursor(oldCursor);
92     }
93   }
94
95   public Vector getEnvisionServiceGroupURLS()
96   {
97     waitForCompletion();
98     return groupURLLinks;
99   }
100
101   /**
102    * indicate if
103    */
104   private static String BACKGROUND = "BACKGROUNDPARAM";
105
106   /**
107    * contains null strings or one of the above constants - indicate if this URL
108    * is a special case.
109    */
110   private Vector additionalPar = new Vector();
111
112   /**
113    * the enfin service URL
114    */
115   private String enfinService = null;
116
117   private String description = null;
118
119   private String wfname;
120
121   /*
122    * (non-Javadoc)
123    * 
124    * @see org.xml.sax.helpers.DefaultHandler#endElement(java.lang.String,
125    * java.lang.String, java.lang.String)
126    */
127   public void endElement(String uri, String localName, String qName)
128           throws SAXException
129   {
130
131     // System.err.println("End element: : '"+uri+" "+localName+" "+qName);
132     if (qName.equalsIgnoreCase("workflow") && description != null
133             && description.length() > 0)
134     {
135       // groupURLLinks.addElement("UNIPROT|EnVision2|http://www.ebi.ac.uk/enfin-srv/envision2/pages/linkin.jsf?tool=Jalview&workflow=Default&datasetName=JalviewIDs$DATASETID$&input=$SEQUENCEIDS$&inputType=0|,");
136       // groupURLLinks.addElement("Seqs|EnVision2|http://www.ebi.ac.uk/enfin-srv/envision2/pages/linkin.jsf?tool=Jalview&workflow=Default&datasetName=JalviewSeqs$DATASETID$&input=$SEQUENCES=/([A-Za-z]+)+/=$&inputType=1|,");
137       System.err.println("Adding entry for " + wfname + " " + description);
138       if (wfname.toLowerCase().indexOf("funcnet") == -1)
139       {
140         groupURLdescr.addElement(description);
141         groupURLdescr.addElement(description);
142         groupURLLinks
143                 .addElement(wfname
144                         + "|"
145                         + "http://www.ebi.ac.uk/enfin-srv/envision2/pages/linkin.jsf?tool=Jalview&workflow="
146                         + wfname
147                         + "&datasetName=JalviewSeqs$DATASETID$&input=$SEQUENCEIDS$&inputType=0|,"); // #"+description+"#");
148         groupURLLinks
149                 .addElement(wfname
150                         + "|"
151                         + "http://www.ebi.ac.uk/enfin-srv/envision2/pages/linkin.jsf?tool=Jalview&workflow="
152                         + wfname
153                         + "&datasetName=JalviewSeqs$DATASETID$&input=$SEQUENCES=/([A-Za-z]+)+/=$&inputType=1|,"); // #"+description+"#");
154       }
155     }
156   }
157
158   /*
159    * (non-Javadoc)
160    * 
161    * @see org.xml.sax.helpers.DefaultHandler#characters(char[], int, int)
162    */
163   public void characters(char[] ch, int start, int length)
164           throws SAXException
165   {
166     if (description != null)
167     {
168       for (int i = start; i < start + length; i++)
169       {
170         description += ch[i];
171       }
172     }
173   }
174
175   /*
176    * (non-Javadoc)
177    * 
178    * @see org.xml.sax.helpers.DefaultHandler#startElement(java.lang.String,
179    * java.lang.String, java.lang.String, org.xml.sax.Attributes)
180    */
181   public void startElement(String uri, String localName, String qName,
182           Attributes attributes) throws SAXException
183   {
184     if (qName.equalsIgnoreCase("workflow"))
185     {
186       description = null;
187       wfname = attributes.getValue("name");
188     }
189     if (qName.equalsIgnoreCase("description"))
190     {
191       description = "";
192     }
193
194     // System.err.println("Start element: : '"+uri+" "+localName+" "+qName+" attributes"+attributes);
195     // super.startElement(uri,localName,qname,attributes);
196   }
197
198   private boolean started = false;
199
200   private boolean running = false;
201
202   private Vector groupURLLinks = null;
203
204   private Vector groupURLdescr = null;
205
206   private static String[] allowedDb = new String[]
207   { "UNIPROT", "EMBL", "PDB" };
208
209   public EnfinEnvision2OneWay()
210   {
211     groupURLLinks = new Vector();
212     groupURLdescr = new Vector();
213
214     enfinService = Cache.getDefault("ENVISION2_WORKFLOWSERVICE",
215             "http://www.ebi.ac.uk/enfin-srv/envision2/pages/workflows.xml");
216     new Thread(this).start();
217   }
218
219   public void run()
220   {
221     started = true;
222     running = true;
223     try
224     {
225       SAXParserFactory spf = SAXParserFactory.newInstance();
226       SAXParser sp = spf.newSAXParser();
227       sp.parse(new URL(enfinService).openStream(), this);
228     } catch (Exception e)
229     {
230       Cache.log.warn("Exception when discovering One Way services: ", e);
231     } catch (Error e)
232     {
233       Cache.log.warn("Error when discovering One Way services: ", e);
234     }
235     running = false;
236     Cache.log.debug("Finished running.");
237   }
238
239   /**
240    * have we finished running yet ?
241    * 
242    * @return false if we have been run.
243    */
244   public boolean isRunning()
245   {
246
247     // TODO Auto-generated method stub
248     return !started || running;
249   }
250
251   public static void main(String[] args)
252   {
253     Cache.initLogger();
254     EnfinEnvision2OneWay ow = new EnfinEnvision2OneWay();
255     while (ow.isRunning())
256     {
257       try
258       {
259         Thread.sleep(50);
260       } catch (Exception e)
261       {
262       }
263       ;
264
265     }
266     for (int i = 0; i < ow.groupURLLinks.size(); i++)
267     {
268       System.err.println("Description" + ow.groupURLdescr.elementAt(i)
269               + "Service URL: " + ow.groupURLLinks.elementAt(i));
270     }
271   }
272
273   // / Copied from jalview.gui.PopupMenu
274   /**
275    * add a late bound URL service item to the given menu
276    * 
277    * @param linkMenu
278    * @param label
279    *          - menu label string
280    * @param urlgenerator
281    *          GroupURLLink used to generate URL
282    * @param urlstub
283    *          Object array returned from the makeUrlStubs function.
284    */
285   private void addshowLink(JMenu linkMenu, String label, String descr,
286           String dbname, final GroupUrlLink urlgenerator, final Object[] urlstub)
287   {
288     Component[] jmi = linkMenu.getMenuComponents();
289     for (int i = 0; i < jmi.length; i++)
290     {
291       if (jmi[i] instanceof JMenuItem
292               && ((JMenuItem) jmi[i]).getText().equalsIgnoreCase(label))
293       {
294         // don't add this - its a repeat of an existing URL.
295         return;
296       }
297     }
298     boolean seqsorids = (urlgenerator.getGroupURLType() & urlgenerator.SEQUENCEIDS) == 0;
299     int i = urlgenerator.getNumberInvolved(urlstub);
300     JMenuItem item = new JMenuItem(label);
301     // 
302     if (dbname==null || dbname.trim().length()==0)
303     {
304       dbname = "";
305     }
306     item.setToolTipText("<html>"
307             + JvSwingUtils.wrapTooltip("Submit " + i + " " +
308                     dbname +" "
309                     + (seqsorids ? "sequence" : "sequence id") + (i > 1 ? "s" : "")
310                     
311             + " to<br/>" + descr) + "</html>");
312     item.addActionListener(new java.awt.event.ActionListener()
313     {
314       public void actionPerformed(ActionEvent e)
315       {
316         new Thread(new Runnable()
317         {
318
319           public void run()
320           {
321             showLink(urlgenerator.constructFrom(urlstub));
322           }
323
324         }).start();
325       }
326     });
327
328     linkMenu.add(item);
329   }
330
331   /**
332    * open the given link in a new browser window
333    * 
334    * @param url
335    */
336   public void showLink(String url)
337   {
338     try
339     {
340       jalview.util.BrowserLauncher.openURL(url);
341     } catch (Exception ex)
342     {
343       JOptionPane
344               .showInternalMessageDialog(
345                       Desktop.desktop,
346                       "Unixers: Couldn't find default web browser."
347                               + "\nAdd the full path to your browser in Preferences.",
348                       "Web browser not found", JOptionPane.WARNING_MESSAGE);
349
350       ex.printStackTrace();
351     }
352   }
353
354   /**
355    * called by a web service menu instance when it is opened.
356    * 
357    * @param enfinServiceMenu
358    * @param alignFrame
359    */
360   private void buildGroupLinkMenu(JMenu enfinServiceMenu,
361           AlignFrame alignFrame)
362   {
363     SequenceI[] seqs = alignFrame.getViewport().getSelectionAsNewSequence();
364     SequenceGroup sg = alignFrame.getViewport().getSelectionGroup();
365     if (sg == null)
366     {
367       // consider visible regions here/
368     }
369     enfinServiceMenu.removeAll();
370     JMenu entries = buildGroupURLMenu(seqs, sg);
371     if (entries != null)
372     {
373       for (int i = 0, iSize = entries.getMenuComponentCount(); i < iSize; i++)
374       {
375         // transfer - menu component is removed from entries automatically
376         enfinServiceMenu.add(entries.getMenuComponent(0));
377       }
378       // entries.removeAll();
379       enfinServiceMenu.setEnabled(true);
380     }
381     else
382     {
383       enfinServiceMenu.setEnabled(false);
384     }
385   }
386
387   /**
388    * construct a dynamic enfin services menu given a sequence selection
389    * 
390    * @param seqs
391    * @param sg
392    * @param groupLinks
393    * @return
394    */
395   private JMenu buildGroupURLMenu(SequenceI[] seqs, SequenceGroup sg)
396   {
397
398     // TODO: usability: thread off the generation of group url content so root
399     // menu appears asap
400     // sequence only URLs
401     // ID/regex match URLs
402     JMenu groupLinksMenu = new JMenu("Group Link");
403     String[][] idandseqs = GroupUrlLink.formStrings(seqs);
404     Hashtable commonDbrefs = new Hashtable();
405     for (int sq = 0; sq < seqs.length; sq++)
406     {
407
408       int start, end;
409       if (sg != null)
410       {
411         start = seqs[sq].findPosition(sg.getStartRes());
412         end = seqs[sq].findPosition(sg.getEndRes());
413       }
414       else
415       {
416         // get total width of alignment.
417         start = seqs[sq].getStart();
418         end = seqs[sq].findPosition(seqs[sq].getLength());
419       }
420       // we skip sequences which do not have any non-gaps in the region of
421       // interest
422       if (start > end)
423       {
424         continue;
425       }
426       // just collect ids from dataset sequence
427       // TODO: check if IDs collected from selecton group intersects with the
428       // current selection, too
429       SequenceI sqi = seqs[sq];
430       while (sqi.getDatasetSequence() != null)
431       {
432         sqi = sqi.getDatasetSequence();
433       }
434       DBRefEntry[] dbr = sqi.getDBRef();
435       if (dbr != null && dbr.length > 0)
436       {
437         for (int d = 0; d < dbr.length; d++)
438         {
439           String src = dbr[d].getSource(); // jalview.util.DBRefUtils.getCanonicalName(dbr[d].getSource()).toUpperCase();
440           Object[] sarray = (Object[]) commonDbrefs.get(src);
441           if (sarray == null)
442           {
443             sarray = new Object[2];
444             sarray[0] = new int[]
445             { 0 };
446             sarray[1] = new String[seqs.length];
447
448             commonDbrefs.put(src, sarray);
449           }
450
451           if (((String[]) sarray[1])[sq] == null)
452           {
453             if (!dbr[d].hasMap()
454                     || (dbr[d].getMap().locateMappedRange(start, end) != null))
455             {
456               ((String[]) sarray[1])[sq] = dbr[d].getAccessionId();
457               ((int[]) sarray[0])[0]++;
458             }
459           }
460         }
461       }
462     }
463     // now create group links for all distinct ID/sequence sets.
464     Hashtable<String, JMenu[]> gurlMenus = new Hashtable<String, JMenu[]>();
465     for (int i = 0; i < groupURLLinks.size(); i++)
466     {
467       String link = groupURLLinks.elementAt(i).toString();
468       String descr = groupURLdescr.elementAt(i).toString();
469
470       // boolean specialCase =
471       // additionalPar.elementAt(i).toString().equals(BACKGROUND);
472       GroupUrlLink urlLink = null;
473       try
474       {
475         urlLink = new GroupUrlLink(link);
476       } catch (Exception foo)
477       {
478         jalview.bin.Cache.log.error("Exception for GroupURLLink '" + link
479                 + "'", foo);
480         continue;
481       }
482       ;
483       if (!urlLink.isValid())
484       {
485         jalview.bin.Cache.log.error(urlLink.getInvalidMessage());
486         continue;
487       }
488       final String label = urlLink.getLabel();
489       // create/recover the sub menus that might be populated for this link.
490       JMenu[] wflinkMenus = gurlMenus.get(label);
491       if (wflinkMenus == null)
492       {
493         // three types of url that might be
494         // created.
495         wflinkMenus = new JMenu[]
496         { null, new JMenu("IDS"), new JMenu("Sequences"),
497             new JMenu("IDS and Sequences") };
498         gurlMenus.put(label, wflinkMenus);
499       }
500
501       boolean usingNames = false;
502       // Now see which parts of the group apply for this URL
503       String ltarget;
504       String[] seqstr, ids; // input to makeUrl
505       for (int t = 0; t < allowedDb.length; t++)
506       {
507         ltarget = allowedDb[t]; // jalview.util.DBRefUtils.getCanonicalName(urlLink.getTarget());
508         Object[] idset = (Object[]) commonDbrefs.get(ltarget.toUpperCase());
509         if (idset != null)
510         {
511           int numinput = ((int[]) idset[0])[0];
512           String[] allids = ((String[]) idset[1]);
513           seqstr = new String[numinput];
514           ids = new String[numinput];
515           for (int sq = 0, idcount = 0; sq < seqs.length; sq++)
516           {
517             if (allids[sq] != null)
518             {
519               ids[idcount] = allids[sq];
520               seqstr[idcount++] = idandseqs[1][sq];
521             }
522           }
523           createAndAddLinks(wflinkMenus, false, urlLink, ltarget, null,
524                   descr, ids, seqstr);
525         }
526       }
527       // also do names only.
528       seqstr = idandseqs[1];
529       ids = idandseqs[0];
530       createAndAddLinks(wflinkMenus, true, urlLink, "Any", null, descr,
531               ids, seqstr);
532     }
533     boolean anyadded = false; // indicates if there are any group links to give
534     // to user
535     for (Map.Entry<String, JMenu[]> menues : gurlMenus.entrySet())
536     {
537       JMenu grouplinkset = new JMenu(menues.getKey());
538       JMenu[] wflinkMenus = menues.getValue();
539       for (int m = 0; m < wflinkMenus.length; m++)
540       {
541         if (wflinkMenus[m] != null
542                 && wflinkMenus[m].getMenuComponentCount() > 0)
543         {
544           anyadded = true;
545           grouplinkset.add(wflinkMenus[m]);
546         }
547       }
548       groupLinksMenu.add(grouplinkset);
549     }
550     if (anyadded)
551     {
552       return groupLinksMenu;
553     }
554     return null;
555   }
556
557   private boolean createAndAddLinks(JMenu[] linkMenus, boolean usingNames,
558           GroupUrlLink urlLink, String label, String ltarget, String descr,
559           String[] ids, String[] seqstr)
560   {
561     Object[] urlset = urlLink.makeUrlStubs(ids, seqstr, "FromJalview"
562             + System.currentTimeMillis(), false);
563     if (urlset != null)
564     {
565       int type = urlLink.getGroupURLType() & 3;
566       // System.out.println(urlLink.getGroupURLType()
567       // +" "+((String[])urlset[3])[0]);
568       // first two bits ofurlLink type bitfield are sequenceids and sequences
569       // TODO: FUTURE: ensure the groupURL menu structure can be generalised
570       addshowLink(
571               linkMenus[type],
572               label
573                       + " "
574                       + (ltarget == null ? (((type & 1) == 1 ? "ID"
575                               : "Sequence") + (urlLink
576                               .getNumberInvolved(urlset) > 1 ? "s" : ""))
577                               : (usingNames ? (((type & 1) == 1) ? "(Names)"
578                                       : "")
579                                       : ("(" + ltarget + ")"))), descr,
580               usingNames ? null : label, urlLink, urlset);
581       return true;
582     }
583     return false;
584   }
585
586   // / end of stuff copied from popupmenu
587   public void attachWSMenuEntry(final JMenu wsmenu,
588           final AlignFrame alignFrame)
589   {
590     final JMenu enfinServiceMenu = new JMenu("Envision 2");
591     wsmenu.add(enfinServiceMenu);
592     enfinServiceMenu.setEnabled(false);
593     wsmenu.addMenuListener(new MenuListener()
594     {
595       // this listener remembers when the menu was first selected, and
596       // doesn't rebuild the session list until it has been cleared and
597       // reselected again.
598       boolean refresh = true;
599
600       public void menuCanceled(MenuEvent e)
601       {
602         refresh = true;
603       }
604
605       public void menuDeselected(MenuEvent e)
606       {
607         refresh = true;
608       }
609
610       public void menuSelected(MenuEvent e)
611       {
612         if (refresh)
613         {
614           try
615           {
616             buildGroupLinkMenu(enfinServiceMenu, alignFrame);
617           } catch (OutOfMemoryError ex)
618           {
619             Cache.log.error(
620                     "Out of memory when calculating the Envision2 links.",
621                     ex);
622             enfinServiceMenu.setEnabled(false);
623           }
624           refresh = false;
625         }
626       }
627     });
628
629   }
630
631 }