ensure that the DAS sequence source checkbox is checked when a local sequence source...
[jalview.git] / src / jalview / gui / DasSourceBrowser.java
1 /*\r
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.4)\r
3  * Copyright (C) 2008 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle\r
4  * \r
5  * This program is free software; you can redistribute it and/or\r
6  * modify it under the terms of the GNU General Public License\r
7  * as published by the Free Software Foundation; either version 2\r
8  * of the License, or (at your option) any later version.\r
9  * \r
10  * This program is distributed in the hope that it will be useful,\r
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
13  * GNU General Public License for more details.\r
14  * \r
15  * You should have received a copy of the GNU General Public License\r
16  * along with this program; if not, write to the Free Software\r
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA\r
18  */\r
19 package jalview.gui;\r
20 \r
21 import java.util.*;\r
22 \r
23 import java.awt.*;\r
24 import java.awt.event.*;\r
25 import javax.swing.*;\r
26 import javax.swing.event.*;\r
27 import javax.swing.table.*;\r
28 \r
29 import org.biojava.dasobert.dasregistry.*;\r
30 import jalview.jbgui.*;\r
31 import jalview.util.*;\r
32 \r
33 public class DasSourceBrowser extends GDasSourceBrowser implements\r
34         Runnable, ListSelectionListener\r
35 {\r
36   static DasSource[] dasSources = null;\r
37 \r
38   Hashtable localSources = null;\r
39 \r
40   Vector selectedSources;\r
41 \r
42   public static String DEFAULT_REGISTRY = "http://www.dasregistry.org/das1/sources/";\r
43 \r
44   /**\r
45    * true if thread is running and we are talking to DAS registry service\r
46    */\r
47   public boolean loadingDasSources = false;\r
48 \r
49   protected static String getDasRegistryURL()\r
50   {\r
51     String registry = jalview.bin.Cache.getDefault("DAS_REGISTRY_URL",\r
52             DEFAULT_REGISTRY);\r
53 \r
54     if (registry.indexOf("/registry/das1/sources/") > -1)\r
55     {\r
56       jalview.bin.Cache.setProperty(jalview.bin.Cache.DAS_REGISTRY_URL, DEFAULT_REGISTRY);\r
57       registry = DEFAULT_REGISTRY;\r
58     }\r
59     return registry;\r
60   }\r
61 \r
62   public DasSourceBrowser()\r
63   {\r
64     String registry = getDasRegistryURL();\r
65 \r
66     registryURL.setText(registry);\r
67 \r
68     setSelectedFromProperties();\r
69 \r
70     displayFullDetails(null);\r
71     table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);\r
72 \r
73     filter1.addListSelectionListener(this);\r
74     filter2.addListSelectionListener(this);\r
75     filter3.addListSelectionListener(this);\r
76 \r
77     // Ask to be notified of selection changes.\r
78     ListSelectionModel rowSM = table.getSelectionModel();\r
79     rowSM.addListSelectionListener(new ListSelectionListener()\r
80     {\r
81       public void valueChanged(ListSelectionEvent e)\r
82       {\r
83         ListSelectionModel lsm = (ListSelectionModel) e.getSource();\r
84         if (!lsm.isSelectionEmpty())\r
85         {\r
86           int selectedRow = lsm.getMinSelectionIndex();\r
87           displayFullDetails(table.getValueAt(selectedRow, 0).toString());\r
88         }\r
89       }\r
90     });\r
91 \r
92     table.addMouseListener(new MouseAdapter()\r
93     {\r
94       public void mouseClicked(MouseEvent evt)\r
95       {\r
96         if (evt.getClickCount() == 2\r
97                 || SwingUtilities.isRightMouseButton(evt))\r
98         {\r
99           editRemoveLocalSource(evt);\r
100         }\r
101       }\r
102     });\r
103 \r
104     if (dasSources != null)\r
105     {\r
106       init();\r
107     }\r
108   }\r
109 \r
110   public void paintComponent(java.awt.Graphics g)\r
111   {\r
112     if (dasSources == null && !loadingDasSources)\r
113     {\r
114       Thread worker = new Thread(this);\r
115       worker.start();\r
116     }\r
117   }\r
118 \r
119   void init()\r
120   {\r
121     int dSize = dasSources.length;\r
122     Object[][] data = new Object[dSize][2];\r
123     for (int i = 0; i < dSize; i++)\r
124     {\r
125       data[i][0] = dasSources[i].getNickname();\r
126       data[i][1] = new Boolean(selectedSources.contains(dasSources[i]\r
127               .getNickname()));\r
128     }\r
129 \r
130     refreshTableData(data);\r
131     setCapabilities(dasSources);\r
132 \r
133     javax.swing.SwingUtilities.invokeLater(new Runnable()\r
134     {\r
135       public void run()\r
136       {\r
137         TableSorter sorter = (TableSorter) table.getModel();\r
138         sorter.setSortingStatus(1, TableSorter.DESCENDING);\r
139         sorter.setSortingStatus(1, TableSorter.NOT_SORTED);\r
140       }\r
141     });\r
142 \r
143     progressBar.setIndeterminate(false);\r
144     progressBar.setVisible(false);\r
145     addLocal.setVisible(true);\r
146     refresh.setVisible(true);\r
147   }\r
148 \r
149   public void refreshTableData(Object[][] data)\r
150   {\r
151     TableSorter sorter = new TableSorter(new DASTableModel(data));\r
152     sorter.setTableHeader(table.getTableHeader());\r
153     table.setModel(sorter);\r
154   }\r
155 \r
156   void displayFullDetails(String nickName)\r
157   {\r
158 \r
159     StringBuffer text = new StringBuffer(\r
160             "<HTML><font size=\"2\" face=\"Verdana, Arial, Helvetica, sans-serif\">");\r
161 \r
162     if (nickName == null)\r
163     {\r
164       fullDetails.setText(text + "Select a DAS service from the table"\r
165               + " to read a full description here.</font></html>");\r
166       return;\r
167     }\r
168 \r
169     int dSize = dasSources.length;\r
170     for (int i = 0; i < dSize; i++)\r
171     {\r
172       if (!dasSources[i].getNickname().equals(nickName))\r
173       {\r
174         continue;\r
175       }\r
176 \r
177       DasSource ds = dasSources[i];\r
178 \r
179       text.append("<font color=\"#0000FF\">Id:</font> "\r
180               + dasSources[i].getId() + "<br>");\r
181       text.append("<font color=\"#0000FF\">Nickname:</font> "\r
182               + dasSources[i].getNickname() + "<br>");\r
183       text.append("<font color=\"#0000FF\">URL:</font> "\r
184               + dasSources[i].getUrl() + "<br>");\r
185 \r
186       text\r
187               .append("<font color=\"#0000FF\">Admin Email:</font> <a href=\"mailto:"\r
188                       + dasSources[i].getAdminemail()\r
189                       + "\">"\r
190                       + dasSources[i].getAdminemail() + "</a>" + "<br>");\r
191 \r
192       text.append("<font color=\"#0000FF\">Registered at:</font> "\r
193               + dasSources[i].getRegisterDate() + "<br>");\r
194 \r
195       text.append("<font color=\"#0000FF\">Last successful test:</font> "\r
196               + dasSources[i].getLeaseDate() + "<br>");\r
197 \r
198       text.append("<font color=\"#0000FF\">Labels:</font> ");\r
199       for (int s = 0; s < dasSources[i].getLabels().length; s++)\r
200       {\r
201         text.append(dasSources[i].getLabels()[s]);\r
202         if (s < dasSources[i].getLabels().length - 1)\r
203         {\r
204           text.append(",");\r
205         }\r
206         text.append(" ");\r
207       }\r
208       text.append("<br>");\r
209 \r
210       text.append("<font color=\"#0000FF\">Capabilities:</font> ");\r
211       String[] scap = dasSources[i].getCapabilities();\r
212       for (int j = 0; j < scap.length; j++)\r
213       {\r
214         text.append(scap[j]);\r
215         if (j < scap.length - 1)\r
216         {\r
217           text.append(", ");\r
218         }\r
219       }\r
220       text.append("<br>");\r
221 \r
222       text.append("<font color=\"#0000FF\">Coordinates:</font> ");\r
223       DasCoordinateSystem[] dcs = ds.getCoordinateSystem();\r
224       for (int j = 0; j < dcs.length; j++)\r
225       {\r
226         text.append("(" + dcs[j].getUniqueId() + ") "\r
227                 + dcs[j].getCategory() + ", " + dcs[j].getName());\r
228         if (dcs[j].getNCBITaxId() != 0)\r
229         {\r
230           text.append(", " + dcs[j].getNCBITaxId());\r
231         }\r
232         if (dcs[j].getOrganismName().length() > 0)\r
233         {\r
234           text.append(", " + dcs[j].getOrganismName());\r
235         }\r
236 \r
237         text.append("<br>");\r
238       }\r
239 \r
240       text.append("<font color=\"#0000FF\">Description:</font> "\r
241               + dasSources[i].getDescription() + "<br>");\r
242 \r
243       if (dasSources[i].getHelperurl() != null\r
244               && dasSources[i].getHelperurl().length() > 0)\r
245       {\r
246         text.append("<font color=\"#0000FF\"><a href=\""\r
247                 + dasSources[i].getHelperurl()\r
248                 + "\">Go to site</a></font<br>");\r
249       }\r
250 \r
251       text.append("</font></html>");\r
252 \r
253       break;\r
254     }\r
255 \r
256     fullDetails.setText(text.toString());\r
257     javax.swing.SwingUtilities.invokeLater(new Runnable()\r
258     {\r
259       public void run()\r
260       {\r
261         fullDetailsScrollpane.getVerticalScrollBar().setValue(0);\r
262       }\r
263     });\r
264   }\r
265 \r
266   public void run()\r
267   {\r
268     loadingDasSources = true;\r
269 \r
270     addLocal.setVisible(false);\r
271     refresh.setVisible(false);\r
272     progressBar.setVisible(true);\r
273     progressBar.setIndeterminate(true);\r
274     // Refresh the source list.\r
275     dasSources = null;\r
276     getDASSource();\r
277 \r
278     init();\r
279 \r
280     loadingDasSources = false;\r
281 \r
282   }\r
283 \r
284   public Vector getSelectedSources()\r
285   {\r
286     // wait around if we're still loading.\r
287     while (dasSources ==null) {\r
288       if (!loadingDasSources)\r
289       {\r
290         new Thread(this).start();\r
291         try { Thread.sleep(5); } catch (Exception e) {};\r
292         while (loadingDasSources)\r
293         {\r
294           try { Thread.sleep(5); } catch (Exception e) {}; \r
295         };\r
296       }\r
297     }\r
298       \r
299     Vector selected = new Vector();\r
300     for (int r = 0; r < selectedSources.size(); r++)\r
301     {\r
302       for (int i = 0; i < dasSources.length; i++)\r
303       {\r
304         if (dasSources[i].getNickname()\r
305                 .equals(selectedSources.elementAt(r)))\r
306         {\r
307           selected.addElement(dasSources[i]);\r
308           break;\r
309         }\r
310       }\r
311     }\r
312 \r
313     return selected;\r
314   }\r
315 \r
316   /**\r
317    * retrieve das sources from registry and add local source list\r
318    * \r
319    * @return\r
320    */\r
321   public DasSource[] getDASSource()\r
322   {\r
323     if (dasSources == null)\r
324     {\r
325       dasSources = jalview.ws.DasSequenceFeatureFetcher.getDASSources();\r
326       appendLocalSources();\r
327     }\r
328 \r
329     return dasSources;\r
330   }\r
331 \r
332   public void refresh_actionPerformed(ActionEvent e)\r
333   {\r
334     saveProperties(jalview.bin.Cache.applicationProperties);\r
335 \r
336     Thread worker = new Thread(this);\r
337     worker.start();\r
338   }\r
339 \r
340   private void setCapabilities(DasSource[] sources)\r
341   {\r
342     Vector authority = new Vector();\r
343     Vector type = new Vector();\r
344     Vector label = new Vector();\r
345 \r
346     authority.addElement("Any");\r
347     type.addElement("Any");\r
348     label.addElement("Any");\r
349 \r
350     for (int i = 0; i < sources.length; i++)\r
351     {\r
352       DasSource ds = sources[i];\r
353 \r
354       DasCoordinateSystem[] dcs = ds.getCoordinateSystem();\r
355 \r
356       for (int j = 0; j < dcs.length; j++)\r
357       {\r
358         if (!type.contains(dcs[j].getCategory()))\r
359         {\r
360           type.addElement(dcs[j].getCategory());\r
361         }\r
362 \r
363         if (!authority.contains(dcs[j].getName()))\r
364         {\r
365           authority.addElement(dcs[j].getName());\r
366         }\r
367       }\r
368 \r
369       String[] slabels = ds.getLabels();\r
370       for (int s = 0; s < slabels.length; s++)\r
371       {\r
372         if (!label.contains(slabels[s]))\r
373         {\r
374           label.addElement(slabels[s]);\r
375         }\r
376       }\r
377 \r
378     }\r
379 \r
380     filter1.setListData(authority);\r
381     filter2.setListData(type);\r
382     filter3.setListData(label);\r
383 \r
384     javax.swing.SwingUtilities.invokeLater(new Runnable()\r
385     {\r
386       public void run()\r
387       {\r
388         filter1.setSelectedIndex(0);\r
389         filter2.setSelectedIndex(0);\r
390         filter3.setSelectedIndex(0);\r
391       }\r
392     });\r
393   }\r
394 \r
395   public void amendLocal(boolean newSource)\r
396   {\r
397     String url = "http://localhost:8080/", nickname = "";\r
398 \r
399     if (!newSource)\r
400     {\r
401       int selectedRow = table.getSelectionModel().getMinSelectionIndex();\r
402       nickname = table.getValueAt(selectedRow, 0).toString();\r
403       url = ((DasSource) localSources.get(nickname)).getUrl();\r
404     }\r
405 \r
406     JTextField nametf = new JTextField(nickname, 40);\r
407     JTextField urltf = new JTextField(url, 40);\r
408     JCheckBox seqs = new JCheckBox("Sequence Source");\r
409     seqs.setSelected(((DasSource)localSources.get(nickname)).hasCapability("sequence"));\r
410     JPanel panel = new JPanel(new BorderLayout());\r
411     JPanel pane12 = new JPanel(new BorderLayout());\r
412     pane12.add(new JLabel("Nickname: "), BorderLayout.CENTER);\r
413     pane12.add(nametf, BorderLayout.EAST);\r
414     panel.add(pane12, BorderLayout.NORTH);\r
415     pane12 = new JPanel(new BorderLayout());\r
416     pane12.add(new JLabel("URL: "), BorderLayout.NORTH);\r
417     pane12.add(seqs, BorderLayout.SOUTH);\r
418     pane12.add(urltf, BorderLayout.EAST);\r
419     panel.add(pane12, BorderLayout.SOUTH);\r
420     \r
421     int reply = JOptionPane.showInternalConfirmDialog(Desktop.desktop,\r
422             panel, "Enter Nickname & URL of Local DAS Source",\r
423             JOptionPane.OK_CANCEL_OPTION);\r
424 \r
425     if (reply != JOptionPane.OK_OPTION)\r
426     {\r
427       return;\r
428     }\r
429 \r
430     if (!urltf.getText().endsWith("/"))\r
431     {\r
432       urltf.setText(urltf.getText() + "/");\r
433     }\r
434 \r
435     Das1Source local = new Das1Source();\r
436 \r
437     local.setUrl(urltf.getText());\r
438     local.setNickname(nametf.getText());\r
439     if (seqs.isSelected())\r
440     {\r
441       local.setCapabilities(new String[] {"features","sequence"});\r
442     }\r
443     if (localSources == null)\r
444     {\r
445       localSources = new Hashtable();\r
446     }\r
447 \r
448     localSources.put(local.getNickname(), local);\r
449 \r
450     if (!newSource && !nickname.equals(nametf.getText()))\r
451     {\r
452       localSources.remove(nickname);\r
453     }\r
454 \r
455     int size = dasSources.length;\r
456     int adjust = newSource ? 1 : 0;\r
457 \r
458     Object[][] data = new Object[size + adjust][2];\r
459     for (int i = 0; i < size; i++)\r
460     {\r
461       if (!newSource && dasSources[i].getNickname().equals(nickname))\r
462       {\r
463         ((DasSource) dasSources[i]).setNickname(local.getNickname());\r
464         ((DasSource) dasSources[i]).setUrl(local.getUrl());\r
465         data[i][0] = local.getNickname();\r
466         data[i][1] = new Boolean(true);\r
467       }\r
468       else\r
469       {\r
470         data[i][0] = dasSources[i].getNickname();\r
471         data[i][1] = new Boolean(selectedSources.contains(dasSources[i]\r
472                 .getNickname()));\r
473       }\r
474     }\r
475 \r
476     if (newSource)\r
477     {\r
478       data[size][0] = local.getNickname();\r
479       data[size][1] = new Boolean(true);\r
480       selectedSources.add(local.getNickname());\r
481     }\r
482 \r
483     DasSource[] tmp = new DasSource[size + adjust];\r
484 \r
485     System.arraycopy(dasSources, 0, tmp, 0, size);\r
486 \r
487     if (newSource)\r
488     {\r
489       tmp[size] = local;\r
490     }\r
491 \r
492     dasSources = tmp;\r
493 \r
494     refreshTableData(data);\r
495 \r
496     SwingUtilities.invokeLater(new Runnable()\r
497     {\r
498       public void run()\r
499       {\r
500         scrollPane.getVerticalScrollBar().setValue(\r
501                 scrollPane.getVerticalScrollBar().getMaximum());\r
502       }\r
503     });\r
504 \r
505     displayFullDetails(local.getNickname());\r
506   }\r
507 \r
508   public void editRemoveLocalSource(MouseEvent evt)\r
509   {\r
510     int selectedRow = table.getSelectionModel().getMinSelectionIndex();\r
511     if (selectedRow == -1)\r
512     {\r
513       return;\r
514     }\r
515 \r
516     String nickname = table.getValueAt(selectedRow, 0).toString();\r
517 \r
518     if (!localSources.containsKey(nickname))\r
519     {\r
520       JOptionPane.showInternalMessageDialog(Desktop.desktop,\r
521               "You can only edit or remove local DAS Sources!",\r
522               "Public DAS source - not editable",\r
523               JOptionPane.WARNING_MESSAGE);\r
524       return;\r
525     }\r
526 \r
527     Object[] options =\r
528     { "Edit", "Remove", "Cancel" };\r
529     int choice = JOptionPane.showInternalOptionDialog(Desktop.desktop,\r
530             "Do you want to edit or remove " + nickname + "?",\r
531             "Edit / Remove Local DAS Source",\r
532             JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE,\r
533             null, options, options[2]);\r
534 \r
535     switch (choice)\r
536     {\r
537     case 0:\r
538       amendLocal(false);\r
539       break;\r
540     case 1:\r
541       localSources.remove(nickname);\r
542       selectedSources.remove(nickname);\r
543       Object[][] data = new Object[dasSources.length - 1][2];\r
544       DasSource[] tmp = new DasSource[dasSources.length - 1];\r
545       int index = 0;\r
546       for (int i = 0; i < dasSources.length; i++)\r
547       {\r
548         if (dasSources[i].getNickname().equals(nickname))\r
549         {\r
550           continue;\r
551         }\r
552         else\r
553         {\r
554           tmp[index] = dasSources[i];\r
555           data[index][0] = dasSources[i].getNickname();\r
556           data[index][1] = new Boolean(selectedSources\r
557                   .contains(dasSources[i].getNickname()));\r
558           index++;\r
559         }\r
560       }\r
561       dasSources = tmp;\r
562       refreshTableData(data);\r
563       SwingUtilities.invokeLater(new Runnable()\r
564       {\r
565         public void run()\r
566         {\r
567           scrollPane.getVerticalScrollBar().setValue(\r
568                   scrollPane.getVerticalScrollBar().getMaximum());\r
569         }\r
570       });\r
571 \r
572       break;\r
573     }\r
574   }\r
575 \r
576   void appendLocalSources()\r
577   {\r
578     if (localSources == null)\r
579     {\r
580       return;\r
581     }\r
582     // note - we add all das sources to list so they can be filtered for the standard fetchDbRefs function\r
583     int size = dasSources != null ? dasSources.length : 0;\r
584     int lsize = localSources.size();\r
585 \r
586     Object[][] data = new Object[size + lsize][2];\r
587     for (int i = 0; i < size; i++)\r
588     {\r
589       data[i][0] = dasSources[i].getNickname();\r
590       data[i][1] = new Boolean(selectedSources.contains(dasSources[i]\r
591               .getNickname()));\r
592     }\r
593 \r
594     DasSource[] tmp = new DasSource[size + lsize];\r
595     if (dasSources != null)\r
596     {\r
597       System.arraycopy(dasSources, 0, tmp, 0, size);\r
598     }\r
599 \r
600     Enumeration en = localSources.keys();\r
601     int index = size;\r
602     while (en.hasMoreElements())\r
603     {\r
604       String key = en.nextElement().toString();\r
605       data[index][0] = key;\r
606       data[index][1] = new Boolean(false);\r
607       tmp[index] = new Das1Source();\r
608       tmp[index].setNickname(key);\r
609       tmp[index].setUrl(((DasSource) localSources.get(key)).getUrl());\r
610 \r
611       index++;\r
612     }\r
613 \r
614     dasSources = tmp;\r
615 \r
616     refreshTableData(data);\r
617   }\r
618 \r
619   public void valueChanged(ListSelectionEvent evt)\r
620   {\r
621     // Called when the MainTable selection changes\r
622     if (evt.getValueIsAdjusting())\r
623     {\r
624       return;\r
625     }\r
626 \r
627     displayFullDetails(null);\r
628 \r
629     // Filter the displayed data sources\r
630     int dSize = dasSources.length;\r
631 \r
632     ArrayList names = new ArrayList();\r
633     ArrayList selected = new ArrayList();\r
634     DasSource ds;\r
635 \r
636     // The features filter is not visible, but we must still\r
637     // filter the das source list here.\r
638     // July 2006 - only 6 sources fo not serve features\r
639     Object[] dummyFeatureList = new Object[]\r
640     { "features" };\r
641 \r
642     for (int i = 0; i < dSize; i++)\r
643     {\r
644       ds = dasSources[i];\r
645       DasCoordinateSystem[] dcs = ds.getCoordinateSystem();\r
646 \r
647       if (dcs.length == 0 && ds.getCapabilities().length == 0\r
648               && filter1.getSelectedIndex() == 0\r
649               && filter2.getSelectedIndex() == 0\r
650               && filter3.getSelectedIndex() == 0)\r
651       {\r
652         // THIS IS A FIX FOR LOCAL SOURCES WHICH DO NOT\r
653         // HAVE COORDINATE SYSTEMS, INFO WHICH AT PRESENT\r
654         // IS ADDED FROM THE REGISTRY\r
655         names.add(ds.getNickname());\r
656         selected\r
657                 .add(new Boolean(selectedSources.contains(ds.getNickname())));\r
658         continue;\r
659       }\r
660 \r
661       if (!selectedInList(dummyFeatureList, ds.getCapabilities())\r
662               || !selectedInList(filter3.getSelectedValues(), ds\r
663                       .getLabels()))\r
664       {\r
665         continue;\r
666       }\r
667 \r
668       for (int j = 0; j < dcs.length; j++)\r
669       {\r
670         if (selectedInList(filter1.getSelectedValues(), new String[]\r
671         { dcs[j].getName() })\r
672                 && selectedInList(filter2.getSelectedValues(), new String[]\r
673                 { dcs[j].getCategory() }))\r
674         {\r
675           names.add(ds.getNickname());\r
676           selected.add(new Boolean(selectedSources.contains(ds\r
677                   .getNickname())));\r
678           break;\r
679         }\r
680       }\r
681     }\r
682 \r
683     dSize = names.size();\r
684     Object[][] data = new Object[dSize][2];\r
685     for (int d = 0; d < dSize; d++)\r
686     {\r
687       data[d][0] = names.get(d);\r
688       data[d][1] = selected.get(d);\r
689     }\r
690 \r
691     refreshTableData(data);\r
692   }\r
693 \r
694   boolean selectedInList(Object[] selection, String[] items)\r
695   {\r
696     for (int i = 0; i < selection.length; i++)\r
697     {\r
698       if (selection[i].equals("Any"))\r
699       {\r
700         return true;\r
701       }\r
702 \r
703       for (int j = 0; j < items.length; j++)\r
704       {\r
705         if (selection[i].equals(items[j]))\r
706         {\r
707           return true;\r
708         }\r
709       }\r
710     }\r
711 \r
712     return false;\r
713   }\r
714 \r
715   void setSelectedFromProperties()\r
716   {\r
717     String active = jalview.bin.Cache.getDefault("DAS_ACTIVE_SOURCE",\r
718             "uniprot");\r
719     StringTokenizer st = new StringTokenizer(active, "\t");\r
720     selectedSources = new Vector();\r
721     while (st.hasMoreTokens())\r
722     {\r
723       selectedSources.addElement(st.nextToken());\r
724     }\r
725     \r
726     Vector _localSources = jalview.bin.Cache.getLocalDasSources();\r
727     if (_localSources!=null)\r
728     {\r
729       if (localSources==null)\r
730       {\r
731         localSources = new Hashtable();\r
732       }\r
733       Enumeration sources = _localSources.elements();\r
734       while (sources.hasMoreElements())\r
735       {\r
736         Das1Source source = (Das1Source) sources.nextElement();\r
737         localSources.put(source.getNickname(), source);\r
738       }\r
739     }\r
740   }\r
741 \r
742   public void reset_actionPerformed(ActionEvent e)\r
743   {\r
744     registryURL.setText(DEFAULT_REGISTRY);\r
745   }\r
746 \r
747   /**\r
748    * set the DAS source settings in the given jalview properties.\r
749    * @param properties\r
750    */\r
751   public void saveProperties(Properties properties)\r
752   {\r
753     if (registryURL.getText() == null || registryURL.getText().length() < 1)\r
754     {\r
755       properties.remove(jalview.bin.Cache.DAS_REGISTRY_URL);\r
756     }\r
757     else\r
758     {\r
759       properties.setProperty(jalview.bin.Cache.DAS_REGISTRY_URL, registryURL.getText());\r
760     }\r
761 \r
762     StringBuffer sb = new StringBuffer();\r
763     for (int r = 0; r < table.getModel().getRowCount(); r++)\r
764     {\r
765       if (((Boolean) table.getValueAt(r, 1)).booleanValue())\r
766       {\r
767         sb.append(table.getValueAt(r, 0) + "\t");\r
768       }\r
769     }\r
770 \r
771     properties.setProperty(jalview.bin.Cache.DAS_ACTIVE_SOURCE, sb.toString());\r
772 \r
773     if (localSources != null)\r
774     {\r
775       sb = new StringBuffer();\r
776       Enumeration en = localSources.keys();\r
777       while (en.hasMoreElements())\r
778       {\r
779         String token = en.nextElement().toString();\r
780         sb.append(token + "|"\r
781                 +(((DasSource) localSources.get(token)).hasCapability("sequence") ? "sequence:" : "")\r
782                 + ((DasSource) localSources.get(token)).getUrl() + "\t");\r
783       }\r
784 \r
785       properties.setProperty(jalview.bin.Cache.DAS_LOCAL_SOURCE, sb.toString());\r
786     }\r
787 \r
788   }\r
789 \r
790   class DASTableModel extends AbstractTableModel\r
791   {\r
792 \r
793     public DASTableModel(Object[][] data)\r
794     {\r
795       this.data = data;\r
796     }\r
797 \r
798     private String[] columnNames = new String[]\r
799     { "Nickname", "Use Source" };\r
800 \r
801     private Object[][] data;\r
802 \r
803     public int getColumnCount()\r
804     {\r
805       return columnNames.length;\r
806     }\r
807 \r
808     public int getRowCount()\r
809     {\r
810       return data.length;\r
811     }\r
812 \r
813     public String getColumnName(int col)\r
814     {\r
815       return columnNames[col];\r
816     }\r
817 \r
818     public Object getValueAt(int row, int col)\r
819     {\r
820       return data[row][col];\r
821     }\r
822 \r
823     /*\r
824      * JTable uses this method to determine the default renderer/ editor for\r
825      * each cell. If we didn't implement this method, then the last column would\r
826      * contain text ("true"/"false"), rather than a check box.\r
827      */\r
828     public Class getColumnClass(int c)\r
829     {\r
830       return getValueAt(0, c).getClass();\r
831     }\r
832 \r
833     /*\r
834      * Don't need to implement this method unless your table's editable.\r
835      */\r
836     public boolean isCellEditable(int row, int col)\r
837     {\r
838       // Note that the data/cell address is constant,\r
839       // no matter where the cell appears onscreen.\r
840       return col == 1;\r
841 \r
842     }\r
843 \r
844     /*\r
845      * Don't need to implement this method unless your table's data can change.\r
846      */\r
847     public void setValueAt(Object value, int row, int col)\r
848     {\r
849       data[row][col] = value;\r
850       fireTableCellUpdated(row, col);\r
851 \r
852       String name = getValueAt(row, 0).toString();\r
853       boolean selected = ((Boolean) value).booleanValue();\r
854 \r
855       if (selectedSources.contains(name) && !selected)\r
856       {\r
857         selectedSources.remove(name);\r
858       }\r
859 \r
860       if (!selectedSources.contains(name) && selected)\r
861       {\r
862         selectedSources.add(name);\r
863       }\r
864     }\r
865   }\r
866 \r
867   public void initDasSources()\r
868   {\r
869 \r
870     Thread thr = new Thread(new Runnable()\r
871     {\r
872       public void run()\r
873       {\r
874         // this actually initialises the das source list\r
875         paintComponent(null); // yuk\r
876       }\r
877     });\r
878     thr.start();\r
879     while (loadingDasSources\r
880             || dasSources == null)\r
881     {\r
882       try\r
883       {\r
884         Thread.sleep(10);\r
885       } catch (Exception e)\r
886       {\r
887       }\r
888       ;\r
889     }\r
890   }\r
891 \r
892   public Vector resolveSourceNicknames(Vector sources)\r
893   {\r
894 \r
895     Vector resolved = new Vector();\r
896     if (sources != null)\r
897     {\r
898       for (int i = 0; i < dasSources.length; i++)\r
899       {\r
900         if (sources.contains(dasSources[i].getNickname()))\r
901         {\r
902           if (!resolved.contains(dasSources[i]))\r
903           {\r
904             resolved.addElement(dasSources[i]);\r
905           }\r
906         }\r
907       }\r
908     }\r
909     return resolved;\r
910   }\r
911 }\r