2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.0b1)
3 * Copyright (C) 2014 The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3 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/>.
17 * The Jalview Authors are detailed in the 'AUTHORS' file.
21 import jalview.jbgui.GDasSourceBrowser;
22 import jalview.util.MessageManager;
23 import jalview.util.TableSorter;
24 import jalview.ws.dbsources.das.api.DasSourceRegistryI;
25 import jalview.ws.dbsources.das.api.jalviewSourceI;
27 import java.awt.BorderLayout;
28 import java.awt.event.ActionEvent;
29 import java.awt.event.MouseAdapter;
30 import java.awt.event.MouseEvent;
31 import java.util.ArrayList;
32 import java.util.List;
33 import java.util.Properties;
34 import java.util.StringTokenizer;
35 import java.util.Vector;
37 import javax.swing.JCheckBox;
38 import javax.swing.JLabel;
39 import javax.swing.JOptionPane;
40 import javax.swing.JPanel;
41 import javax.swing.JTextField;
42 import javax.swing.ListSelectionModel;
43 import javax.swing.SwingUtilities;
44 import javax.swing.event.ListSelectionEvent;
45 import javax.swing.event.ListSelectionListener;
46 import javax.swing.table.AbstractTableModel;
48 import org.biodas.jdas.schema.sources.CAPABILITY;
49 import org.biodas.jdas.schema.sources.COORDINATES;
50 import org.biodas.jdas.schema.sources.PROP;
51 import org.biodas.jdas.schema.sources.VERSION;
53 public class DasSourceBrowser extends GDasSourceBrowser implements
54 Runnable, ListSelectionListener
56 DasSourceRegistryI sourceRegistry = null;
58 Vector<String> selectedSources;
60 public DasSourceBrowser(FeatureSettings featureSettings)
63 // TODO DasSourceRegistryProvider API
64 sourceRegistry = jalview.bin.Cache.getDasSourceRegistry();
65 String registry = sourceRegistry.getDasRegistryURL();
67 registryURL.setText(registry);
69 setSelectedFromProperties();
71 displayFullDetails(null);
72 table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
74 filter1.addListSelectionListener(this);
75 filter2.addListSelectionListener(this);
76 filter3.addListSelectionListener(this);
78 // Ask to be notified of selection changes.
79 ListSelectionModel rowSM = table.getSelectionModel();
80 rowSM.addListSelectionListener(new ListSelectionListener()
82 public void valueChanged(ListSelectionEvent e)
84 ListSelectionModel lsm = (ListSelectionModel) e.getSource();
85 if (!lsm.isSelectionEmpty())
87 int selectedRow = lsm.getMinSelectionIndex();
88 displayFullDetails(table.getValueAt(selectedRow, 0).toString());
93 table.addMouseListener(new MouseAdapter()
95 public void mouseClicked(MouseEvent evt)
97 if (evt.getClickCount() == 2
98 || SwingUtilities.isRightMouseButton(evt))
100 editRemoveLocalSource(evt);
105 if (sourceRegistry.getSources() != null)
111 FeatureSettings fs = null;
113 private boolean loadingDasSources;
115 public DasSourceBrowser()
120 public void paintComponent(java.awt.Graphics g)
122 if (sourceRegistry == null)
124 Thread worker = new Thread(this);
131 List<jalviewSourceI> sources = sourceRegistry.getSources();
132 int dSize = sources.size();
133 Object[][] data = new Object[dSize][2];
134 for (int i = 0; i < dSize; i++)
136 data[i][0] = sources.get(i).getTitle(); // what's equivalent of nickname
137 data[i][1] = new Boolean(selectedSources.contains(sources.get(i)
141 refreshTableData(data);
142 setCapabilities(sourceRegistry);
144 javax.swing.SwingUtilities.invokeLater(new Runnable()
148 TableSorter sorter = (TableSorter) table.getModel();
149 sorter.setSortingStatus(1, TableSorter.DESCENDING);
150 sorter.setSortingStatus(1, TableSorter.NOT_SORTED);
154 progressBar.setIndeterminate(false);
155 progressBar.setVisible(false);
156 addLocal.setVisible(true);
157 refresh.setVisible(true);
160 public void refreshTableData(Object[][] data)
162 TableSorter sorter = new TableSorter(new DASTableModel(data));
163 sorter.setTableHeader(table.getTableHeader());
164 table.setModel(sorter);
167 void displayFullDetails(String nickName)
170 StringBuffer text = new StringBuffer(
171 "<HTML><font size=\"2\" face=\"Verdana, Arial, Helvetica, sans-serif\">");
173 if (nickName == null)
175 fullDetails.setText(text + MessageManager.getString("label.select_das_service_from_table"));
179 int dSize = sourceRegistry.getSources().size();
180 for (jalviewSourceI ds : sourceRegistry.getSources())
182 if (!ds.getTitle().equals(nickName))
187 VERSION latest = ds.getVersion();
188 text.append("<font color=\"#0000FF\">Id:</font> " + ds.getUri()
190 text.append("<font color=\"#0000FF\">Nickname:</font> "
191 + ds.getTitle() + "<br>");
193 text.append("<font color=\"#0000FF\">URL:</font> <a href=\""
194 + ds.getSourceURL() + "\">" + ds.getSourceURL() + "</a>"
198 if (ds.getDocHref() != null && ds.getDocHref().length() > 0)
200 text.append("<font color=\"#0000FF\">Site:</font> <a href=\""
201 + ds.getDocHref() + "\">" + ds.getDocHref() + "</a>"
205 text.append("<font color=\"#0000FF\">Description:</font> "
206 + ds.getDescription() + "<br>");
208 text.append("<font color=\"#0000FF\">Admin Email:</font> <a href=\"mailto:"
209 + ds.getEmail() + "\">" + ds.getEmail() + "</a>" + "<br>");
211 text.append("<font color=\"#0000FF\">Registered at:</font> "
212 + latest.getCreated() + "<br>");
214 // TODO: Identify last successful test date
215 // text.append("<font color=\"#0000FF\">Last successful test:</font> "
216 // + latest.dasSources[i].getLeaseDate() + "<br>");
220 text.append("Source was added manually.<br/>");
222 text.append("<font color=\"#0000FF\">Labels:</font> ");
224 for (PROP labl : latest.getPROP())
226 if (labl.getName().equalsIgnoreCase("LABEL"))
234 text.append(labl.getValue());
241 text.append("<font color=\"#0000FF\">Capabilities:</font> ");
242 CAPABILITY[] scap = latest.getCAPABILITY().toArray(new CAPABILITY[0]);
243 for (int j = 0; j < scap.length; j++)
245 text.append(scap[j].getType());
246 if (j < scap.length - 1)
253 text.append("<font color=\"#0000FF\">Coordinates:</font>");
255 for (COORDINATES dcs : latest.getCOORDINATES())
257 text.append("<br/>" + i++ + ". ");
258 text.append(dcs.getAuthority() + " : " + dcs.getSource());
259 if (dcs.getTaxid() != null && dcs.getTaxid().trim().length() > 0)
261 text.append(" [TaxId:" + dcs.getTaxid() + "]");
263 if (dcs.getVersion() != null
264 && dcs.getVersion().trim().length() > 0)
267 text.append(" {v. " + dcs.getVersion() + "}");
270 text.append(" (<a href=\"" + dcs.getUri() + "\">" + dcs.getUri()
273 text.append("</font></html>");
278 fullDetails.setText(text.toString());
279 javax.swing.SwingUtilities.invokeLater(new Runnable()
283 fullDetailsScrollpane.getVerticalScrollBar().setValue(0);
290 loadingDasSources = true;
292 addLocal.setVisible(false);
293 refresh.setVisible(false);
294 progressBar.setVisible(true);
295 progressBar.setIndeterminate(true);
296 setParentGuiEnabled(false);
297 // Refresh the source list.
298 sourceRegistry.refreshSources();
302 setParentGuiEnabled(true);
303 loadingDasSources = false;
307 private void setParentGuiEnabled(boolean b)
311 fs.fetchDAS.setEnabled(b);
312 fs.saveDAS.setEnabled(b);
316 public Vector<jalviewSourceI> getSelectedSources()
318 // wait around if we're still loading.
319 while (sourceRegistry == null)
321 if (!loadingDasSources)
323 new Thread(this).start();
327 } catch (Exception e)
331 while (loadingDasSources)
336 } catch (Exception e)
345 Vector<jalviewSourceI> selected = new Vector<jalviewSourceI>();
346 for (String source : selectedSources)
348 jalviewSourceI srce = sourceRegistry.getSource(source);
351 selected.addElement(srce);
357 public void refresh_actionPerformed(ActionEvent e)
359 saveProperties(jalview.bin.Cache.applicationProperties);
361 Thread worker = new Thread(this);
365 private void setCapabilities(DasSourceRegistryI sourceRegistry2)
367 Vector<String> authority = new Vector<String>();
368 Vector<String> type = new Vector<String>();
369 Vector<String> label = new Vector<String>();
370 Vector<String> taxIds = new Vector<String>();
371 authority.add("Any");
375 for (jalviewSourceI ds : sourceRegistry2.getSources())
377 VERSION latest = ds.getVersion();
379 for (COORDINATES cs : latest.getCOORDINATES())
381 if (!type.contains(cs.getSource()))
383 type.add(cs.getSource()); // source==category
386 if (!authority.contains(cs.getAuthority()))
388 authority.add(cs.getAuthority());
392 for (PROP slabel : latest.getPROP())
394 if (slabel.getName().equalsIgnoreCase("LABEL")
395 && !label.contains(slabel.getValue()))
397 label.add(slabel.getValue());
403 filter1.setListData(authority);
404 filter2.setListData(type);
405 filter3.setListData(label);
408 javax.swing.SwingUtilities.invokeLater(new Runnable()
412 filter1.setSelectedIndex(0);
413 filter2.setSelectedIndex(0);
414 filter3.setSelectedIndex(0);
419 public void amendLocal(boolean newSource)
421 String url = "http://localhost:8080/", nickname = "";
422 boolean seqsrc = false;
425 int selectedRow = table.getSelectionModel().getMinSelectionIndex();
426 nickname = table.getValueAt(selectedRow, 0).toString();
427 jalviewSourceI source = sourceRegistry.getSource(nickname);
428 url = source.getUri();
429 seqsrc = source.isSequenceSource();
432 JTextField nametf = new JTextField(nickname, 40);
433 JTextField urltf = new JTextField(url, 40);
434 JCheckBox seqs = new JCheckBox(MessageManager.getString("label.sequence_source"));
435 seqs.setSelected(seqsrc);
436 JPanel panel = new JPanel(new BorderLayout());
437 JPanel pane12 = new JPanel(new BorderLayout());
438 pane12.add(new JLabel(MessageManager.getString("label.structure_manager")), BorderLayout.CENTER);
439 pane12.add(nametf, BorderLayout.EAST);
440 panel.add(pane12, BorderLayout.NORTH);
441 pane12 = new JPanel(new BorderLayout());
442 pane12.add(new JLabel(MessageManager.getString("label.url")), BorderLayout.NORTH);
443 pane12.add(seqs, BorderLayout.SOUTH);
444 pane12.add(urltf, BorderLayout.EAST);
445 panel.add(pane12, BorderLayout.SOUTH);
447 int reply = JOptionPane.showInternalConfirmDialog(Desktop.desktop,
448 panel, MessageManager.getString("label.enter_local_das_source"),
449 JOptionPane.OK_CANCEL_OPTION);
451 if (reply != JOptionPane.OK_OPTION)
456 if (!urltf.getText().endsWith("/"))
458 urltf.setText(urltf.getText() + "/");
461 jalviewSourceI local = sourceRegistry.createLocalSource(
462 urltf.getText(), nametf.getText(), seqs.isSelected(), true);
463 List sources = sourceRegistry.getSources();
464 int osize = sources.size();
465 int size = osize + (newSource ? 1 : 0);
467 Object[][] data = new Object[size][2];
468 DASTableModel dtm = (table != null) ? (DASTableModel) ((TableSorter) table
469 .getModel()).getTableModel() : null;
470 for (int i = 0; i < osize; i++)
472 String osrc = (dtm == null || i >= osize) ? null : (String) dtm
474 if (!newSource && osrc != null
475 && dtm.getValueAt(i, 0).equals(nickname))
477 data[i][0] = local.getTitle();
478 data[i][1] = new Boolean(true);
483 data[i][1] = new Boolean(selectedSources.contains(osrc));
486 // Always add a new source at the end
489 data[osize][0] = local.getTitle();
490 data[osize][1] = new Boolean(true);
491 selectedSources.add(local.getTitle());
494 refreshTableData(data);
496 SwingUtilities.invokeLater(new Runnable()
500 scrollPane.getVerticalScrollBar().setValue(
501 scrollPane.getVerticalScrollBar().getMaximum());
505 displayFullDetails(local.getTitle());
508 public void editRemoveLocalSource(MouseEvent evt)
510 int selectedRow = table.getSelectionModel().getMinSelectionIndex();
511 if (selectedRow == -1)
516 String nickname = table.getValueAt(selectedRow, 0).toString();
518 if (!sourceRegistry.getSource(nickname).isLocal())
520 JOptionPane.showInternalMessageDialog(Desktop.desktop,
521 MessageManager.getString("label.you_can_only_edit_or_remove_local_das_sources"),
522 MessageManager.getString("label.public_das_source"),
523 JOptionPane.WARNING_MESSAGE);
528 { "Edit", "Remove", "Cancel" };
529 int choice = JOptionPane.showInternalOptionDialog(Desktop.desktop,
530 "Do you want to edit or remove " + nickname + "?",
531 "Edit / Remove Local DAS Source",
532 JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE,
533 null, options, options[2]);
541 sourceRegistry.removeLocalSource(sourceRegistry.getSource(nickname));
542 selectedSources.remove(nickname);
543 Object[][] data = new Object[sourceRegistry.getSources().size()][2];
545 l = table.getRowCount();
547 for (int i = 0; i < l; i++)
550 if ((nm = (String) table.getValueAt(i, 0)).equals(nickname))
557 data[index][1] = new Boolean(selectedSources.contains(nm));
561 refreshTableData(data);
562 SwingUtilities.invokeLater(new Runnable()
566 scrollPane.getVerticalScrollBar().setValue(
567 scrollPane.getVerticalScrollBar().getMaximum());
575 public void valueChanged(ListSelectionEvent evt)
577 // Called when the MainTable selection changes
578 if (evt.getValueIsAdjusting())
583 displayFullDetails(null);
585 // Filter the displayed data sources
587 ArrayList names = new ArrayList();
588 ArrayList selected = new ArrayList();
590 // The features filter is not visible, but we must still
591 // filter the das source list here.
592 // July 2006 - only 6 sources fo not serve features
593 Object[] dummyFeatureList = new Object[]
595 List<jalviewSourceI> srcs = sourceRegistry.getSources();
596 for (jalviewSourceI ds : srcs)
599 VERSION v = ds.getVersion();
600 List<COORDINATES> coords = v.getCOORDINATES();
602 || ((coords == null || coords.size() == 0)
603 && filter1.getSelectedIndex() == 0
604 && filter2.getSelectedIndex() == 0 && filter3
605 .getSelectedIndex() == 0))
607 // THIS IS A FIX FOR LOCAL SOURCES WHICH DO NOT
608 // HAVE COORDINATE SYSTEMS, INFO WHICH AT PRESENT
609 // IS ADDED FROM THE REGISTRY
610 names.add(ds.getTitle());
611 selected.add(new Boolean(selectedSources.contains(ds.getTitle())));
615 if (!selectedInList(dummyFeatureList, ds.getCapabilityList(v))
616 || !selectedInList(filter3.getSelectedValues(),
622 for (int j = 0; j < coords.size(); j++)
624 if (selectedInList(filter1.getSelectedValues(), new String[]
625 { coords.get(j).getAuthority() })
626 && selectedInList(filter2.getSelectedValues(), new String[]
627 { coords.get(j).getSource() }))
629 names.add(ds.getTitle());
630 selected.add(new Boolean(selectedSources.contains(ds.getTitle())));
636 int dSize = names.size();
637 Object[][] data = new Object[dSize][2];
638 for (int d = 0; d < dSize; d++)
640 data[d][0] = names.get(d);
641 data[d][1] = selected.get(d);
644 refreshTableData(data);
647 private boolean selectedInList(Object[] selection, String[] items)
649 for (int i = 0; i < selection.length; i++)
651 if (selection[i].equals("Any"))
655 if (items == null || items.length == 0)
659 String sel = (items[0].startsWith("das1:") ? "das1:" : "")
661 for (int j = 0; j < items.length; j++)
663 if (sel.equals(items[j]))
673 void setSelectedFromProperties()
675 String active = jalview.bin.Cache.getDefault("DAS_ACTIVE_SOURCE",
677 StringTokenizer st = new StringTokenizer(active, "\t");
678 selectedSources = new Vector();
679 while (st.hasMoreTokens())
681 selectedSources.addElement(st.nextToken());
685 public void reset_actionPerformed(ActionEvent e)
687 registryURL.setText(sourceRegistry.getDasRegistryURL());
691 * set the DAS source settings in the given jalview properties.
695 public void saveProperties(Properties properties)
697 if (registryURL.getText() == null || registryURL.getText().length() < 1)
699 properties.remove(jalview.bin.Cache.DAS_REGISTRY_URL);
703 properties.setProperty(jalview.bin.Cache.DAS_REGISTRY_URL,
704 registryURL.getText());
707 StringBuffer sb = new StringBuffer();
708 for (int r = 0; r < table.getModel().getRowCount(); r++)
710 if (((Boolean) table.getValueAt(r, 1)).booleanValue())
712 sb.append(table.getValueAt(r, 0) + "\t");
716 properties.setProperty(jalview.bin.Cache.DAS_ACTIVE_SOURCE,
719 String sourceprop = sourceRegistry.getLocalSourceString();
720 properties.setProperty(jalview.bin.Cache.DAS_LOCAL_SOURCE, sourceprop);
723 class DASTableModel extends AbstractTableModel
726 public DASTableModel(Object[][] data)
731 private String[] columnNames = new String[]
732 { "Nickname", "Use Source" };
734 private Object[][] data;
736 public int getColumnCount()
738 return columnNames.length;
741 public int getRowCount()
746 public String getColumnName(int col)
748 return columnNames[col];
751 public Object getValueAt(int row, int col)
753 return data[row][col];
757 * JTable uses this method to determine the default renderer/ editor for
758 * each cell. If we didn't implement this method, then the last column would
759 * contain text ("true"/"false"), rather than a check box.
761 public Class getColumnClass(int c)
763 return getValueAt(0, c).getClass();
767 * Don't need to implement this method unless your table's editable.
769 public boolean isCellEditable(int row, int col)
771 // Note that the data/cell address is constant,
772 // no matter where the cell appears onscreen.
778 * Don't need to implement this method unless your table's data can change.
780 public void setValueAt(Object value, int row, int col)
782 data[row][col] = value;
783 fireTableCellUpdated(row, col);
785 String name = getValueAt(row, 0).toString();
786 boolean selected = ((Boolean) value).booleanValue();
788 if (selectedSources.contains(name) && !selected)
790 selectedSources.remove(name);
793 if (!selectedSources.contains(name) && selected)
795 selectedSources.add(name);
800 public void initDasSources()
803 Thread thr = new Thread(new Runnable()
807 // this actually initialises the das source list
808 paintComponent(null); // yuk
812 while (loadingDasSources || sourceRegistry == null)
817 } catch (Exception e)
825 * disable or enable the buttons on the source browser
829 public void setGuiEnabled(boolean b)
831 refresh.setEnabled(b);
832 addLocal.setEnabled(b);