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)
79 System.out.println(ex + "\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)
101 System.out.println(ex + "\nError parsing sequence links");
105 private HashMap<String, UrlLink> parseUrlStrings(String urlStrings)
107 // cachedUrlList is in form <label>|<url>|<label>|<url>...
108 // parse cachedUrlList into labels (used as id) and url links
109 HashMap<String, UrlLink> urls = new HashMap<String, UrlLink>();
111 StringTokenizer st = new StringTokenizer(urlStrings, SEP);
112 while (st.hasMoreElements())
114 String name = st.nextToken();
116 if (!isMiriamId(name))
118 // this one of our custom urls
119 String url = st.nextToken();
120 // check for '|' within a regex
121 int rxstart = url.indexOf(DELIM + DB_ACCESSION + DELIM);
124 rxstart = url.indexOf(DELIM + SEQUENCE_ID + DELIM);
126 while (rxstart == -1 && url.indexOf("/=" + DELIM) == -1
127 && st.hasMoreTokens())
129 url = url + SEP + st.nextToken();
131 urls.put(name, new UrlLink(name + SEP + url));
134 upgradeOldLinks(urls);
138 private HashMap<String, UrlLink> parseUrlList(Map<String, String> urlList)
140 HashMap<String, UrlLink> urls = new HashMap<String, UrlLink>();
141 Iterator<Map.Entry<String, String>> it = urlList.entrySet().iterator();
144 Map.Entry<String, String> pair = it.next();
145 urls.put(pair.getKey(),
146 new UrlLink(pair.getKey() + SEP + pair.getValue()));
148 upgradeOldLinks(urls);
153 * Upgrade any legacy links which may have been left lying around
155 private void upgradeOldLinks(HashMap<String, UrlLink> urls)
157 // upgrade old SRS link
158 if (urls.containsKey(SRS_LABEL))
160 urls.remove(SRS_LABEL);
161 urls.put(UrlConstants.DEFAULT_LABEL, new UrlLink(UrlConstants.DEFAULT_STRING));
166 public Vector<String> getLinksForMenu()
168 Vector<String> links = new Vector<String>();
169 Iterator<Map.Entry<String, UrlLink>> it = selectedUrls.entrySet()
173 Map.Entry<String, UrlLink> pair = it.next();
174 links.add(pair.getValue().toString());
180 public List<UrlLinkDisplay> getLinksForTable()
182 ArrayList<UrlLinkDisplay> displayLinks = new ArrayList<UrlLinkDisplay>();
183 displayLinks = getLinksForTable(selectedUrls, true);
184 displayLinks.addAll(getLinksForTable(nonselectedUrls, false));
188 private ArrayList<UrlLinkDisplay> getLinksForTable(
189 HashMap<String, UrlLink> urlList, boolean selected)
191 return super.getLinksForTable(urlList, null, selected);
195 public boolean setDefaultUrl(String id)
201 if (selectedUrls.containsKey(id))
205 else if (nonselectedUrls.containsKey(id))
210 return (defaultUrl != null);
214 public String writeUrlsAsString(boolean selected)
216 StringBuffer links = new StringBuffer();
217 HashMap<String, UrlLink> urls;
224 urls = nonselectedUrls;
228 for (Entry<String, UrlLink> entry : urls.entrySet())
230 links.append(entry.getValue().toString());
235 links.setLength(links.length() - 1);
241 return links.toString();
245 public String getDefaultUrl(String seqid)
247 String result = super.getDefaultUrl(seqid, selectedUrls);
250 result = super.getDefaultUrl(seqid, nonselectedUrls);
256 public String getDefaultUrlId()
262 public String getDefaultTarget(String seqid)
264 return selectedUrls.get(defaultUrl).getTarget();
268 public void setUrlData(List<UrlLinkDisplay> links)
270 HashMap<String, UrlLink> unselurls = new HashMap<String, UrlLink>();
271 HashMap<String, UrlLink> selurls = new HashMap<String, UrlLink>();
273 Iterator<UrlLinkDisplay> it = links.iterator();
276 UrlLinkDisplay link = it.next();
278 // MIRIAM ids will be handled by a different UrlProvider class
279 if (!isMiriamId(link.getId()))
281 // don't allow duplicate key names as entries will be overwritten
282 if (unselurls.containsKey(link.getId())
283 || selurls.containsKey(link.getId()))
285 throw new IllegalArgumentException(MessageManager.formatMessage(
286 "exception.url_cannot_have_duplicate_id", link.getId()));
288 if (link.getIsSelected())
290 selurls.put(link.getId(),
291 new UrlLink(link.getName() + SEP + link.getUrl()));
295 unselurls.put(link.getId(), new UrlLink(link.getName() + SEP
298 // sort out default and selected ids
299 if (link.getIsDefault())
301 setDefaultUrl(link.getId());
306 nonselectedUrls = unselurls;
307 selectedUrls = selurls;
311 public String chooseDefaultUrl()
313 // unilaterally set the default id to the EMBL_EBI link
315 if (nonselectedUrls.containsKey(UrlConstants.DEFAULT_LABEL))
317 // default key must be selected so remove from nonselected list
318 nonselectedUrls.remove(UrlConstants.DEFAULT_LABEL);
321 if (!selectedUrls.containsKey(UrlConstants.DEFAULT_LABEL))
323 selectedUrls.put(UrlConstants.DEFAULT_LABEL, new UrlLink(
324 UrlConstants.DEFAULT_STRING));
326 defaultUrl = UrlConstants.DEFAULT_LABEL;
327 return UrlConstants.DEFAULT_LABEL;