From: kiramt Date: Tue, 1 Nov 2016 14:55:02 +0000 (+0000) Subject: JAL-2282 Refactor SequenceAnnotationReport to use refactored UrlLink X-Git-Tag: Release_2_10_1^2~18^2~2^2~7 X-Git-Url: http://source.jalview.org/gitweb/?a=commitdiff_plain;h=9fc1c4d049a16cccb933e80b5be07ec36e6bda7f;p=jalview.git JAL-2282 Refactor SequenceAnnotationReport to use refactored UrlLink code. --- diff --git a/src/jalview/gui/PopupMenu.java b/src/jalview/gui/PopupMenu.java index aa3cad4..a487fc7 100644 --- a/src/jalview/gui/PopupMenu.java +++ b/src/jalview/gui/PopupMenu.java @@ -616,7 +616,7 @@ public class PopupMenu extends JPopupMenu void addFeatureLinks(final SequenceI seq, List links) { JMenu linkMenu = new JMenu(MessageManager.getString("action.link")); - Map linkset = new LinkedHashMap(); + Map> linkset = new LinkedHashMap>(); for (String link : links) { @@ -927,12 +927,12 @@ public class PopupMenu extends JPopupMenu } } - private void addshowLinks(JMenu linkMenu, Collection linkset) + private void addshowLinks(JMenu linkMenu, Collection> linkset) { - for (String[] linkstrset : linkset) + for (List linkstrset : linkset) { // split linkstr into label and url - addshowLink(linkMenu, linkstrset[1], linkstrset[3]); + addshowLink(linkMenu, linkstrset.get(1), linkstrset.get(3)); } } diff --git a/src/jalview/io/SequenceAnnotationReport.java b/src/jalview/io/SequenceAnnotationReport.java index 07b88bf..89f1068 100644 --- a/src/jalview/io/SequenceAnnotationReport.java +++ b/src/jalview/io/SequenceAnnotationReport.java @@ -24,10 +24,11 @@ import jalview.datamodel.DBRefEntry; import jalview.datamodel.SequenceFeature; import jalview.datamodel.SequenceI; import jalview.io.gff.GffConstants; -import jalview.util.DBRefUtils; import jalview.util.UrlLink; import java.util.ArrayList; +import java.util.Collection; +import java.util.LinkedHashMap; import java.util.List; import java.util.Map; @@ -206,16 +207,17 @@ public class SequenceAnnotationReport { try { - for (String[] urllink : createLinksFrom(null, urlstring)) + for (List urllink : createLinksFrom(null, urlstring)) { sb.append("
" - + (urllink[0].toLowerCase().equals( - urllink[1].toLowerCase()) ? urllink[0] - : (urllink[0] + ":" + urllink[1])) + + (urllink.get(0).toLowerCase() + .equals(urllink.get(1).toLowerCase()) ? urllink + .get(0) : (urllink.get(0) + ":" + urllink + .get(1))) + "
"); } } catch (Exception x) @@ -234,124 +236,22 @@ public class SequenceAnnotationReport * * @param seq * @param link - * @return String[][] { String[] { link target, link label, dynamic component - * inserted (if any), url }} + * @return Collection< List > { List { link target, link + * label, dynamic component inserted (if any), url }} */ - String[][] createLinksFrom(SequenceI seq, String link) + Collection> createLinksFrom(SequenceI seq, String link) { - List urlSets = new ArrayList(); - List uniques = new ArrayList(); + Map> urlSets = new LinkedHashMap>(); UrlLink urlLink = new UrlLink(link); if (!urlLink.isValid()) { System.err.println(urlLink.getInvalidMessage()); return null; } - if (seq != null && urlLink.isDynamic()) - { - urlSets.addAll(createDynamicLinks(seq, urlLink, uniques)); - } - else - { - String target = urlLink.getTarget(); - String label = urlLink.getLabel(); - String unq = label + "|" + urlLink.getUrl_prefix(); - if (!uniques.contains(unq)) - { - uniques.add(unq); - urlSets.add(new String[] { target, label, null, - urlLink.getUrl_prefix() }); - } - } - - return urlSets.toArray(new String[][] {}); - } - /** - * Formats and returns a list of dynamic href links - * - * @param seq - * @param urlLink - * @param uniques - */ - List createDynamicLinks(SequenceI seq, UrlLink urlLink, - List uniques) - { - List result = new ArrayList(); - final String target = urlLink.getTarget(); - final String label = urlLink.getLabel(); + urlLink.createLinksFromSeq(seq, urlSets); - // collect matching db-refs - DBRefEntry[] dbr = DBRefUtils.selectRefs(seq.getDBRefs(), - new String[] { target }); - // collect id string too - String id = seq.getName(); - String descr = seq.getDescription(); - if (descr != null && descr.length() < 1) - { - descr = null; - } - 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 - String[] urls = urlLink.makeUrls(dbr[r].getAccessionId(), true); - if (urls != null) - { - for (int u = 0; u < urls.length; u += 2) - { - String unq = urls[u] + "|" + urls[u + 1]; - if (!uniques.contains(unq)) - { - result.add(new String[] { target, label, urls[u], urls[u + 1] }); - uniques.add(unq); - } - } - } - } - } - if (id != null) - { - // create Bare ID link for this URL - String[] urls = urlLink.makeUrls(id, true); - if (urls != null) - { - for (int u = 0; u < urls.length; u += 2) - { - String unq = urls[u] + "|" + urls[u + 1]; - if (!uniques.contains(unq)) - { - result.add(new String[] { target, label, urls[u], urls[u + 1] }); - uniques.add(unq); - } - } - } - } - if (descr != null && urlLink.getRegexReplace() != null) - { - // create link for this URL from description only if regex matches - String[] urls = urlLink.makeUrls(descr, true); - if (urls != null) - { - for (int u = 0; u < urls.length; u += 2) - { - String unq = urls[u] + "|" + urls[u + 1]; - if (!uniques.contains(unq)) - { - result.add(new String[] { target, label, urls[u], urls[u + 1] }); - uniques.add(unq); - } - } - } - } - return result; + return urlSets.values(); } public void createSequenceAnnotationReport(final StringBuffer tip, diff --git a/src/jalview/util/UrlLink.java b/src/jalview/util/UrlLink.java index 0968d36..e14ff63 100644 --- a/src/jalview/util/UrlLink.java +++ b/src/jalview/util/UrlLink.java @@ -26,6 +26,8 @@ import static jalview.util.UrlConstants.SEQUENCE_NAME; import jalview.datamodel.DBRefEntry; import jalview.datamodel.SequenceI; +import java.util.Arrays; +import java.util.List; import java.util.Map; import java.util.Vector; @@ -388,7 +390,7 @@ public class UrlLink * @param linkset */ public void createLinksFromSeq(final SequenceI seq, - Map linkset) + Map> linkset) { if (seq != null && dynamic) { @@ -405,13 +407,13 @@ public class UrlLink * * @param linkset */ - public void createStaticLink(Map 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() }); + linkset.put(label + "|" + getUrl_prefix(), + Arrays.asList(target, label, null, getUrl_prefix())); } } @@ -422,7 +424,7 @@ public class UrlLink * @param linkset */ public void createDynamicLinks(final SequenceI seq, - Map linkset) + Map> linkset) { // collect id string too String id = seq.getName(); @@ -467,7 +469,7 @@ public class UrlLink * Create a bare URL Link */ protected void createBareURLLink(String id, - Map linkset, Boolean combineLabel) + Map> linkset, Boolean combineLabel) { String[] urls = makeUrls(id, true); if (urls != null) @@ -482,8 +484,8 @@ public class UrlLink thisLabel = label + "|" + urls[u]; } - linkset.put(urls[u] + "|" + urls[u + 1], new String[] { target, - thisLabel, urls[u], urls[u + 1] }); + linkset.put(urls[u] + "|" + urls[u + 1], + Arrays.asList(target, thisLabel, urls[u], urls[u + 1])); } } }