From 3d50fbabdcd2d6b3dcf60353f5e5e3bdf32b62c6 Mon Sep 17 00:00:00 2001 From: jprocter Date: Wed, 11 Apr 2012 12:02:25 +0100 Subject: [PATCH] dialog for browsing and selecting datasources hierarchically JAL-671 JAL-1064 --- src/jalview/gui/JDatabaseTree.java | 306 ++++++++++++++++++++++++++++++++++++ 1 file changed, 306 insertions(+) create mode 100644 src/jalview/gui/JDatabaseTree.java diff --git a/src/jalview/gui/JDatabaseTree.java b/src/jalview/gui/JDatabaseTree.java new file mode 100644 index 0000000..6b18e84 --- /dev/null +++ b/src/jalview/gui/JDatabaseTree.java @@ -0,0 +1,306 @@ +package jalview.gui; + +import jalview.ws.seqfetcher.DbSourceProxy; + +import java.awt.BorderLayout; +import java.awt.Component; +import java.awt.Container; +import java.awt.FlowLayout; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.util.ArrayList; +import java.util.Hashtable; +import java.util.List; +import java.util.Vector; + +import javax.swing.JButton; +import javax.swing.JFrame; +import javax.swing.JLabel; +import javax.swing.JPanel; +import javax.swing.JScrollPane; +import javax.swing.JTree; +import javax.swing.JViewport; +import javax.swing.tree.DefaultMutableTreeNode; +import javax.swing.tree.DefaultTreeCellRenderer; +import javax.swing.tree.DefaultTreeModel; +import javax.swing.tree.MutableTreeNode; +import javax.swing.tree.TreeCellRenderer; +import javax.swing.tree.TreeModel; +import javax.swing.tree.TreePath; +import javax.swing.tree.TreeSelectionModel; + +import com.sun.java.help.search.DBPartParameters; + +public class JDatabaseTree extends JalviewDialog +{ + JButton getDatabaseSelectorButton() + { + final JButton viewdbs = new JButton("--- Select Database ---"); + viewdbs.addActionListener(new ActionListener() + { + + @Override + public void actionPerformed(ActionEvent arg0) + { + showDialog(null); + } + }); + return viewdbs; + } + + JScrollPane svp = new JScrollPane(); + + JTree dbviews; + + private jalview.ws.SequenceFetcher sfetcher; + + public JDatabaseTree(jalview.ws.SequenceFetcher sfetch) + { + super(); + /* + * Dynamically generated database list will need a translation function from + * internal source to externally distinct names. UNIPROT and UP_NAME are + * identical DB sources, and should be collapsed. + */ + DefaultMutableTreeNode tn = null, root = new DefaultMutableTreeNode(); + Hashtable tns = new Hashtable(); + Hashtable source = new Hashtable(); + sfetcher=sfetch; + String dbs[] = sfetch.getOrderedSupportedSources(); + for (int i = 0; i < dbs.length; i++) + { + tn = source.get(dbs[i]); + List srcs = sfetch.getSourceProxy(dbs[i]); + if (tn == null) + { + source.put(dbs[i], tn = new DefaultMutableTreeNode(dbs[i], true)); + + } + for (DbSourceProxy dbp : srcs) + { + tn.add(new DefaultMutableTreeNode(dbp, false)); + } + + } + for (int i = 0; i < dbs.length; i++) + { + tn = source.get(dbs[i]); + if (tn.getChildCount() == 1) + { + // remove nodes with only one child + source.put(dbs[i], tn = (DefaultMutableTreeNode) tn.getChildAt(0)); + } + root.add(tn); + } + initDialogFrame(this, true, false, "Select Database Retrieval Source", + 400, 300); + + dbviews = new JTree(new DefaultTreeModel(root, false)); + dbviews.setCellRenderer(new DbTreeRenderer(this)); + + dbviews.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION); + add(svp, BorderLayout.CENTER); + JPanel j=new JPanel(new FlowLayout()); + j.add(ok); + j.add(cancel); + add(j,BorderLayout.SOUTH); + svp.getViewport().setView(dbviews); + } + + private class DbTreeRenderer extends DefaultTreeCellRenderer implements TreeCellRenderer + { + JDatabaseTree us; + + public DbTreeRenderer(JDatabaseTree me) + { + us = me; + } + + private Component returnLabel(String txt) + { + JLabel jl = new JLabel(txt); + jl.setFont(JvSwingUtils.getLabelFont()); + return jl; + } + + @Override + public Component getTreeCellRendererComponent(JTree tree, Object value, + boolean selected, boolean expanded, boolean leaf, int row, + boolean hasFocus) + { + String val=""; + if (value != null && value instanceof DefaultMutableTreeNode) + { + DefaultMutableTreeNode vl = (DefaultMutableTreeNode) value; + value = vl.getUserObject(); + if (value instanceof DbSourceProxy) + { + val=(((DbSourceProxy) value).getDbName()); + } + else + { + if (value instanceof String) + { + val=((String) value); + } + } + } + if (value == null) + { + val = (""); + } + return super.getTreeCellRendererComponent(tree, val, selected, expanded, leaf, row, hasFocus); + + } + } + + List selection = null; + + @Override + protected void raiseClosed() + { + for (ActionListener al : lstners) + { + al.actionPerformed(null); + } + } + + @Override + protected void okPressed() + { + _setSelectionState(); + closeDialog(); + } + + @Override + protected void cancelPressed() + { + closeDialog(); + } + + private void showDialog(Container parent) + { + waitForInput(); + } + + public boolean hasSelection() + { + return selection == null ? false : selection.size() == 0 ? false : true; + } + + public List getSelectedSources() + { + return selection; + } + + private void _setSelectionState() + { + if (dbviews.getSelectionCount() == 0) + { + selection = null; + } + List srcs = new ArrayList(); + for (TreePath tp : dbviews.getSelectionPaths()) + { + DefaultMutableTreeNode dmt = (DefaultMutableTreeNode) tp + .getLastPathComponent(); + if (dmt.getUserObject()!=null) { + if (dmt.getUserObject() instanceof DbSourceProxy) { + srcs.add((DbSourceProxy) dmt.getUserObject()); + } + else + { + srcs.addAll(sfetcher.getSourceProxy((String) dmt.getUserObject())); + } + } + } + selection = srcs; + } + + public String getSelectedItem() + { + if (hasSelection()) + { + return getSelectedSources().get(0).getDbName(); + } + return null; + } + + public String getExampleQueries() + { + if (!hasSelection()) + { + return null; + } + StringBuffer sb = new StringBuffer(); + for (DbSourceProxy dbs : getSelectedSources()) + { + if (sb.length() > 0) + { + sb.append(";"); + } + sb.append(dbs.getTestQuery()); + } + return sb.toString(); + } + + List lstners = new Vector(); + + public void addActionListener(ActionListener actionListener) + { + lstners.add(actionListener); + } + + public void removeActionListener(ActionListener actionListener) + { + lstners.remove(actionListener); + } + + public static void main(String args[]) + { + JDatabaseTree jdt = new JDatabaseTree(new jalview.ws.SequenceFetcher()); + JFrame foo = new JFrame(); + foo.setLayout(new BorderLayout()); + foo.add(jdt.getDatabaseSelectorButton(), BorderLayout.CENTER); + foo.pack(); + foo.setVisible(true); + int nultimes = 5; + final Thread us = Thread.currentThread(); + jdt.addActionListener(new ActionListener() + { + + @Override + public void actionPerformed(ActionEvent e) + { + us.interrupt(); + } + }); + do + { + try + { + Thread.sleep(50); + } catch (InterruptedException x) + { + nultimes--; + if (!jdt.hasSelection()) + { + System.out.println("No Selection"); + } + else + { + System.out.println("Selection: " + jdt.getSelectedItem()); + int s = 1; + for (DbSourceProxy pr : jdt.getSelectedSources()) + { + System.out.println("Source " + s++ + ": " + pr.getDbName() + + " (" + pr.getDbSource() + ") Version " + + pr.getDbVersion() + ". Test:\t" + pr.getTestQuery()); + } + System.out.println("Test queries: " + jdt.getExampleQueries()); + } + } + } while (nultimes > 0 && foo.isVisible()); + foo.setVisible(false); + } +} -- 1.7.10.2