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
56 implements 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(
142 selectedSources.contains(sources.get(i).getTitle()));
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 + MessageManager
181 .getString("label.select_das_service_from_table"));
185 int dSize = sourceRegistry.getSources().size();
186 for (jalviewSourceI ds : sourceRegistry.getSources())
188 if (!ds.getTitle().equals(nickName))
193 VERSION latest = ds.getVersion();
195 "<font color=\"#0000FF\">Id:</font> " + ds.getUri() + "<br>");
196 text.append("<font color=\"#0000FF\">Nickname:</font> "
197 + ds.getTitle() + "<br>");
199 text.append("<font color=\"#0000FF\">URL:</font> <a href=\""
200 + ds.getSourceURL() + "\">" + ds.getSourceURL() + "</a>"
204 if (ds.getDocHref() != null && ds.getDocHref().length() > 0)
206 text.append("<font color=\"#0000FF\">Site:</font> <a href=\""
207 + ds.getDocHref() + "\">" + ds.getDocHref() + "</a>"
211 text.append("<font color=\"#0000FF\">Description:</font> "
212 + ds.getDescription() + "<br>");
215 "<font color=\"#0000FF\">Admin Email:</font> <a href=\"mailto:"
216 + ds.getEmail() + "\">" + ds.getEmail() + "</a>"
219 text.append("<font color=\"#0000FF\">Registered at:</font> "
220 + latest.getCreated() + "<br>");
222 // TODO: Identify last successful test date
223 // text.append("<font color=\"#0000FF\">Last successful test:</font> "
224 // + latest.dasSources[i].getLeaseDate() + "<br>");
228 text.append("Source was added manually.<br/>");
230 text.append("<font color=\"#0000FF\">Labels:</font> ");
232 for (PROP labl : latest.getPROP())
234 if (labl.getName().equalsIgnoreCase("LABEL"))
242 text.append(labl.getValue());
249 text.append("<font color=\"#0000FF\">Capabilities:</font> ");
250 CAPABILITY[] scap = latest.getCAPABILITY().toArray(new CAPABILITY[0]);
251 for (int j = 0; j < scap.length; j++)
253 text.append(scap[j].getType());
254 if (j < scap.length - 1)
261 text.append("<font color=\"#0000FF\">Coordinates:</font>");
263 for (COORDINATES dcs : latest.getCOORDINATES())
265 text.append("<br/>" + i++ + ". ");
266 text.append(dcs.getAuthority() + " : " + dcs.getSource());
267 if (dcs.getTaxid() != null && dcs.getTaxid().trim().length() > 0)
269 text.append(" [TaxId:" + dcs.getTaxid() + "]");
271 if (dcs.getVersion() != null
272 && dcs.getVersion().trim().length() > 0)
275 text.append(" {v. " + dcs.getVersion() + "}");
278 text.append(" (<a href=\"" + dcs.getUri() + "\">" + dcs.getUri()
281 text.append("</font></html>");
286 fullDetails.setText(text.toString());
287 javax.swing.SwingUtilities.invokeLater(new Runnable()
292 fullDetailsScrollpane.getVerticalScrollBar().setValue(0);
300 loadingDasSources = true;
302 addLocal.setVisible(false);
303 refresh.setVisible(false);
304 progressBar.setVisible(true);
305 progressBar.setIndeterminate(true);
306 setParentGuiEnabled(false);
307 // Refresh the source list.
308 sourceRegistry.refreshSources();
312 setParentGuiEnabled(true);
313 loadingDasSources = false;
317 private void setParentGuiEnabled(boolean b)
321 fs.fetchDAS.setEnabled(b);
322 fs.saveDAS.setEnabled(b);
326 public Vector<jalviewSourceI> getSelectedSources()
328 // wait around if we're still loading.
329 while (sourceRegistry == null)
331 if (!loadingDasSources)
333 new Thread(this).start();
337 } catch (Exception e)
341 while (loadingDasSources)
346 } catch (Exception e)
355 Vector<jalviewSourceI> selected = new Vector<jalviewSourceI>();
356 for (String source : selectedSources)
358 jalviewSourceI srce = sourceRegistry.getSource(source);
361 selected.addElement(srce);
368 public void refresh_actionPerformed(ActionEvent e)
370 saveProperties(jalview.bin.Cache.applicationProperties);
372 Thread worker = new Thread(this);
376 private void setCapabilities(DasSourceRegistryI sourceRegistry2)
378 Vector<String> authority = new Vector<String>();
379 Vector<String> type = new Vector<String>();
380 Vector<String> label = new Vector<String>();
381 Vector<String> taxIds = new Vector<String>();
382 authority.add("Any");
386 for (jalviewSourceI ds : sourceRegistry2.getSources())
388 VERSION latest = ds.getVersion();
390 for (COORDINATES cs : latest.getCOORDINATES())
392 if (!type.contains(cs.getSource()))
394 type.add(cs.getSource()); // source==category
397 if (!authority.contains(cs.getAuthority()))
399 authority.add(cs.getAuthority());
403 for (PROP slabel : latest.getPROP())
405 if (slabel.getName().equalsIgnoreCase("LABEL")
406 && !label.contains(slabel.getValue()))
408 label.add(slabel.getValue());
414 filter1.setListData(authority);
415 filter2.setListData(type);
416 filter3.setListData(label);
419 javax.swing.SwingUtilities.invokeLater(new Runnable()
424 filter1.setSelectedIndex(0);
425 filter2.setSelectedIndex(0);
426 filter3.setSelectedIndex(0);
432 public void amendLocal(boolean newSource)
434 String url = "http://localhost:8080/", nickname = "";
435 boolean seqsrc = false;
438 int selectedRow = table.getSelectionModel().getMinSelectionIndex();
439 nickname = table.getValueAt(selectedRow, 0).toString();
440 jalviewSourceI source = sourceRegistry.getSource(nickname);
441 url = source.getUri();
442 seqsrc = source.isSequenceSource();
445 JTextField nametf = new JTextField(nickname, 40);
446 JTextField urltf = new JTextField(url, 40);
447 JCheckBox seqs = new JCheckBox(
448 MessageManager.getString("label.sequence_source"));
449 seqs.setSelected(seqsrc);
450 JPanel panel = new JPanel(new BorderLayout());
451 JPanel pane12 = new JPanel(new BorderLayout());
452 pane12.add(new JLabel(MessageManager.getString("label.name:")),
453 BorderLayout.CENTER);
454 pane12.add(nametf, BorderLayout.EAST);
455 panel.add(pane12, BorderLayout.NORTH);
456 pane12 = new JPanel(new BorderLayout());
457 pane12.add(new JLabel(MessageManager.getString("label.url:")),
459 pane12.add(seqs, BorderLayout.SOUTH);
460 pane12.add(urltf, BorderLayout.EAST);
461 panel.add(pane12, BorderLayout.SOUTH);
463 int reply = JvOptionPane.showInternalConfirmDialog(Desktop.desktop,
464 panel, 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(urltf.getText(),
478 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)
485 ? (DASTableModel) ((TableSorter) table.getModel())
488 for (int i = 0; i < osize; i++)
490 String osrc = (dtm == null || i >= osize) ? null
491 : (String) dtm.getValueAt(i, 0);
492 if (!newSource && osrc != null
493 && dtm.getValueAt(i, 0).equals(nickname))
495 data[i][0] = local.getTitle();
496 data[i][1] = new Boolean(true);
501 data[i][1] = new Boolean(selectedSources.contains(osrc));
504 // Always add a new source at the end
507 data[osize][0] = local.getTitle();
508 data[osize][1] = new Boolean(true);
509 selectedSources.add(local.getTitle());
512 refreshTableData(data);
514 SwingUtilities.invokeLater(new Runnable()
519 scrollPane.getVerticalScrollBar()
520 .setValue(scrollPane.getVerticalScrollBar().getMaximum());
524 displayFullDetails(local.getTitle());
527 public void editRemoveLocalSource(MouseEvent evt)
529 int selectedRow = table.getSelectionModel().getMinSelectionIndex();
530 if (selectedRow == -1)
535 String nickname = table.getValueAt(selectedRow, 0).toString();
537 if (!sourceRegistry.getSource(nickname).isLocal())
539 JvOptionPane.showInternalMessageDialog(Desktop.desktop,
540 MessageManager.getString(
541 "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,
552 JvOptionPane.QUESTION_MESSAGE, null, options, options[2]);
560 sourceRegistry.removeLocalSource(sourceRegistry.getSource(nickname));
561 selectedSources.remove(nickname);
562 Object[][] data = new Object[sourceRegistry.getSources().size()][2];
563 int index = 0, l = table.getRowCount();
565 for (int i = 0; i < l; i++)
568 if ((nm = (String) table.getValueAt(i, 0)).equals(nickname))
575 data[index][1] = new Boolean(selectedSources.contains(nm));
579 refreshTableData(data);
580 SwingUtilities.invokeLater(new Runnable()
585 scrollPane.getVerticalScrollBar()
586 .setValue(scrollPane.getVerticalScrollBar().getMaximum());
595 public void valueChanged(ListSelectionEvent evt)
597 // Called when the MainTable selection changes
598 if (evt.getValueIsAdjusting())
603 displayFullDetails(null);
605 // Filter the displayed data sources
607 ArrayList names = new ArrayList();
608 ArrayList selected = new ArrayList();
610 // The features filter is not visible, but we must still
611 // filter the das source list here.
612 // July 2006 - only 6 sources fo not serve features
613 Object[] dummyFeatureList = new Object[] { "features" };
614 List<jalviewSourceI> srcs = sourceRegistry.getSources();
615 for (jalviewSourceI ds : srcs)
618 VERSION v = ds.getVersion();
619 List<COORDINATES> coords = v.getCOORDINATES();
620 if (ds.isLocal() || ((coords == null || coords.size() == 0)
621 && filter1.getSelectedIndex() == 0
622 && filter2.getSelectedIndex() == 0
623 && filter3.getSelectedIndex() == 0))
625 // THIS IS A FIX FOR LOCAL SOURCES WHICH DO NOT
626 // HAVE COORDINATE SYSTEMS, INFO WHICH AT PRESENT
627 // IS ADDED FROM THE REGISTRY
628 names.add(ds.getTitle());
629 selected.add(new Boolean(selectedSources.contains(ds.getTitle())));
633 if (!selectedInList(dummyFeatureList, ds.getCapabilityList(v))
634 || !selectedInList(filter3.getSelectedValues(),
640 for (int j = 0; j < coords.size(); j++)
642 if (selectedInList(filter1.getSelectedValues(),
644 { coords.get(j).getAuthority() })
645 && selectedInList(filter2.getSelectedValues(), new String[]
646 { coords.get(j).getSource() }))
648 names.add(ds.getTitle());
650 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);