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