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 java.awt.BorderLayout;
24 import java.awt.event.ActionEvent;
25 import java.awt.event.MouseAdapter;
26 import java.awt.event.MouseEvent;
27 import java.util.ArrayList;
28 import java.util.List;
29 import java.util.Properties;
30 import java.util.StringTokenizer;
31 import java.util.Vector;
33 import javax.swing.JCheckBox;
34 import javax.swing.JLabel;
35 import javax.swing.JOptionPane;
36 import javax.swing.JPanel;
37 import javax.swing.JTextField;
38 import javax.swing.ListSelectionModel;
39 import javax.swing.SwingUtilities;
40 import javax.swing.event.ListSelectionEvent;
41 import javax.swing.event.ListSelectionListener;
42 import javax.swing.table.AbstractTableModel;
44 import org.biodas.jdas.schema.sources.CAPABILITY;
45 import org.biodas.jdas.schema.sources.COORDINATES;
46 import org.biodas.jdas.schema.sources.PROP;
47 import org.biodas.jdas.schema.sources.VERSION;
49 import jalview.jbgui.GDasSourceBrowser;
50 import jalview.util.MessageManager;
51 import jalview.util.TableSorter;
52 import jalview.ws.dbsources.das.api.DasSourceRegistryI;
53 import jalview.ws.dbsources.das.api.jalviewSourceI;
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()
84 public void valueChanged(ListSelectionEvent e)
86 ListSelectionModel lsm = (ListSelectionModel) e.getSource();
87 if (!lsm.isSelectionEmpty())
89 int selectedRow = lsm.getMinSelectionIndex();
90 displayFullDetails(table.getValueAt(selectedRow, 0).toString());
95 table.addMouseListener(new MouseAdapter()
97 public void mouseClicked(MouseEvent evt)
99 if (evt.getClickCount() == 2
100 || SwingUtilities.isRightMouseButton(evt))
102 editRemoveLocalSource(evt);
107 if (sourceRegistry.getSources() != null)
113 FeatureSettings fs = null;
115 private boolean loadingDasSources;
117 public DasSourceBrowser()
122 public void paintComponent(java.awt.Graphics g)
124 if (sourceRegistry == null)
126 Thread worker = new Thread(this);
133 List<jalviewSourceI> sources = sourceRegistry.getSources();
134 int dSize = sources.size();
135 Object[][] data = new Object[dSize][2];
136 for (int i = 0; i < dSize; i++)
138 data[i][0] = sources.get(i).getTitle(); // what's equivalent of nickname
139 data[i][1] = new Boolean(selectedSources.contains(sources.get(i)
143 refreshTableData(data);
144 setCapabilities(sourceRegistry);
146 javax.swing.SwingUtilities.invokeLater(new Runnable()
150 TableSorter sorter = (TableSorter) table.getModel();
151 sorter.setSortingStatus(1, TableSorter.DESCENDING);
152 sorter.setSortingStatus(1, TableSorter.NOT_SORTED);
156 progressBar.setIndeterminate(false);
157 progressBar.setVisible(false);
158 addLocal.setVisible(true);
159 refresh.setVisible(true);
162 public void refreshTableData(Object[][] data)
164 TableSorter sorter = new TableSorter(new DASTableModel(data));
165 sorter.setTableHeader(table.getTableHeader());
166 table.setModel(sorter);
169 void displayFullDetails(String nickName)
172 StringBuffer text = new StringBuffer(
173 "<HTML><font size=\"2\" face=\"Verdana, Arial, Helvetica, sans-serif\">");
175 if (nickName == null)
177 fullDetails.setText(text
179 .getString("label.select_das_service_from_table"));
183 int dSize = sourceRegistry.getSources().size();
184 for (jalviewSourceI ds : sourceRegistry.getSources())
186 if (!ds.getTitle().equals(nickName))
191 VERSION latest = ds.getVersion();
192 text.append("<font color=\"#0000FF\">Id:</font> " + ds.getUri()
194 text.append("<font color=\"#0000FF\">Nickname:</font> "
195 + ds.getTitle() + "<br>");
197 text.append("<font color=\"#0000FF\">URL:</font> <a href=\""
198 + ds.getSourceURL() + "\">" + ds.getSourceURL() + "</a>"
202 if (ds.getDocHref() != null && ds.getDocHref().length() > 0)
204 text.append("<font color=\"#0000FF\">Site:</font> <a href=\""
205 + ds.getDocHref() + "\">" + ds.getDocHref() + "</a>"
209 text.append("<font color=\"#0000FF\">Description:</font> "
210 + ds.getDescription() + "<br>");
212 text.append("<font color=\"#0000FF\">Admin Email:</font> <a href=\"mailto:"
213 + ds.getEmail() + "\">" + ds.getEmail() + "</a>" + "<br>");
215 text.append("<font color=\"#0000FF\">Registered at:</font> "
216 + latest.getCreated() + "<br>");
218 // TODO: Identify last successful test date
219 // text.append("<font color=\"#0000FF\">Last successful test:</font> "
220 // + latest.dasSources[i].getLeaseDate() + "<br>");
224 text.append("Source was added manually.<br/>");
226 text.append("<font color=\"#0000FF\">Labels:</font> ");
228 for (PROP labl : latest.getPROP())
230 if (labl.getName().equalsIgnoreCase("LABEL"))
238 text.append(labl.getValue());
245 text.append("<font color=\"#0000FF\">Capabilities:</font> ");
246 CAPABILITY[] scap = latest.getCAPABILITY().toArray(new CAPABILITY[0]);
247 for (int j = 0; j < scap.length; j++)
249 text.append(scap[j].getType());
250 if (j < scap.length - 1)
257 text.append("<font color=\"#0000FF\">Coordinates:</font>");
259 for (COORDINATES dcs : latest.getCOORDINATES())
261 text.append("<br/>" + i++ + ". ");
262 text.append(dcs.getAuthority() + " : " + dcs.getSource());
263 if (dcs.getTaxid() != null && dcs.getTaxid().trim().length() > 0)
265 text.append(" [TaxId:" + dcs.getTaxid() + "]");
267 if (dcs.getVersion() != null
268 && dcs.getVersion().trim().length() > 0)
271 text.append(" {v. " + dcs.getVersion() + "}");
274 text.append(" (<a href=\"" + dcs.getUri() + "\">" + dcs.getUri()
277 text.append("</font></html>");
282 fullDetails.setText(text.toString());
283 javax.swing.SwingUtilities.invokeLater(new Runnable()
287 fullDetailsScrollpane.getVerticalScrollBar().setValue(0);
294 loadingDasSources = true;
296 addLocal.setVisible(false);
297 refresh.setVisible(false);
298 progressBar.setVisible(true);
299 progressBar.setIndeterminate(true);
300 setParentGuiEnabled(false);
301 // Refresh the source list.
302 sourceRegistry.refreshSources();
306 setParentGuiEnabled(true);
307 loadingDasSources = false;
311 private void setParentGuiEnabled(boolean b)
315 fs.fetchDAS.setEnabled(b);
316 fs.saveDAS.setEnabled(b);
320 public Vector<jalviewSourceI> getSelectedSources()
322 // wait around if we're still loading.
323 while (sourceRegistry == null)
325 if (!loadingDasSources)
327 new Thread(this).start();
331 } catch (Exception e)
335 while (loadingDasSources)
340 } catch (Exception e)
349 Vector<jalviewSourceI> selected = new Vector<jalviewSourceI>();
350 for (String source : selectedSources)
352 jalviewSourceI srce = sourceRegistry.getSource(source);
355 selected.addElement(srce);
361 public void refresh_actionPerformed(ActionEvent e)
363 saveProperties(jalview.bin.Cache.applicationProperties);
365 Thread worker = new Thread(this);
369 private void setCapabilities(DasSourceRegistryI sourceRegistry2)
371 Vector<String> authority = new Vector<String>();
372 Vector<String> type = new Vector<String>();
373 Vector<String> label = new Vector<String>();
374 Vector<String> taxIds = new Vector<String>();
375 authority.add("Any");
379 for (jalviewSourceI ds : sourceRegistry2.getSources())
381 VERSION latest = ds.getVersion();
383 for (COORDINATES cs : latest.getCOORDINATES())
385 if (!type.contains(cs.getSource()))
387 type.add(cs.getSource()); // source==category
390 if (!authority.contains(cs.getAuthority()))
392 authority.add(cs.getAuthority());
396 for (PROP slabel : latest.getPROP())
398 if (slabel.getName().equalsIgnoreCase("LABEL")
399 && !label.contains(slabel.getValue()))
401 label.add(slabel.getValue());
407 filter1.setListData(authority);
408 filter2.setListData(type);
409 filter3.setListData(label);
412 javax.swing.SwingUtilities.invokeLater(new Runnable()
416 filter1.setSelectedIndex(0);
417 filter2.setSelectedIndex(0);
418 filter3.setSelectedIndex(0);
423 public void amendLocal(boolean newSource)
425 String url = "http://localhost:8080/", nickname = "";
426 boolean seqsrc = false;
429 int selectedRow = table.getSelectionModel().getMinSelectionIndex();
430 nickname = table.getValueAt(selectedRow, 0).toString();
431 jalviewSourceI source = sourceRegistry.getSource(nickname);
432 url = source.getUri();
433 seqsrc = source.isSequenceSource();
436 JTextField nametf = new JTextField(nickname, 40);
437 JTextField urltf = new JTextField(url, 40);
438 JCheckBox seqs = new JCheckBox(
439 MessageManager.getString("label.sequence_source"));
440 seqs.setSelected(seqsrc);
441 JPanel panel = new JPanel(new BorderLayout());
442 JPanel pane12 = new JPanel(new BorderLayout());
443 pane12.add(new JLabel(MessageManager.getString("label.name")),
444 BorderLayout.CENTER);
445 pane12.add(nametf, BorderLayout.EAST);
446 panel.add(pane12, BorderLayout.NORTH);
447 pane12 = new JPanel(new BorderLayout());
448 pane12.add(new JLabel(MessageManager.getString("label.url")),
450 pane12.add(seqs, BorderLayout.SOUTH);
451 pane12.add(urltf, BorderLayout.EAST);
452 panel.add(pane12, BorderLayout.SOUTH);
454 int reply = JOptionPane.showInternalConfirmDialog(Desktop.desktop,
456 MessageManager.getString("label.enter_local_das_source"),
457 JOptionPane.OK_CANCEL_OPTION);
459 if (reply != JOptionPane.OK_OPTION)
464 if (!urltf.getText().endsWith("/"))
466 urltf.setText(urltf.getText() + "/");
469 jalviewSourceI local = sourceRegistry.createLocalSource(
470 urltf.getText(), nametf.getText(), seqs.isSelected(), true);
471 List sources = sourceRegistry.getSources();
472 int osize = sources.size();
473 int size = osize + (newSource ? 1 : 0);
475 Object[][] data = new Object[size][2];
476 DASTableModel dtm = (table != null) ? (DASTableModel) ((TableSorter) table
477 .getModel()).getTableModel() : null;
478 for (int i = 0; i < osize; i++)
480 String osrc = (dtm == null || i >= osize) ? null : (String) dtm
482 if (!newSource && osrc != null
483 && dtm.getValueAt(i, 0).equals(nickname))
485 data[i][0] = local.getTitle();
486 data[i][1] = new Boolean(true);
491 data[i][1] = new Boolean(selectedSources.contains(osrc));
494 // Always add a new source at the end
497 data[osize][0] = local.getTitle();
498 data[osize][1] = new Boolean(true);
499 selectedSources.add(local.getTitle());
502 refreshTableData(data);
504 SwingUtilities.invokeLater(new Runnable()
508 scrollPane.getVerticalScrollBar().setValue(
509 scrollPane.getVerticalScrollBar().getMaximum());
513 displayFullDetails(local.getTitle());
516 public void editRemoveLocalSource(MouseEvent evt)
518 int selectedRow = table.getSelectionModel().getMinSelectionIndex();
519 if (selectedRow == -1)
524 String nickname = table.getValueAt(selectedRow, 0).toString();
526 if (!sourceRegistry.getSource(nickname).isLocal())
529 .showInternalMessageDialog(
532 .getString("label.you_can_only_edit_or_remove_local_das_sources"),
533 MessageManager.getString("label.public_das_source"),
534 JOptionPane.WARNING_MESSAGE);
539 { "Edit", "Remove", "Cancel" };
540 int choice = JOptionPane.showInternalOptionDialog(Desktop.desktop,
541 "Do you want to edit or remove " + nickname + "?",
542 "Edit / Remove Local DAS Source",
543 JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE,
544 null, options, options[2]);
552 sourceRegistry.removeLocalSource(sourceRegistry.getSource(nickname));
553 selectedSources.remove(nickname);
554 Object[][] data = new Object[sourceRegistry.getSources().size()][2];
556 l = table.getRowCount();
558 for (int i = 0; i < l; i++)
561 if ((nm = (String) table.getValueAt(i, 0)).equals(nickname))
568 data[index][1] = new Boolean(selectedSources.contains(nm));
572 refreshTableData(data);
573 SwingUtilities.invokeLater(new Runnable()
577 scrollPane.getVerticalScrollBar().setValue(
578 scrollPane.getVerticalScrollBar().getMaximum());
586 public void valueChanged(ListSelectionEvent evt)
588 // Called when the MainTable selection changes
589 if (evt.getValueIsAdjusting())
594 displayFullDetails(null);
596 // Filter the displayed data sources
598 ArrayList names = new ArrayList();
599 ArrayList selected = new ArrayList();
601 // The features filter is not visible, but we must still
602 // filter the das source list here.
603 // July 2006 - only 6 sources fo not serve features
604 Object[] dummyFeatureList = new Object[]
606 List<jalviewSourceI> srcs = sourceRegistry.getSources();
607 for (jalviewSourceI ds : srcs)
610 VERSION v = ds.getVersion();
611 List<COORDINATES> coords = v.getCOORDINATES();
613 || ((coords == null || coords.size() == 0)
614 && filter1.getSelectedIndex() == 0
615 && filter2.getSelectedIndex() == 0 && filter3
616 .getSelectedIndex() == 0))
618 // THIS IS A FIX FOR LOCAL SOURCES WHICH DO NOT
619 // HAVE COORDINATE SYSTEMS, INFO WHICH AT PRESENT
620 // IS ADDED FROM THE REGISTRY
621 names.add(ds.getTitle());
622 selected.add(new Boolean(selectedSources.contains(ds.getTitle())));
626 if (!selectedInList(dummyFeatureList, ds.getCapabilityList(v))
627 || !selectedInList(filter3.getSelectedValues(),
633 for (int j = 0; j < coords.size(); j++)
635 if (selectedInList(filter1.getSelectedValues(), new String[]
636 { coords.get(j).getAuthority() })
637 && selectedInList(filter2.getSelectedValues(), new String[]
638 { coords.get(j).getSource() }))
640 names.add(ds.getTitle());
641 selected.add(new Boolean(selectedSources.contains(ds.getTitle())));
647 int dSize = names.size();
648 Object[][] data = new Object[dSize][2];
649 for (int d = 0; d < dSize; d++)
651 data[d][0] = names.get(d);
652 data[d][1] = selected.get(d);
655 refreshTableData(data);
658 private boolean selectedInList(Object[] selection, String[] items)
660 for (int i = 0; i < selection.length; i++)
662 if (selection[i].equals("Any"))
666 if (items == null || items.length == 0)
670 String sel = (items[0].startsWith("das1:") ? "das1:" : "")
672 for (int j = 0; j < items.length; j++)
674 if (sel.equals(items[j]))
684 void setSelectedFromProperties()
686 String active = jalview.bin.Cache.getDefault("DAS_ACTIVE_SOURCE",
688 StringTokenizer st = new StringTokenizer(active, "\t");
689 selectedSources = new Vector();
690 while (st.hasMoreTokens())
692 selectedSources.addElement(st.nextToken());
696 public void reset_actionPerformed(ActionEvent e)
698 registryURL.setText(sourceRegistry.getDasRegistryURL());
702 * set the DAS source settings in the given jalview properties.
706 public void saveProperties(Properties properties)
708 if (registryURL.getText() == null || registryURL.getText().length() < 1)
710 properties.remove(jalview.bin.Cache.DAS_REGISTRY_URL);
714 properties.setProperty(jalview.bin.Cache.DAS_REGISTRY_URL,
715 registryURL.getText());
718 StringBuffer sb = new StringBuffer();
719 for (int r = 0; r < table.getModel().getRowCount(); r++)
721 if (((Boolean) table.getValueAt(r, 1)).booleanValue())
723 sb.append(table.getValueAt(r, 0) + "\t");
727 properties.setProperty(jalview.bin.Cache.DAS_ACTIVE_SOURCE,
730 String sourceprop = sourceRegistry.getLocalSourceString();
731 properties.setProperty(jalview.bin.Cache.DAS_LOCAL_SOURCE, sourceprop);
734 class DASTableModel extends AbstractTableModel
737 public DASTableModel(Object[][] data)
742 private String[] columnNames = new String[]
743 { MessageManager.getString("label.nickname"), MessageManager.getString("label.use_source") };
745 private Object[][] data;
747 public int getColumnCount()
749 return columnNames.length;
752 public int getRowCount()
757 public String getColumnName(int col)
759 return columnNames[col];
762 public Object getValueAt(int row, int col)
764 return data[row][col];
768 * JTable uses this method to determine the default renderer/ editor for
769 * each cell. If we didn't implement this method, then the last column would
770 * contain text ("true"/"false"), rather than a check box.
772 public Class getColumnClass(int c)
774 return getValueAt(0, c).getClass();
778 * Don't need to implement this method unless your table's editable.
780 public boolean isCellEditable(int row, int col)
782 // Note that the data/cell address is constant,
783 // no matter where the cell appears onscreen.
789 * Don't need to implement this method unless your table's data can change.
791 public void setValueAt(Object value, int row, int col)
793 data[row][col] = value;
794 fireTableCellUpdated(row, col);
796 String name = getValueAt(row, 0).toString();
797 boolean selected = ((Boolean) value).booleanValue();
799 if (selectedSources.contains(name) && !selected)
801 selectedSources.remove(name);
804 if (!selectedSources.contains(name) && selected)
806 selectedSources.add(name);
811 public void initDasSources()
814 Thread thr = new Thread(new Runnable()
818 // this actually initialises the das source list
819 paintComponent(null); // yuk
823 while (loadingDasSources || sourceRegistry == null)
828 } catch (Exception e)
836 * disable or enable the buttons on the source browser
840 public void setGuiEnabled(boolean b)
842 refresh.setEnabled(b);
843 addLocal.setEnabled(b);