37e95e2ff8fa7c2bc0f27550912b43cfafefbaf2
[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 static jalview.util.UrlConstants.SEP;
24
25 import java.util.ArrayList;
26 import java.util.List;
27 import java.util.Map;
28 import java.util.Vector;
29
30 /**
31  * 
32  * Implements the UrlProviderI interface for a composite UrlProvider object
33  * 
34  * @author $author$
35  * @version $Revision$
36  */
37 public class UrlProvider implements UrlProviderI
38 {
39   // List of actual URL link providers
40   private List<UrlProviderI> providers;
41
42   // Specific reference to custom URL link provider
43   private UrlProviderI customProvider;
44
45   /**
46    * Construct URL provider from string of cached URLs, and set default URL
47    * 
48    * @param defaultUrlString
49    *          id of the current default URL
50    * @param cachedUrlList
51    *          string listing current active URLs, expected to be custom URLs
52    *          separated by |, or ids of URLs
53    */
54   public UrlProvider(String defaultUrlString, String cachedUrlList)
55   {
56     // create all the UrlProviders we need
57     providers = new ArrayList<UrlProviderI>();
58     
59     UrlProviderI idProvider = new IdentifiersUrlProvider(cachedUrlList,
60             ID_ORG_FILE);
61     customProvider = new CustomUrlProvider(cachedUrlList);
62     providers.add(idProvider);
63     providers.add(customProvider);
64
65     // check that the defaultUrl still exists
66     if (!setDefaultUrl(defaultUrlString))
67     {
68       chooseDefaultUrl();
69     }
70   }
71   
72   /**
73    * Construct URL provider from a map of (label,url) pairs, and set default URL
74    * 
75    * @param defaultUrlString
76    *          id of the current default URL
77    * @param urlList
78    *          vector of (label, url) pairs
79    */
80   public UrlProvider(String defaultUrlString, Map<String, String> urlList)
81   {
82     // create all the UrlProviders we need
83     providers = new ArrayList<UrlProviderI>();
84
85     UrlProviderI idProvider = new IdentifiersUrlProvider(null,
86             ID_ORG_FILE);
87     customProvider = new CustomUrlProvider(urlList);
88     providers.add(idProvider);
89     providers.add(customProvider);
90
91     // check that the defaultUrl still exists
92     if (!setDefaultUrl(defaultUrlString))
93     {
94       chooseDefaultUrl();
95     }
96   }
97
98   @Override
99   public String getDefaultUrl()
100   {
101     String defaultUrl = null;
102     for (UrlProviderI p : providers)
103     {
104       defaultUrl = p.getDefaultUrl();
105       if (defaultUrl != null)
106       {
107         return defaultUrl;
108       }
109     }
110
111     // no provider has a default set, just choose one
112     if (defaultUrl == null)
113     {
114       defaultUrl = chooseDefaultUrl();
115     }
116     return defaultUrl;
117   }
118   
119   @Override
120   public boolean setDefaultUrl(String id)
121   {
122     for (UrlProviderI p : providers)
123     {
124       if (p.setDefaultUrl(id))
125       {
126         return true;
127       }
128     }
129     return false;
130   }
131   
132   @Override
133   public String writeUrlsAsString()
134   {
135     String result = "";
136     for (UrlProviderI p : providers)
137     {
138       result += p.writeUrlsAsString();
139       result += SEP;
140     }
141     // remove last sep
142     result = result.substring(0, result.length() - 1);
143     return result;
144   }
145
146   @Override
147   public Vector<String> getLinksForDisplay()
148   {
149     Vector<String> fullLinks = new Vector<String>();
150     for (UrlProviderI p : providers)
151     {
152       List<String> links = p.getLinksForDisplay();
153       if (links != null)
154       {
155         // will obliterate links with same keys from different providers
156         // must have checks in place to prevent user from duplicating ids
157         fullLinks.addAll(links);
158       }
159     }
160     return fullLinks;
161   }
162
163   @Override
164   public List<UrlLinkDisplay> getLinksForTable()
165   {
166     ArrayList<UrlLinkDisplay> displayLinks = new ArrayList<UrlLinkDisplay>();
167     for (UrlProviderI p : providers)
168     {
169       displayLinks.addAll(p.getLinksForTable());
170     }
171     return displayLinks;
172   }
173
174   @Override
175   public void setUrlData(List<UrlLinkDisplay> links)
176   {
177     for (UrlProviderI p : providers)
178     {
179       p.setUrlData(links);
180     }
181   }
182
183   @Override
184   public String getDefaultUrl(String seqid)
185   {
186     String link = null;
187     for (UrlProviderI p : providers)
188     {
189       if (p.getDefaultUrl(seqid) == null)
190       {
191         continue;
192       }
193       else
194       {
195         link = p.getDefaultUrl(seqid);
196         break;
197       }
198     }
199     return link;
200   }
201
202   @Override
203   public String getDefaultTarget(String seqid)
204   {
205     String target = null;
206     for (UrlProviderI p : providers)
207     {
208       if (p.getDefaultTarget(seqid) == null)
209       {
210         continue;
211       }
212       else
213       {
214         target = p.getDefaultTarget(seqid);
215         break;
216       }
217     }
218     return target;
219   }
220
221   @Override
222   public void setUrlLinks(Vector<String> names, Vector<String> urls)
223   {
224     // only allow custom urls to be updated by user
225     customProvider.setUrlLinks(names, urls);
226   }
227   
228   @Override
229   public String chooseDefaultUrl()
230   {
231     // choose a custom url default
232     return customProvider.chooseDefaultUrl();
233   }
234
235   @Override
236   public boolean isUserEntry(String id)
237   {
238     return customProvider.isUserEntry(id);
239   }
240 }