JAL-2316 Adjusted unit tests. Tidied UrlLinkProviderI interface.
[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   // List of columns which this provider will supply
46   private List<String> colNames = new ArrayList<String>();
47
48   /**
49    * Construct URL provider from string of cached URLs, and set default URL
50    * 
51    * @param defaultUrlString
52    *          id of the current default URL
53    * @param cachedUrlList
54    *          string listing current active URLs, expected to be custom URLs
55    *          separated by |, or ids of URLs
56    */
57   public UrlProvider(String defaultUrlString, String cachedUrlList)
58   {
59     // create all the UrlProviders we need
60     providers = new ArrayList<UrlProviderI>();
61     
62     UrlProviderI idProvider = new IdentifiersUrlProvider(cachedUrlList,
63             IdentifiersUrlProvider.ID_ORG_FILE);
64     customProvider = new CustomUrlProvider(cachedUrlList);
65     providers.add(idProvider);
66     providers.add(customProvider);
67
68     setUpColumns();
69
70     // check that the defaultUrl still exists
71     if (!setDefaultUrl(defaultUrlString))
72     {
73       chooseDefaultUrl();
74     }
75   }
76   
77   /**
78    * Construct URL provider from a map of (label,url) pairs, and set default URL
79    * 
80    * @param defaultUrlString
81    *          id of the current default URL
82    * @param urlList
83    *          vector of (label, url) pairs
84    */
85   public UrlProvider(String defaultUrlString, Map<String, String> urlList)
86   {
87     // create all the UrlProviders we need
88     providers = new ArrayList<UrlProviderI>();
89
90     UrlProviderI idProvider = new IdentifiersUrlProvider(null,
91             IdentifiersUrlProvider.ID_ORG_FILE);
92     customProvider = new CustomUrlProvider(urlList);
93     providers.add(idProvider);
94     providers.add(customProvider);
95
96     setUpColumns();
97
98     // check that the defaultUrl still exists
99     if (!setDefaultUrl(defaultUrlString))
100     {
101       chooseDefaultUrl();
102     }
103   }
104
105   private void setUpColumns()
106   {
107     colNames.add("Name");
108     colNames.add("URL");
109     colNames.add("In Menu");
110     colNames.add("Default");
111     colNames.add("ID");
112   }
113   
114   @Override
115   public boolean setDefaultUrl(String id)
116   {
117     for (UrlProviderI p : providers)
118     {
119       if (p.setDefaultUrl(id))
120       {
121         return true;
122       }
123     }
124     return false;
125   }
126   
127   @Override
128   public String writeUrlsAsString()
129   {
130     String result = "";
131     for (UrlProviderI p : providers)
132     {
133       result += p.writeUrlsAsString();
134       result += SEP;
135     }
136     // remove last sep
137     result = result.substring(0, result.length() - 1);
138     return result;
139   }
140
141   @Override
142   public Vector<String> getLinksForMenu()
143   {
144     Vector<String> fullLinks = new Vector<String>();
145     for (UrlProviderI p : providers)
146     {
147       List<String> links = p.getLinksForMenu();
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 List<UrlLinkDisplay> getLinksForTable()
160   {
161     ArrayList<UrlLinkDisplay> displayLinks = new ArrayList<UrlLinkDisplay>();
162     for (UrlProviderI p : providers)
163     {
164       displayLinks.addAll(p.getLinksForTable());
165     }
166     return displayLinks;
167   }
168
169   @Override
170   public void setUrlData(List<UrlLinkDisplay> links)
171   {
172     for (UrlProviderI p : providers)
173     {
174       p.setUrlData(links);
175     }
176   }
177
178   @Override
179   public String getDefaultUrl(String seqid)
180   {
181     String link = null;
182     for (UrlProviderI p : providers)
183     {
184       if (p.getDefaultUrl(seqid) == null)
185       {
186         continue;
187       }
188       else
189       {
190         link = p.getDefaultUrl(seqid);
191         break;
192       }
193     }
194     return link;
195   }
196
197   @Override
198   public String getDefaultTarget(String seqid)
199   {
200     String target = null;
201     for (UrlProviderI p : providers)
202     {
203       if (p.getDefaultTarget(seqid) == null)
204       {
205         continue;
206       }
207       else
208       {
209         target = p.getDefaultTarget(seqid);
210         break;
211       }
212     }
213     return target;
214   }
215   
216   @Override
217   public String chooseDefaultUrl()
218   {
219     // choose a custom url default
220     return customProvider.chooseDefaultUrl();
221   }
222
223   @Override
224   public boolean isUserEntry(String id)
225   {
226     return customProvider.isUserEntry(id);
227   }
228
229   @Override
230   public List<String> getDisplayColumnNames()
231   {
232     return colNames.subList(0, 4);
233   }
234 }