/*
* 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.List;
import java.util.Map;
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;
// Specific reference to custom URL link provider
private UrlProviderI customProvider;
/**
* Construct URL provider from string of cached URLs, and set default URL
*
* @param defaultUrlString
* 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 defaultUrlString, String cachedUrlList)
{
// create all the UrlProviders we need
providers = new ArrayList();
UrlProviderI idProvider = new IdentifiersUrlProvider(cachedUrlList);
customProvider = new CustomUrlProvider(cachedUrlList);
providers.add(idProvider);
providers.add(customProvider);
// check that the defaultUrl still exists
if (!setDefaultUrl(defaultUrlString))
{
chooseDefaultUrl();
}
}
/**
* Construct URL provider from a map of (label,url) pairs, and set default URL
*
* @param defaultUrlString
* id of the current default URL
* @param urlList
* vector of (label, url) pairs
*/
public UrlProvider(String defaultUrlString, Map urlList)
{
// create all the UrlProviders we need
providers = new ArrayList();
UrlProviderI idProvider = new IdentifiersUrlProvider(urlList);
customProvider = new CustomUrlProvider(urlList);
providers.add(idProvider);
providers.add(customProvider);
// check that the defaultUrl still exists
if (!setDefaultUrl(defaultUrlString))
{
chooseDefaultUrl();
}
}
@Override
public String getDefaultUrl()
{
String defaultUrl = null;
for (UrlProviderI p : providers)
{
defaultUrl = p.getDefaultUrl();
if (defaultUrl != null)
{
return defaultUrl;
}
}
// no provider has a default set, just choose one
if (defaultUrl == null)
{
defaultUrl = chooseDefaultUrl();
}
return defaultUrl;
}
@Override
public boolean setDefaultUrl(String id)
{
for (UrlProviderI p : providers)
{
if (p.setDefaultUrl(id))
{
return true;
}
}
return false;
}
@Override
public String writeUrlsAsString()
{
String result = "";
for (UrlProviderI p : providers)
{
result += p.writeUrlsAsString();
}
return result;
}
@Override
public Vector getLinksForDisplay()
{
Vector fullLinks = new Vector();
for (UrlProviderI p : providers)
{
List links = p.getLinksForDisplay();
if (links != null)
{
// will obliterate links with same keys from different providers
// must have checks in place to prevent user from duplicating ids
fullLinks.addAll(links);
}
}
return fullLinks;
}
@Override
public String getDefaultUrl(String seqid)
{
String link = null;
for (UrlProviderI p : providers)
{
if (p.getDefaultUrl(seqid) == null)
{
continue;
}
else
{
link = p.getDefaultUrl(seqid);
break;
}
}
return link;
}
@Override
public String getDefaultTarget(String seqid)
{
String target = null;
for (UrlProviderI p : providers)
{
if (p.getDefaultTarget(seqid) == null)
{
continue;
}
else
{
target = p.getDefaultTarget(seqid);
break;
}
}
return target;
}
@Override
public void setUrlLinks(Vector names, Vector urls)
{
// only allow custom urls to be updated by user
customProvider.setUrlLinks(names, urls);
}
@Override
public String chooseDefaultUrl()
{
// choose a custom url default
return customProvider.chooseDefaultUrl();
}
}