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