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;
43 * Implements the UrlProviderI interface for a UrlProvider object which serves
44 * custom URLs defined by the user
49 public class CustomUrlProvider extends UrlProviderImpl
51 // Default sequence URL link label for SRS
52 private static final String SRS_LABEL = "SRS";
54 // map of string ids to urlLinks (selected)
55 private HashMap<String, UrlLink> selectedUrls;
57 // map of string ids to urlLinks (not selected)
58 private HashMap<String, UrlLink> nonselectedUrls;
61 * Construct UrlProvider for custom (user-entered) URLs
63 * @param inMenuUrlList
64 * list of URLs set to be displayed in menu, in form stored in Cache.
65 * i.e. SEP delimited string
66 * @param storedUrlList
67 * list of custom URLs entered by user but not currently displayed in
68 * menu, in form stored in Cache
70 public CustomUrlProvider(String inMenuUrlList, String storedUrlList)
74 selectedUrls = parseUrlStrings(inMenuUrlList);
75 nonselectedUrls = parseUrlStrings(storedUrlList);
76 } catch (Exception ex)
79 .println(ex.getMessage() + "\nError parsing sequence links");
84 * Construct UrlProvider for custom (user-entered) URLs
87 * list of URLs to be displayed in menu, as (label,url) pairs
88 * @param storedUrlList
89 * list of custom URLs entered by user but not currently displayed in
90 * menu, as (label,url) pairs
92 public CustomUrlProvider(Map<String, String> inMenuUrlList,
93 Map<String, String> storedUrlList)
97 selectedUrls = parseUrlList(inMenuUrlList);
98 nonselectedUrls = parseUrlList(storedUrlList);
99 } catch (Exception ex)
102 .println(ex.getMessage() + "\nError parsing sequence links");
106 private HashMap<String, UrlLink> parseUrlStrings(String urlStrings)
108 // cachedUrlList is in form <label>|<url>|<label>|<url>...
109 // parse cachedUrlList into labels (used as id) and url links
110 HashMap<String, UrlLink> urls = new HashMap<String, UrlLink>();
112 StringTokenizer st = new StringTokenizer(urlStrings, SEP);
113 while (st.hasMoreElements())
115 String name = st.nextToken();
117 if (!isMiriamId(name))
119 // this one of our custom urls
120 String url = st.nextToken();
121 // check for '|' within a regex
122 int rxstart = url.indexOf(DELIM + DB_ACCESSION + DELIM);
125 rxstart = url.indexOf(DELIM + SEQUENCE_ID + DELIM);
127 while (rxstart == -1 && url.indexOf("/=" + DELIM) == -1
128 && st.hasMoreTokens())
130 url = url + SEP + st.nextToken();
132 urls.put(name, new UrlLink(name, url, name));
135 upgradeOldLinks(urls);
139 private HashMap<String, UrlLink> parseUrlList(Map<String, String> urlList)
141 HashMap<String, UrlLink> urls = new HashMap<String, UrlLink>();
147 Iterator<Map.Entry<String, String>> it = urlList.entrySet().iterator();
150 Map.Entry<String, String> pair = it.next();
151 urls.put(pair.getKey(),
152 new UrlLink(pair.getKey(), pair.getValue(), pair.getKey()));
154 upgradeOldLinks(urls);
159 * Upgrade any legacy links which may have been left lying around
161 private void upgradeOldLinks(HashMap<String, UrlLink> urls)
163 // upgrade old SRS link
164 if (urls.containsKey(SRS_LABEL))
166 urls.remove(SRS_LABEL);
167 UrlLink link = new UrlLink(UrlConstants.DEFAULT_STRING);
168 link.setLabel(UrlConstants.DEFAULT_LABEL);
169 urls.put(UrlConstants.DEFAULT_LABEL, link);
174 public List<String> getLinksForMenu()
176 List<String> links = new ArrayList<String>();
177 Iterator<Map.Entry<String, UrlLink>> it = selectedUrls.entrySet()
181 Map.Entry<String, UrlLink> pair = it.next();
182 links.add(pair.getValue().toString());
188 public List<UrlLinkDisplay> getLinksForTable()
190 ArrayList<UrlLinkDisplay> displayLinks = new ArrayList<UrlLinkDisplay>();
191 displayLinks = getLinksForTable(selectedUrls, true);
192 displayLinks.addAll(getLinksForTable(nonselectedUrls, false));
196 private ArrayList<UrlLinkDisplay> getLinksForTable(
197 HashMap<String, UrlLink> urlList, boolean selected)
199 return super.getLinksForTable(urlList, null, selected);
203 public boolean setPrimaryUrl(String id)
205 if (selectedUrls.containsKey(id))
209 else if (nonselectedUrls.containsKey(id))
218 return (primaryUrl != null);
222 public String writeUrlsAsString(boolean selected)
224 StringBuffer links = new StringBuffer();
225 HashMap<String, UrlLink> urls;
232 urls = nonselectedUrls;
236 for (Entry<String, UrlLink> entry : urls.entrySet())
238 links.append(entry.getValue().toString());
243 links.setLength(links.length() - 1);
249 return links.toString();
253 public String getPrimaryUrl(String seqid)
255 String result = super.getPrimaryUrl(seqid, selectedUrls);
258 result = super.getPrimaryUrl(seqid, nonselectedUrls);
264 public String getPrimaryUrlId()
270 public String getPrimaryTarget(String seqid)
272 return selectedUrls.get(primaryUrl).getTarget();
276 public void setUrlData(List<UrlLinkDisplay> links)
278 HashMap<String, UrlLink> unselurls = new HashMap<String, UrlLink>();
279 HashMap<String, UrlLink> selurls = new HashMap<String, UrlLink>();
281 Iterator<UrlLinkDisplay> it = links.iterator();
284 UrlLinkDisplay link = it.next();
286 // MIRIAM ids will be handled by a different UrlProvider class
287 if (!isMiriamId(link.getId()))
289 // don't allow duplicate key names as entries will be overwritten
290 if (unselurls.containsKey(link.getId())
291 || selurls.containsKey(link.getId()))
293 throw new IllegalArgumentException(MessageManager.formatMessage(
294 "exception.url_cannot_have_duplicate_id", link.getId()));
296 if (link.getIsSelected())
298 selurls.put(link.getId(), new UrlLink(link.getDescription(),
299 link.getUrl(), link.getDescription()));
303 unselurls.put(link.getId(), new UrlLink(link.getDescription(),
304 link.getUrl(), link.getDescription()));
306 // sort out primary and selected ids
307 if (link.getIsPrimary())
309 setPrimaryUrl(link.getId());
314 nonselectedUrls = unselurls;
315 selectedUrls = selurls;
319 public String choosePrimaryUrl()
321 // unilaterally set the primary id to the EMBL_EBI link
322 if ((!nonselectedUrls.containsKey(UrlConstants.DEFAULT_LABEL))
323 && (!selectedUrls.containsKey(UrlConstants.DEFAULT_LABEL)))
325 UrlLink link = new UrlLink(UrlConstants.DEFAULT_STRING);
326 link.setLabel(UrlConstants.DEFAULT_LABEL);
327 selectedUrls.put(UrlConstants.DEFAULT_LABEL, link);
329 primaryUrl = UrlConstants.DEFAULT_LABEL;
330 return UrlConstants.DEFAULT_LABEL;
334 public boolean contains(String id)
336 return (selectedUrls.containsKey(id)
337 || nonselectedUrls.containsKey(id));