JAL-2316 Refactoring of functionality to provide urls to gui
[jalview.git] / src / jalview / util / 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.util;
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     customProvider = new CustomUrlProvider(cachedUrlList);
59     providers.add(idProvider);
60     providers.add(customProvider);
61
62     // check that the defaultUrl still exists
63     if (!setDefaultUrl(defaultUrlString))
64     {
65       chooseDefaultUrl();
66     }
67   }
68   
69   /**
70    * Construct URL provider from a map of (label,url) pairs, and set default URL
71    * 
72    * @param defaultUrlString
73    *          id of the current default URL
74    * @param urlList
75    *          vector of (label, url) pairs
76    */
77   public UrlProvider(String defaultUrlString, Map<String, String> urlList)
78   {
79     // create all the UrlProviders we need
80     providers = new ArrayList<UrlProviderI>();
81
82     UrlProviderI idProvider = new IdentifiersUrlProvider(urlList);
83     customProvider = new CustomUrlProvider(urlList);
84     providers.add(idProvider);
85     providers.add(customProvider);
86
87     // check that the defaultUrl still exists
88     if (!setDefaultUrl(defaultUrlString))
89     {
90       chooseDefaultUrl();
91     }
92   }
93
94   @Override
95   public String getDefaultUrl()
96   {
97     String defaultUrl = null;
98     for (UrlProviderI p : providers)
99     {
100       defaultUrl = p.getDefaultUrl();
101       if (defaultUrl != null)
102       {
103         return defaultUrl;
104       }
105     }
106
107     // no provider has a default set, just choose one
108     if (defaultUrl == null)
109     {
110       defaultUrl = chooseDefaultUrl();
111     }
112     return defaultUrl;
113   }
114   
115   @Override
116   public boolean setDefaultUrl(String id)
117   {
118     for (UrlProviderI p : providers)
119     {
120       if (p.setDefaultUrl(id))
121       {
122         return true;
123       }
124     }
125     return false;
126   }
127   
128   @Override
129   public String writeUrlsAsString()
130   {
131     String result = "";
132     for (UrlProviderI p : providers)
133     {
134       result += p.writeUrlsAsString();
135     }
136     return result;
137   }
138
139   @Override
140   public Vector<String> getLinksForDisplay()
141   {
142     Vector<String> fullLinks = new Vector<String>();
143     for (UrlProviderI p : providers)
144     {
145       List<String> links = p.getLinksForDisplay();
146       if (links != null)
147       {
148         // will obliterate links with same keys from different providers
149         // must have checks in place to prevent user from duplicating ids
150         fullLinks.addAll(links);
151       }
152     }
153     return fullLinks;
154   }
155
156   @Override
157   public String getDefaultUrl(String seqid)
158   {
159     String link = null;
160     for (UrlProviderI p : providers)
161     {
162       if (p.getDefaultUrl(seqid) == null)
163       {
164         continue;
165       }
166       else
167       {
168         link = p.getDefaultUrl(seqid);
169         break;
170       }
171     }
172     return link;
173   }
174
175   @Override
176   public String getDefaultTarget(String seqid)
177   {
178     String target = null;
179     for (UrlProviderI p : providers)
180     {
181       if (p.getDefaultTarget(seqid) == null)
182       {
183         continue;
184       }
185       else
186       {
187         target = p.getDefaultTarget(seqid);
188         break;
189       }
190     }
191     return target;
192   }
193
194   @Override
195   public void setUrlLinks(Vector<String> names, Vector<String> urls)
196   {
197     // only allow custom urls to be updated by user
198     customProvider.setUrlLinks(names, urls);
199   }
200   
201   @Override
202   public String chooseDefaultUrl()
203   {
204     // choose a custom url default
205     return customProvider.chooseDefaultUrl();
206   }
207
208 }