From: kiramt Date: Tue, 1 Nov 2016 12:00:55 +0000 (+0000) Subject: JAL-2282 Refactored URL link creation into UrlLink X-Git-Tag: Release_2_10_1^2~18^2~2^2~10 X-Git-Url: http://source.jalview.org/gitweb/?a=commitdiff_plain;h=bb90751010ab798f54dabddb8cfb6f51e847072c;p=jalview.git JAL-2282 Refactored URL link creation into UrlLink --- diff --git a/src/jalview/gui/PopupMenu.java b/src/jalview/gui/PopupMenu.java index e2d397f..aa3cad4 100644 --- a/src/jalview/gui/PopupMenu.java +++ b/src/jalview/gui/PopupMenu.java @@ -54,7 +54,6 @@ import jalview.schemes.TaylorColourScheme; import jalview.schemes.TurnColourScheme; import jalview.schemes.UserColourScheme; import jalview.schemes.ZappoColourScheme; -import jalview.util.DBRefUtils; import jalview.util.GroupUrlLink; import jalview.util.GroupUrlLink.UrlStringTooLongException; import jalview.util.MessageManager; @@ -63,8 +62,8 @@ import jalview.util.UrlLink; import java.awt.Color; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; -import java.util.ArrayList; import java.util.Arrays; +import java.util.Collection; import java.util.Collections; import java.util.Hashtable; import java.util.LinkedHashMap; @@ -617,7 +616,8 @@ public class PopupMenu extends JPopupMenu void addFeatureLinks(final SequenceI seq, List links) { JMenu linkMenu = new JMenu(MessageManager.getString("action.link")); - List linkset = new ArrayList(); + Map linkset = new LinkedHashMap(); + for (String link : links) { UrlLink urlLink = null; @@ -629,80 +629,18 @@ public class PopupMenu extends JPopupMenu Cache.log.error("Exception for URLLink '" + link + "'", foo); continue; } - ; + if (!urlLink.isValid()) { Cache.log.error(urlLink.getInvalidMessage()); continue; } - final String label = urlLink.getLabel(); - - // collect id string too - String id = seq.getName(); - String descr = seq.getDescription(); - if (descr != null && descr.length() < 1) - { - descr = null; - } - - if (seq != null && urlLink.usesSeqId()) // link is ID - { - // collect matching db-refs - DBRefEntry[] dbr = DBRefUtils.selectRefs(seq.getDBRefs(), - new String[] { urlLink.getTarget() }); - - // if there are any dbrefs which match up with the link - if (dbr != null) - { - for (int r = 0; r < dbr.length; r++) - { - if (id != null && dbr[r].getAccessionId().equals(id)) - { - // suppress duplicate link creation for the bare sequence ID - // string with this link - id = null; - } - // create Bare ID link for this URL - createBareURLLink(urlLink, dbr[r].getAccessionId(), linkset, - linkMenu, label, true); - } - } - // Create urls from description but only for URL links which are regex - // links - if (descr != null && urlLink.getRegexReplace() != null) - { - // create link for this URL from description where regex matches - createBareURLLink(urlLink, descr, linkset, linkMenu, label, false); - } + urlLink.createLinksFromSeq(seq, linkset); + } - } - else if (seq != null && !urlLink.usesSeqId()) // link is name - { - if (id != null) - { - // create Bare ID link for this URL - createBareURLLink(urlLink, id, linkset, linkMenu, label, false); - } - // Create urls from description but only for URL links which are regex - // links - if (descr != null && urlLink.getRegexReplace() != null) - { - // create link for this URL from description where regex matches - createBareURLLink(urlLink, descr, linkset, linkMenu, label, false); - } - } - else - { - if (!linkset.contains(label + "|" + urlLink.getUrl_prefix())) - { - linkset.add(label + "|" + urlLink.getUrl_prefix()); - // Add a non-dynamic link - addshowLink(linkMenu, label, urlLink.getUrl_prefix()); - } - } + addshowLinks(linkMenu, linkset.values()); - } if (sequence != null) { sequenceMenu.add(linkMenu); @@ -713,33 +651,7 @@ public class PopupMenu extends JPopupMenu } } - /* - * Create a bare URL Link - */ - private void createBareURLLink(UrlLink urlLink, String id, - List linkset, JMenu linkMenu, String label, - Boolean addSepToLabel) - { - String[] urls = urlLink.makeUrls(id, true); - if (urls != null) - { - for (int u = 0; u < urls.length; u += 2) - { - if (!linkset.contains(urls[u] + "|" + urls[u + 1])) - { - linkset.add(urls[u] + "|" + urls[u + 1]); - if (addSepToLabel) - { - addshowLink(linkMenu, label + "|" + urls[u], urls[u + 1]); - } - else - { - addshowLink(linkMenu, label, urls[u + 1]); - } - } - } - } - } + /** * Add annotation types to 'Show annotations' and/or 'Hide annotations' menus. @@ -1015,6 +927,15 @@ public class PopupMenu extends JPopupMenu } } + private void addshowLinks(JMenu linkMenu, Collection linkset) + { + for (String[] linkstrset : linkset) + { + // split linkstr into label and url + addshowLink(linkMenu, linkstrset[1], linkstrset[3]); + } + } + /** * add a show URL menu item to the given linkMenu * diff --git a/src/jalview/util/UrlLink.java b/src/jalview/util/UrlLink.java index 872f432..0968d36 100644 --- a/src/jalview/util/UrlLink.java +++ b/src/jalview/util/UrlLink.java @@ -23,6 +23,10 @@ package jalview.util; import static jalview.util.UrlConstants.SEQUENCE_ID; import static jalview.util.UrlConstants.SEQUENCE_NAME; +import jalview.datamodel.DBRefEntry; +import jalview.datamodel.SequenceI; + +import java.util.Map; import java.util.Vector; public class UrlLink @@ -377,6 +381,114 @@ public class UrlLink } } + /** + * + * @param urlLink + * @param seq + * @param linkset + */ + public void createLinksFromSeq(final SequenceI seq, + Map linkset) + { + if (seq != null && dynamic) + { + createDynamicLinks(seq, linkset); + } + else + { + createStaticLink(linkset); + } + } + + /** + * Create a static URL link + * + * @param linkset + */ + public void createStaticLink(Map linkset) + { + if (!linkset.containsKey(label + "|" + getUrl_prefix())) + { + // Add a non-dynamic link + linkset.put(label + "|" + getUrl_prefix(), new String[] { "", label, + "", getUrl_prefix() }); + } + } + + /** + * Create a dynamic URL link + * + * @param seq + * @param linkset + */ + public void createDynamicLinks(final SequenceI seq, + Map linkset) + { + // collect id string too + String id = seq.getName(); + String descr = seq.getDescription(); + if (descr != null && descr.length() < 1) + { + descr = null; + } + + if (usesSeqId()) // link is ID + { + // collect matching db-refs + DBRefEntry[] dbr = DBRefUtils.selectRefs(seq.getDBRefs(), + new String[] { target }); + + // if there are any dbrefs which match up with the link + if (dbr != null) + { + for (int r = 0; r < dbr.length; r++) + { + // create Bare ID link for this URL + createBareURLLink(dbr[r].getAccessionId(), linkset, true); + } + } + } + else if (!usesSeqId() && id != null) // link is name + { + // create Bare ID link for this URL + createBareURLLink(id, linkset, false); + } + + // Create urls from description but only for URL links which are regex + // links + if (descr != null && getRegexReplace() != null) + { + // create link for this URL from description where regex matches + createBareURLLink(descr, linkset, false); + } + } + + /* + * Create a bare URL Link + */ + protected void createBareURLLink(String id, + Map linkset, Boolean combineLabel) + { + String[] urls = makeUrls(id, true); + if (urls != null) + { + for (int u = 0; u < urls.length; u += 2) + { + if (!linkset.containsKey(urls[u] + "|" + urls[u + 1])) + { + String thisLabel = label; + if (combineLabel) + { + thisLabel = label + "|" + urls[u]; + } + + linkset.put(urls[u] + "|" + urls[u + 1], new String[] { target, + thisLabel, urls[u], urls[u + 1] }); + } + } + } + } + private static void testUrls(UrlLink ul, String idstring, String[] urls) { @@ -457,7 +569,6 @@ public class UrlLink public boolean isDynamic() { - // TODO Auto-generated method stub return dynamic; }