JAL-2316 More unit testing and adjustments
[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.Vector;
28
29 /**
30  * 
31  * Implements the UrlProviderI interface for a composite UrlProvider object
32  * 
33  * @author $author$
34  * @version $Revision$
35  */
36 public class UrlProvider implements UrlProviderI
37 {
38   // List of actual URL link providers
39   private List<UrlProviderI> providers;
40
41   // Specific reference to custom URL link provider
42   private UrlProviderI customProvider;
43
44   /**
45    * Constructor for UrlProvider composite
46    * 
47    * @param defaultUrlString
48    *          id of default url
49    * @param allProviders
50    *          list of UrlProviders this provider gives access to
51    */
52   public UrlProvider(String defaultUrlString,
53           List<UrlProviderI> allProviders)
54   {
55     providers = allProviders;
56
57     customProvider = findCustomProvider();
58
59     // check that the defaultUrl still exists
60     if (!contains(defaultUrlString))
61     {
62       // if the defaultUrl can't be found in any of the providers
63       // set up a custom default url
64       chooseDefaultUrl();
65     }
66     else
67     {
68       setDefaultUrl(defaultUrlString);
69     }
70   }
71
72   /*
73    * Store ref to custom url provider
74    */
75   private UrlProviderI findCustomProvider()
76   {
77     for (UrlProviderI p : providers)
78     {
79       if (p.getClass().equals(CustomUrlProvider.class))
80       {
81         return p;
82       }
83     }
84
85     System.out
86             .println("Error initialising UrlProvider - no custom url provider");
87     return null;
88   }
89   
90   @Override
91   public boolean setDefaultUrl(String id)
92   {
93     boolean outcome = false;
94     for (UrlProviderI p : providers)
95     {
96       if (p.setDefaultUrl(id))
97       {
98         outcome = true;
99       }
100     }
101     if (!outcome)
102     {
103       throw new IllegalArgumentException();
104     }
105     return outcome;
106   }
107
108   @Override
109   public boolean contains(String id)
110   {
111     boolean outcome = false;
112     for (UrlProviderI p : providers)
113     {
114       if (p.contains(id))
115       {
116         outcome = true;
117       }
118     }
119     return outcome;
120   }
121   
122   @Override
123   public String writeUrlsAsString(boolean selected)
124   {
125     String result = "";
126     for (UrlProviderI p : providers)
127     {
128       String next = p.writeUrlsAsString(selected);
129       if (!next.isEmpty())
130       {
131         result += next;
132         result += SEP;
133       }
134     }
135     // remove last sep
136     if (!result.isEmpty())
137     {
138       result = result.substring(0, result.length() - 1);
139     }
140     return result;
141   }
142
143   @Override
144   public Vector<String> getLinksForMenu()
145   {
146     Vector<String> fullLinks = new Vector<String>();
147     for (UrlProviderI p : providers)
148     {
149       List<String> links = p.getLinksForMenu();
150       if (links != null)
151       {
152         // will obliterate links with same keys from different providers
153         // must have checks in place to prevent user from duplicating ids
154         fullLinks.addAll(links);
155       }
156     }
157     return fullLinks;
158   }
159
160   @Override
161   public List<UrlLinkDisplay> getLinksForTable()
162   {
163     ArrayList<UrlLinkDisplay> displayLinks = new ArrayList<UrlLinkDisplay>();
164     for (UrlProviderI p : providers)
165     {
166       displayLinks.addAll(p.getLinksForTable());
167     }
168     return displayLinks;
169   }
170
171   @Override
172   public void setUrlData(List<UrlLinkDisplay> links)
173   {
174     for (UrlProviderI p : providers)
175     {
176       p.setUrlData(links);
177     }
178   }
179
180   @Override
181   public String getDefaultUrl(String seqid)
182   {
183     String link = null;
184     for (UrlProviderI p : providers)
185     {
186       if (p.getDefaultUrl(seqid) == null)
187       {
188         continue;
189       }
190       else
191       {
192         link = p.getDefaultUrl(seqid);
193         break;
194       }
195     }
196     return link;
197   }
198
199   @Override
200   public String getDefaultUrlId()
201   {
202     String id = null;
203     for (UrlProviderI p : providers)
204     {
205       if (p.getDefaultUrlId() == null)
206       {
207         continue;
208       }
209       else
210       {
211         id = p.getDefaultUrlId();
212         break;
213       }
214     }
215     return id;
216   }
217
218   @Override
219   public String getDefaultTarget(String seqid)
220   {
221     String target = null;
222     for (UrlProviderI p : providers)
223     {
224       if (p.getDefaultTarget(seqid) == null)
225       {
226         continue;
227       }
228       else
229       {
230         target = p.getDefaultTarget(seqid);
231         break;
232       }
233     }
234     return target;
235   }
236   
237   @Override
238   public String chooseDefaultUrl()
239   {
240     // choose a custom url default
241     return customProvider.chooseDefaultUrl();
242   }
243
244   @Override
245   public boolean isUserEntry(String id)
246   {
247     return customProvider.isUserEntry(id);
248   }
249 }