package jalview.urls;
-//import static jalview.util.UrlConstants.EMBLEBI_LABEL;
-//import static jalview.util.UrlConstants.EMBLEBI_STRING;
-//import static jalview.util.UrlConstants.SRS_LABEL;
-
-
import static jalview.util.UrlConstants.DB_ACCESSION;
import static jalview.util.UrlConstants.DELIM;
import static jalview.util.UrlConstants.SEP;
import static jalview.util.UrlConstants.SEQUENCE_ID;
import jalview.util.MessageManager;
+import jalview.util.UrlConstants;
import jalview.util.UrlLink;
+import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
+import java.util.List;
import java.util.Map;
+import java.util.Map.Entry;
import java.util.StringTokenizer;
-import java.util.Vector;
/**
*
*/
public class CustomUrlProvider extends UrlProviderImpl
{
-
- // minimum length of substitution in url link string
- private static final int MIN_SUBST_LENGTH = 4;
-
// Default sequence URL link label for SRS
private static final String SRS_LABEL = "SRS";
- // map of string ids to urlLinks
- private HashMap<String, UrlLink> urls;
+ // map of string ids to urlLinks (selected)
+ private HashMap<String, UrlLink> selectedUrls;
+
+ // map of string ids to urlLinks (not selected)
+ private HashMap<String, UrlLink> nonselectedUrls;
+
+ /**
+ * Construct UrlProvider for custom (user-entered) URLs
+ *
+ * @param inMenuUrlList
+ * list of URLs set to be displayed in menu, in form stored in Cache.
+ * i.e. SEP delimited string
+ * @param storedUrlList
+ * list of custom URLs entered by user but not currently displayed in
+ * menu, in form stored in Cache
+ */
+ public CustomUrlProvider(String inMenuUrlList, String storedUrlList)
+ {
+ try
+ {
+ selectedUrls = parseUrlStrings(inMenuUrlList);
+ nonselectedUrls = parseUrlStrings(storedUrlList);
+ } catch (Exception ex)
+ {
+ System.out
+ .println(ex.getMessage() + "\nError parsing sequence links");
+ }
+ }
/**
* Construct UrlProvider for custom (user-entered) URLs
*
- * @param cachedUrlList
- * list of URLs in form stored in Cache. i.e. SEP delimited string
+ * @param urlList
+ * list of URLs to be displayed in menu, as (label,url) pairs
+ * @param storedUrlList
+ * list of custom URLs entered by user but not currently displayed in
+ * menu, as (label,url) pairs
*/
- public CustomUrlProvider(String cachedUrlList)
+ public CustomUrlProvider(Map<String, String> inMenuUrlList,
+ Map<String, String> storedUrlList)
{
try
{
- urls = new HashMap<String, UrlLink>();
+ selectedUrls = parseUrlList(inMenuUrlList);
+ nonselectedUrls = parseUrlList(storedUrlList);
+ } catch (Exception ex)
+ {
+ System.out
+ .println(ex.getMessage() + "\nError parsing sequence links");
+ }
+ }
+
+ private HashMap<String, UrlLink> parseUrlStrings(String urlStrings)
+ {
+ // cachedUrlList is in form <label>|<url>|<label>|<url>...
+ // parse cachedUrlList into labels (used as id) and url links
+ HashMap<String, UrlLink> urls = new HashMap<>();
+
+ StringTokenizer st = new StringTokenizer(urlStrings, SEP);
+ while (st.hasMoreElements())
+ {
+ String name = st.nextToken().trim();
- // cachedUrlList is in form <label>|<url>|<label>|<url>...
- // parse cachedUrlList into labels (used as id) and url links
- StringTokenizer st = new StringTokenizer(cachedUrlList, SEP);
- while (st.hasMoreElements())
+ if (!isMiriamId(name))
{
- String name = st.nextToken();
+ // this one of our custom urls
String url = st.nextToken();
// check for '|' within a regex
int rxstart = url.indexOf(DELIM + DB_ACCESSION + DELIM);
{
rxstart = url.indexOf(DELIM + SEQUENCE_ID + DELIM);
}
- while (rxstart == -1 && url.indexOf("/=" + DELIM) == -1)
+ while (rxstart == -1 && url.indexOf("/=" + DELIM) == -1
+ && st.hasMoreTokens())
{
url = url + SEP + st.nextToken();
}
- urls.put(name, new UrlLink(name + SEP + url));
+ url = url.trim();
+ urls.put(name, new UrlLink(name, url, name));
}
- } catch (Exception ex)
- {
- System.out.println(ex + "\nError parsing sequence links");
}
- upgradeOldLinks();
-
+ upgradeOldLinks(urls);
+ return urls;
}
- /**
- * Construct UrlProvider for custom (user-entered) URLs
- *
- * @param urlList
- * list of URLs as (label,url) pairs
- */
- public CustomUrlProvider(Map<String, String> urlList)
+ private HashMap<String, UrlLink> parseUrlList(Map<String, String> urlList)
{
- try
+ HashMap<String, UrlLink> urls = new HashMap<>();
+ if (urlList == null)
{
- urls = new HashMap<String, UrlLink>();
- Iterator<Map.Entry<String, String>> it = urlList.entrySet()
- .iterator();
- while (it.hasNext())
- {
- Map.Entry<String, String> pair = it.next();
- urls.put(pair.getKey(),
- new UrlLink(pair.getKey() + SEP + pair.getValue()));
- }
- } catch (Exception ex)
+ return urls;
+ }
+
+ Iterator<Map.Entry<String, String>> it = urlList.entrySet().iterator();
+ while (it.hasNext())
{
- System.out.println(ex + "\nError parsing sequence links");
+ Map.Entry<String, String> pair = it.next();
+ urls.put(pair.getKey(),
+ new UrlLink(pair.getKey(), pair.getValue(), pair.getKey()));
}
- upgradeOldLinks();
+ upgradeOldLinks(urls);
+ return urls;
}
/*
* Upgrade any legacy links which may have been left lying around
*/
- private void upgradeOldLinks()
+ private void upgradeOldLinks(HashMap<String, UrlLink> urls)
{
+ boolean upgrade = false;
// upgrade old SRS link
if (urls.containsKey(SRS_LABEL))
{
urls.remove(SRS_LABEL);
- urls.put(DEFAULT_LABEL, new UrlLink(DEFAULT_STRING));
+ upgrade = true;
+ }
+ // upgrade old EBI link - easier just to remove and re-add than faffing
+ // around checking exact url
+ if (urls.containsKey(UrlConstants.DEFAULT_LABEL))
+ {
+ // note because this is called separately for selected and nonselected
+ // urls, the default url will not always be present
+ urls.remove(UrlConstants.DEFAULT_LABEL);
+ upgrade = true;
+ }
+ if (upgrade)
+ {
+ UrlLink link = new UrlLink(UrlConstants.DEFAULT_STRING);
+ link.setLabel(UrlConstants.DEFAULT_LABEL);
+ urls.put(UrlConstants.DEFAULT_LABEL, link);
}
}
@Override
- public Vector<String> getLinksForDisplay()
+ public List<String> getLinksForMenu()
{
- Vector<String> links = new Vector<String>();
- Iterator<Map.Entry<String, UrlLink>> it = urls.entrySet().iterator();
+ List<String> links = new ArrayList<>();
+ Iterator<Map.Entry<String, UrlLink>> it = selectedUrls.entrySet()
+ .iterator();
while (it.hasNext())
{
Map.Entry<String, UrlLink> pair = it.next();
}
@Override
- public boolean setDefaultUrl(String id)
+ public List<UrlLinkDisplay> getLinksForTable()
+ {
+ ArrayList<UrlLinkDisplay> displayLinks = new ArrayList<>();
+ displayLinks = getLinksForTable(selectedUrls, true);
+ displayLinks.addAll(getLinksForTable(nonselectedUrls, false));
+ return displayLinks;
+ }
+
+ private ArrayList<UrlLinkDisplay> getLinksForTable(
+ HashMap<String, UrlLink> urlList, boolean selected)
{
- if (urls.containsKey(id))
+ return super.getLinksForTable(urlList, null, selected);
+ }
+
+ @Override
+ public boolean setPrimaryUrl(String id)
+ {
+ if (selectedUrls.containsKey(id))
{
- defaultUrl = id;
+ primaryUrl = id;
+ }
+ else if (nonselectedUrls.containsKey(id))
+ {
+ primaryUrl = id;
}
else
{
- defaultUrl = null;
+ primaryUrl = null;
}
- return urls.containsKey(id);
+
+ return (primaryUrl != null);
}
@Override
- public String writeUrlsAsString()
+ public String writeUrlsAsString(boolean selected)
{
StringBuffer links = new StringBuffer();
+ HashMap<String, UrlLink> urls;
+ if (selected)
+ {
+ urls = selectedUrls;
+ }
+ else
+ {
+ urls = nonselectedUrls;
+ }
if (urls.size() > 0)
{
- for (UrlLink link : urls.values())
+ for (Entry<String, UrlLink> entry : urls.entrySet())
{
- links.append(link.toString());
+ links.append(entry.getValue().toString());
links.append(SEP);
}
}
@Override
- public String getDefaultUrl(String seqid)
+ public String getPrimaryUrl(String seqid)
{
- if (defaultUrl == null)
- {
- return null;
- }
-
- String url = null;
- UrlLink urlLink = urls.get(defaultUrl);
- String[] defaultUrls = urlLink.makeUrls(seqid, true);
- if (defaultUrls == null || defaultUrls[0] == null
- || defaultUrls[0].length() < MIN_SUBST_LENGTH)
- {
- url = null;
- }
- else
+ String result = super.getPrimaryUrl(seqid, selectedUrls);
+ if (result == null)
{
- // just take first URL made from regex
- url = defaultUrls[1];
+ result = super.getPrimaryUrl(seqid, nonselectedUrls);
}
- return url;
+ return result;
}
@Override
- public String getDefaultTarget(String seqid)
+ public String getPrimaryUrlId()
{
- return urls.get(defaultUrl).getTarget();
+ return primaryUrl;
}
@Override
- public void setUrlLinks(Vector<String> names, Vector<String> urlstrings)
+ public String getPrimaryTarget(String seqid)
{
- HashMap<String, UrlLink> newurls = new HashMap<String, UrlLink>();
+ return selectedUrls.get(primaryUrl).getTarget();
+ }
- // should check that lists are same length but this function is likely
- // to change once the Preferences dialog is rebuilt
+ @Override
+ public void setUrlData(List<UrlLinkDisplay> links)
+ {
+ HashMap<String, UrlLink> unselurls = new HashMap<>();
+ HashMap<String, UrlLink> selurls = new HashMap<>();
- for (int i = 0; i < names.size(); ++i)
+ Iterator<UrlLinkDisplay> it = links.iterator();
+ while (it.hasNext())
{
- // don't allow MIRIAM ids as custom url names (as the links will overwrite
- // each other)
- // unlikely user would try to do this, but...
- if (isMiriamId(names.elementAt(i)))
- {
- throw new IllegalArgumentException(MessageManager.formatMessage(
- "exception.url_cannot_have_miriam_id", names.elementAt(i)));
- }
- // don't allow duplicate key names as entries will be overwritten
- if (newurls.containsKey(names.elementAt(i)))
+ UrlLinkDisplay link = it.next();
+
+ // MIRIAM ids will be handled by a different UrlProvider class
+ if (!isMiriamId(link.getId()))
{
- throw new IllegalArgumentException(MessageManager.formatMessage(
- "exception.url_cannot_have_duplicate_id",
- names.elementAt(i)));
+ // don't allow duplicate key names as entries will be overwritten
+ if (unselurls.containsKey(link.getId())
+ || selurls.containsKey(link.getId()))
+ {
+ throw new IllegalArgumentException(MessageManager.formatMessage(
+ "exception.url_cannot_have_duplicate_id", link.getId()));
+ }
+ if (link.getIsSelected())
+ {
+ selurls.put(link.getId(), new UrlLink(link.getDescription(),
+ link.getUrl(), link.getDescription()));
+ }
+ else
+ {
+ unselurls.put(link.getId(), new UrlLink(link.getDescription(),
+ link.getUrl(), link.getDescription()));
+ }
+ // sort out primary and selected ids
+ if (link.getIsPrimary())
+ {
+ setPrimaryUrl(link.getId());
+ }
}
- newurls.put(names.elementAt(i), new UrlLink(names.elementAt(i) + SEP
- + urlstrings.elementAt(i)));
- }
-
- // don't update until we're sure this set is ok
- urls = newurls;
+ }
+ nonselectedUrls = unselurls;
+ selectedUrls = selurls;
}
@Override
- public String chooseDefaultUrl()
+ public String choosePrimaryUrl()
{
- // unilaterally set the default id to the EMBL_EBI link
-
- if (!urls.containsKey(DEFAULT_LABEL))
+ // unilaterally set the primary id to the EMBL_EBI link
+ if ((!nonselectedUrls.containsKey(UrlConstants.DEFAULT_LABEL))
+ && (!selectedUrls.containsKey(UrlConstants.DEFAULT_LABEL)))
{
- urls.put(DEFAULT_LABEL, new UrlLink(DEFAULT_STRING));
+ UrlLink link = new UrlLink(UrlConstants.DEFAULT_STRING);
+ link.setLabel(UrlConstants.DEFAULT_LABEL);
+ selectedUrls.put(UrlConstants.DEFAULT_LABEL, link);
}
- return DEFAULT_LABEL;
+ primaryUrl = UrlConstants.DEFAULT_LABEL;
+ return UrlConstants.DEFAULT_LABEL;
+ }
+
+ @Override
+ public boolean contains(String id)
+ {
+ return (selectedUrls.containsKey(id)
+ || nonselectedUrls.containsKey(id));
}
}