Url links changes - incomplete checkin as battery low
[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.HashMap;
25 import java.util.List;
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   // Record of default URL
41   private String defaultUrl;
42   
43   /**
44    * Construct URL provider from string of cached URLs, and set default URL
45    * 
46    * @param defaultUrl
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 defaultUrl, String cachedUrlList)
53   {
54     // create all the UrlProviders we need
55     providers = new ArrayList<UrlProviderI>();
56     
57     UrlProviderI idProvider = new IdentifiersUrlProvider(cachedUrlList);
58     UrlProviderI customProvider = new CustomUrlProvider(cachedUrlList);
59     providers.add(idProvider);
60     providers.add(customProvider);
61
62     // check that the defaultUrl still exists
63   }
64   
65   /**
66    * Get all the URL links
67    * @return all URL links
68    */
69   @Override
70   public HashMap<String, UrlLink> getUrlLinks()
71   {
72     HashMap<String, UrlLink> urls = new HashMap<String, UrlLink>();
73     for (UrlProviderI p : providers)
74     {
75       urls.putAll(p.getUrlLinks());
76     }
77     return urls;
78   }
79   
80   /**
81    * Get the default URL
82    * @return id of the default URL
83    */
84   @Override
85   public String getDefaultUrl()
86   {
87     return defaultUrl;
88   }
89   
90   /**
91    * Set the default URL
92    * @param id the id of the URL to set as default
93    */
94   @Override
95   public void setDefaultUrl(String id)
96   {
97     defaultUrl = id;
98   }
99   
100   /**
101    * Write out all URLs as a string suitable for serialising
102    * @return string representation of available URLs
103    */
104   @Override
105   public String writeUrlsAsString()
106   {
107     String result = "";
108     for (UrlProviderI p : providers)
109     {
110       result += p.writeUrlsAsString();
111     }
112     return result;
113   }
114   
115   /**
116    * Set URL links from string
117    * 
118    * @param cachedUrl
119    *          string representation of URL links, i.e. | separated
120    */
121   @Override
122   public void setUrlLinks(String cachedUrl)
123   {
124     for (UrlProviderI p : providers)
125     {
126       p.setUrlLinks(cachedUrl);
127     }
128   }
129   
130   /**
131    * Set URL links from collection
132    * 
133    * @param links
134    *          hashmap containing string indexed UrlLinks
135    */
136   @Override
137   public void setUrlLinks(HashMap<String, UrlLink> links)
138   {
139     for (UrlProviderI p : providers)
140     {
141       p.setUrlLinks(links);
142     }
143   }
144
145   @Override
146   public void getLinksForDisplay(Vector<String> nameLinks,
147           Vector<String> urlLinks)
148   {
149     Vector<String> names = new Vector<String>();
150     Vector<String> links = new Vector<String>();
151     for (UrlProviderI p : providers)
152     {
153       p.getLinksForDisplay(names, links);
154       nameLinks.addAll(names);
155       urlLinks.addAll(links);
156     }
157   }
158   
159 }