2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
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.
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.
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.
24 import static jalview.util.UrlConstants.DB_ACCESSION;
25 import static jalview.util.UrlConstants.DELIM;
26 import static jalview.util.UrlConstants.SEP;
27 import static jalview.util.UrlConstants.SEQUENCE_ID;
29 import jalview.util.MessageManager;
30 import jalview.util.UrlConstants;
31 import jalview.util.UrlLink;
33 import java.util.ArrayList;
34 import java.util.HashMap;
35 import java.util.Iterator;
36 import java.util.List;
38 import java.util.Map.Entry;
39 import java.util.StringTokenizer;
40 import java.util.Vector;
44 * Implements the UrlProviderI interface for a UrlProvider object which serves
45 * custom URLs defined by the user
50 public class CustomUrlProvider extends UrlProviderImpl
52 // Default sequence URL link label for SRS
53 private static final String SRS_LABEL = "SRS";
55 // map of string ids to urlLinks (selected)
56 private HashMap<String, UrlLink> selectedUrls;
58 // map of string ids to urlLinks (not selected)
59 private HashMap<String, UrlLink> nonselectedUrls;
62 * Construct UrlProvider for custom (user-entered) URLs
64 * @param inMenuUrlList
65 * list of URLs set to be displayed in menu, in form stored in Cache.
66 * i.e. SEP delimited string
67 * @param storedUrlList
68 * list of custom URLs entered by user but not currently displayed in
69 * menu, in form stored in Cache
71 public CustomUrlProvider(String inMenuUrlList, String storedUrlList)
75 selectedUrls = parseUrlStrings(inMenuUrlList);
76 nonselectedUrls = parseUrlStrings(storedUrlList);
77 } catch (Exception ex)
80 .println(ex.getMessage() + "\nError parsing sequence links");
85 * Construct UrlProvider for custom (user-entered) URLs
88 * list of URLs to be displayed in menu, as (label,url) pairs
89 * @param storedUrlList
90 * list of custom URLs entered by user but not currently displayed in
91 * menu, as (label,url) pairs
93 public CustomUrlProvider(Map<String, String> inMenuUrlList,
94 Map<String, String> storedUrlList)
98 selectedUrls = parseUrlList(inMenuUrlList);
99 nonselectedUrls = parseUrlList(storedUrlList);
100 } catch (Exception ex)
103 .println(ex.getMessage() + "\nError parsing sequence links");
107 private HashMap<String, UrlLink> parseUrlStrings(String urlStrings)
109 // cachedUrlList is in form <label>|<url>|<label>|<url>...
110 // parse cachedUrlList into labels (used as id) and url links
111 HashMap<String, UrlLink> urls = new HashMap<String, UrlLink>();
113 StringTokenizer st = new StringTokenizer(urlStrings, SEP);
114 while (st.hasMoreElements())
116 String name = st.nextToken();
118 if (!isMiriamId(name))
120 // this one of our custom urls
121 String url = st.nextToken();
122 // check for '|' within a regex
123 int rxstart = url.indexOf(DELIM + DB_ACCESSION + DELIM);
126 rxstart = url.indexOf(DELIM + SEQUENCE_ID + DELIM);
128 while (rxstart == -1 && url.indexOf("/=" + DELIM) == -1
129 && st.hasMoreTokens())
131 url = url + SEP + st.nextToken();
133 urls.put(name, new UrlLink(name + SEP + url));
136 upgradeOldLinks(urls);
140 private HashMap<String, UrlLink> parseUrlList(Map<String, String> urlList)
142 HashMap<String, UrlLink> urls = new HashMap<String, UrlLink>();
148 Iterator<Map.Entry<String, String>> it = urlList.entrySet().iterator();
151 Map.Entry<String, String> pair = it.next();
152 urls.put(pair.getKey(),
153 new UrlLink(pair.getKey() + SEP + pair.getValue()));
155 upgradeOldLinks(urls);
160 * Upgrade any legacy links which may have been left lying around
162 private void upgradeOldLinks(HashMap<String, UrlLink> urls)
164 // upgrade old SRS link
165 if (urls.containsKey(SRS_LABEL))
167 urls.remove(SRS_LABEL);
168 urls.put(UrlConstants.DEFAULT_LABEL, new UrlLink(UrlConstants.DEFAULT_STRING));
173 public Vector<String> getLinksForMenu()
175 Vector<String> links = new Vector<String>();
176 Iterator<Map.Entry<String, UrlLink>> it = selectedUrls.entrySet()
180 Map.Entry<String, UrlLink> pair = it.next();
181 links.add(pair.getValue().toString());
187 public List<UrlLinkDisplay> getLinksForTable()
189 ArrayList<UrlLinkDisplay> displayLinks = new ArrayList<UrlLinkDisplay>();
190 displayLinks = getLinksForTable(selectedUrls, true);
191 displayLinks.addAll(getLinksForTable(nonselectedUrls, false));
195 private ArrayList<UrlLinkDisplay> getLinksForTable(
196 HashMap<String, UrlLink> urlList, boolean selected)
198 return super.getLinksForTable(urlList, null, selected);
202 public boolean setDefaultUrl(String id)
204 if (selectedUrls.containsKey(id))
208 else if (nonselectedUrls.containsKey(id))
217 return (defaultUrl != null);
221 public String writeUrlsAsString(boolean selected)
223 StringBuffer links = new StringBuffer();
224 HashMap<String, UrlLink> urls;
231 urls = nonselectedUrls;
235 for (Entry<String, UrlLink> entry : urls.entrySet())
237 links.append(entry.getValue().toString());
242 links.setLength(links.length() - 1);
248 return links.toString();
252 public String getDefaultUrl(String seqid)
254 String result = super.getDefaultUrl(seqid, selectedUrls);
257 result = super.getDefaultUrl(seqid, nonselectedUrls);
263 public String getDefaultUrlId()
269 public String getDefaultTarget(String seqid)
271 return selectedUrls.get(defaultUrl).getTarget();
275 public void setUrlData(List<UrlLinkDisplay> links)
277 HashMap<String, UrlLink> unselurls = new HashMap<String, UrlLink>();
278 HashMap<String, UrlLink> selurls = new HashMap<String, UrlLink>();
280 Iterator<UrlLinkDisplay> it = links.iterator();
283 UrlLinkDisplay link = it.next();
285 // MIRIAM ids will be handled by a different UrlProvider class
286 if (!isMiriamId(link.getId()))
288 // don't allow duplicate key names as entries will be overwritten
289 if (unselurls.containsKey(link.getId())
290 || selurls.containsKey(link.getId()))
292 throw new IllegalArgumentException(MessageManager.formatMessage(
293 "exception.url_cannot_have_duplicate_id", link.getId()));
295 if (link.getIsSelected())
297 selurls.put(link.getId(),
298 new UrlLink(link.getName() + SEP + link.getUrl()));
302 unselurls.put(link.getId(), new UrlLink(link.getName() + SEP
305 // sort out default and selected ids
306 if (link.getIsDefault())
308 setDefaultUrl(link.getId());
313 nonselectedUrls = unselurls;
314 selectedUrls = selurls;
318 public String chooseDefaultUrl()
320 // unilaterally set the default id to the EMBL_EBI link
321 if ((!nonselectedUrls.containsKey(UrlConstants.DEFAULT_LABEL))
322 && (!selectedUrls.containsKey(UrlConstants.DEFAULT_LABEL)))
324 selectedUrls.put(UrlConstants.DEFAULT_LABEL, new UrlLink(
325 UrlConstants.DEFAULT_STRING));
327 defaultUrl = UrlConstants.DEFAULT_LABEL;
328 return UrlConstants.DEFAULT_LABEL;
332 public boolean contains(String id)
334 return (selectedUrls.containsKey(id) || nonselectedUrls.containsKey(id));