2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
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
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()
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 + MessageManager.getString("label.select_das_service_from_table"));
181 int dSize = sourceRegistry.getSources().size();
182 for (jalviewSourceI ds : sourceRegistry.getSources())
184 if (!ds.getTitle().equals(nickName))
189 VERSION latest = ds.getVersion();
190 text.append("<font color=\"#0000FF\">Id:</font> " + ds.getUri()
192 text.append("<font color=\"#0000FF\">Nickname:</font> "
193 + ds.getTitle() + "<br>");
195 text.append("<font color=\"#0000FF\">URL:</font> <a href=\""
196 + ds.getSourceURL() + "\">" + ds.getSourceURL() + "</a>"
200 if (ds.getDocHref() != null && ds.getDocHref().length() > 0)
202 text.append("<font color=\"#0000FF\">Site:</font> <a href=\""
203 + ds.getDocHref() + "\">" + ds.getDocHref() + "</a>"
207 text.append("<font color=\"#0000FF\">Description:</font> "
208 + ds.getDescription() + "<br>");
210 text.append("<font color=\"#0000FF\">Admin Email:</font> <a href=\"mailto:"
211 + ds.getEmail() + "\">" + ds.getEmail() + "</a>" + "<br>");
213 text.append("<font color=\"#0000FF\">Registered at:</font> "
214 + latest.getCreated() + "<br>");
216 // TODO: Identify last successful test date
217 // text.append("<font color=\"#0000FF\">Last successful test:</font> "
218 // + latest.dasSources[i].getLeaseDate() + "<br>");
222 text.append("Source was added manually.<br/>");
224 text.append("<font color=\"#0000FF\">Labels:</font> ");
226 for (PROP labl : latest.getPROP())
228 if (labl.getName().equalsIgnoreCase("LABEL"))
236 text.append(labl.getValue());
243 text.append("<font color=\"#0000FF\">Capabilities:</font> ");
244 CAPABILITY[] scap = latest.getCAPABILITY().toArray(new CAPABILITY[0]);
245 for (int j = 0; j < scap.length; j++)
247 text.append(scap[j].getType());
248 if (j < scap.length - 1)
255 text.append("<font color=\"#0000FF\">Coordinates:</font>");
257 for (COORDINATES dcs : latest.getCOORDINATES())
259 text.append("<br/>" + i++ + ". ");
260 text.append(dcs.getAuthority() + " : " + dcs.getSource());
261 if (dcs.getTaxid() != null && dcs.getTaxid().trim().length() > 0)
263 text.append(" [TaxId:" + dcs.getTaxid() + "]");
265 if (dcs.getVersion() != null
266 && dcs.getVersion().trim().length() > 0)
269 text.append(" {v. " + dcs.getVersion() + "}");
272 text.append(" (<a href=\"" + dcs.getUri() + "\">" + dcs.getUri()
275 text.append("</font></html>");
280 fullDetails.setText(text.toString());
281 javax.swing.SwingUtilities.invokeLater(new Runnable()
285 fullDetailsScrollpane.getVerticalScrollBar().setValue(0);
292 loadingDasSources = true;
294 addLocal.setVisible(false);
295 refresh.setVisible(false);
296 progressBar.setVisible(true);
297 progressBar.setIndeterminate(true);
298 setParentGuiEnabled(false);
299 // Refresh the source list.
300 sourceRegistry.refreshSources();
304 setParentGuiEnabled(true);
305 loadingDasSources = false;
309 private void setParentGuiEnabled(boolean b)
313 fs.fetchDAS.setEnabled(b);
314 fs.saveDAS.setEnabled(b);
318 public Vector<jalviewSourceI> getSelectedSources()
320 // wait around if we're still loading.
321 while (sourceRegistry == null)
323 if (!loadingDasSources)
325 new Thread(this).start();
329 } catch (Exception e)
333 while (loadingDasSources)
338 } catch (Exception e)
347 Vector<jalviewSourceI> selected = new Vector<jalviewSourceI>();
348 for (String source : selectedSources)
350 jalviewSourceI srce = sourceRegistry.getSource(source);
353 selected.addElement(srce);
359 public void refresh_actionPerformed(ActionEvent e)
361 saveProperties(jalview.bin.Cache.applicationProperties);
363 Thread worker = new Thread(this);
367 private void setCapabilities(DasSourceRegistryI sourceRegistry2)
369 Vector<String> authority = new Vector<String>();
370 Vector<String> type = new Vector<String>();
371 Vector<String> label = new Vector<String>();
372 Vector<String> taxIds = new Vector<String>();
373 authority.add("Any");
377 for (jalviewSourceI ds : sourceRegistry2.getSources())
379 VERSION latest = ds.getVersion();
381 for (COORDINATES cs : latest.getCOORDINATES())
383 if (!type.contains(cs.getSource()))
385 type.add(cs.getSource()); // source==category
388 if (!authority.contains(cs.getAuthority()))
390 authority.add(cs.getAuthority());
394 for (PROP slabel : latest.getPROP())
396 if (slabel.getName().equalsIgnoreCase("LABEL")
397 && !label.contains(slabel.getValue()))
399 label.add(slabel.getValue());
405 filter1.setListData(authority);
406 filter2.setListData(type);
407 filter3.setListData(label);
410 javax.swing.SwingUtilities.invokeLater(new Runnable()
414 filter1.setSelectedIndex(0);
415 filter2.setSelectedIndex(0);
416 filter3.setSelectedIndex(0);
421 public void amendLocal(boolean newSource)
423 String url = "http://localhost:8080/", nickname = "";
424 boolean seqsrc = false;
427 int selectedRow = table.getSelectionModel().getMinSelectionIndex();
428 nickname = table.getValueAt(selectedRow, 0).toString();
429 jalviewSourceI source = sourceRegistry.getSource(nickname);
430 url = source.getUri();
431 seqsrc = source.isSequenceSource();
434 JTextField nametf = new JTextField(nickname, 40);
435 JTextField urltf = new JTextField(url, 40);
436 JCheckBox seqs = new JCheckBox(MessageManager.getString("label.sequence_source"));
437 seqs.setSelected(seqsrc);
438 JPanel panel = new JPanel(new BorderLayout());
439 JPanel pane12 = new JPanel(new BorderLayout());
440 pane12.add(new JLabel(MessageManager.getString("label.structure_manager")), BorderLayout.CENTER);
441 pane12.add(nametf, BorderLayout.EAST);
442 panel.add(pane12, BorderLayout.NORTH);
443 pane12 = new JPanel(new BorderLayout());
444 pane12.add(new JLabel(MessageManager.getString("label.url")), BorderLayout.NORTH);
445 pane12.add(seqs, BorderLayout.SOUTH);
446 pane12.add(urltf, BorderLayout.EAST);
447 panel.add(pane12, BorderLayout.SOUTH);
449 int reply = JOptionPane.showInternalConfirmDialog(Desktop.desktop,
450 panel, MessageManager.getString("label.enter_local_das_source"),
451 JOptionPane.OK_CANCEL_OPTION);
453 if (reply != JOptionPane.OK_OPTION)
458 if (!urltf.getText().endsWith("/"))
460 urltf.setText(urltf.getText() + "/");
463 jalviewSourceI local = sourceRegistry.createLocalSource(
464 urltf.getText(), nametf.getText(), seqs.isSelected(), true);
465 List sources = sourceRegistry.getSources();
466 int osize = sources.size();
467 int size = osize + (newSource ? 1 : 0);
469 Object[][] data = new Object[size][2];
470 DASTableModel dtm = (table != null) ? (DASTableModel) ((TableSorter) table
471 .getModel()).getTableModel() : null;
472 for (int i = 0; i < osize; i++)
474 String osrc = (dtm == null || i >= osize) ? null : (String) dtm
476 if (!newSource && osrc != null
477 && dtm.getValueAt(i, 0).equals(nickname))
479 data[i][0] = local.getTitle();
480 data[i][1] = new Boolean(true);
485 data[i][1] = new Boolean(selectedSources.contains(osrc));
488 // Always add a new source at the end
491 data[osize][0] = local.getTitle();
492 data[osize][1] = new Boolean(true);
493 selectedSources.add(local.getTitle());
496 refreshTableData(data);
498 SwingUtilities.invokeLater(new Runnable()
502 scrollPane.getVerticalScrollBar().setValue(
503 scrollPane.getVerticalScrollBar().getMaximum());
507 displayFullDetails(local.getTitle());
510 public void editRemoveLocalSource(MouseEvent evt)
512 int selectedRow = table.getSelectionModel().getMinSelectionIndex();
513 if (selectedRow == -1)
518 String nickname = table.getValueAt(selectedRow, 0).toString();
520 if (!sourceRegistry.getSource(nickname).isLocal())
522 JOptionPane.showInternalMessageDialog(Desktop.desktop,
523 MessageManager.getString("label.you_can_only_edit_or_remove_local_das_sources"),
524 MessageManager.getString("label.public_das_source"),
525 JOptionPane.WARNING_MESSAGE);
530 { "Edit", "Remove", "Cancel" };
531 int choice = JOptionPane.showInternalOptionDialog(Desktop.desktop,
532 "Do you want to edit or remove " + nickname + "?",
533 "Edit / Remove Local DAS Source",
534 JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE,
535 null, options, options[2]);
543 sourceRegistry.removeLocalSource(sourceRegistry.getSource(nickname));
544 selectedSources.remove(nickname);
545 Object[][] data = new Object[sourceRegistry.getSources().size()][2];
547 l = table.getRowCount();
549 for (int i = 0; i < l; i++)
552 if ((nm = (String) table.getValueAt(i, 0)).equals(nickname))
559 data[index][1] = new Boolean(selectedSources.contains(nm));
563 refreshTableData(data);
564 SwingUtilities.invokeLater(new Runnable()
568 scrollPane.getVerticalScrollBar().setValue(
569 scrollPane.getVerticalScrollBar().getMaximum());
577 public void valueChanged(ListSelectionEvent evt)
579 // Called when the MainTable selection changes
580 if (evt.getValueIsAdjusting())
585 displayFullDetails(null);
587 // Filter the displayed data sources
589 ArrayList names = new ArrayList();
590 ArrayList selected = new ArrayList();
592 // The features filter is not visible, but we must still
593 // filter the das source list here.
594 // July 2006 - only 6 sources fo not serve features
595 Object[] dummyFeatureList = new Object[]
597 List<jalviewSourceI> srcs = sourceRegistry.getSources();
598 for (jalviewSourceI ds : srcs)
601 VERSION v = ds.getVersion();
602 List<COORDINATES> coords = v.getCOORDINATES();
604 || ((coords == null || coords.size() == 0)
605 && filter1.getSelectedIndex() == 0
606 && filter2.getSelectedIndex() == 0 && filter3
607 .getSelectedIndex() == 0))
609 // THIS IS A FIX FOR LOCAL SOURCES WHICH DO NOT
610 // HAVE COORDINATE SYSTEMS, INFO WHICH AT PRESENT
611 // IS ADDED FROM THE REGISTRY
612 names.add(ds.getTitle());
613 selected.add(new Boolean(selectedSources.contains(ds.getTitle())));
617 if (!selectedInList(dummyFeatureList, ds.getCapabilityList(v))
618 || !selectedInList(filter3.getSelectedValues(),
624 for (int j = 0; j < coords.size(); j++)
626 if (selectedInList(filter1.getSelectedValues(), new String[]
627 { coords.get(j).getAuthority() })
628 && selectedInList(filter2.getSelectedValues(), new String[]
629 { coords.get(j).getSource() }))
631 names.add(ds.getTitle());
632 selected.add(new Boolean(selectedSources.contains(ds.getTitle())));
638 int dSize = names.size();
639 Object[][] data = new Object[dSize][2];
640 for (int d = 0; d < dSize; d++)
642 data[d][0] = names.get(d);
643 data[d][1] = selected.get(d);
646 refreshTableData(data);
649 private boolean selectedInList(Object[] selection, String[] items)
651 for (int i = 0; i < selection.length; i++)
653 if (selection[i].equals("Any"))
657 if (items == null || items.length == 0)
661 String sel = (items[0].startsWith("das1:") ? "das1:" : "")
663 for (int j = 0; j < items.length; j++)
665 if (sel.equals(items[j]))
675 void setSelectedFromProperties()
677 String active = jalview.bin.Cache.getDefault("DAS_ACTIVE_SOURCE",
679 StringTokenizer st = new StringTokenizer(active, "\t");
680 selectedSources = new Vector();
681 while (st.hasMoreTokens())
683 selectedSources.addElement(st.nextToken());
687 public void reset_actionPerformed(ActionEvent e)
689 registryURL.setText(sourceRegistry.getDasRegistryURL());
693 * set the DAS source settings in the given jalview properties.
697 public void saveProperties(Properties properties)
699 if (registryURL.getText() == null || registryURL.getText().length() < 1)
701 properties.remove(jalview.bin.Cache.DAS_REGISTRY_URL);
705 properties.setProperty(jalview.bin.Cache.DAS_REGISTRY_URL,
706 registryURL.getText());
709 StringBuffer sb = new StringBuffer();
710 for (int r = 0; r < table.getModel().getRowCount(); r++)
712 if (((Boolean) table.getValueAt(r, 1)).booleanValue())
714 sb.append(table.getValueAt(r, 0) + "\t");
718 properties.setProperty(jalview.bin.Cache.DAS_ACTIVE_SOURCE,
721 String sourceprop = sourceRegistry.getLocalSourceString();
722 properties.setProperty(jalview.bin.Cache.DAS_LOCAL_SOURCE, sourceprop);
725 class DASTableModel extends AbstractTableModel
728 public DASTableModel(Object[][] data)
733 private String[] columnNames = new String[]
734 { "Nickname", "Use Source" };
736 private Object[][] data;
738 public int getColumnCount()
740 return columnNames.length;
743 public int getRowCount()
748 public String getColumnName(int col)
750 return columnNames[col];
753 public Object getValueAt(int row, int col)
755 return data[row][col];
759 * JTable uses this method to determine the default renderer/ editor for
760 * each cell. If we didn't implement this method, then the last column would
761 * contain text ("true"/"false"), rather than a check box.
763 public Class getColumnClass(int c)
765 return getValueAt(0, c).getClass();
769 * Don't need to implement this method unless your table's editable.
771 public boolean isCellEditable(int row, int col)
773 // Note that the data/cell address is constant,
774 // no matter where the cell appears onscreen.
780 * Don't need to implement this method unless your table's data can change.
782 public void setValueAt(Object value, int row, int col)
784 data[row][col] = value;
785 fireTableCellUpdated(row, col);
787 String name = getValueAt(row, 0).toString();
788 boolean selected = ((Boolean) value).booleanValue();
790 if (selectedSources.contains(name) && !selected)
792 selectedSources.remove(name);
795 if (!selectedSources.contains(name) && selected)
797 selectedSources.add(name);
802 public void initDasSources()
805 Thread thr = new Thread(new Runnable()
809 // this actually initialises the das source list
810 paintComponent(null); // yuk
814 while (loadingDasSources || sourceRegistry == null)
819 } catch (Exception e)
827 * disable or enable the buttons on the source browser
831 public void setGuiEnabled(boolean b)
833 refresh.setEnabled(b);
834 addLocal.setEnabled(b);