X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;ds=sidebyside;f=src%2Fjalview%2Fgui%2FJDatabaseTree.java;h=0a6b9d64e9388f7c06acf22bc251aef06f3cef6a;hb=53b99b41ed7f26f2724f3ed87d524eb83c5208f4;hp=6b18e843415edc327b1d6fd757516bd3bcff9014;hpb=3d50fbabdcd2d6b3dcf60353f5e5e3bdf32b62c6;p=jalview.git
diff --git a/src/jalview/gui/JDatabaseTree.java b/src/jalview/gui/JDatabaseTree.java
index 6b18e84..0a6b9d6 100644
--- a/src/jalview/gui/JDatabaseTree.java
+++ b/src/jalview/gui/JDatabaseTree.java
@@ -1,14 +1,42 @@
+/*
+ * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
+ * Copyright (C) $$Year-Rel$$ The Jalview Authors
+ *
+ * This file is part of Jalview.
+ *
+ * Jalview is free software: you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation, either version 3
+ * of the License, or (at your option) any later version.
+ *
+ * Jalview is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty
+ * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
+ * PURPOSE. See the GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Jalview. If not, see .
+ * The Jalview Authors are detailed in the 'AUTHORS' file.
+ */
package jalview.gui;
+import jalview.bin.Cache;
+import jalview.util.MessageManager;
import jalview.ws.seqfetcher.DbSourceProxy;
import java.awt.BorderLayout;
import java.awt.Component;
-import java.awt.Container;
+import java.awt.Dimension;
import java.awt.FlowLayout;
+import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
+import java.awt.event.KeyEvent;
+import java.awt.event.KeyListener;
+import java.awt.event.MouseAdapter;
+import java.awt.event.MouseEvent;
import java.util.ArrayList;
+import java.util.HashSet;
import java.util.Hashtable;
import java.util.List;
import java.util.Vector;
@@ -19,54 +47,64 @@ import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTree;
-import javax.swing.JViewport;
+import javax.swing.ToolTipManager;
+import javax.swing.event.TreeSelectionEvent;
+import javax.swing.event.TreeSelectionListener;
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.TreeNode;
import javax.swing.tree.TreePath;
import javax.swing.tree.TreeSelectionModel;
-import com.sun.java.help.search.DBPartParameters;
-
-public class JDatabaseTree extends JalviewDialog
+public class JDatabaseTree extends JalviewDialog implements KeyListener
{
+ boolean allowMultiSelections = false;
+
+ public int action;
+
JButton getDatabaseSelectorButton()
{
- final JButton viewdbs = new JButton("--- Select Database ---");
+ final JButton viewdbs = new JButton(
+ MessageManager.getString("action.select_ddbb"));
viewdbs.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent arg0)
{
- showDialog(null);
+ showDialog();
}
});
return viewdbs;
}
- JScrollPane svp = new JScrollPane();
+ JScrollPane svp;
JTree dbviews;
private jalview.ws.SequenceFetcher sfetcher;
+ private JLabel dbstatus, dbstatex;
+
+ private JPanel mainPanel = new JPanel(new BorderLayout());
+
public JDatabaseTree(jalview.ws.SequenceFetcher sfetch)
{
- super();
+ mainPanel.add(this);
+ initDialogFrame(mainPanel, true, false, MessageManager
+ .getString("label.select_database_retrieval_source"), 650, 490);
/*
* 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();
+ sfetcher = sfetch;
+ String dbs[] = sfetch.getSupportedDb();
+ Hashtable ht = new Hashtable();
for (int i = 0; i < dbs.length; i++)
{
tn = source.get(dbs[i]);
@@ -74,46 +112,154 @@ public class JDatabaseTree extends JalviewDialog
if (tn == null)
{
source.put(dbs[i], tn = new DefaultMutableTreeNode(dbs[i], true));
-
}
for (DbSourceProxy dbp : srcs)
{
- tn.add(new DefaultMutableTreeNode(dbp, false));
+ if (ht.get(dbp.getDbName()) == null)
+ {
+ tn.add(new DefaultMutableTreeNode(dbp, false));
+ ht.put(dbp.getDbName(), dbp.getDbName());
+ }
+ else
+ {
+ System.err.println("dupe ig for : " + dbs[i] + " \t"
+ + dbp.getDbName() + " (" + dbp.getDbSource() + ")");
+ source.remove(tn);
+ }
}
-
}
for (int i = 0; i < dbs.length; i++)
{
tn = source.get(dbs[i]);
+ if (tn == null)
+ {
+ continue;
+ }
if (tn.getChildCount() == 1)
{
+ DefaultMutableTreeNode ttn = (DefaultMutableTreeNode) tn
+ .getChildAt(0);
// remove nodes with only one child
- source.put(dbs[i], tn = (DefaultMutableTreeNode) tn.getChildAt(0));
+ tn.setUserObject(ttn.getUserObject());
+ tn.removeAllChildren();
+ source.put(dbs[i], tn);
+ tn.setAllowsChildren(false);
}
root.add(tn);
}
- initDialogFrame(this, true, false, "Select Database Retrieval Source",
- 400, 300);
-
+ // and sort the tree
+ sortTreeNodes(root);
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());
+
+ dbviews.getSelectionModel()
+ .setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
+ svp = new JScrollPane(dbviews);
+ svp.setMinimumSize(new Dimension(100, 200));
+ svp.setPreferredSize(new Dimension(200, 400));
+ svp.setMaximumSize(new Dimension(300, 600));
+
+ JPanel panel = new JPanel(new BorderLayout());
+ panel.setSize(new Dimension(350, 220));
+ panel.add(svp);
+ dbviews.addTreeSelectionListener(new TreeSelectionListener()
+ {
+
+ @Override
+ public void valueChanged(TreeSelectionEvent arg0)
+ {
+ _setSelectionState();
+ }
+ });
+ dbviews.addMouseListener(new MouseAdapter()
+ {
+
+ @Override
+ public void mousePressed(MouseEvent e)
+ {
+ if (e.getClickCount() == 2)
+ {
+ okPressed();
+ closeDialog();
+ }
+ }
+ });
+ JPanel jc = new JPanel(new BorderLayout()),
+ j = new JPanel(new FlowLayout());
+ jc.add(svp, BorderLayout.CENTER);
+
+ java.awt.Font f;
+ // TODO: make the panel stay a fixed size for longest dbname+example set.
+ JPanel dbstat = new JPanel(new GridLayout(2, 1));
+ dbstatus = new JLabel(" "); // set the height correctly for layout
+ dbstatus.setFont(f = JvSwingUtils.getLabelFont(false, true));
+ dbstatus.setSize(new Dimension(290, 50));
+ dbstatex = new JLabel(" ");
+ dbstatex.setFont(f);
+ dbstatex.setSize(new Dimension(290, 50));
+ dbstat.add(dbstatus);
+ dbstat.add(dbstatex);
+ jc.add(dbstat, BorderLayout.SOUTH);
+ jc.validate();
+ add(jc, BorderLayout.CENTER);
+ ok.setEnabled(false);
j.add(ok);
j.add(cancel);
- add(j,BorderLayout.SOUTH);
- svp.getViewport().setView(dbviews);
+ add(j, BorderLayout.SOUTH);
+ dbviews.addKeyListener(this);
+ validate();
+ }
+
+ private void sortTreeNodes(DefaultMutableTreeNode root)
+ {
+ if (root.getChildCount() == 0)
+ {
+ return;
+ }
+ int count = root.getChildCount();
+ String[] names = new String[count];
+ DefaultMutableTreeNode[] nodes = new DefaultMutableTreeNode[count];
+ for (int i = 0; i < count; i++)
+ {
+ TreeNode node = root.getChildAt(i);
+ if (node instanceof DefaultMutableTreeNode)
+ {
+ DefaultMutableTreeNode child = (DefaultMutableTreeNode) node;
+ nodes[i] = child;
+ if (child.getUserObject() instanceof DbSourceProxy)
+ {
+ names[i] = ((DbSourceProxy) child.getUserObject()).getDbName()
+ .toLowerCase();
+ }
+ else
+ {
+ names[i] = ((String) child.getUserObject()).toLowerCase();
+ sortTreeNodes(child);
+ }
+ }
+ else
+ {
+ throw new Error(MessageManager
+ .getString("error.implementation_error_cant_reorder_tree"));
+ }
+ }
+ jalview.util.QuickSort.sort(names, nodes);
+ root.removeAllChildren();
+ for (int i = count - 1; i >= 0; i--)
+ {
+ root.add(nodes[i]);
+ }
}
- private class DbTreeRenderer extends DefaultTreeCellRenderer implements TreeCellRenderer
+ private class DbTreeRenderer extends DefaultTreeCellRenderer
+ implements TreeCellRenderer
{
JDatabaseTree us;
public DbTreeRenderer(JDatabaseTree me)
{
us = me;
+ ToolTipManager.sharedInstance().registerComponent(dbviews);
}
private Component returnLabel(String txt)
@@ -128,33 +274,40 @@ public class JDatabaseTree extends JalviewDialog
boolean selected, boolean expanded, boolean leaf, int row,
boolean hasFocus)
{
- String val="";
+ String val = "";
if (value != null && value instanceof DefaultMutableTreeNode)
{
DefaultMutableTreeNode vl = (DefaultMutableTreeNode) value;
value = vl.getUserObject();
if (value instanceof DbSourceProxy)
{
- val=(((DbSourceProxy) value).getDbName());
+ val = ((DbSourceProxy) value).getDbName();
+ if (((DbSourceProxy) value).getDescription() != null)
+ { // getName()
+ this.setToolTipText(((DbSourceProxy) value).getDescription());
+ }
}
else
{
if (value instanceof String)
{
- val=((String) value);
+ val = (String) value;
}
}
}
if (value == null)
{
- val = ("");
+ val = "";
}
- return super.getTreeCellRendererComponent(tree, val, selected, expanded, leaf, row, hasFocus);
-
+ return super.getTreeCellRendererComponent(tree, val, selected,
+ expanded, leaf, row, hasFocus);
+
}
}
- List selection = null;
+ List oldselection, selection = null;
+
+ TreePath[] tsel = null, oldtsel = null;
@Override
protected void raiseClosed()
@@ -169,17 +322,22 @@ public class JDatabaseTree extends JalviewDialog
protected void okPressed()
{
_setSelectionState();
- closeDialog();
}
@Override
protected void cancelPressed()
{
+ selection = oldselection;
+ tsel = oldtsel;
+ _revertSelectionState();
closeDialog();
}
- private void showDialog(Container parent)
+ void showDialog()
{
+ oldselection = selection;
+ oldtsel = tsel;
+ validate();
waitForInput();
}
@@ -193,28 +351,126 @@ public class JDatabaseTree extends JalviewDialog
return selection;
}
+ /**
+ * disable or enable selection handler
+ */
+ boolean handleSelections = true;
+
private void _setSelectionState()
{
+ if (!handleSelections)
+ {
+ return;
+ }
+ ok.setEnabled(false);
if (dbviews.getSelectionCount() == 0)
{
selection = null;
}
+
+ tsel = dbviews.getSelectionPaths();
+ boolean forcedFirstChild = false;
List srcs = new ArrayList();
- for (TreePath tp : dbviews.getSelectionPaths())
+ if (tsel != null)
{
- DefaultMutableTreeNode dmt = (DefaultMutableTreeNode) tp
- .getLastPathComponent();
- if (dmt.getUserObject()!=null) {
- if (dmt.getUserObject() instanceof DbSourceProxy) {
- srcs.add((DbSourceProxy) dmt.getUserObject());
+ for (TreePath tp : tsel)
+ {
+ DefaultMutableTreeNode admt,
+ dmt = (DefaultMutableTreeNode) tp.getLastPathComponent();
+ if (dmt.getUserObject() != null)
+ {
+ /*
+ * enable OK button once a selection has been made
+ */
+ ok.setEnabled(true);
+ if (dmt.getUserObject() instanceof DbSourceProxy)
+ {
+ srcs.add((DbSourceProxy) dmt.getUserObject());
+ }
+ else
+ {
+ if (allowMultiSelections)
+ {
+ srcs.addAll(sfetcher
+ .getSourceProxy((String) dmt.getUserObject()));
+ }
+ else
+ {
+ srcs.add(sfetcher.getSourceProxy((String) dmt.getUserObject())
+ .get(0));
+ forcedFirstChild = true;
+ }
+ }
+ }
}
- else
+ }
+ updateDbStatus(srcs, forcedFirstChild);
+ selection = srcs;
+ }
+
+ private void _revertSelectionState()
+ {
+ handleSelections = false;
+ if (selection == null || selection.size() == 0)
+ {
+ dbviews.clearSelection();
+ }
+ else
+ {
+ dbviews.setSelectionPaths(tsel);
+ }
+ handleSelections = true;
+ }
+
+ private void updateDbStatus(List srcs,
+ boolean forcedFirstChild)
+ {
+ int x = 0;
+ String nm = "", qr = "";
+ for (DbSourceProxy dbs : srcs)
+ {
+ String tq = dbs.getTestQuery();
+ nm = dbs.getDbName();
+ if (tq != null && tq.trim().length() > 0 && dbs.isValidReference(tq))
{
- srcs.addAll(sfetcher.getSourceProxy((String) dmt.getUserObject()));
+ qr = tq;
+ x++;
}
+ }
+
+ dbstatex.setText(" ");
+ if (allowMultiSelections)
+ {
+ dbstatus.setText(MessageManager.formatMessage(
+ "label.selected_database_to_fetch_from", new String[]
+ { Integer.valueOf(srcs.size()).toString(),
+ (srcs.size() == 1 ? "" : "s"),
+ (srcs.size() > 0
+ ? " with " + x + " test quer"
+ + (x == 1 ? "y" : "ies")
+ : ".") }));
+ }
+ else
+ {
+ if (nm.length() > 0)
+ {
+ dbstatus.setText(MessageManager
+ .formatMessage("label.database_param", new String[]
+ { nm }));
+ if (qr.length() > 0)
+ {
+ dbstatex.setText(MessageManager
+ .formatMessage("label.example_param", new String[]
+ { qr }));
+ }
+ }
+ else
+ {
+ dbstatus.setText(" ");
}
}
- selection = srcs;
+ dbstatus.invalidate();
+ dbstatex.invalidate();
}
public String getSelectedItem()
@@ -233,13 +489,19 @@ public class JDatabaseTree extends JalviewDialog
return null;
}
StringBuffer sb = new StringBuffer();
+ HashSet hs = new HashSet();
for (DbSourceProxy dbs : getSelectedSources())
{
- if (sb.length() > 0)
+ String tq = dbs.getTestQuery();
+ ;
+ if (hs.add(tq))
{
- sb.append(";");
+ if (sb.length() > 0)
+ {
+ sb.append(";");
+ }
+ sb.append(tq);
}
- sb.append(dbs.getTestQuery());
}
return sb.toString();
}
@@ -258,6 +520,7 @@ public class JDatabaseTree extends JalviewDialog
public static void main(String args[])
{
+ Cache.getDasSourceRegistry();
JDatabaseTree jdt = new JDatabaseTree(new jalview.ws.SequenceFetcher());
JFrame foo = new JFrame();
foo.setLayout(new BorderLayout());
@@ -303,4 +566,34 @@ public class JDatabaseTree extends JalviewDialog
} while (nultimes > 0 && foo.isVisible());
foo.setVisible(false);
}
+
+ @Override
+ public void keyPressed(KeyEvent arg0)
+ {
+ if (!arg0.isConsumed() && arg0.getKeyCode() == KeyEvent.VK_ENTER)
+ {
+ action = arg0.getKeyCode();
+ okPressed();
+ closeDialog();
+ }
+ if (!arg0.isConsumed() && arg0.getKeyChar() == KeyEvent.VK_ESCAPE)
+ {
+ action = arg0.getKeyCode();
+ cancelPressed();
+ }
+ }
+
+ @Override
+ public void keyReleased(KeyEvent arg0)
+ {
+ // TODO Auto-generated method stub
+
+ }
+
+ @Override
+ public void keyTyped(KeyEvent arg0)
+ {
+ // TODO Auto-generated method stub
+
+ }
}