JAL-2316 Restructure identifiers.org download
[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 (!setDefaultUrl(defaultUrlString))
61     {
62       chooseDefaultUrl();
63     }
64   }
65
66   /*
67    * Store ref to custom url provider
68    */
69   private UrlProviderI findCustomProvider()
70   {
71     for (UrlProviderI p : providers)
72     {
73       if (p.getClass().equals(CustomUrlProvider.class))
74       {
75         return p;
76       }
77     }
78
79     System.out
80             .println("Error initialising UrlProvider - no custom url provider");
81     return null;
82   }
83   
84   @Override
85   public boolean setDefaultUrl(String id)
86   {
87     boolean outcome = false;
88     for (UrlProviderI p : providers)
89     {
90       if (p.setDefaultUrl(id))
91       {
92         outcome = true;
93       }
94     }
95     return outcome;
96   }
97   
98   @Override
99   public String writeUrlsAsString(boolean selected)
100   {
101     String result = "";
102     for (UrlProviderI p : providers)
103     {
104       String next = p.writeUrlsAsString(selected);
105       if (!next.isEmpty())
106       {
107         result += next;
108         result += SEP;
109       }
110     }
111     // remove last sep
112     if (!result.isEmpty())
113     {
114       result = result.substring(0, result.length() - 1);
115     }
116     return result;
117   }
118
119   @Override
120   public Vector<String> getLinksForMenu()
121   {
122     Vector<String> fullLinks = new Vector<String>();
123     for (UrlProviderI p : providers)
124     {
125       List<String> links = p.getLinksForMenu();
126       if (links != null)
127       {
128         // will obliterate links with same keys from different providers
129         // must have checks in place to prevent user from duplicating ids
130         fullLinks.addAll(links);
131       }
132     }
133     return fullLinks;
134   }
135
136   @Override
137   public List<UrlLinkDisplay> getLinksForTable()
138   {
139     ArrayList<UrlLinkDisplay> displayLinks = new ArrayList<UrlLinkDisplay>();
140     for (UrlProviderI p : providers)
141     {
142       displayLinks.addAll(p.getLinksForTable());
143     }
144     return displayLinks;
145   }
146
147   @Override
148   public void setUrlData(List<UrlLinkDisplay> links)
149   {
150     for (UrlProviderI p : providers)
151     {
152       p.setDefaultUrl(null);
153       p.setUrlData(links);
154     }
155   }
156
157   @Override
158   public String getDefaultUrl(String seqid)
159   {
160     String link = null;
161     for (UrlProviderI p : providers)
162     {
163       if (p.getDefaultUrl(seqid) == null)
164       {
165         continue;
166       }
167       else
168       {
169         link = p.getDefaultUrl(seqid);
170         break;
171       }
172     }
173     return link;
174   }
175
176   @Override
177   public String getDefaultUrlId()
178   {
179     String id = null;
180     for (UrlProviderI p : providers)
181     {
182       if (p.getDefaultUrlId() == null)
183       {
184         continue;
185       }
186       else
187       {
188         id = p.getDefaultUrlId();
189         break;
190       }
191     }
192     return id;
193   }
194
195   @Override
196   public String getDefaultTarget(String seqid)
197   {
198     String target = null;
199     for (UrlProviderI p : providers)
200     {
201       if (p.getDefaultTarget(seqid) == null)
202       {
203         continue;
204       }
205       else
206       {
207         target = p.getDefaultTarget(seqid);
208         break;
209       }
210     }
211     return target;
212   }
213   
214   @Override
215   public String chooseDefaultUrl()
216   {
217     // choose a custom url default
218     return customProvider.chooseDefaultUrl();
219   }
220
221   @Override
222   public boolean isUserEntry(String id)
223   {
224     return customProvider.isUserEntry(id);
225   }
226 }