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.JPanel;
42 import javax.swing.JTextField;
43 import javax.swing.ListSelectionModel;
44 import javax.swing.SwingUtilities;
45 import javax.swing.event.ListSelectionEvent;
46 import javax.swing.event.ListSelectionListener;
47 import javax.swing.table.AbstractTableModel;
49 import org.biodas.jdas.schema.sources.CAPABILITY;
50 import org.biodas.jdas.schema.sources.COORDINATES;
51 import org.biodas.jdas.schema.sources.PROP;
52 import org.biodas.jdas.schema.sources.VERSION;
54 public class DasSourceBrowser extends GDasSourceBrowser
55 implements Runnable, ListSelectionListener
57 DasSourceRegistryI sourceRegistry = null;
59 Vector<String> selectedSources;
61 public DasSourceBrowser(FeatureSettings featureSettings)
64 // TODO DasSourceRegistryProvider API
65 sourceRegistry = jalview.bin.Cache.getDasSourceRegistry();
66 String registry = sourceRegistry.getDasRegistryURL();
68 registryURL.setText(registry);
70 setSelectedFromProperties();
72 displayFullDetails(null);
73 table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
75 filter1.addListSelectionListener(this);
76 filter2.addListSelectionListener(this);
77 filter3.addListSelectionListener(this);
79 // Ask to be notified of selection changes.
80 ListSelectionModel rowSM = table.getSelectionModel();
81 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()
98 public void mouseClicked(MouseEvent evt)
100 if (evt.getClickCount() == 2 || evt.isPopupTrigger())
102 editRemoveLocalSource(evt);
107 if (sourceRegistry.getSources() != null)
113 FeatureSettings fs = null;
115 private boolean loadingDasSources;
117 public DasSourceBrowser()
123 public void paintComponent(java.awt.Graphics g)
125 if (sourceRegistry == null)
127 Thread worker = new Thread(this, "PaintDasThread");
134 List<jalviewSourceI> sources = sourceRegistry.getSources();
135 int dSize = sources.size();
136 Object[][] data = new Object[dSize][2];
137 for (int i = 0; i < dSize; i++)
139 data[i][0] = sources.get(i).getTitle(); // what's equivalent of nickname
140 data[i][1] = new Boolean(
141 selectedSources.contains(sources.get(i).getTitle()));
144 refreshTableData(data);
145 setCapabilities(sourceRegistry);
147 javax.swing.SwingUtilities.invokeLater(new Runnable()
152 TableSorter sorter = (TableSorter) table.getModel();
153 sorter.setSortingStatus(1, TableSorter.DESCENDING);
154 sorter.setSortingStatus(1, TableSorter.NOT_SORTED);
158 progressBar.setIndeterminate(false);
159 progressBar.setVisible(false);
160 addLocal.setVisible(true);
161 refresh.setVisible(true);
164 public void refreshTableData(Object[][] data)
166 TableSorter sorter = new TableSorter(new DASTableModel(data));
167 sorter.setTableHeader(table.getTableHeader());
168 table.setModel(sorter);
171 void displayFullDetails(String nickName)
174 StringBuffer text = new StringBuffer(
175 "<HTML><font size=\"2\" face=\"Verdana, Arial, Helvetica, sans-serif\">");
177 if (nickName == null)
179 fullDetails.setText(text + MessageManager
180 .getString("label.select_das_service_from_table"));
184 int dSize = sourceRegistry.getSources().size();
185 for (jalviewSourceI ds : sourceRegistry.getSources())
187 if (!ds.getTitle().equals(nickName))
192 VERSION latest = ds.getVersion();
194 "<font color=\"#0000FF\">Id:</font> " + ds.getUri() + "<br>");
195 text.append("<font color=\"#0000FF\">Nickname:</font> "
196 + ds.getTitle() + "<br>");
198 text.append("<font color=\"#0000FF\">URL:</font> <a href=\""
199 + ds.getSourceURL() + "\">" + ds.getSourceURL() + "</a>"
203 if (ds.getDocHref() != null && ds.getDocHref().length() > 0)
205 text.append("<font color=\"#0000FF\">Site:</font> <a href=\""
206 + ds.getDocHref() + "\">" + ds.getDocHref() + "</a>"
210 text.append("<font color=\"#0000FF\">Description:</font> "
211 + ds.getDescription() + "<br>");
214 "<font color=\"#0000FF\">Admin Email:</font> <a href=\"mailto:"
215 + ds.getEmail() + "\">" + ds.getEmail() + "</a>"
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<>();
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, "RepaintDasThread");
375 private void setCapabilities(DasSourceRegistryI sourceRegistry2)
377 Vector<String> authority = new Vector<>();
378 Vector<String> type = new Vector<>();
379 Vector<String> label = new Vector<>();
380 Vector<String> taxIds = new Vector<>();
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,
463 panel, MessageManager.getString("label.enter_local_das_source"),
464 JvOptionPane.OK_CANCEL_OPTION);
466 if (reply != JvOptionPane.OK_OPTION)
471 if (!urltf.getText().endsWith("/"))
473 urltf.setText(urltf.getText() + "/");
476 jalviewSourceI local = sourceRegistry.createLocalSource(urltf.getText(),
477 nametf.getText(), seqs.isSelected(), true);
478 List sources = sourceRegistry.getSources();
479 int osize = sources.size();
480 int size = osize + (newSource ? 1 : 0);
482 Object[][] data = new Object[size][2];
483 DASTableModel dtm = (table != null)
484 ? (DASTableModel) ((TableSorter) table.getModel())
487 for (int i = 0; i < osize; i++)
489 String osrc = (dtm == null || i >= osize) ? null
490 : (String) dtm.getValueAt(i, 0);
491 if (!newSource && osrc != null
492 && dtm.getValueAt(i, 0).equals(nickname))
494 data[i][0] = local.getTitle();
495 data[i][1] = new Boolean(true);
500 data[i][1] = new Boolean(selectedSources.contains(osrc));
503 // Always add a new source at the end
506 data[osize][0] = local.getTitle();
507 data[osize][1] = new Boolean(true);
508 selectedSources.add(local.getTitle());
511 refreshTableData(data);
513 SwingUtilities.invokeLater(new Runnable()
518 scrollPane.getVerticalScrollBar()
519 .setValue(scrollPane.getVerticalScrollBar().getMaximum());
523 displayFullDetails(local.getTitle());
526 public void editRemoveLocalSource(MouseEvent evt)
528 int selectedRow = table.getSelectionModel().getMinSelectionIndex();
529 if (selectedRow == -1)
534 String nickname = table.getValueAt(selectedRow, 0).toString();
536 if (!sourceRegistry.getSource(nickname).isLocal())
538 JvOptionPane.showInternalMessageDialog(Desktop.desktop,
539 MessageManager.getString(
540 "label.you_can_only_edit_or_remove_local_das_sources"),
541 MessageManager.getString("label.public_das_source"),
542 JvOptionPane.WARNING_MESSAGE);
546 Object[] options = { "Edit", "Remove", "Cancel" };
547 int choice = JvOptionPane.showInternalOptionDialog(Desktop.desktop,
548 "Do you want to edit or remove " + nickname + "?",
549 "Edit / Remove Local DAS Source",
550 JvOptionPane.YES_NO_CANCEL_OPTION,
551 JvOptionPane.QUESTION_MESSAGE, null, options, options[2]);
559 sourceRegistry.removeLocalSource(sourceRegistry.getSource(nickname));
560 selectedSources.remove(nickname);
561 Object[][] data = new Object[sourceRegistry.getSources().size()][2];
562 int index = 0, l = table.getRowCount();
564 for (int i = 0; i < l; i++)
567 if ((nm = (String) table.getValueAt(i, 0)).equals(nickname))
574 data[index][1] = new Boolean(selectedSources.contains(nm));
578 refreshTableData(data);
579 SwingUtilities.invokeLater(new Runnable()
584 scrollPane.getVerticalScrollBar()
585 .setValue(scrollPane.getVerticalScrollBar().getMaximum());
594 public void valueChanged(ListSelectionEvent evt)
596 // Called when the MainTable selection changes
597 if (evt.getValueIsAdjusting())
602 displayFullDetails(null);
604 // Filter the displayed data sources
606 ArrayList names = new ArrayList();
607 ArrayList selected = new ArrayList();
609 // The features filter is not visible, but we must still
610 // filter the das source list here.
611 // July 2006 - only 6 sources fo not serve features
612 Object[] dummyFeatureList = new Object[] { "features" };
613 List<jalviewSourceI> srcs = sourceRegistry.getSources();
614 for (jalviewSourceI ds : srcs)
617 VERSION v = ds.getVersion();
618 List<COORDINATES> coords = v.getCOORDINATES();
619 if (ds.isLocal() || ((coords == null || coords.size() == 0)
620 && filter1.getSelectedIndex() == 0
621 && filter2.getSelectedIndex() == 0
622 && filter3.getSelectedIndex() == 0))
624 // THIS IS A FIX FOR LOCAL SOURCES WHICH DO NOT
625 // HAVE COORDINATE SYSTEMS, INFO WHICH AT PRESENT
626 // IS ADDED FROM THE REGISTRY
627 names.add(ds.getTitle());
628 selected.add(new Boolean(selectedSources.contains(ds.getTitle())));
632 if (!selectedInList(dummyFeatureList, ds.getCapabilityList(v))
633 || !selectedInList(filter3.getSelectedValues(),
639 for (int j = 0; j < coords.size(); j++)
641 if (selectedInList(filter1.getSelectedValues(),
643 { coords.get(j).getAuthority() })
644 && selectedInList(filter2.getSelectedValues(), new String[]
645 { coords.get(j).getSource() }))
647 names.add(ds.getTitle());
649 new Boolean(selectedSources.contains(ds.getTitle())));
655 int dSize = names.size();
656 Object[][] data = new Object[dSize][2];
657 for (int d = 0; d < dSize; d++)
659 data[d][0] = names.get(d);
660 data[d][1] = selected.get(d);
663 refreshTableData(data);
666 private boolean selectedInList(Object[] selection, String[] items)
668 for (int i = 0; i < selection.length; i++)
670 if (selection[i].equals("Any"))
674 if (items == null || items.length == 0)
678 String sel = (items[0].startsWith("das1:") ? "das1:" : "")
680 for (int j = 0; j < items.length; j++)
682 if (sel.equals(items[j]))
692 void setSelectedFromProperties()
694 String active = jalview.bin.Cache.getDefault("DAS_ACTIVE_SOURCE",
696 StringTokenizer st = new StringTokenizer(active, "\t");
697 selectedSources = new Vector();
698 while (st.hasMoreTokens())
700 selectedSources.addElement(st.nextToken());
705 public void reset_actionPerformed(ActionEvent e)
707 registryURL.setText(sourceRegistry.getDasRegistryURL());
711 * set the DAS source settings in the given jalview properties.
715 public void saveProperties(Properties properties)
717 if (registryURL.getText() == null || registryURL.getText().length() < 1)
719 properties.remove(jalview.bin.Cache.DAS_REGISTRY_URL);
723 properties.setProperty(jalview.bin.Cache.DAS_REGISTRY_URL,
724 registryURL.getText());
727 StringBuffer sb = new StringBuffer();
728 for (int r = 0; r < table.getModel().getRowCount(); r++)
730 if (((Boolean) table.getValueAt(r, 1)).booleanValue())
732 sb.append(table.getValueAt(r, 0) + "\t");
736 properties.setProperty(jalview.bin.Cache.DAS_ACTIVE_SOURCE,
739 String sourceprop = sourceRegistry.getLocalSourceString();
740 properties.setProperty(jalview.bin.Cache.DAS_LOCAL_SOURCE, sourceprop);
743 class DASTableModel extends AbstractTableModel
746 public DASTableModel(Object[][] data)
751 private String[] columnNames = new String[] {
752 MessageManager.getString("label.nickname"),
753 MessageManager.getString("label.use_source") };
755 private Object[][] data;
758 public int getColumnCount()
760 return columnNames.length;
764 public int getRowCount()
770 public String getColumnName(int col)
772 return columnNames[col];
776 public Object getValueAt(int row, int col)
778 return data[row][col];
782 * JTable uses this method to determine the default renderer/ editor for
783 * each cell. If we didn't implement this method, then the last column would
784 * contain text ("true"/"false"), rather than a check box.
787 public Class getColumnClass(int c)
789 return getValueAt(0, c).getClass();
793 * Don't need to implement this method unless your table's editable.
796 public boolean isCellEditable(int row, int col)
798 // Note that the data/cell address is constant,
799 // no matter where the cell appears onscreen.
805 * Don't need to implement this method unless your table's data can change.
808 public void setValueAt(Object value, int row, int col)
810 data[row][col] = value;
811 fireTableCellUpdated(row, col);
813 String name = getValueAt(row, 0).toString();
814 boolean selected = ((Boolean) value).booleanValue();
816 if (selectedSources.contains(name) && !selected)
818 selectedSources.remove(name);
821 if (!selectedSources.contains(name) && selected)
823 selectedSources.add(name);
828 public void initDasSources()
831 Thread thr = new Thread(new Runnable()
836 // this actually initialises the das source list
837 paintComponent(null); // yuk
841 while (loadingDasSources || sourceRegistry == null)
846 } catch (Exception e)
854 * disable or enable the buttons on the source browser
858 public void setGuiEnabled(boolean b)
860 refresh.setEnabled(b);
861 addLocal.setEnabled(b);