/*
* 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.urls;
import static jalview.util.UrlConstants.SEP;
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;
// List of columns which this provider will supply
private List colNames = new ArrayList();
/**
* 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,
IdentifiersUrlProvider.ID_ORG_FILE);
customProvider = new CustomUrlProvider(cachedUrlList);
providers.add(idProvider);
providers.add(customProvider);
setUpColumns();
// 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(null,
IdentifiersUrlProvider.ID_ORG_FILE);
customProvider = new CustomUrlProvider(urlList);
providers.add(idProvider);
providers.add(customProvider);
setUpColumns();
// check that the defaultUrl still exists
if (!setDefaultUrl(defaultUrlString))
{
chooseDefaultUrl();
}
}
private void setUpColumns()
{
colNames.add("Name");
colNames.add("URL");
colNames.add("In Menu");
colNames.add("Default");
colNames.add("ID");
}
@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();
result += SEP;
}
// remove last sep
result = result.substring(0, result.length() - 1);
return result;
}
@Override
public Vector getLinksForMenu()
{
Vector fullLinks = new Vector();
for (UrlProviderI p : providers)
{
List links = p.getLinksForMenu();
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 List getLinksForTable()
{
ArrayList displayLinks = new ArrayList();
for (UrlProviderI p : providers)
{
displayLinks.addAll(p.getLinksForTable());
}
return displayLinks;
}
@Override
public void setUrlData(List links)
{
for (UrlProviderI p : providers)
{
p.setUrlData(links);
}
}
@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 String chooseDefaultUrl()
{
// choose a custom url default
return customProvider.chooseDefaultUrl();
}
@Override
public boolean isUserEntry(String id)
{
return customProvider.isUserEntry(id);
}
@Override
public List getDisplayColumnNames()
{
return colNames.subList(0, 4);
}
}