2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ 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
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
23 import jalview.jbgui.GDasSourceBrowser;
24 import jalview.util.MessageManager;
25 import jalview.util.TableSorter;
26 import jalview.ws.dbsources.das.api.DasSourceRegistryI;
27 import jalview.ws.dbsources.das.api.jalviewSourceI;
29 import java.awt.BorderLayout;
30 import java.awt.event.ActionEvent;
31 import java.awt.event.MouseAdapter;
32 import java.awt.event.MouseEvent;
33 import java.util.ArrayList;
34 import java.util.List;
35 import java.util.Properties;
36 import java.util.StringTokenizer;
37 import java.util.Vector;
39 import javax.swing.JCheckBox;
40 import javax.swing.JLabel;
41 import javax.swing.JOptionPane;
42 import javax.swing.JPanel;
43 import javax.swing.JTextField;
44 import javax.swing.ListSelectionModel;
45 import javax.swing.SwingUtilities;
46 import javax.swing.event.ListSelectionEvent;
47 import javax.swing.event.ListSelectionListener;
48 import javax.swing.table.AbstractTableModel;
50 import org.biodas.jdas.schema.sources.CAPABILITY;
51 import org.biodas.jdas.schema.sources.COORDINATES;
52 import org.biodas.jdas.schema.sources.PROP;
53 import org.biodas.jdas.schema.sources.VERSION;
55 public class DasSourceBrowser extends GDasSourceBrowser implements
56 Runnable, ListSelectionListener
58 DasSourceRegistryI sourceRegistry = null;
60 Vector<String> selectedSources;
62 public DasSourceBrowser(FeatureSettings featureSettings)
65 // TODO DasSourceRegistryProvider API
66 sourceRegistry = jalview.bin.Cache.getDasSourceRegistry();
67 String registry = sourceRegistry.getDasRegistryURL();
69 registryURL.setText(registry);
71 setSelectedFromProperties();
73 displayFullDetails(null);
74 table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
76 filter1.addListSelectionListener(this);
77 filter2.addListSelectionListener(this);
78 filter3.addListSelectionListener(this);
80 // Ask to be notified of selection changes.
81 ListSelectionModel rowSM = table.getSelectionModel();
82 rowSM.addListSelectionListener(new ListSelectionListener()
85 public void valueChanged(ListSelectionEvent e)
87 ListSelectionModel lsm = (ListSelectionModel) e.getSource();
88 if (!lsm.isSelectionEmpty())
90 int selectedRow = lsm.getMinSelectionIndex();
91 displayFullDetails(table.getValueAt(selectedRow, 0).toString());
96 table.addMouseListener(new MouseAdapter()
99 public void mouseClicked(MouseEvent evt)
101 if (evt.getClickCount() == 2 || evt.isPopupTrigger())
103 editRemoveLocalSource(evt);
108 if (sourceRegistry.getSources() != null)
114 FeatureSettings fs = null;
116 private boolean loadingDasSources;
118 public DasSourceBrowser()
124 public void paintComponent(java.awt.Graphics g)
126 if (sourceRegistry == null)
128 Thread worker = new Thread(this);
135 List<jalviewSourceI> sources = sourceRegistry.getSources();
136 int dSize = sources.size();
137 Object[][] data = new Object[dSize][2];
138 for (int i = 0; i < dSize; i++)
140 data[i][0] = sources.get(i).getTitle(); // what's equivalent of nickname
141 data[i][1] = new Boolean(selectedSources.contains(sources.get(i)
145 refreshTableData(data);
146 setCapabilities(sourceRegistry);
148 javax.swing.SwingUtilities.invokeLater(new Runnable()
153 TableSorter sorter = (TableSorter) table.getModel();
154 sorter.setSortingStatus(1, TableSorter.DESCENDING);
155 sorter.setSortingStatus(1, TableSorter.NOT_SORTED);
159 progressBar.setIndeterminate(false);
160 progressBar.setVisible(false);
161 addLocal.setVisible(true);
162 refresh.setVisible(true);
165 public void refreshTableData(Object[][] data)
167 TableSorter sorter = new TableSorter(new DASTableModel(data));
168 sorter.setTableHeader(table.getTableHeader());
169 table.setModel(sorter);
172 void displayFullDetails(String nickName)
175 StringBuffer text = new StringBuffer(
176 "<HTML><font size=\"2\" face=\"Verdana, Arial, Helvetica, sans-serif\">");
178 if (nickName == null)
180 fullDetails.setText(text
182 .getString("label.select_das_service_from_table"));
186 int dSize = sourceRegistry.getSources().size();
187 for (jalviewSourceI ds : sourceRegistry.getSources())
189 if (!ds.getTitle().equals(nickName))
194 VERSION latest = ds.getVersion();
195 text.append("<font color=\"#0000FF\">Id:</font> " + ds.getUri()
197 text.append("<font color=\"#0000FF\">Nickname:</font> "
198 + ds.getTitle() + "<br>");
200 text.append("<font color=\"#0000FF\">URL:</font> <a href=\""
201 + ds.getSourceURL() + "\">" + ds.getSourceURL() + "</a>"
205 if (ds.getDocHref() != null && ds.getDocHref().length() > 0)
207 text.append("<font color=\"#0000FF\">Site:</font> <a href=\""
208 + ds.getDocHref() + "\">" + ds.getDocHref() + "</a>"
212 text.append("<font color=\"#0000FF\">Description:</font> "
213 + ds.getDescription() + "<br>");
215 text.append("<font color=\"#0000FF\">Admin Email:</font> <a href=\"mailto:"
216 + ds.getEmail() + "\">" + ds.getEmail() + "</a>" + "<br>");
218 text.append("<font color=\"#0000FF\">Registered at:</font> "
219 + latest.getCreated() + "<br>");
221 // TODO: Identify last successful test date
222 // text.append("<font color=\"#0000FF\">Last successful test:</font> "
223 // + latest.dasSources[i].getLeaseDate() + "<br>");
227 text.append("Source was added manually.<br/>");
229 text.append("<font color=\"#0000FF\">Labels:</font> ");
231 for (PROP labl : latest.getPROP())
233 if (labl.getName().equalsIgnoreCase("LABEL"))
241 text.append(labl.getValue());
248 text.append("<font color=\"#0000FF\">Capabilities:</font> ");
249 CAPABILITY[] scap = latest.getCAPABILITY().toArray(new CAPABILITY[0]);
250 for (int j = 0; j < scap.length; j++)
252 text.append(scap[j].getType());
253 if (j < scap.length - 1)
260 text.append("<font color=\"#0000FF\">Coordinates:</font>");
262 for (COORDINATES dcs : latest.getCOORDINATES())
264 text.append("<br/>" + i++ + ". ");
265 text.append(dcs.getAuthority() + " : " + dcs.getSource());
266 if (dcs.getTaxid() != null && dcs.getTaxid().trim().length() > 0)
268 text.append(" [TaxId:" + dcs.getTaxid() + "]");
270 if (dcs.getVersion() != null
271 && dcs.getVersion().trim().length() > 0)
274 text.append(" {v. " + dcs.getVersion() + "}");
277 text.append(" (<a href=\"" + dcs.getUri() + "\">" + dcs.getUri()
280 text.append("</font></html>");
285 fullDetails.setText(text.toString());
286 javax.swing.SwingUtilities.invokeLater(new Runnable()
291 fullDetailsScrollpane.getVerticalScrollBar().setValue(0);
299 loadingDasSources = true;
301 addLocal.setVisible(false);
302 refresh.setVisible(false);
303 progressBar.setVisible(true);
304 progressBar.setIndeterminate(true);
305 setParentGuiEnabled(false);
306 // Refresh the source list.
307 sourceRegistry.refreshSources();
311 setParentGuiEnabled(true);
312 loadingDasSources = false;
316 private void setParentGuiEnabled(boolean b)
320 fs.fetchDAS.setEnabled(b);
321 fs.saveDAS.setEnabled(b);
325 public Vector<jalviewSourceI> getSelectedSources()
327 // wait around if we're still loading.
328 while (sourceRegistry == null)
330 if (!loadingDasSources)
332 new Thread(this).start();
336 } catch (Exception e)
340 while (loadingDasSources)
345 } catch (Exception e)
354 Vector<jalviewSourceI> selected = new Vector<jalviewSourceI>();
355 for (String source : selectedSources)
357 jalviewSourceI srce = sourceRegistry.getSource(source);
360 selected.addElement(srce);
367 public void refresh_actionPerformed(ActionEvent e)
369 saveProperties(jalview.bin.Cache.applicationProperties);
371 Thread worker = new Thread(this);
375 private void setCapabilities(DasSourceRegistryI sourceRegistry2)
377 Vector<String> authority = new Vector<String>();
378 Vector<String> type = new Vector<String>();
379 Vector<String> label = new Vector<String>();
380 Vector<String> taxIds = new Vector<String>();
381 authority.add("Any");
385 for (jalviewSourceI ds : sourceRegistry2.getSources())
387 VERSION latest = ds.getVersion();
389 for (COORDINATES cs : latest.getCOORDINATES())
391 if (!type.contains(cs.getSource()))
393 type.add(cs.getSource()); // source==category
396 if (!authority.contains(cs.getAuthority()))
398 authority.add(cs.getAuthority());
402 for (PROP slabel : latest.getPROP())
404 if (slabel.getName().equalsIgnoreCase("LABEL")
405 && !label.contains(slabel.getValue()))
407 label.add(slabel.getValue());
413 filter1.setListData(authority);
414 filter2.setListData(type);
415 filter3.setListData(label);
418 javax.swing.SwingUtilities.invokeLater(new Runnable()
423 filter1.setSelectedIndex(0);
424 filter2.setSelectedIndex(0);
425 filter3.setSelectedIndex(0);
431 public void amendLocal(boolean newSource)
433 String url = "http://localhost:8080/", nickname = "";
434 boolean seqsrc = false;
437 int selectedRow = table.getSelectionModel().getMinSelectionIndex();
438 nickname = table.getValueAt(selectedRow, 0).toString();
439 jalviewSourceI source = sourceRegistry.getSource(nickname);
440 url = source.getUri();
441 seqsrc = source.isSequenceSource();
444 JTextField nametf = new JTextField(nickname, 40);
445 JTextField urltf = new JTextField(url, 40);
446 JCheckBox seqs = new JCheckBox(
447 MessageManager.getString("label.sequence_source"));
448 seqs.setSelected(seqsrc);
449 JPanel panel = new JPanel(new BorderLayout());
450 JPanel pane12 = new JPanel(new BorderLayout());
451 pane12.add(new JLabel(MessageManager.getString("label.name:")),
452 BorderLayout.CENTER);
453 pane12.add(nametf, BorderLayout.EAST);
454 panel.add(pane12, BorderLayout.NORTH);
455 pane12 = new JPanel(new BorderLayout());
456 pane12.add(new JLabel(MessageManager.getString("label.url:")),
458 pane12.add(seqs, BorderLayout.SOUTH);
459 pane12.add(urltf, BorderLayout.EAST);
460 panel.add(pane12, BorderLayout.SOUTH);
462 int reply = JvOptionPane.showInternalConfirmDialog(Desktop.desktop,
464 MessageManager.getString("label.enter_local_das_source"),
465 JvOptionPane.OK_CANCEL_OPTION);
467 if (reply != JvOptionPane.OK_OPTION)
472 if (!urltf.getText().endsWith("/"))
474 urltf.setText(urltf.getText() + "/");
477 jalviewSourceI local = sourceRegistry.createLocalSource(
478 urltf.getText(), nametf.getText(), seqs.isSelected(), true);
479 List sources = sourceRegistry.getSources();
480 int osize = sources.size();
481 int size = osize + (newSource ? 1 : 0);
483 Object[][] data = new Object[size][2];
484 DASTableModel dtm = (table != null) ? (DASTableModel) ((TableSorter) table
485 .getModel()).getTableModel() : null;
486 for (int i = 0; i < osize; i++)
488 String osrc = (dtm == null || i >= osize) ? null : (String) dtm
490 if (!newSource && osrc != null
491 && dtm.getValueAt(i, 0).equals(nickname))
493 data[i][0] = local.getTitle();
494 data[i][1] = new Boolean(true);
499 data[i][1] = new Boolean(selectedSources.contains(osrc));
502 // Always add a new source at the end
505 data[osize][0] = local.getTitle();
506 data[osize][1] = new Boolean(true);
507 selectedSources.add(local.getTitle());
510 refreshTableData(data);
512 SwingUtilities.invokeLater(new Runnable()
517 scrollPane.getVerticalScrollBar().setValue(
518 scrollPane.getVerticalScrollBar().getMaximum());
522 displayFullDetails(local.getTitle());
525 public void editRemoveLocalSource(MouseEvent evt)
527 int selectedRow = table.getSelectionModel().getMinSelectionIndex();
528 if (selectedRow == -1)
533 String nickname = table.getValueAt(selectedRow, 0).toString();
535 if (!sourceRegistry.getSource(nickname).isLocal())
538 .showInternalMessageDialog(
541 .getString("label.you_can_only_edit_or_remove_local_das_sources"),
542 MessageManager.getString("label.public_das_source"),
543 JvOptionPane.WARNING_MESSAGE);
547 Object[] options = { "Edit", "Remove", "Cancel" };
548 int choice = JvOptionPane.showInternalOptionDialog(Desktop.desktop,
549 "Do you want to edit or remove " + nickname + "?",
550 "Edit / Remove Local DAS Source",
551 JvOptionPane.YES_NO_CANCEL_OPTION, JvOptionPane.QUESTION_MESSAGE,
552 null, options, options[2]);
560 sourceRegistry.removeLocalSource(sourceRegistry.getSource(nickname));
561 selectedSources.remove(nickname);
562 Object[][] data = new Object[sourceRegistry.getSources().size()][2];
564 l = table.getRowCount();
566 for (int i = 0; i < l; i++)
569 if ((nm = (String) table.getValueAt(i, 0)).equals(nickname))
576 data[index][1] = new Boolean(selectedSources.contains(nm));
580 refreshTableData(data);
581 SwingUtilities.invokeLater(new Runnable()
586 scrollPane.getVerticalScrollBar().setValue(
587 scrollPane.getVerticalScrollBar().getMaximum());
596 public void valueChanged(ListSelectionEvent evt)
598 // Called when the MainTable selection changes
599 if (evt.getValueIsAdjusting())
604 displayFullDetails(null);
606 // Filter the displayed data sources
608 ArrayList names = new ArrayList();
609 ArrayList selected = new ArrayList();
611 // The features filter is not visible, but we must still
612 // filter the das source list here.
613 // July 2006 - only 6 sources fo not serve features
614 Object[] dummyFeatureList = new Object[] { "features" };
615 List<jalviewSourceI> srcs = sourceRegistry.getSources();
616 for (jalviewSourceI ds : srcs)
619 VERSION v = ds.getVersion();
620 List<COORDINATES> coords = v.getCOORDINATES();
622 || ((coords == null || coords.size() == 0)
623 && filter1.getSelectedIndex() == 0
624 && filter2.getSelectedIndex() == 0 && filter3
625 .getSelectedIndex() == 0))
627 // THIS IS A FIX FOR LOCAL SOURCES WHICH DO NOT
628 // HAVE COORDINATE SYSTEMS, INFO WHICH AT PRESENT
629 // IS ADDED FROM THE REGISTRY
630 names.add(ds.getTitle());
631 selected.add(new Boolean(selectedSources.contains(ds.getTitle())));
635 if (!selectedInList(dummyFeatureList, ds.getCapabilityList(v))
636 || !selectedInList(filter3.getSelectedValues(),
642 for (int j = 0; j < coords.size(); j++)
644 if (selectedInList(filter1.getSelectedValues(),
645 new String[] { coords.get(j).getAuthority() })
646 && selectedInList(filter2.getSelectedValues(), new String[]
647 { coords.get(j).getSource() }))
649 names.add(ds.getTitle());
650 selected.add(new Boolean(selectedSources.contains(ds.getTitle())));
656 int dSize = names.size();
657 Object[][] data = new Object[dSize][2];
658 for (int d = 0; d < dSize; d++)
660 data[d][0] = names.get(d);
661 data[d][1] = selected.get(d);
664 refreshTableData(data);
667 private boolean selectedInList(Object[] selection, String[] items)
669 for (int i = 0; i < selection.length; i++)
671 if (selection[i].equals("Any"))
675 if (items == null || items.length == 0)
679 String sel = (items[0].startsWith("das1:") ? "das1:" : "")
681 for (int j = 0; j < items.length; j++)
683 if (sel.equals(items[j]))
693 void setSelectedFromProperties()
695 String active = jalview.bin.Cache.getDefault("DAS_ACTIVE_SOURCE",
697 StringTokenizer st = new StringTokenizer(active, "\t");
698 selectedSources = new Vector();
699 while (st.hasMoreTokens())
701 selectedSources.addElement(st.nextToken());
706 public void reset_actionPerformed(ActionEvent e)
708 registryURL.setText(sourceRegistry.getDasRegistryURL());
712 * set the DAS source settings in the given jalview properties.
716 public void saveProperties(Properties properties)
718 if (registryURL.getText() == null || registryURL.getText().length() < 1)
720 properties.remove(jalview.bin.Cache.DAS_REGISTRY_URL);
724 properties.setProperty(jalview.bin.Cache.DAS_REGISTRY_URL,
725 registryURL.getText());
728 StringBuffer sb = new StringBuffer();
729 for (int r = 0; r < table.getModel().getRowCount(); r++)
731 if (((Boolean) table.getValueAt(r, 1)).booleanValue())
733 sb.append(table.getValueAt(r, 0) + "\t");
737 properties.setProperty(jalview.bin.Cache.DAS_ACTIVE_SOURCE,
740 String sourceprop = sourceRegistry.getLocalSourceString();
741 properties.setProperty(jalview.bin.Cache.DAS_LOCAL_SOURCE, sourceprop);
744 class DASTableModel extends AbstractTableModel
747 public DASTableModel(Object[][] data)
752 private String[] columnNames = new String[] {
753 MessageManager.getString("label.nickname"),
754 MessageManager.getString("label.use_source") };
756 private Object[][] data;
759 public int getColumnCount()
761 return columnNames.length;
765 public int getRowCount()
771 public String getColumnName(int col)
773 return columnNames[col];
777 public Object getValueAt(int row, int col)
779 return data[row][col];
783 * JTable uses this method to determine the default renderer/ editor for
784 * each cell. If we didn't implement this method, then the last column would
785 * contain text ("true"/"false"), rather than a check box.
788 public Class getColumnClass(int c)
790 return getValueAt(0, c).getClass();
794 * Don't need to implement this method unless your table's editable.
797 public boolean isCellEditable(int row, int col)
799 // Note that the data/cell address is constant,
800 // no matter where the cell appears onscreen.
806 * Don't need to implement this method unless your table's data can change.
809 public void setValueAt(Object value, int row, int col)
811 data[row][col] = value;
812 fireTableCellUpdated(row, col);
814 String name = getValueAt(row, 0).toString();
815 boolean selected = ((Boolean) value).booleanValue();
817 if (selectedSources.contains(name) && !selected)
819 selectedSources.remove(name);
822 if (!selectedSources.contains(name) && selected)
824 selectedSources.add(name);
829 public void initDasSources()
832 Thread thr = new Thread(new Runnable()
837 // this actually initialises the das source list
838 paintComponent(null); // yuk
842 while (loadingDasSources || sourceRegistry == null)
847 } catch (Exception e)
855 * disable or enable the buttons on the source browser
859 public void setGuiEnabled(boolean b)
861 refresh.setEnabled(b);
862 addLocal.setEnabled(b);