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, url, name));
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(), pair.getValue(),
156 upgradeOldLinks(urls);
161 * Upgrade any legacy links which may have been left lying around
163 private void upgradeOldLinks(HashMap<String, UrlLink> urls)
165 // upgrade old SRS link
166 if (urls.containsKey(SRS_LABEL))
168 urls.remove(SRS_LABEL);
169 UrlLink link = new UrlLink(UrlConstants.DEFAULT_STRING);
170 link.setDescription(UrlConstants.DEFAULT_LABEL);
171 urls.put(UrlConstants.DEFAULT_LABEL, link);
176 public Vector<String> getLinksForMenu()
178 Vector<String> links = new Vector<String>();
179 Iterator<Map.Entry<String, UrlLink>> it = selectedUrls.entrySet()
183 Map.Entry<String, UrlLink> pair = it.next();
184 links.add(pair.getValue().toString());
190 public List<UrlLinkDisplay> getLinksForTable()
192 ArrayList<UrlLinkDisplay> displayLinks = new ArrayList<UrlLinkDisplay>();
193 displayLinks = getLinksForTable(selectedUrls, true);
194 displayLinks.addAll(getLinksForTable(nonselectedUrls, false));
198 private ArrayList<UrlLinkDisplay> getLinksForTable(
199 HashMap<String, UrlLink> urlList, boolean selected)
201 return super.getLinksForTable(urlList, null, selected);
205 public boolean setPrimaryUrl(String id)
207 if (selectedUrls.containsKey(id))
211 else if (nonselectedUrls.containsKey(id))
220 return (primaryUrl != null);
224 public String writeUrlsAsString(boolean selected)
226 StringBuffer links = new StringBuffer();
227 HashMap<String, UrlLink> urls;
234 urls = nonselectedUrls;
238 for (Entry<String, UrlLink> entry : urls.entrySet())
240 links.append(entry.getValue().toString());
245 links.setLength(links.length() - 1);
251 return links.toString();
255 public String getPrimaryUrl(String seqid)
257 String result = super.getPrimaryUrl(seqid, selectedUrls);
260 result = super.getPrimaryUrl(seqid, nonselectedUrls);
266 public String getPrimaryUrlId()
272 public String getPrimaryTarget(String seqid)
274 return selectedUrls.get(primaryUrl).getTarget();
278 public void setUrlData(List<UrlLinkDisplay> links)
280 HashMap<String, UrlLink> unselurls = new HashMap<String, UrlLink>();
281 HashMap<String, UrlLink> selurls = new HashMap<String, UrlLink>();
283 Iterator<UrlLinkDisplay> it = links.iterator();
286 UrlLinkDisplay link = it.next();
288 // MIRIAM ids will be handled by a different UrlProvider class
289 if (!isMiriamId(link.getId()))
291 // don't allow duplicate key names as entries will be overwritten
292 if (unselurls.containsKey(link.getId())
293 || selurls.containsKey(link.getId()))
295 throw new IllegalArgumentException(MessageManager.formatMessage(
296 "exception.url_cannot_have_duplicate_id", link.getId()));
298 if (link.getIsSelected())
300 selurls.put(link.getId(),
301 new UrlLink(link.getName(), link.getUrl(), link.getName()));
307 new UrlLink(link.getName(), link.getUrl(), link
310 // sort out primary and selected ids
311 if (link.getIsPrimary())
313 setPrimaryUrl(link.getId());
318 nonselectedUrls = unselurls;
319 selectedUrls = selurls;
323 public String choosePrimaryUrl()
325 // unilaterally set the primary id to the EMBL_EBI link
326 if ((!nonselectedUrls.containsKey(UrlConstants.DEFAULT_LABEL))
327 && (!selectedUrls.containsKey(UrlConstants.DEFAULT_LABEL)))
329 UrlLink link = new UrlLink(UrlConstants.DEFAULT_STRING);
330 link.setDescription(UrlConstants.DEFAULT_LABEL);
331 selectedUrls.put(UrlConstants.DEFAULT_LABEL, link);
333 primaryUrl = UrlConstants.DEFAULT_LABEL;
334 return UrlConstants.DEFAULT_LABEL;
338 public boolean contains(String id)
340 return (selectedUrls.containsKey(id) || nonselectedUrls.containsKey(id));