2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8)
3 * Copyright (C) 2012 J Procter, AM Waterhouse, LM Lui, J Engelhardt, G Barton, M Clamp, S Searle
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 of the License, or (at your option) any later version.
11 * Jalview is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
20 import jalview.jbgui.GDasSourceBrowser;
21 import jalview.util.TableSorter;
22 import jalview.ws.dbsources.das.api.DasSourceRegistryI;
23 import jalview.ws.dbsources.das.api.jalviewSourceI;
25 import java.awt.BorderLayout;
26 import java.awt.event.ActionEvent;
27 import java.awt.event.MouseAdapter;
28 import java.awt.event.MouseEvent;
29 import java.util.ArrayList;
30 import java.util.List;
31 import java.util.Properties;
32 import java.util.StringTokenizer;
33 import java.util.Vector;
35 import javax.swing.JCheckBox;
36 import javax.swing.JLabel;
37 import javax.swing.JOptionPane;
38 import javax.swing.JPanel;
39 import javax.swing.JTextField;
40 import javax.swing.ListSelectionModel;
41 import javax.swing.SwingUtilities;
42 import javax.swing.event.ListSelectionEvent;
43 import javax.swing.event.ListSelectionListener;
44 import javax.swing.table.AbstractTableModel;
46 import org.biodas.jdas.schema.sources.CAPABILITY;
47 import org.biodas.jdas.schema.sources.COORDINATES;
48 import org.biodas.jdas.schema.sources.PROP;
49 import org.biodas.jdas.schema.sources.VERSION;
51 public class DasSourceBrowser extends GDasSourceBrowser implements
52 Runnable, ListSelectionListener
54 DasSourceRegistryI sourceRegistry = null;
56 Vector<String> selectedSources;
58 public DasSourceBrowser(FeatureSettings featureSettings)
61 // TODO DasSourceRegistryProvider API
62 sourceRegistry = jalview.bin.Cache.getDasSourceRegistry();
63 String registry = sourceRegistry.getDasRegistryURL();
65 registryURL.setText(registry);
67 setSelectedFromProperties();
69 displayFullDetails(null);
70 table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
72 filter1.addListSelectionListener(this);
73 filter2.addListSelectionListener(this);
74 filter3.addListSelectionListener(this);
76 // Ask to be notified of selection changes.
77 ListSelectionModel rowSM = table.getSelectionModel();
78 rowSM.addListSelectionListener(new ListSelectionListener()
80 public void valueChanged(ListSelectionEvent e)
82 ListSelectionModel lsm = (ListSelectionModel) e.getSource();
83 if (!lsm.isSelectionEmpty())
85 int selectedRow = lsm.getMinSelectionIndex();
86 displayFullDetails(table.getValueAt(selectedRow, 0).toString());
91 table.addMouseListener(new MouseAdapter()
93 public void mouseClicked(MouseEvent evt)
95 if (evt.getClickCount() == 2
96 || SwingUtilities.isRightMouseButton(evt))
98 editRemoveLocalSource(evt);
103 if (sourceRegistry.getSources() != null)
109 FeatureSettings fs = null;
111 private boolean loadingDasSources;
113 public DasSourceBrowser()
118 public void paintComponent(java.awt.Graphics g)
120 if (sourceRegistry == null)
122 Thread worker = new Thread(this);
129 List<jalviewSourceI> sources = sourceRegistry.getSources();
130 int dSize = sources.size();
131 Object[][] data = new Object[dSize][2];
132 for (int i = 0; i < dSize; i++)
134 data[i][0] = sources.get(i).getTitle(); // what's equivalent of nickname
135 data[i][1] = new Boolean(selectedSources.contains(sources.get(i)
139 refreshTableData(data);
140 setCapabilities(sourceRegistry);
142 javax.swing.SwingUtilities.invokeLater(new Runnable()
146 TableSorter sorter = (TableSorter) table.getModel();
147 sorter.setSortingStatus(1, TableSorter.DESCENDING);
148 sorter.setSortingStatus(1, TableSorter.NOT_SORTED);
152 progressBar.setIndeterminate(false);
153 progressBar.setVisible(false);
154 addLocal.setVisible(true);
155 refresh.setVisible(true);
158 public void refreshTableData(Object[][] data)
160 TableSorter sorter = new TableSorter(new DASTableModel(data));
161 sorter.setTableHeader(table.getTableHeader());
162 table.setModel(sorter);
165 void displayFullDetails(String nickName)
168 StringBuffer text = new StringBuffer(
169 "<HTML><font size=\"2\" face=\"Verdana, Arial, Helvetica, sans-serif\">");
171 if (nickName == null)
173 fullDetails.setText(text + "Select a DAS service from the table"
174 + " to read a full description here.</font></html>");
178 int dSize = sourceRegistry.getSources().size();
179 for (jalviewSourceI ds : sourceRegistry.getSources())
181 if (!ds.getTitle().equals(nickName))
186 VERSION latest = ds.getVersion();
187 text.append("<font color=\"#0000FF\">Id:</font> " + ds.getUri()
189 text.append("<font color=\"#0000FF\">Nickname:</font> "
190 + ds.getTitle() + "<br>");
192 text.append("<font color=\"#0000FF\">URL:</font> <a href=\""
193 + ds.getSourceURL() + "\">" + ds.getSourceURL() + "</a>"
197 if (ds.getDocHref() != null && ds.getDocHref().length() > 0)
199 text.append("<font color=\"#0000FF\">Site:</font> <a href=\""
200 + ds.getDocHref() + "\">" + ds.getDocHref() + "</a>"
204 text.append("<font color=\"#0000FF\">Description:</font> "
205 + ds.getDescription() + "<br>");
207 text.append("<font color=\"#0000FF\">Admin Email:</font> <a href=\"mailto:"
208 + ds.getEmail() + "\">" + ds.getEmail() + "</a>" + "<br>");
210 text.append("<font color=\"#0000FF\">Registered at:</font> "
211 + latest.getCreated() + "<br>");
213 // TODO: Identify last successful test date
214 // text.append("<font color=\"#0000FF\">Last successful test:</font> "
215 // + latest.dasSources[i].getLeaseDate() + "<br>");
219 text.append("Source was added manually.<br/>");
221 text.append("<font color=\"#0000FF\">Labels:</font> ");
223 for (PROP labl : latest.getPROP())
225 if (labl.getName().equalsIgnoreCase("LABEL"))
233 text.append(labl.getValue());
240 text.append("<font color=\"#0000FF\">Capabilities:</font> ");
241 CAPABILITY[] scap = latest.getCAPABILITY().toArray(new CAPABILITY[0]);
242 for (int j = 0; j < scap.length; j++)
244 text.append(scap[j].getType());
245 if (j < scap.length - 1)
252 text.append("<font color=\"#0000FF\">Coordinates:</font>");
254 for (COORDINATES dcs : latest.getCOORDINATES())
256 text.append("<br/>" + i++ + ". ");
257 text.append(dcs.getAuthority() + " : " + dcs.getSource());
258 if (dcs.getTaxid() != null && dcs.getTaxid().trim().length() > 0)
260 text.append(" [TaxId:" + dcs.getTaxid() + "]");
262 if (dcs.getVersion() != null
263 && dcs.getVersion().trim().length() > 0)
266 text.append(" {v. " + dcs.getVersion() + "}");
269 text.append(" (<a href=\"" + dcs.getUri() + "\">" + dcs.getUri()
272 text.append("</font></html>");
277 fullDetails.setText(text.toString());
278 javax.swing.SwingUtilities.invokeLater(new Runnable()
282 fullDetailsScrollpane.getVerticalScrollBar().setValue(0);
289 loadingDasSources = true;
291 addLocal.setVisible(false);
292 refresh.setVisible(false);
293 progressBar.setVisible(true);
294 progressBar.setIndeterminate(true);
295 setParentGuiEnabled(false);
296 // Refresh the source list.
297 sourceRegistry.refreshSources();
301 setParentGuiEnabled(true);
302 loadingDasSources = false;
306 private void setParentGuiEnabled(boolean b)
310 fs.fetchDAS.setEnabled(b);
311 fs.saveDAS.setEnabled(b);
315 public Vector<jalviewSourceI> getSelectedSources()
317 // wait around if we're still loading.
318 while (sourceRegistry == null)
320 if (!loadingDasSources)
322 new Thread(this).start();
326 } catch (Exception e)
330 while (loadingDasSources)
335 } catch (Exception e)
344 Vector<jalviewSourceI> selected = new Vector<jalviewSourceI>();
345 for (String source : selectedSources)
347 jalviewSourceI srce = sourceRegistry.getSource(source);
350 selected.addElement(srce);
356 public void refresh_actionPerformed(ActionEvent e)
358 saveProperties(jalview.bin.Cache.applicationProperties);
360 Thread worker = new Thread(this);
364 private void setCapabilities(DasSourceRegistryI sourceRegistry2)
366 Vector<String> authority = new Vector<String>();
367 Vector<String> type = new Vector<String>();
368 Vector<String> label = new Vector<String>();
369 Vector<String> taxIds = new Vector<String>();
370 authority.add("Any");
374 for (jalviewSourceI ds : sourceRegistry2.getSources())
376 VERSION latest = ds.getVersion();
378 for (COORDINATES cs : latest.getCOORDINATES())
380 if (!type.contains(cs.getSource()))
382 type.add(cs.getSource()); // source==category
385 if (!authority.contains(cs.getAuthority()))
387 authority.add(cs.getAuthority());
391 for (PROP slabel : latest.getPROP())
393 if (slabel.getName().equalsIgnoreCase("LABEL")
394 && !label.contains(slabel.getValue()))
396 label.add(slabel.getValue());
402 filter1.setListData(authority);
403 filter2.setListData(type);
404 filter3.setListData(label);
407 javax.swing.SwingUtilities.invokeLater(new Runnable()
411 filter1.setSelectedIndex(0);
412 filter2.setSelectedIndex(0);
413 filter3.setSelectedIndex(0);
418 public void amendLocal(boolean newSource)
420 String url = "http://localhost:8080/", nickname = "";
421 boolean seqsrc = false;
424 int selectedRow = table.getSelectionModel().getMinSelectionIndex();
425 nickname = table.getValueAt(selectedRow, 0).toString();
426 jalviewSourceI source = sourceRegistry.getSource(nickname);
427 url = source.getUri();
428 seqsrc = source.isSequenceSource();
431 JTextField nametf = new JTextField(nickname, 40);
432 JTextField urltf = new JTextField(url, 40);
433 JCheckBox seqs = new JCheckBox("Sequence Source");
434 seqs.setSelected(seqsrc);
435 JPanel panel = new JPanel(new BorderLayout());
436 JPanel pane12 = new JPanel(new BorderLayout());
437 pane12.add(new JLabel("Nickname: "), BorderLayout.CENTER);
438 pane12.add(nametf, BorderLayout.EAST);
439 panel.add(pane12, BorderLayout.NORTH);
440 pane12 = new JPanel(new BorderLayout());
441 pane12.add(new JLabel("URL: "), BorderLayout.NORTH);
442 pane12.add(seqs, BorderLayout.SOUTH);
443 pane12.add(urltf, BorderLayout.EAST);
444 panel.add(pane12, BorderLayout.SOUTH);
446 int reply = JOptionPane.showInternalConfirmDialog(Desktop.desktop,
447 panel, "Enter Nickname & URL of Local DAS Source",
448 JOptionPane.OK_CANCEL_OPTION);
450 if (reply != JOptionPane.OK_OPTION)
455 if (!urltf.getText().endsWith("/"))
457 urltf.setText(urltf.getText() + "/");
460 jalviewSourceI local = sourceRegistry.createLocalSource(
461 urltf.getText(), nametf.getText(), seqs.isSelected(), true);
462 List sources = sourceRegistry.getSources();
463 int osize = sources.size();
464 int size = osize + (newSource ? 1 : 0);
466 Object[][] data = new Object[size][2];
467 DASTableModel dtm = (table != null) ? (DASTableModel) ((TableSorter) table
468 .getModel()).getTableModel() : null;
469 for (int i = 0; i < osize; i++)
471 String osrc = (dtm == null || i >= osize) ? null : (String) dtm
473 if (!newSource && osrc != null
474 && dtm.getValueAt(i, 0).equals(nickname))
476 data[i][0] = local.getTitle();
477 data[i][1] = new Boolean(true);
482 data[i][1] = new Boolean(selectedSources.contains(osrc));
485 // Always add a new source at the end
488 data[osize][0] = local.getTitle();
489 data[osize][1] = new Boolean(true);
490 selectedSources.add(local.getTitle());
493 refreshTableData(data);
495 SwingUtilities.invokeLater(new Runnable()
499 scrollPane.getVerticalScrollBar().setValue(
500 scrollPane.getVerticalScrollBar().getMaximum());
504 displayFullDetails(local.getTitle());
507 public void editRemoveLocalSource(MouseEvent evt)
509 int selectedRow = table.getSelectionModel().getMinSelectionIndex();
510 if (selectedRow == -1)
515 String nickname = table.getValueAt(selectedRow, 0).toString();
517 if (!sourceRegistry.getSource(nickname).isLocal())
519 JOptionPane.showInternalMessageDialog(Desktop.desktop,
520 "You can only edit or remove local DAS Sources!",
521 "Public DAS source - not editable",
522 JOptionPane.WARNING_MESSAGE);
527 { "Edit", "Remove", "Cancel" };
528 int choice = JOptionPane.showInternalOptionDialog(Desktop.desktop,
529 "Do you want to edit or remove " + nickname + "?",
530 "Edit / Remove Local DAS Source",
531 JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE,
532 null, options, options[2]);
540 sourceRegistry.removeLocalSource(sourceRegistry.getSource(nickname));
541 selectedSources.remove(nickname);
542 Object[][] data = new Object[sourceRegistry.getSources().size()][2];
544 l = table.getRowCount();
546 for (int i = 0; i < l; i++)
549 if ((nm = (String) table.getValueAt(i, 0)).equals(nickname))
556 data[index][1] = new Boolean(selectedSources.contains(nm));
560 refreshTableData(data);
561 SwingUtilities.invokeLater(new Runnable()
565 scrollPane.getVerticalScrollBar().setValue(
566 scrollPane.getVerticalScrollBar().getMaximum());
574 public void valueChanged(ListSelectionEvent evt)
576 // Called when the MainTable selection changes
577 if (evt.getValueIsAdjusting())
582 displayFullDetails(null);
584 // Filter the displayed data sources
586 ArrayList names = new ArrayList();
587 ArrayList selected = new ArrayList();
589 // The features filter is not visible, but we must still
590 // filter the das source list here.
591 // July 2006 - only 6 sources fo not serve features
592 Object[] dummyFeatureList = new Object[]
594 List<jalviewSourceI> srcs = sourceRegistry.getSources();
595 for (jalviewSourceI ds : srcs)
598 VERSION v = ds.getVersion();
599 List<COORDINATES> coords = v.getCOORDINATES();
601 || ((coords == null || coords.size() == 0)
602 && filter1.getSelectedIndex() == 0
603 && filter2.getSelectedIndex() == 0 && filter3
604 .getSelectedIndex() == 0))
606 // THIS IS A FIX FOR LOCAL SOURCES WHICH DO NOT
607 // HAVE COORDINATE SYSTEMS, INFO WHICH AT PRESENT
608 // IS ADDED FROM THE REGISTRY
609 names.add(ds.getTitle());
610 selected.add(new Boolean(selectedSources.contains(ds.getTitle())));
614 if (!selectedInList(dummyFeatureList, ds.getCapabilityList(v))
615 || !selectedInList(filter3.getSelectedValues(),
621 for (int j = 0; j < coords.size(); j++)
623 if (selectedInList(filter1.getSelectedValues(), new String[]
624 { coords.get(j).getAuthority() })
625 && selectedInList(filter2.getSelectedValues(), new String[]
626 { coords.get(j).getSource() }))
628 names.add(ds.getTitle());
629 selected.add(new Boolean(selectedSources.contains(ds.getTitle())));
635 int dSize = names.size();
636 Object[][] data = new Object[dSize][2];
637 for (int d = 0; d < dSize; d++)
639 data[d][0] = names.get(d);
640 data[d][1] = selected.get(d);
643 refreshTableData(data);
646 private boolean selectedInList(Object[] selection, String[] items)
648 for (int i = 0; i < selection.length; i++)
650 if (selection[i].equals("Any"))
654 if (items == null || items.length == 0)
658 String sel = (items[0].startsWith("das1:") ? "das1:" : "")
660 for (int j = 0; j < items.length; j++)
662 if (sel.equals(items[j]))
672 void setSelectedFromProperties()
674 String active = jalview.bin.Cache.getDefault("DAS_ACTIVE_SOURCE",
676 StringTokenizer st = new StringTokenizer(active, "\t");
677 selectedSources = new Vector();
678 while (st.hasMoreTokens())
680 selectedSources.addElement(st.nextToken());
684 public void reset_actionPerformed(ActionEvent e)
686 registryURL.setText(sourceRegistry.getDasRegistryURL());
690 * set the DAS source settings in the given jalview properties.
694 public void saveProperties(Properties properties)
696 if (registryURL.getText() == null || registryURL.getText().length() < 1)
698 properties.remove(jalview.bin.Cache.DAS_REGISTRY_URL);
702 properties.setProperty(jalview.bin.Cache.DAS_REGISTRY_URL,
703 registryURL.getText());
706 StringBuffer sb = new StringBuffer();
707 for (int r = 0; r < table.getModel().getRowCount(); r++)
709 if (((Boolean) table.getValueAt(r, 1)).booleanValue())
711 sb.append(table.getValueAt(r, 0) + "\t");
715 properties.setProperty(jalview.bin.Cache.DAS_ACTIVE_SOURCE,
718 String sourceprop = sourceRegistry.getLocalSourceString();
719 properties.setProperty(jalview.bin.Cache.DAS_LOCAL_SOURCE, sourceprop);
722 class DASTableModel extends AbstractTableModel
725 public DASTableModel(Object[][] data)
730 private String[] columnNames = new String[]
731 { "Nickname", "Use Source" };
733 private Object[][] data;
735 public int getColumnCount()
737 return columnNames.length;
740 public int getRowCount()
745 public String getColumnName(int col)
747 return columnNames[col];
750 public Object getValueAt(int row, int col)
752 return data[row][col];
756 * JTable uses this method to determine the default renderer/ editor for
757 * each cell. If we didn't implement this method, then the last column would
758 * contain text ("true"/"false"), rather than a check box.
760 public Class getColumnClass(int c)
762 return getValueAt(0, c).getClass();
766 * Don't need to implement this method unless your table's editable.
768 public boolean isCellEditable(int row, int col)
770 // Note that the data/cell address is constant,
771 // no matter where the cell appears onscreen.
777 * Don't need to implement this method unless your table's data can change.
779 public void setValueAt(Object value, int row, int col)
781 data[row][col] = value;
782 fireTableCellUpdated(row, col);
784 String name = getValueAt(row, 0).toString();
785 boolean selected = ((Boolean) value).booleanValue();
787 if (selectedSources.contains(name) && !selected)
789 selectedSources.remove(name);
792 if (!selectedSources.contains(name) && selected)
794 selectedSources.add(name);
799 public void initDasSources()
802 Thread thr = new Thread(new Runnable()
806 // this actually initialises the das source list
807 paintComponent(null); // yuk
811 while (loadingDasSources || sourceRegistry == null)
816 } catch (Exception e)
824 * disable or enable the buttons on the source browser
828 public void setGuiEnabled(boolean b)
830 refresh.setEnabled(b);
831 addLocal.setEnabled(b);