JAL-2316 Setting up IdentifiersUrlProvider
[jalview.git] / src / jalview / urls / UrlProvider.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21 package jalview.urls;
22
23 import java.util.ArrayList;
24 import java.util.List;
25 import java.util.Map;
26 import java.util.Vector;
27
28 /**
29  * 
30  * Implements the UrlProviderI interface for a composite UrlProvider object
31  * 
32  * @author $author$
33  * @version $Revision$
34  */
35 public class UrlProvider implements UrlProviderI
36 {
37   // List of actual URL link providers
38   private List<UrlProviderI> providers;
39
40   // Specific reference to custom URL link provider
41   private UrlProviderI customProvider;
42
43   /**
44    * Construct URL provider from string of cached URLs, and set default URL
45    * 
46    * @param defaultUrlString
47    *          id of the current default URL
48    * @param cachedUrlList
49    *          string listing current active URLs, expected to be custom URLs
50    *          separated by |, or ids of URLs
51    */
52   public UrlProvider(String defaultUrlString, String cachedUrlList)
53   {
54     // create all the UrlProviders we need
55     providers = new ArrayList<UrlProviderI>();
56     
57     UrlProviderI idProvider = new IdentifiersUrlProvider(cachedUrlList,
58             ID_ORG_FILE);
59     customProvider = new CustomUrlProvider(cachedUrlList);
60     providers.add(idProvider);
61     providers.add(customProvider);
62
63     // check that the defaultUrl still exists
64     if (!setDefaultUrl(defaultUrlString))
65     {
66       chooseDefaultUrl();
67     }
68   }
69   
70   /**
71    * Construct URL provider from a map of (label,url) pairs, and set default URL
72    * 
73    * @param defaultUrlString
74    *          id of the current default URL
75    * @param urlList
76    *          vector of (label, url) pairs
77    */
78   public UrlProvider(String defaultUrlString, Map<String, String> urlList)
79   {
80     // create all the UrlProviders we need
81     providers = new ArrayList<UrlProviderI>();
82
83     UrlProviderI idProvider = new IdentifiersUrlProvider(urlList,
84             ID_ORG_FILE);
85     customProvider = new CustomUrlProvider(urlList);
86     providers.add(idProvider);
87     providers.add(customProvider);
88
89     // check that the defaultUrl still exists
90     if (!setDefaultUrl(defaultUrlString))
91     {
92       chooseDefaultUrl();
93     }
94   }
95
96   @Override
97   public String getDefaultUrl()
98   {
99     String defaultUrl = null;
100     for (UrlProviderI p : providers)
101     {
102       defaultUrl = p.getDefaultUrl();
103       if (defaultUrl != null)
104       {
105         return defaultUrl;
106       }
107     }
108
109     // no provider has a default set, just choose one
110     if (defaultUrl == null)
111     {
112       defaultUrl = chooseDefaultUrl();
113     }
114     return defaultUrl;
115   }
116   
117   @Override
118   public boolean setDefaultUrl(String id)
119   {
120     for (UrlProviderI p : providers)
121     {
122       if (p.setDefaultUrl(id))
123       {
124         return true;
125       }
126     }
127     return false;
128   }
129   
130   @Override
131   public String writeUrlsAsString()
132   {
133     String result = "";
134     for (UrlProviderI p : providers)
135     {
136       result += p.writeUrlsAsString();
137     }
138     return result;
139   }
140
141   @Override
142   public Vector<String> getLinksForDisplay()
143   {
144     Vector<String> fullLinks = new Vector<String>();
145     for (UrlProviderI p : providers)
146     {
147       List<String> links = p.getLinksForDisplay();
148       if (links != null)
149       {
150         // will obliterate links with same keys from different providers
151         // must have checks in place to prevent user from duplicating ids
152         fullLinks.addAll(links);
153       }
154     }
155     return fullLinks;
156   }
157
158   @Override
159   public String getDefaultUrl(String seqid)
160   {
161     String link = null;
162     for (UrlProviderI p : providers)
163     {
164       if (p.getDefaultUrl(seqid) == null)
165       {
166         continue;
167       }
168       else
169       {
170         link = p.getDefaultUrl(seqid);
171         break;
172       }
173     }
174     return link;
175   }
176
177   @Override
178   public String getDefaultTarget(String seqid)
179   {
180     String target = null;
181     for (UrlProviderI p : providers)
182     {
183       if (p.getDefaultTarget(seqid) == null)
184       {
185         continue;
186       }
187       else
188       {
189         target = p.getDefaultTarget(seqid);
190         break;
191       }
192     }
193     return target;
194   }
195
196   @Override
197   public void setUrlLinks(Vector<String> names, Vector<String> urls)
198   {
199     // only allow custom urls to be updated by user
200     customProvider.setUrlLinks(names, urls);
201   }
202   
203   @Override
204   public String chooseDefaultUrl()
205   {
206     // choose a custom url default
207     return customProvider.chooseDefaultUrl();
208   }
209
210 }