From 0ef6205b5b4caef00329b34d9ad118804e5b29be Mon Sep 17 00:00:00 2001 From: jprocter Date: Mon, 11 Aug 2008 14:56:31 +0000 Subject: [PATCH] regular expression based URL link generation --- src/jalview/appletgui/APopupMenu.java | 58 +++++- src/jalview/gui/PopupMenu.java | 53 +++-- src/jalview/jbgui/GSequenceLink.java | 290 +++++++++++++------------- src/jalview/util/UrlLink.java | 369 +++++++++++++++++++++++++++++++++ 4 files changed, 601 insertions(+), 169 deletions(-) create mode 100644 src/jalview/util/UrlLink.java diff --git a/src/jalview/appletgui/APopupMenu.java b/src/jalview/appletgui/APopupMenu.java index ff1c3c8..7c1ea3f 100755 --- a/src/jalview/appletgui/APopupMenu.java +++ b/src/jalview/appletgui/APopupMenu.java @@ -28,6 +28,7 @@ import jalview.analysis.*; import jalview.commands.*; import jalview.datamodel.*; import jalview.schemes.*; +import jalview.util.UrlLink; import jalview.io.AppletFormatAdapter; public class APopupMenu @@ -134,10 +135,59 @@ public class APopupMenu for (int i = 0; i < links.size(); i++) { link = links.elementAt(i).toString(); - final String target = link.substring(0, link.indexOf("|")); - - final String url; + UrlLink urlLink = new UrlLink(link); + if (!urlLink.isValid()) + { + System.err.println(urlLink.getInvalidMessage()); + continue; + } + final String target = urlLink.getTarget(); // link.substring(0, link.indexOf("|")); + final String label = urlLink.getLabel(); + if (urlLink.isDynamic()) + { + // collect matching db-refs + DBRefEntry[] dbr = jalview.util.DBRefUtils.selectRefs(seq.getDBRef(), new String[]{target}); + // collect id string too + String id = seq.getName(); + if (dbr!=null) + { + for (int r=0;r -1) { // Substitute SEQUENCE_ID string and any matching database reference accessions @@ -174,7 +224,7 @@ public class APopupMenu } } else { addshowLink(linkMenu, target, link.substring(link.lastIndexOf("|")+1)); - } + } */ } if (seq != null) diff --git a/src/jalview/gui/PopupMenu.java b/src/jalview/gui/PopupMenu.java index 546ce65..97f7cba 100755 --- a/src/jalview/gui/PopupMenu.java +++ b/src/jalview/gui/PopupMenu.java @@ -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,56 @@ 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 = new UrlLink(link); + 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/=$", + "URL not valid", + JOptionPane.WARNING_MESSAGE); + return false; + } + + JTextField nameTB = new JTextField(); + JTextField urlTB = new JTextField(); + JLabel jLabel1 = new JLabel(); + JLabel jLabel2 = new JLabel(); + JLabel jLabel3 = new JLabel(); + JPanel jPanel1 = new JPanel(); + GridBagLayout gridBagLayout1 = new GridBagLayout(); + public void nameTB_keyTyped(KeyEvent e) + { + if (e.getKeyChar() == '|') + { + e.consume(); + } + } + + public void urlTB_keyTyped(KeyEvent e) + { + if (e.getKeyChar() == '|' || e.getKeyChar() == ' ') + { + e.consume(); + } + + } +} diff --git a/src/jalview/util/UrlLink.java b/src/jalview/util/UrlLink.java new file mode 100644 index 0000000..21dee38 --- /dev/null +++ b/src/jalview/util/UrlLink.java @@ -0,0 +1,369 @@ +package jalview.util; + +import java.util.Vector; + +public class UrlLink +{ + /** + * helper class to parse URL Link strings taken from applet parameters or + * jalview properties file using the com.stevesoft.pat.Regex implementation. + * Jalview 2.4 extension allows regular expressions to be used to parse ID + * strings and replace the result in the URL. Regex's operate on the whole ID + * string given to the matchURL method, if no regex is supplied, then only + * text following the first pipe symbol will be susbstituted. + */ + private String url_suffix, url_prefix, target, label, regexReplace; + + private boolean dynamic = false; + + private String invalidMessage = null; + + /** + * parse the given linkString of the form '