f93656fe0270011d18bf4b694eefcc7d33c425d7
[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     for (UrlProviderI p : providers)
88     {
89       if (p.setDefaultUrl(id))
90       {
91         return true;
92       }
93     }
94     return false;
95   }
96   
97   @Override
98   public String writeUrlsAsString()
99   {
100     String result = "";
101     for (UrlProviderI p : providers)
102     {
103       result += p.writeUrlsAsString();
104       result += SEP;
105     }
106     // remove last sep
107     result = result.substring(0, result.length() - 1);
108     return result;
109   }
110
111   @Override
112   public Vector<String> getLinksForMenu()
113   {
114     Vector<String> fullLinks = new Vector<String>();
115     for (UrlProviderI p : providers)
116     {
117       List<String> links = p.getLinksForMenu();
118       if (links != null)
119       {
120         // will obliterate links with same keys from different providers
121         // must have checks in place to prevent user from duplicating ids
122         fullLinks.addAll(links);
123       }
124     }
125     return fullLinks;
126   }
127
128   @Override
129   public List<UrlLinkDisplay> getLinksForTable()
130   {
131     ArrayList<UrlLinkDisplay> displayLinks = new ArrayList<UrlLinkDisplay>();
132     for (UrlProviderI p : providers)
133     {
134       displayLinks.addAll(p.getLinksForTable());
135     }
136     return displayLinks;
137   }
138
139   @Override
140   public void setUrlData(List<UrlLinkDisplay> links)
141   {
142     for (UrlProviderI p : providers)
143     {
144       p.setUrlData(links);
145     }
146   }
147
148   @Override
149   public String getDefaultUrl(String seqid)
150   {
151     String link = null;
152     for (UrlProviderI p : providers)
153     {
154       if (p.getDefaultUrl(seqid) == null)
155       {
156         continue;
157       }
158       else
159       {
160         link = p.getDefaultUrl(seqid);
161         break;
162       }
163     }
164     return link;
165   }
166
167   @Override
168   public String getDefaultTarget(String seqid)
169   {
170     String target = null;
171     for (UrlProviderI p : providers)
172     {
173       if (p.getDefaultTarget(seqid) == null)
174       {
175         continue;
176       }
177       else
178       {
179         target = p.getDefaultTarget(seqid);
180         break;
181       }
182     }
183     return target;
184   }
185   
186   @Override
187   public String chooseDefaultUrl()
188   {
189     // choose a custom url default
190     return customProvider.chooseDefaultUrl();
191   }
192
193   @Override
194   public boolean isUserEntry(String id)
195   {
196     return customProvider.isUserEntry(id);
197   }
198 }