From 0ccf65bf92f9d299339437b139458c93b8fdaf19 Mon Sep 17 00:00:00 2001 From: kiramt Date: Mon, 14 Nov 2016 07:39:44 +0000 Subject: [PATCH] Url links changes - incomplete checkin as battery low --- src/jalview/gui/Preferences.java | 127 +++++++++++--------- src/jalview/util/CustomUrlProvider.java | 157 +++++++++++++++++++++++++ src/jalview/util/IdentifiersUrlProvider.java | 93 +++++++++++++++ src/jalview/util/UrlProvider.java | 159 ++++++++++++++++++++++++++ src/jalview/util/UrlProviderI.java | 87 ++++++++++++++ 5 files changed, 566 insertions(+), 57 deletions(-) create mode 100644 src/jalview/util/CustomUrlProvider.java create mode 100644 src/jalview/util/IdentifiersUrlProvider.java create mode 100644 src/jalview/util/UrlProvider.java create mode 100644 src/jalview/util/UrlProviderI.java diff --git a/src/jalview/gui/Preferences.java b/src/jalview/gui/Preferences.java index 3bffd3a..718e14f 100755 --- a/src/jalview/gui/Preferences.java +++ b/src/jalview/gui/Preferences.java @@ -20,10 +20,7 @@ */ package jalview.gui; -import static jalview.util.UrlConstants.DB_ACCESSION; import static jalview.util.UrlConstants.EMBLEBI_STRING; -import static jalview.util.UrlConstants.SEQUENCE_ID; -import static jalview.util.UrlConstants.SRS_STRING; import jalview.analysis.AnnotationSorter.SequenceAnnotationOrder; import jalview.bin.Cache; @@ -36,6 +33,8 @@ import jalview.jbgui.GSequenceLink; import jalview.schemes.ColourSchemeProperty; import jalview.util.MessageManager; import jalview.util.Platform; +import jalview.util.UrlProvider; +import jalview.util.UrlProviderI; import jalview.ws.sifts.SiftsSettings; import java.awt.BorderLayout; @@ -48,7 +47,6 @@ import java.awt.event.MouseEvent; import java.io.File; import java.util.ArrayList; import java.util.List; -import java.util.StringTokenizer; import java.util.Vector; import javax.help.HelpSetException; @@ -102,7 +100,8 @@ public class Preferences extends GPreferences * Holds name and link separated with | character. Sequence ID must be * $SEQUENCE_ID$ or $SEQUENCE_ID=/.possible | chars ./=$ */ - public static Vector sequenceURLLinks; + // public static Vector sequenceURLLinks; + public static UrlProviderI sequenceUrlLinks; /** * Holds name and link separated with | character. Sequence IDS and Sequences @@ -116,40 +115,42 @@ public class Preferences extends GPreferences static { String string = Cache.getDefault("SEQUENCE_LINKS", EMBLEBI_STRING); - sequenceURLLinks = new Vector(); + sequenceUrlLinks = new UrlProvider(EMBLEBI_STRING.split("\\|")[0], + string); // TODO sort out the default string + // sequenceURLLinks = new Vector(); - try - { - StringTokenizer st = new StringTokenizer(string, "|"); - while (st.hasMoreElements()) - { - String name = st.nextToken(); - String url = st.nextToken(); - // check for '|' within a regex - int rxstart = url.indexOf("$" + DB_ACCESSION + "$"); - if (rxstart == -1) + /* try + { + StringTokenizer st = new StringTokenizer(string, "|"); + while (st.hasMoreElements()) + { + String name = st.nextToken(); + String url = st.nextToken(); + // check for '|' within a regex + int rxstart = url.indexOf("$" + DB_ACCESSION + "$"); + if (rxstart == -1) + { + rxstart = url.indexOf("$" + SEQUENCE_ID + "$"); + } + while (rxstart == -1 && url.indexOf("/=$") == -1) + { + url = url + "|" + st.nextToken(); + } + sequenceURLLinks.addElement(name + "|" + url); + } + } catch (Exception ex) { - rxstart = url.indexOf("$" + SEQUENCE_ID + "$"); + System.out.println(ex + "\nError parsing sequence links"); } - while (rxstart == -1 && url.indexOf("/=$") == -1) { - url = url + "|" + st.nextToken(); + // upgrade old SRS link + int srsPos = sequenceURLLinks.indexOf(SRS_STRING); + if (srsPos > -1) + { + sequenceURLLinks.setElementAt(EMBLEBI_STRING, srsPos); + } } - sequenceURLLinks.addElement(name + "|" + url); - } - } catch (Exception ex) - { - System.out.println(ex + "\nError parsing sequence links"); - } - { - // upgrade old SRS link - int srsPos = sequenceURLLinks.indexOf(SRS_STRING); - if (srsPos > -1) - { - sequenceURLLinks.setElementAt(EMBLEBI_STRING, srsPos); - } - } - + */ /** * TODO: reformulate groupURL encoding so two or more can be stored in the * .properties file as '|' separated strings @@ -345,13 +346,14 @@ public class Preferences extends GPreferences */ nameLinks = new Vector(); urlLinks = new Vector(); - for (int i = 0; i < sequenceURLLinks.size(); i++) - { - String link = sequenceURLLinks.elementAt(i).toString(); - nameLinks.addElement(link.substring(0, link.indexOf("|"))); - urlLinks.addElement(link.substring(link.indexOf("|") + 1)); - } - + sequenceUrlLinks.getLinksForDisplay(nameLinks, urlLinks); + /* for (HashMap.Entry entry: sequenceUrlLinks.getUrlLinks()) + { + String link = sequenceURLLinks.elementAt(i).toString(); + nameLinks.addElement(link.substring(0, link.indexOf("|"))); + urlLinks.addElement(link.substring(link.indexOf("|") + 1)); + } + */ updateLinkData(); useProxy.setSelected(Cache.getDefault("USE_PROXY", false)); @@ -548,28 +550,39 @@ public class Preferences extends GPreferences jalview.util.BrowserLauncher.resetBrowser(); - if (nameLinks.size() > 0) + String links = sequenceUrlLinks.writeUrlsAsString(); + if (links.isEmpty()) { - StringBuffer links = new StringBuffer(); - sequenceURLLinks = new Vector(); - for (int i = 0; i < nameLinks.size(); i++) - { - sequenceURLLinks.addElement(nameLinks.elementAt(i) + "|" - + urlLinks.elementAt(i)); - links.append(sequenceURLLinks.elementAt(i).toString()); - links.append("|"); - } - // remove last "|" - links.setLength(links.length() - 1); - Cache.applicationProperties.setProperty("SEQUENCE_LINKS", - links.toString()); + Cache.applicationProperties.remove("SEQUENCE_LINKS"); } else { - Cache.applicationProperties.remove("SEQUENCE_LINKS"); - sequenceURLLinks.clear(); + Cache.applicationProperties.setProperty("SEQUENCE_LINKS", + links.toString()); } + /* if (nameLinks.size() > 0) + { + StringBuffer links = new StringBuffer(); + sequenceURLLinks = new Vector(); + for (int i = 0; i < nameLinks.size(); i++) + { + sequenceURLLinks.addElement(nameLinks.elementAt(i) + "|" + + urlLinks.elementAt(i)); + links.append(sequenceURLLinks.elementAt(i).toString()); + links.append("|"); + } + // remove last "|" + links.setLength(links.length() - 1); + Cache.applicationProperties.setProperty("SEQUENCE_LINKS", + links.toString()); + } + else + { + Cache.applicationProperties.remove("SEQUENCE_LINKS"); + sequenceURLLinks.clear(); + }*/ + Cache.applicationProperties.setProperty("USE_PROXY", Boolean.toString(useProxy.isSelected())); diff --git a/src/jalview/util/CustomUrlProvider.java b/src/jalview/util/CustomUrlProvider.java new file mode 100644 index 0000000..f679e36 --- /dev/null +++ b/src/jalview/util/CustomUrlProvider.java @@ -0,0 +1,157 @@ +/* + * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$) + * Copyright (C) $$Year-Rel$$ The Jalview Authors + * + * This file is part of Jalview. + * + * Jalview is free software: you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation, either version 3 + * of the License, or (at your option) any later version. + * + * Jalview is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR + * PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Jalview. If not, see . + * The Jalview Authors are detailed in the 'AUTHORS' file. + */ + +package jalview.util; + +import static jalview.util.UrlConstants.DB_ACCESSION; +import static jalview.util.UrlConstants.EMBLEBI_STRING; +import static jalview.util.UrlConstants.SEQUENCE_ID; +import static jalview.util.UrlConstants.SRS_STRING; + +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; +import java.util.StringTokenizer; +import java.util.Vector; + +/** + * + * Implements the UrlProviderI interface for a UrlProvider object which serves + * custom URLs defined by the user + * + * @author $author$ + * @version $Revision$ + */ +public class CustomUrlProvider implements UrlProviderI +{ + + private HashMap urls; + + public CustomUrlProvider(String cachedUrlList) + { + try + { + StringTokenizer st = new StringTokenizer(cachedUrlList, "|"); + while (st.hasMoreElements()) + { + String name = st.nextToken(); + String url = st.nextToken(); + // check for '|' within a regex + int rxstart = url.indexOf("$" + DB_ACCESSION + "$"); + if (rxstart == -1) + { + rxstart = url.indexOf("$" + SEQUENCE_ID + "$"); + } + while (rxstart == -1 && url.indexOf("/=$") == -1) + { + url = url + "|" + st.nextToken(); + } + urls.put(name, new UrlLink(url)); + // sequenceURLLinks.addElement(name + "|" + url); + } + } catch (Exception ex) + { + System.out.println(ex + "\nError parsing sequence links"); + } + + // upgrade old SRS link + String srs_key = SRS_STRING.split("\\|")[0]; + if (urls.containsKey(srs_key)) + { + urls.remove(srs_key); + urls.put(EMBLEBI_STRING.split("//|")[0], + new UrlLink(EMBLEBI_STRING.split("//|")[1])); + } + } + + @Override + public HashMap getUrlLinks() + { + // TODO Auto-generated method stub + return null; + } + + @Override + public void getLinksForDisplay(Vector nameLinks, + Vector urlLinks) + { + Iterator> it = urls.entrySet().iterator(); + while (it.hasNext()) + { + Map.Entry pair = it.next(); + nameLinks.addElement(pair.getKey()); + urlLinks.addElement(pair.getValue().toString()); // TODO check this is + // right + } + } + + @Override + public String getDefaultUrl() + { + // TODO Auto-generated method stub + return null; + } + + @Override + public void setDefaultUrl(String id) + { + // TODO Auto-generated method stub + + } + + @Override + public String writeUrlsAsString() + { + StringBuffer links = new StringBuffer(); + if (urls.size() > 0) + { + for (UrlLink link : urls.values()) + { + links.append(link.toString()); + links.append("|"); + } + + // remove last "|" + links.setLength(links.length() - 1); + } + else + { + urls.clear(); + } + return links.toString(); + } + + @Override + public void setUrlLinks(String cachedUrl) + { + // TODO Auto-generated method stub, maybe don't need this? watch out for + // superclass interactions + + } + + @Override + public void setUrlLinks(HashMap links) + { + // TODO Auto-generated method stub + + } + +} diff --git a/src/jalview/util/IdentifiersUrlProvider.java b/src/jalview/util/IdentifiersUrlProvider.java new file mode 100644 index 0000000..e13eab5 --- /dev/null +++ b/src/jalview/util/IdentifiersUrlProvider.java @@ -0,0 +1,93 @@ +/* + * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$) + * Copyright (C) $$Year-Rel$$ The Jalview Authors + * + * This file is part of Jalview. + * + * Jalview is free software: you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation, either version 3 + * of the License, or (at your option) any later version. + * + * Jalview is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR + * PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Jalview. If not, see . + * The Jalview Authors are detailed in the 'AUTHORS' file. + */ + +package jalview.util; + +import java.util.HashMap; +import java.util.Vector; + +/** + * + * Implements the UrlProviderI interface for a UrlProvider object which serves + * URLs from identifiers.org + * + * @author $author$ + * @version $Revision$ + */ +public class IdentifiersUrlProvider implements UrlProviderI +{ + + public IdentifiersUrlProvider(String cachedUrlList) + { + + } + + @Override + public HashMap getUrlLinks() + { + // TODO Auto-generated method stub + return null; + } + + @Override + public String getDefaultUrl() + { + // TODO Auto-generated method stub + return null; + } + + @Override + public void setDefaultUrl(String id) + { + // TODO Auto-generated method stub + + } + + @Override + public String writeUrlsAsString() + { + // TODO Auto-generated method stub + return null; + } + + @Override + public void setUrlLinks(String cachedUrl) + { + // TODO Auto-generated method stub + + } + + @Override + public void setUrlLinks(HashMap links) + { + // TODO Auto-generated method stub + + } + + @Override + public void getLinksForDisplay(Vector nameLinks, + Vector urlLinks) + { + // TODO Auto-generated method stub + + } + +} diff --git a/src/jalview/util/UrlProvider.java b/src/jalview/util/UrlProvider.java new file mode 100644 index 0000000..3cd033b --- /dev/null +++ b/src/jalview/util/UrlProvider.java @@ -0,0 +1,159 @@ +/* + * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$) + * Copyright (C) $$Year-Rel$$ The Jalview Authors + * + * This file is part of Jalview. + * + * Jalview is free software: you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation, either version 3 + * of the License, or (at your option) any later version. + * + * Jalview is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR + * PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Jalview. If not, see . + * The Jalview Authors are detailed in the 'AUTHORS' file. + */ +package jalview.util; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Vector; + +/** + * + * Implements the UrlProviderI interface for a composite UrlProvider object + * + * @author $author$ + * @version $Revision$ + */ +public class UrlProvider implements UrlProviderI +{ + // List of actual URL link providers + private List providers; + + // Record of default URL + private String defaultUrl; + + /** + * Construct URL provider from string of cached URLs, and set default URL + * + * @param defaultUrl + * id of the current default URL + * @param cachedUrlList + * string listing current active URLs, expected to be custom URLs + * separated by |, or ids of URLs + */ + public UrlProvider(String defaultUrl, String cachedUrlList) + { + // create all the UrlProviders we need + providers = new ArrayList(); + + UrlProviderI idProvider = new IdentifiersUrlProvider(cachedUrlList); + UrlProviderI customProvider = new CustomUrlProvider(cachedUrlList); + providers.add(idProvider); + providers.add(customProvider); + + // check that the defaultUrl still exists + } + + /** + * Get all the URL links + * @return all URL links + */ + @Override + public HashMap getUrlLinks() + { + HashMap urls = new HashMap(); + for (UrlProviderI p : providers) + { + urls.putAll(p.getUrlLinks()); + } + return urls; + } + + /** + * Get the default URL + * @return id of the default URL + */ + @Override + public String getDefaultUrl() + { + return defaultUrl; + } + + /** + * Set the default URL + * @param id the id of the URL to set as default + */ + @Override + public void setDefaultUrl(String id) + { + defaultUrl = id; + } + + /** + * Write out all URLs as a string suitable for serialising + * @return string representation of available URLs + */ + @Override + public String writeUrlsAsString() + { + String result = ""; + for (UrlProviderI p : providers) + { + result += p.writeUrlsAsString(); + } + return result; + } + + /** + * Set URL links from string + * + * @param cachedUrl + * string representation of URL links, i.e. | separated + */ + @Override + public void setUrlLinks(String cachedUrl) + { + for (UrlProviderI p : providers) + { + p.setUrlLinks(cachedUrl); + } + } + + /** + * Set URL links from collection + * + * @param links + * hashmap containing string indexed UrlLinks + */ + @Override + public void setUrlLinks(HashMap links) + { + for (UrlProviderI p : providers) + { + p.setUrlLinks(links); + } + } + + @Override + public void getLinksForDisplay(Vector nameLinks, + Vector urlLinks) + { + Vector names = new Vector(); + Vector links = new Vector(); + for (UrlProviderI p : providers) + { + p.getLinksForDisplay(names, links); + nameLinks.addAll(names); + urlLinks.addAll(links); + } + } + +} diff --git a/src/jalview/util/UrlProviderI.java b/src/jalview/util/UrlProviderI.java new file mode 100644 index 0000000..2118f7e --- /dev/null +++ b/src/jalview/util/UrlProviderI.java @@ -0,0 +1,87 @@ +/* + * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$) + * Copyright (C) $$Year-Rel$$ The Jalview Authors + * + * This file is part of Jalview. + * + * Jalview is free software: you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation, either version 3 + * of the License, or (at your option) any later version. + * + * Jalview is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR + * PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Jalview. If not, see . + * The Jalview Authors are detailed in the 'AUTHORS' file. + */ +package jalview.util; + +import java.util.HashMap; +import java.util.Vector; + +/** + * Methods for providing consistent access to up-to-date URLs + * + * @author $author$ + * @version $Revision$ + */ +public interface UrlProviderI +{ + /** + * Get all the URL links + * + * @return all URL links + */ + public HashMap getUrlLinks(); + + /** + * Get names and urls in as strings for display + * + * @param nameLinks + * @param urlLinks + */ + public void getLinksForDisplay(Vector nameLinks, + Vector urlLinks); + + /** + * Get the default URL + * + * @return id of the default URL + */ + public String getDefaultUrl(); + + /** + * Set the default URL + * + * @param id + * the id of the URL to set as default + */ + public void setDefaultUrl(String id); + + /** + * Write out all URLs as a string suitable for serialising + * + * @return string representation of available URLs + */ + public String writeUrlsAsString(); + + /** + * Set URL links from string + * + * @param cachedUrl + * string representation of URL links, i.e. | separated + */ + public void setUrlLinks(String cachedUrl); + + /** + * Set URL links from collection + * + * @param links + * hashmap containing string indexed UrlLinks + */ + public void setUrlLinks(HashMap links); +} -- 1.7.10.2