X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fgui%2FPopupMenu.java;h=3145f7cab016d3cf217339c443b0629bd678f0f8;hb=6533e7b5e6ecd706257ac963c6e0002ae106046d;hp=5a12a0232363a33308739bd5f11d80e10bae9563;hpb=c45f4774f08ed968eaefb6cff55025ce9d21ad0c;p=jalview.git diff --git a/src/jalview/gui/PopupMenu.java b/src/jalview/gui/PopupMenu.java index 5a12a02..3145f7c 100644 --- a/src/jalview/gui/PopupMenu.java +++ b/src/jalview/gui/PopupMenu.java @@ -25,7 +25,6 @@ import jalview.analysis.AlignmentAnnotationUtils; import jalview.analysis.AlignmentUtils; import jalview.analysis.Conservation; import jalview.bin.Cache; -import jalview.bin.Jalview; import jalview.commands.ChangeCaseCommand; import jalview.commands.EditCommand; import jalview.commands.EditCommand.Action; @@ -48,12 +47,13 @@ import jalview.schemes.Blosum62ColourScheme; import jalview.schemes.ColourSchemeI; import jalview.schemes.ColourSchemes; import jalview.schemes.PIDColourScheme; +import jalview.schemes.ResidueColourScheme; import jalview.util.GroupUrlLink; import jalview.util.GroupUrlLink.UrlStringTooLongException; import jalview.util.MessageManager; +import jalview.util.Platform; import jalview.util.StringUtils; import jalview.util.UrlLink; -import jalview.util.dialogrunner.RunResponse; import java.awt.BorderLayout; import java.awt.Color; @@ -72,21 +72,20 @@ import java.util.SortedMap; import java.util.TreeMap; import java.util.Vector; +import javax.swing.ButtonGroup; import javax.swing.JCheckBoxMenuItem; -import javax.swing.JComponent; import javax.swing.JInternalFrame; import javax.swing.JLabel; import javax.swing.JMenu; import javax.swing.JMenuItem; import javax.swing.JPanel; import javax.swing.JPopupMenu; +import javax.swing.JRadioButtonMenuItem; import javax.swing.JScrollPane; /** - * DOCUMENT ME! - * - * @author $author$ - * @version $Revision: 1.118 $ + * The popup menu that is displayed on right-click on a sequence id, or in the + * sequence alignment. */ public class PopupMenu extends JPopupMenu implements ColourChangeListener { @@ -100,6 +99,8 @@ public class PopupMenu extends JPopupMenu implements ColourChangeListener protected JCheckBoxMenuItem conservationMenuItem = new JCheckBoxMenuItem(); + protected JRadioButtonMenuItem annotationColour; + protected JMenuItem modifyConservation = new JMenuItem(); AlignmentPanel ap; @@ -181,6 +182,198 @@ public class PopupMenu extends JPopupMenu implements ColourChangeListener JMenuItem hideInsertions = new JMenuItem(); /** + * Constructs a menu with sub-menu items for any hyperlinks for the sequence + * and/or features provided. Hyperlinks may include a lookup by sequence id, + * or database cross-references, depending on which links are enabled in user + * preferences. + * + * @param seq + * @param features + * @return + */ + static JMenu buildLinkMenu(final SequenceI seq, + List features) + { + JMenu linkMenu = new JMenu(MessageManager.getString("action.link")); + + List nlinks = null; + if (seq != null) + { + nlinks = Preferences.sequenceUrlLinks.getLinksForMenu(); + UrlLink.sort(nlinks); + } + else + { + nlinks = new ArrayList<>(); + } + + if (features != null) + { + for (SequenceFeature sf : features) + { + if (sf.links != null) + { + for (String link : sf.links) + { + nlinks.add(link); + } + } + } + } + + /* + * instantiate the hyperlinklink templates from sequence data; + * note the order of the templates is preserved in the map + */ + Map> linkset = new LinkedHashMap<>(); + for (String link : nlinks) + { + UrlLink urlLink = null; + try + { + urlLink = new UrlLink(link); + } catch (Exception foo) + { + Cache.log.error("Exception for URLLink '" + link + "'", foo); + continue; + } + + if (!urlLink.isValid()) + { + Cache.log.error(urlLink.getInvalidMessage()); + continue; + } + + urlLink.createLinksFromSeq(seq, linkset); + } + + /* + * construct menu items for the hyperlinks (still preserving + * the order of the sorted templates) + */ + addUrlLinks(linkMenu, linkset.values()); + + return linkMenu; + } + + /** + * A helper method that builds menu items from the given links, with action + * handlers to open the link URL, and adds them to the linkMenu. Each provided + * link should be a list whose second item is the menu text, and whose fourth + * item is the URL to open when the menu item is selected. + * + * @param linkMenu + * @param linkset + */ + static private void addUrlLinks(JMenu linkMenu, + Collection> linkset) + { + for (List linkstrset : linkset) + { + final String url = linkstrset.get(3); + JMenuItem item = new JMenuItem(linkstrset.get(1)); + item.setToolTipText(MessageManager + .formatMessage("label.open_url_param", new Object[] + { url })); + item.addActionListener(new ActionListener() + { + @Override + public void actionPerformed(ActionEvent e) + { + new Thread(new Runnable() + { + @Override + public void run() + { + showLink(url); + } + }).start(); + } + }); + linkMenu.add(item); + } + } + + /** + * Opens the provided url in the default web browser, or shows an error + * message if this fails + * + * @param url + */ + static void showLink(String url) + { + try + { + Platform.openURL(url); + } catch (Exception ex) + { + JvOptionPane.showInternalMessageDialog(Desktop.getDesktopPane(), + MessageManager.getString("label.web_browser_not_found_unix"), + MessageManager.getString("label.web_browser_not_found"), + JvOptionPane.WARNING_MESSAGE); + + ex.printStackTrace(); + } + } + + /** + * For the popup menu on the idPanel. + * + * Add a late bound groupURL item to the given linkMenu + * + * @param linkMenu + * @param label + * - menu label string + * @param urlgenerator + * GroupURLLink used to generate URL + * @param urlstub + * Object array returned from the makeUrlStubs function. + */ + static void addshowLink(JMenu linkMenu, String label, + final GroupUrlLink urlgenerator, final Object[] urlstub) + { + JMenuItem item = new JMenuItem(label); + item.setToolTipText(MessageManager + .formatMessage("label.open_url_seqs_param", new Object[] + { urlgenerator.getUrl_prefix(), + urlgenerator.getNumberInvolved(urlstub) })); + // TODO: put in info about what is being sent. + item.addActionListener(new ActionListener() + { + @Override + public void actionPerformed(ActionEvent e) + { + new Thread(new Runnable() + { + + @Override + public void run() + { + try + { + // Object[] { int[] { number of matches seqs }, + // boolean[] { which matched }, + // StringBuffer[] { segment generated from inputs }, + // String[] { url } + // } + + // TODO bug: urlstub is { int[], boolean[] } but constructFrom + // requires something else. + + showLink(urlgenerator.constructFrom(urlstub)); + } catch (UrlStringTooLongException e2) + { + } + } + + }).start(); + } + }); + + linkMenu.add(item); + } + + /** * Creates a new PopupMenu object. * * @param ap @@ -206,6 +399,7 @@ public class PopupMenu extends JPopupMenu implements ColourChangeListener * non-positional features if in the Id panel, features at the * clicked residue if in the sequence panel * @param groupLinks + * not implemented -- empty list */ public PopupMenu(final AlignmentPanel alignPanel, final SequenceI seq, List features, List groupLinks) @@ -409,9 +603,20 @@ public class PopupMenu extends JPopupMenu implements ColourChangeListener } } } - // for the case when no sequences are even visible + + /* + * offer 'Reveal All' + * - in the IdPanel (seq not null) if any sequence is hidden + * - in the IdPanel or SeqPanel if all sequences are hidden (seq is null) + */ if (alignPanel.av.hasHiddenRows()) { + boolean addOption = seq != null; + if (!addOption && alignPanel.av.getAlignment().getHeight() == 0) + { + addOption = true; + } + if (addOption) { menuItem = new JMenuItem( MessageManager.getString("action.reveal_all")); @@ -427,7 +632,6 @@ public class PopupMenu extends JPopupMenu implements ColourChangeListener } } }); - add(menuItem); } } @@ -466,6 +670,7 @@ public class PopupMenu extends JPopupMenu implements ColourChangeListener // add any groupURLs to the groupURL submenu and make it visible if (groupLinks != null && groupLinks.size() > 0) { + // not implemented -- empty list buildGroupURLMenu(sg, groupLinks); } // Add a 'show all structures' for the current selection @@ -599,23 +804,28 @@ public class PopupMenu extends JPopupMenu implements ColourChangeListener protected void showFeatureDetails(SequenceFeature sf) { JInternalFrame details; - if (/** @j2sNative true || */ false) + if (Platform.isJS()) { details = new JInternalFrame(); JPanel panel = new JPanel(new BorderLayout()); panel.setOpaque(true); panel.setBackground(Color.white); // TODO JAL-3026 set style of table correctly for feature details - JLabel reprt = new JLabel(MessageManager.formatMessage("label.html_content", - new Object[] - { sf.getDetailsReport()})); + JLabel reprt = new JLabel(MessageManager + .formatMessage("label.html_content", new Object[] + { sf.getDetailsReport() })); reprt.setBackground(Color.WHITE); reprt.setOpaque(true); - panel.add(reprt,BorderLayout.CENTER); + panel.add(reprt, BorderLayout.CENTER); details.setContentPane(panel); details.pack(); } else + /** + * Java only + * + * @j2sIgnore + */ { CutAndPasteHtmlTransfer cap = new CutAndPasteHtmlTransfer(); // it appears Java's CSS does not support border-collaps :-( @@ -633,63 +843,14 @@ public class PopupMenu extends JPopupMenu implements ColourChangeListener * When seq is not null, these are links for the sequence id, which may be to * external web sites for the sequence accession, and/or links embedded in * non-positional features. When seq is null, only links embedded in the - * provided features are added. + * provided features are added. If no links are found, the menu is not added. * * @param seq * @param features */ void addLinks(final SequenceI seq, List features) { - JMenu linkMenu = new JMenu(MessageManager.getString("action.link")); - - List nlinks = null; - if (seq != null) - { - nlinks = Preferences.sequenceUrlLinks.getLinksForMenu(); - } - else - { - nlinks = new ArrayList<>(); - } - - if (features != null) - { - for (SequenceFeature sf : features) - { - if (sf.links != null) - { - for (String link : sf.links) - { - nlinks.add(link); - } - } - } - } - - Map> linkset = new LinkedHashMap<>(); - - for (String link : nlinks) - { - UrlLink urlLink = null; - try - { - urlLink = new UrlLink(link); - } catch (Exception foo) - { - Cache.log.error("Exception for URLLink '" + link + "'", foo); - continue; - } - - if (!urlLink.isValid()) - { - Cache.log.error(urlLink.getInvalidMessage()); - continue; - } - - urlLink.createLinksFromSeq(seq, linkset); - } - - addshowLinks(linkMenu, linkset.values()); + JMenu linkMenu = buildLinkMenu(seq, features); // only add link menu if it has entries if (linkMenu.getItemCount() > 0) @@ -835,6 +996,12 @@ public class PopupMenu extends JPopupMenu implements ColourChangeListener showOrHideMenu.add(item); } + /** + * + * @param sg + * @param groupLinks + * not implemented -- empty list + */ private void buildGroupURLMenu(SequenceGroup sg, List groupLinks) { @@ -866,12 +1033,14 @@ public class PopupMenu extends JPopupMenu implements ColourChangeListener { sqi = sqi.getDatasetSequence(); } - DBRefEntry[] dbr = sqi.getDBRefs(); - if (dbr != null && dbr.length > 0) + List dbr = sqi.getDBRefs(); + int nd; + if (dbr != null && (nd = dbr.size()) > 0) { - for (int d = 0; d < dbr.length; d++) + for (int d = 0; d < nd; d++) { - String src = dbr[d].getSource(); // jalview.util.DBRefUtils.getCanonicalName(dbr[d].getSource()).toUpperCase(); + DBRefEntry e = dbr.get(d); + String src = e.getSource(); // jalview.util.DBRefUtils.getCanonicalName(dbr[d].getSource()).toUpperCase(); Object[] sarray = commonDbrefs.get(src); if (sarray == null) { @@ -884,19 +1053,25 @@ public class PopupMenu extends JPopupMenu implements ColourChangeListener if (((String[]) sarray[1])[sq] == null) { - if (!dbr[d].hasMap() || (dbr[d].getMap() + if (!e.hasMap() || (e.getMap() .locateMappedRange(start, end) != null)) { - ((String[]) sarray[1])[sq] = dbr[d].getAccessionId(); + ((String[]) sarray[1])[sq] = e.getAccessionId(); ((int[]) sarray[0])[0]++; } } } } } + if (groupLinks.size() == 0) + { + return; + } // now create group links for all distinct ID/sequence sets. boolean addMenu = false; // indicates if there are any group links to give // to user + + // not implmeented -- empty list for (String link : groupLinks) { GroupUrlLink urlLink = null; @@ -949,6 +1124,7 @@ public class PopupMenu extends JPopupMenu implements ColourChangeListener { urlset = urlLink.makeUrlStubs(ids, seqstr, "FromJalview" + System.currentTimeMillis(), false); + // { int[], boolean[] } only here } catch (UrlStringTooLongException e) { } @@ -977,103 +1153,10 @@ public class PopupMenu extends JPopupMenu implements ColourChangeListener groupLinksMenu.add(linkMenus[m]); } } - groupMenu.add(groupLinksMenu); } } - private void addshowLinks(JMenu linkMenu, - Collection> linkset) - { - for (List linkstrset : linkset) - { - // split linkstr into label and url - addshowLink(linkMenu, linkstrset.get(1), linkstrset.get(3)); - } - } - - /** - * add a show URL menu item to the given linkMenu - * - * @param linkMenu - * @param label - * - menu label string - * @param url - * - url to open - */ - private void addshowLink(JMenu linkMenu, String label, final String url) - { - JMenuItem item = new JMenuItem(label); - item.setToolTipText(MessageManager.formatMessage("label.open_url_param", - new Object[] - { url })); - item.addActionListener(new ActionListener() - { - @Override - public void actionPerformed(ActionEvent e) - { - new Thread(new Runnable() - { - - @Override - public void run() - { - showLink(url); - } - - }).start(); - } - }); - - linkMenu.add(item); - } - - /** - * add a late bound groupURL item to the given linkMenu - * - * @param linkMenu - * @param label - * - menu label string - * @param urlgenerator - * GroupURLLink used to generate URL - * @param urlstub - * Object array returned from the makeUrlStubs function. - */ - private void addshowLink(JMenu linkMenu, String label, - final GroupUrlLink urlgenerator, final Object[] urlstub) - { - JMenuItem item = new JMenuItem(label); - item.setToolTipText(MessageManager - .formatMessage("label.open_url_seqs_param", new Object[] - { urlgenerator.getUrl_prefix(), - urlgenerator.getNumberInvolved(urlstub) })); - // TODO: put in info about what is being sent. - item.addActionListener(new ActionListener() - { - @Override - public void actionPerformed(ActionEvent e) - { - new Thread(new Runnable() - { - - @Override - public void run() - { - try - { - showLink(urlgenerator.constructFrom(urlstub)); - } catch (UrlStringTooLongException e2) - { - } - } - - }).start(); - } - }); - - linkMenu.add(item); - } - /** * DOCUMENT ME! * @@ -1429,6 +1512,13 @@ public class PopupMenu extends JPopupMenu implements ColourChangeListener } }); + annotationColour = new JRadioButtonMenuItem( + MessageManager.getString("action.by_annotation")); + annotationColour.setName(ResidueColourScheme.ANNOTATION_COLOUR); + annotationColour.setEnabled(false); + annotationColour.setToolTipText( + MessageManager.getString("label.by_annotation_tooltip")); + modifyConservation.setText(MessageManager .getString("label.modify_conservation_threshold")); modifyConservation.addActionListener(new ActionListener() @@ -1459,7 +1549,10 @@ public class PopupMenu extends JPopupMenu implements ColourChangeListener colourMenu.add(textColour); colourMenu.addSeparator(); - ColourMenuHelper.addMenuItems(colourMenu, this, sg, false); + ButtonGroup bg = ColourMenuHelper.addMenuItems(colourMenu, this, sg, + false); + bg.add(annotationColour); + colourMenu.add(annotationColour); colourMenu.addSeparator(); colourMenu.add(conservationMenuItem); @@ -1650,21 +1743,26 @@ public class PopupMenu extends JPopupMenu implements ColourChangeListener } contents.append(""); String report = contents.toString(); - + JInternalFrame frame; - if (Jalview.isJS()) + if (Platform.isJS()) { JLabel textLabel = new JLabel(); textLabel.setText(report); textLabel.setBackground(Color.WHITE); JPanel pane = new JPanel(new BorderLayout()); - ((JPanel) pane).setOpaque(true); + pane.setOpaque(true); pane.setBackground(Color.WHITE); - ((JPanel) pane).add(textLabel, BorderLayout.NORTH); + pane.add(textLabel, BorderLayout.NORTH); frame = new JInternalFrame(); frame.getContentPane().add(new JScrollPane(pane)); } else + /** + * Java only + * + * @j2sIgnore + */ { CutAndPasteHtmlTransfer cap = new CutAndPasteHtmlTransfer(); cap.setText(report); @@ -1808,7 +1906,7 @@ public class PopupMenu extends JPopupMenu implements ColourChangeListener MessageManager.getString("label.group_description")); dialog.showDialog(ap.alignFrame, MessageManager.getString("label.edit_group_name_description"), - new RunResponse(JvOptionPane.OK_OPTION) + new Runnable() { @Override public void run() @@ -1849,7 +1947,7 @@ public class PopupMenu extends JPopupMenu implements ColourChangeListener dialog.showDialog(ap.alignFrame, MessageManager.getString( "label.edit_sequence_name_description"), - new RunResponse(JvOptionPane.OK_OPTION) + new Runnable() { @Override public void run() @@ -1912,7 +2010,7 @@ public class PopupMenu extends JPopupMenu implements ColourChangeListener refresh(); }; }; - JalviewColourChooser.showColourChooser(Desktop.getDesktop(), + JalviewColourChooser.showColourChooser(Desktop.getDesktopPane(), title, Color.BLUE, listener); } @@ -1952,22 +2050,6 @@ public class PopupMenu extends JPopupMenu implements ColourChangeListener refresh(); } - public void showLink(String url) - { - try - { - jalview.util.BrowserLauncher.openURL(url); - } catch (Exception ex) - { - JvOptionPane.showInternalMessageDialog(Desktop.desktop, - MessageManager.getString("label.web_browser_not_found_unix"), - MessageManager.getString("label.web_browser_not_found"), - JvOptionPane.WARNING_MESSAGE); - - ex.printStackTrace(); - } - } - void hideSequences(boolean representGroup) { ap.av.hideSequences(sequence, representGroup); @@ -1975,12 +2057,12 @@ public class PopupMenu extends JPopupMenu implements ColourChangeListener public void copy_actionPerformed() { - ap.alignFrame.copy_actionPerformed(null); + ap.alignFrame.copy_actionPerformed(); } public void cut_actionPerformed() { - ap.alignFrame.cut_actionPerformed(null); + ap.alignFrame.cut_actionPerformed(); } void changeCase(ActionEvent e) @@ -2130,7 +2212,7 @@ public class PopupMenu extends JPopupMenu implements ColourChangeListener null, MessageManager.getString("label.edit_sequence"), null); dialog.showDialog(ap.alignFrame, MessageManager.getString("label.edit_sequence"), - new RunResponse(JvOptionPane.OK_OPTION) + new Runnable() { @Override public void run() @@ -2166,7 +2248,7 @@ public class PopupMenu extends JPopupMenu implements ColourChangeListener * switch to the chosen colour scheme (or null for None) */ ColourSchemeI colourScheme = ColourSchemes.getInstance() - .getColourScheme(colourSchemeName, sg, + .getColourScheme(colourSchemeName, ap.av, sg, ap.av.getHiddenRepSequences()); sg.setColourScheme(colourScheme); if (colourScheme instanceof Blosum62ColourScheme