Jalview 2.6 source licence
[jalview.git] / src / jalview / gui / DasSourceBrowser.java
1 /*\r
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.6)\r
3  * Copyright (C) 2010 J Procter, AM Waterhouse, 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   FeatureSettings fs = null;\r
111   public DasSourceBrowser()\r
112   {\r
113     this(null);\r
114   }\r
115 \r
116   public void paintComponent(java.awt.Graphics g)\r
117   {\r
118     if (dasSources == null && !loadingDasSources)\r
119     {\r
120       Thread worker = new Thread(this);\r
121       worker.start();\r
122     }\r
123   }\r
124 \r
125   void init()\r
126   {\r
127     int dSize = dasSources.length;\r
128     Object[][] data = new Object[dSize][2];\r
129     for (int i = 0; i < dSize; i++)\r
130     {\r
131       data[i][0] = dasSources[i].getNickname();\r
132       data[i][1] = new Boolean(selectedSources.contains(dasSources[i]\r
133               .getNickname()));\r
134     }\r
135 \r
136     refreshTableData(data);\r
137     setCapabilities(dasSources);\r
138 \r
139     javax.swing.SwingUtilities.invokeLater(new Runnable()\r
140     {\r
141       public void run()\r
142       {\r
143         TableSorter sorter = (TableSorter) table.getModel();\r
144         sorter.setSortingStatus(1, TableSorter.DESCENDING);\r
145         sorter.setSortingStatus(1, TableSorter.NOT_SORTED);\r
146       }\r
147     });\r
148 \r
149     progressBar.setIndeterminate(false);\r
150     progressBar.setVisible(false);\r
151     addLocal.setVisible(true);\r
152     refresh.setVisible(true);\r
153   }\r
154 \r
155   public void refreshTableData(Object[][] data)\r
156   {\r
157     TableSorter sorter = new TableSorter(new DASTableModel(data));\r
158     sorter.setTableHeader(table.getTableHeader());\r
159     table.setModel(sorter);\r
160   }\r
161 \r
162   void displayFullDetails(String nickName)\r
163   {\r
164 \r
165     StringBuffer text = new StringBuffer(\r
166             "<HTML><font size=\"2\" face=\"Verdana, Arial, Helvetica, sans-serif\">");\r
167 \r
168     if (nickName == null)\r
169     {\r
170       fullDetails.setText(text + "Select a DAS service from the table"\r
171               + " to read a full description here.</font></html>");\r
172       return;\r
173     }\r
174 \r
175     int dSize = dasSources.length;\r
176     for (int i = 0; i < dSize; i++)\r
177     {\r
178       if (!dasSources[i].getNickname().equals(nickName))\r
179       {\r
180         continue;\r
181       }\r
182 \r
183       DasSource ds = dasSources[i];\r
184 \r
185       text.append("<font color=\"#0000FF\">Id:</font> "\r
186               + dasSources[i].getId() + "<br>");\r
187       text.append("<font color=\"#0000FF\">Nickname:</font> "\r
188               + dasSources[i].getNickname() + "<br>");\r
189       text.append("<font color=\"#0000FF\">URL:</font> "\r
190               + dasSources[i].getUrl() + "<br>");\r
191 \r
192       text\r
193               .append("<font color=\"#0000FF\">Admin Email:</font> <a href=\"mailto:"\r
194                       + dasSources[i].getAdminemail()\r
195                       + "\">"\r
196                       + dasSources[i].getAdminemail() + "</a>" + "<br>");\r
197 \r
198       text.append("<font color=\"#0000FF\">Registered at:</font> "\r
199               + dasSources[i].getRegisterDate() + "<br>");\r
200 \r
201       text.append("<font color=\"#0000FF\">Last successful test:</font> "\r
202               + dasSources[i].getLeaseDate() + "<br>");\r
203 \r
204       text.append("<font color=\"#0000FF\">Labels:</font> ");\r
205       for (int s = 0; s < dasSources[i].getLabels().length; s++)\r
206       {\r
207         text.append(dasSources[i].getLabels()[s]);\r
208         if (s < dasSources[i].getLabels().length - 1)\r
209         {\r
210           text.append(",");\r
211         }\r
212         text.append(" ");\r
213       }\r
214       text.append("<br>");\r
215 \r
216       text.append("<font color=\"#0000FF\">Capabilities:</font> ");\r
217       String[] scap = dasSources[i].getCapabilities();\r
218       for (int j = 0; j < scap.length; j++)\r
219       {\r
220         text.append(scap[j]);\r
221         if (j < scap.length - 1)\r
222         {\r
223           text.append(", ");\r
224         }\r
225       }\r
226       text.append("<br>");\r
227 \r
228       text.append("<font color=\"#0000FF\">Coordinates:</font> ");\r
229       DasCoordinateSystem[] dcs = ds.getCoordinateSystem();\r
230       for (int j = 0; j < dcs.length; j++)\r
231       {\r
232         text.append("(" + dcs[j].getUniqueId() + ") "\r
233                 + dcs[j].getCategory() + ", " + dcs[j].getName());\r
234         if (dcs[j].getNCBITaxId() != 0)\r
235         {\r
236           text.append(", " + dcs[j].getNCBITaxId());\r
237         }\r
238         if (dcs[j].getOrganismName().length() > 0)\r
239         {\r
240           text.append(", " + dcs[j].getOrganismName());\r
241         }\r
242 \r
243         text.append("<br>");\r
244       }\r
245 \r
246       text.append("<font color=\"#0000FF\">Description:</font> "\r
247               + dasSources[i].getDescription() + "<br>");\r
248 \r
249       if (dasSources[i].getHelperurl() != null\r
250               && dasSources[i].getHelperurl().length() > 0)\r
251       {\r
252         text.append("<font color=\"#0000FF\"><a href=\""\r
253                 + dasSources[i].getHelperurl()\r
254                 + "\">Go to site</a></font<br>");\r
255       }\r
256 \r
257       text.append("</font></html>");\r
258 \r
259       break;\r
260     }\r
261 \r
262     fullDetails.setText(text.toString());\r
263     javax.swing.SwingUtilities.invokeLater(new Runnable()\r
264     {\r
265       public void run()\r
266       {\r
267         fullDetailsScrollpane.getVerticalScrollBar().setValue(0);\r
268       }\r
269     });\r
270   }\r
271 \r
272   public void run()\r
273   {\r
274     loadingDasSources = true;\r
275 \r
276     addLocal.setVisible(false);\r
277     refresh.setVisible(false);\r
278     progressBar.setVisible(true);\r
279     progressBar.setIndeterminate(true);\r
280     setParentGuiEnabled(false);\r
281     // Refresh the source list.\r
282     dasSources = null;\r
283     getDASSource();\r
284 \r
285     init();\r
286 \r
287     setParentGuiEnabled(true);\r
288     loadingDasSources = false;\r
289 \r
290   }\r
291 \r
292   private void setParentGuiEnabled(boolean b)\r
293   {\r
294     if (fs!=null)\r
295     {\r
296       fs.fetchDAS.setEnabled(b);\r
297       fs.saveDAS.setEnabled(b);\r
298     }\r
299   }\r
300 \r
301   public Vector getSelectedSources()\r
302   {\r
303     // wait around if we're still loading.\r
304     while (dasSources == null)\r
305     {\r
306       if (!loadingDasSources)\r
307       {\r
308         new Thread(this).start();\r
309         try\r
310         {\r
311           Thread.sleep(5);\r
312         } catch (Exception e)\r
313         {\r
314         }\r
315         ;\r
316         while (loadingDasSources)\r
317         {\r
318           try\r
319           {\r
320             Thread.sleep(5);\r
321           } catch (Exception e)\r
322           {\r
323           }\r
324           ;\r
325         }\r
326         ;\r
327       }\r
328     }\r
329 \r
330     Vector selected = new Vector();\r
331     for (int r = 0; r < selectedSources.size(); r++)\r
332     {\r
333       for (int i = 0; i < dasSources.length; i++)\r
334       {\r
335         if (dasSources[i].getNickname()\r
336                 .equals(selectedSources.elementAt(r)))\r
337         {\r
338           selected.addElement(dasSources[i]);\r
339           break;\r
340         }\r
341       }\r
342     }\r
343 \r
344     return selected;\r
345   }\r
346 \r
347   /**\r
348    * retrieve das sources from registry and add local source list\r
349    * \r
350    * @return\r
351    */\r
352   public DasSource[] getDASSource()\r
353   {\r
354     if (dasSources == null)\r
355     {\r
356       dasSources = jalview.ws.DasSequenceFeatureFetcher.getDASSources();\r
357       appendLocalSources();\r
358     }\r
359 \r
360     return dasSources;\r
361   }\r
362 \r
363   public void refresh_actionPerformed(ActionEvent e)\r
364   {\r
365     saveProperties(jalview.bin.Cache.applicationProperties);\r
366 \r
367     Thread worker = new Thread(this);\r
368     worker.start();\r
369   }\r
370 \r
371   private void setCapabilities(DasSource[] sources)\r
372   {\r
373     Vector authority = new Vector();\r
374     Vector type = new Vector();\r
375     Vector label = new Vector();\r
376 \r
377     authority.addElement("Any");\r
378     type.addElement("Any");\r
379     label.addElement("Any");\r
380 \r
381     for (int i = 0; i < sources.length; i++)\r
382     {\r
383       DasSource ds = sources[i];\r
384 \r
385       DasCoordinateSystem[] dcs = ds.getCoordinateSystem();\r
386 \r
387       for (int j = 0; j < dcs.length; j++)\r
388       {\r
389         if (!type.contains(dcs[j].getCategory()))\r
390         {\r
391           type.addElement(dcs[j].getCategory());\r
392         }\r
393 \r
394         if (!authority.contains(dcs[j].getName()))\r
395         {\r
396           authority.addElement(dcs[j].getName());\r
397         }\r
398       }\r
399 \r
400       String[] slabels = ds.getLabels();\r
401       for (int s = 0; s < slabels.length; s++)\r
402       {\r
403         if (!label.contains(slabels[s]))\r
404         {\r
405           label.addElement(slabels[s]);\r
406         }\r
407       }\r
408 \r
409     }\r
410 \r
411     filter1.setListData(authority);\r
412     filter2.setListData(type);\r
413     filter3.setListData(label);\r
414 \r
415     javax.swing.SwingUtilities.invokeLater(new Runnable()\r
416     {\r
417       public void run()\r
418       {\r
419         filter1.setSelectedIndex(0);\r
420         filter2.setSelectedIndex(0);\r
421         filter3.setSelectedIndex(0);\r
422       }\r
423     });\r
424   }\r
425 \r
426   public void amendLocal(boolean newSource)\r
427   {\r
428     String url = "http://localhost:8080/", nickname = "";\r
429     boolean seqsrc = false;\r
430     if (!newSource)\r
431     {\r
432       int selectedRow = table.getSelectionModel().getMinSelectionIndex();\r
433       nickname = table.getValueAt(selectedRow, 0).toString();\r
434       url = ((DasSource) localSources.get(nickname)).getUrl();\r
435       seqsrc = ((DasSource) localSources.get(nickname))\r
436               .hasCapability("sequence");\r
437     }\r
438 \r
439     JTextField nametf = new JTextField(nickname, 40);\r
440     JTextField urltf = new JTextField(url, 40);\r
441     JCheckBox seqs = new JCheckBox("Sequence Source");\r
442     seqs.setSelected(seqsrc);\r
443     JPanel panel = new JPanel(new BorderLayout());\r
444     JPanel pane12 = new JPanel(new BorderLayout());\r
445     pane12.add(new JLabel("Nickname: "), BorderLayout.CENTER);\r
446     pane12.add(nametf, BorderLayout.EAST);\r
447     panel.add(pane12, BorderLayout.NORTH);\r
448     pane12 = new JPanel(new BorderLayout());\r
449     pane12.add(new JLabel("URL: "), BorderLayout.NORTH);\r
450     pane12.add(seqs, BorderLayout.SOUTH);\r
451     pane12.add(urltf, BorderLayout.EAST);\r
452     panel.add(pane12, BorderLayout.SOUTH);\r
453 \r
454     int reply = JOptionPane.showInternalConfirmDialog(Desktop.desktop,\r
455             panel, "Enter Nickname & URL of Local DAS Source",\r
456             JOptionPane.OK_CANCEL_OPTION);\r
457 \r
458     if (reply != JOptionPane.OK_OPTION)\r
459     {\r
460       return;\r
461     }\r
462 \r
463     if (!urltf.getText().endsWith("/"))\r
464     {\r
465       urltf.setText(urltf.getText() + "/");\r
466     }\r
467 \r
468     Das1Source local = new Das1Source();\r
469 \r
470     local.setUrl(urltf.getText());\r
471     local.setNickname(nametf.getText());\r
472     if (seqs.isSelected())\r
473     {\r
474       local.setCapabilities(new String[]\r
475       { "features", "sequence" });\r
476     }\r
477     if (localSources == null)\r
478     {\r
479       localSources = new Hashtable();\r
480     }\r
481 \r
482     localSources.put(local.getNickname(), local);\r
483 \r
484     if (!newSource && !nickname.equals(nametf.getText()))\r
485     {\r
486       localSources.remove(nickname);\r
487     }\r
488 \r
489     int size = dasSources.length;\r
490     int adjust = newSource ? 1 : 0;\r
491 \r
492     Object[][] data = new Object[size + adjust][2];\r
493     for (int i = 0; i < size; i++)\r
494     {\r
495       if (!newSource && dasSources[i].getNickname().equals(nickname))\r
496       {\r
497         ((DasSource) dasSources[i]).setNickname(local.getNickname());\r
498         ((DasSource) dasSources[i]).setUrl(local.getUrl());\r
499         data[i][0] = local.getNickname();\r
500         data[i][1] = new Boolean(true);\r
501       }\r
502       else\r
503       {\r
504         data[i][0] = dasSources[i].getNickname();\r
505         data[i][1] = new Boolean(selectedSources.contains(dasSources[i]\r
506                 .getNickname()));\r
507       }\r
508     }\r
509 \r
510     if (newSource)\r
511     {\r
512       data[size][0] = local.getNickname();\r
513       data[size][1] = new Boolean(true);\r
514       selectedSources.add(local.getNickname());\r
515     }\r
516 \r
517     DasSource[] tmp = new DasSource[size + adjust];\r
518 \r
519     System.arraycopy(dasSources, 0, tmp, 0, size);\r
520 \r
521     if (newSource)\r
522     {\r
523       tmp[size] = local;\r
524     }\r
525 \r
526     dasSources = tmp;\r
527 \r
528     refreshTableData(data);\r
529 \r
530     SwingUtilities.invokeLater(new Runnable()\r
531     {\r
532       public void run()\r
533       {\r
534         scrollPane.getVerticalScrollBar().setValue(\r
535                 scrollPane.getVerticalScrollBar().getMaximum());\r
536       }\r
537     });\r
538 \r
539     displayFullDetails(local.getNickname());\r
540   }\r
541 \r
542   public void editRemoveLocalSource(MouseEvent evt)\r
543   {\r
544     int selectedRow = table.getSelectionModel().getMinSelectionIndex();\r
545     if (selectedRow == -1)\r
546     {\r
547       return;\r
548     }\r
549 \r
550     String nickname = table.getValueAt(selectedRow, 0).toString();\r
551 \r
552     if (!localSources.containsKey(nickname))\r
553     {\r
554       JOptionPane.showInternalMessageDialog(Desktop.desktop,\r
555               "You can only edit or remove local DAS Sources!",\r
556               "Public DAS source - not editable",\r
557               JOptionPane.WARNING_MESSAGE);\r
558       return;\r
559     }\r
560 \r
561     Object[] options =\r
562     { "Edit", "Remove", "Cancel" };\r
563     int choice = JOptionPane.showInternalOptionDialog(Desktop.desktop,\r
564             "Do you want to edit or remove " + nickname + "?",\r
565             "Edit / Remove Local DAS Source",\r
566             JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE,\r
567             null, options, options[2]);\r
568 \r
569     switch (choice)\r
570     {\r
571     case 0:\r
572       amendLocal(false);\r
573       break;\r
574     case 1:\r
575       localSources.remove(nickname);\r
576       selectedSources.remove(nickname);\r
577       Object[][] data = new Object[dasSources.length - 1][2];\r
578       DasSource[] tmp = new DasSource[dasSources.length - 1];\r
579       int index = 0;\r
580       for (int i = 0; i < dasSources.length; i++)\r
581       {\r
582         if (dasSources[i].getNickname().equals(nickname))\r
583         {\r
584           continue;\r
585         }\r
586         else\r
587         {\r
588           tmp[index] = dasSources[i];\r
589           data[index][0] = dasSources[i].getNickname();\r
590           data[index][1] = new Boolean(selectedSources\r
591                   .contains(dasSources[i].getNickname()));\r
592           index++;\r
593         }\r
594       }\r
595       dasSources = tmp;\r
596       refreshTableData(data);\r
597       SwingUtilities.invokeLater(new Runnable()\r
598       {\r
599         public void run()\r
600         {\r
601           scrollPane.getVerticalScrollBar().setValue(\r
602                   scrollPane.getVerticalScrollBar().getMaximum());\r
603         }\r
604       });\r
605 \r
606       break;\r
607     }\r
608   }\r
609 \r
610   void appendLocalSources()\r
611   {\r
612     if (localSources == null)\r
613     {\r
614       return;\r
615     }\r
616     // note - we add all das sources to list so they can be filtered for the\r
617     // standard fetchDbRefs function\r
618     int size = dasSources != null ? dasSources.length : 0;\r
619     int lsize = localSources.size();\r
620 \r
621     Object[][] data = new Object[size + lsize][2];\r
622     for (int i = 0; i < size; i++)\r
623     {\r
624       data[i][0] = dasSources[i].getNickname();\r
625       data[i][1] = new Boolean(selectedSources.contains(dasSources[i]\r
626               .getNickname()));\r
627     }\r
628 \r
629     DasSource[] tmp = new DasSource[size + lsize];\r
630     if (dasSources != null)\r
631     {\r
632       System.arraycopy(dasSources, 0, tmp, 0, size);\r
633     }\r
634 \r
635     Enumeration en = localSources.keys();\r
636     int index = size;\r
637     while (en.hasMoreElements())\r
638     {\r
639       String key = en.nextElement().toString();\r
640       data[index][0] = key;\r
641       data[index][1] = new Boolean(false);\r
642       tmp[index] = new Das1Source();\r
643       tmp[index].setNickname(key);\r
644       tmp[index].setUrl(((DasSource) localSources.get(key)).getUrl());\r
645 \r
646       index++;\r
647     }\r
648 \r
649     dasSources = tmp;\r
650 \r
651     refreshTableData(data);\r
652   }\r
653 \r
654   public void valueChanged(ListSelectionEvent evt)\r
655   {\r
656     // Called when the MainTable selection changes\r
657     if (evt.getValueIsAdjusting())\r
658     {\r
659       return;\r
660     }\r
661 \r
662     displayFullDetails(null);\r
663 \r
664     // Filter the displayed data sources\r
665     int dSize = dasSources.length;\r
666 \r
667     ArrayList names = new ArrayList();\r
668     ArrayList selected = new ArrayList();\r
669     DasSource ds;\r
670 \r
671     // The features filter is not visible, but we must still\r
672     // filter the das source list here.\r
673     // July 2006 - only 6 sources fo not serve features\r
674     Object[] dummyFeatureList = new Object[]\r
675     { "features" };\r
676 \r
677     for (int i = 0; i < dSize; i++)\r
678     {\r
679       ds = dasSources[i];\r
680       DasCoordinateSystem[] dcs = ds.getCoordinateSystem();\r
681 \r
682       if (dcs.length == 0 && ds.getCapabilities().length == 0\r
683               && filter1.getSelectedIndex() == 0\r
684               && filter2.getSelectedIndex() == 0\r
685               && filter3.getSelectedIndex() == 0)\r
686       {\r
687         // THIS IS A FIX FOR LOCAL SOURCES WHICH DO NOT\r
688         // HAVE COORDINATE SYSTEMS, INFO WHICH AT PRESENT\r
689         // IS ADDED FROM THE REGISTRY\r
690         names.add(ds.getNickname());\r
691         selected\r
692                 .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(), ds\r
698                       .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, sb\r
809             .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, sb\r
826               .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    * @param b\r
955    */\r
956   public void setGuiEnabled(boolean b)\r
957   {\r
958     refresh.setEnabled(b);\r
959     addLocal.setEnabled(b);\r
960   }\r
961 }\r