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.bin.Cache;
24 import jalview.util.MessageManager;
25 import jalview.ws.seqfetcher.DbSourceProxy;
27 import java.awt.BorderLayout;
28 import java.awt.Component;
29 import java.awt.Dimension;
30 import java.awt.FlowLayout;
31 import java.awt.GridLayout;
32 import java.awt.event.ActionEvent;
33 import java.awt.event.ActionListener;
34 import java.awt.event.KeyEvent;
35 import java.awt.event.KeyListener;
36 import java.awt.event.MouseAdapter;
37 import java.awt.event.MouseEvent;
38 import java.util.ArrayList;
39 import java.util.HashSet;
40 import java.util.Hashtable;
41 import java.util.List;
42 import java.util.Vector;
44 import javax.swing.JButton;
45 import javax.swing.JFrame;
46 import javax.swing.JLabel;
47 import javax.swing.JPanel;
48 import javax.swing.JScrollPane;
49 import javax.swing.JTree;
50 import javax.swing.ToolTipManager;
51 import javax.swing.event.TreeSelectionEvent;
52 import javax.swing.event.TreeSelectionListener;
53 import javax.swing.tree.DefaultMutableTreeNode;
54 import javax.swing.tree.DefaultTreeCellRenderer;
55 import javax.swing.tree.DefaultTreeModel;
56 import javax.swing.tree.TreeCellRenderer;
57 import javax.swing.tree.TreeNode;
58 import javax.swing.tree.TreePath;
59 import javax.swing.tree.TreeSelectionModel;
61 public class JDatabaseTree extends JalviewDialog implements KeyListener
63 boolean allowMultiSelections = false;
67 JButton getDatabaseSelectorButton()
69 final JButton viewdbs = new JButton(
70 MessageManager.getString("action.select_ddbb"));
71 viewdbs.addActionListener(new ActionListener()
75 public void actionPerformed(ActionEvent arg0)
87 private jalview.ws.SequenceFetcher sfetcher;
89 private JLabel dbstatus, dbstatex;
91 public JDatabaseTree(jalview.ws.SequenceFetcher sfetch)
93 initDialogFrame(this, true, false,
95 .getString("label.select_database_retrieval_source"),
98 * Dynamically generated database list will need a translation function from
99 * internal source to externally distinct names. UNIPROT and UP_NAME are
100 * identical DB sources, and should be collapsed.
102 DefaultMutableTreeNode tn = null, root = new DefaultMutableTreeNode();
103 Hashtable<String, DefaultMutableTreeNode> source = new Hashtable<String, DefaultMutableTreeNode>();
105 String dbs[] = sfetch.getSupportedDb();
106 Hashtable<String, String> ht = new Hashtable<String, String>();
107 for (int i = 0; i < dbs.length; i++)
109 tn = source.get(dbs[i]);
110 List<DbSourceProxy> srcs = sfetch.getSourceProxy(dbs[i]);
113 source.put(dbs[i], tn = new DefaultMutableTreeNode(dbs[i], true));
115 for (DbSourceProxy dbp : srcs)
117 if (ht.get(dbp.getDbName()) == null)
119 tn.add(new DefaultMutableTreeNode(dbp, false));
120 ht.put(dbp.getDbName(), dbp.getDbName());
124 System.err.println("dupe ig for : " + dbs[i] + " \t"
125 + dbp.getDbName() + " (" + dbp.getDbSource() + ")");
130 for (int i = 0; i < dbs.length; i++)
132 tn = source.get(dbs[i]);
137 if (tn.getChildCount() == 1)
139 DefaultMutableTreeNode ttn = (DefaultMutableTreeNode) tn
141 // remove nodes with only one child
142 tn.setUserObject(ttn.getUserObject());
143 tn.removeAllChildren();
144 source.put(dbs[i], tn);
145 tn.setAllowsChildren(false);
151 svp = new JScrollPane();
152 // svp.setAutoscrolls(true);
153 dbviews = new JTree(new DefaultTreeModel(root, false));
154 dbviews.setCellRenderer(new DbTreeRenderer(this));
156 dbviews.getSelectionModel().setSelectionMode(
157 TreeSelectionModel.SINGLE_TREE_SELECTION);
158 svp.getViewport().setView(dbviews);
159 // svp.getViewport().setMinimumSize(new Dimension(300,200));
160 // svp.setSize(300,250);
161 // JPanel panel=new JPanel();
162 // panel.setSize(new Dimension(350,220));
164 dbviews.addTreeSelectionListener(new TreeSelectionListener()
168 public void valueChanged(TreeSelectionEvent arg0)
170 _setSelectionState();
173 dbviews.addMouseListener(new MouseAdapter()
177 public void mousePressed(MouseEvent e)
179 if (e.getClickCount() == 2)
186 JPanel jc = new JPanel(new BorderLayout()), j = new JPanel(
188 jc.add(svp, BorderLayout.CENTER);
191 // TODO: make the panel stay a fixed size for longest dbname+example set.
192 JPanel dbstat = new JPanel(new GridLayout(2, 1));
193 dbstatus = new JLabel(" "); // set the height correctly for layout
194 dbstatus.setFont(f = JvSwingUtils.getLabelFont(false, true));
195 dbstatus.setSize(new Dimension(290, 50));
196 dbstatex = new JLabel(" ");
198 dbstatex.setSize(new Dimension(290, 50));
199 dbstat.add(dbstatus);
200 dbstat.add(dbstatex);
201 jc.add(dbstat, BorderLayout.SOUTH);
203 // j.setPreferredSize(new Dimension(300,50));
204 add(jc, BorderLayout.CENTER);
205 ok.setEnabled(false);
208 add(j, BorderLayout.SOUTH);
209 dbviews.addKeyListener(this);
213 private void sortTreeNodes(DefaultMutableTreeNode root)
215 if (root.getChildCount() == 0)
219 int count = root.getChildCount();
220 String[] names = new String[count];
221 DefaultMutableTreeNode[] nodes = new DefaultMutableTreeNode[count];
222 for (int i = 0; i < count; i++)
224 TreeNode node = root.getChildAt(i);
225 if (node instanceof DefaultMutableTreeNode)
227 DefaultMutableTreeNode child = (DefaultMutableTreeNode) node;
229 if (child.getUserObject() instanceof DbSourceProxy)
231 names[i] = ((DbSourceProxy) child.getUserObject()).getDbName()
236 names[i] = ((String) child.getUserObject()).toLowerCase();
237 sortTreeNodes(child);
244 .getString("error.implementation_error_cant_reorder_tree"));
247 jalview.util.QuickSort.sort(names, nodes);
248 root.removeAllChildren();
249 for (int i = count - 1; i >= 0; i--)
255 private class DbTreeRenderer extends DefaultTreeCellRenderer implements
260 public DbTreeRenderer(JDatabaseTree me)
263 ToolTipManager.sharedInstance().registerComponent(dbviews);
266 private Component returnLabel(String txt)
268 JLabel jl = new JLabel(txt);
269 jl.setFont(JvSwingUtils.getLabelFont());
274 public Component getTreeCellRendererComponent(JTree tree, Object value,
275 boolean selected, boolean expanded, boolean leaf, int row,
279 if (value != null && value instanceof DefaultMutableTreeNode)
281 DefaultMutableTreeNode vl = (DefaultMutableTreeNode) value;
282 value = vl.getUserObject();
283 if (value instanceof DbSourceProxy)
285 val = ((DbSourceProxy) value).getDbName();
286 if (((DbSourceProxy) value).getDescription() != null)
288 this.setToolTipText(((DbSourceProxy) value).getDescription());
293 if (value instanceof String)
295 val = (String) value;
303 return super.getTreeCellRendererComponent(tree, val, selected,
304 expanded, leaf, row, hasFocus);
309 List<DbSourceProxy> oldselection, selection = null;
311 TreePath[] tsel = null, oldtsel = null;
314 protected void raiseClosed()
316 for (ActionListener al : lstners)
318 al.actionPerformed(null);
323 protected void okPressed()
325 _setSelectionState();
329 protected void cancelPressed()
331 selection = oldselection;
333 _revertSelectionState();
339 oldselection = selection;
345 public boolean hasSelection()
347 return selection == null ? false : selection.size() == 0 ? false : true;
350 public List<DbSourceProxy> getSelectedSources()
356 * disable or enable selection handler
358 boolean handleSelections = true;
360 private void _setSelectionState()
362 if (!handleSelections)
366 ok.setEnabled(false);
367 if (dbviews.getSelectionCount() == 0)
372 tsel = dbviews.getSelectionPaths();
373 boolean forcedFirstChild = false;
374 List<DbSourceProxy> srcs = new ArrayList<DbSourceProxy>();
377 for (TreePath tp : tsel)
379 DefaultMutableTreeNode admt, dmt = (DefaultMutableTreeNode) tp
380 .getLastPathComponent();
381 if (dmt.getUserObject() != null)
384 * enable OK button once a selection has been made
387 if (dmt.getUserObject() instanceof DbSourceProxy)
389 srcs.add((DbSourceProxy) dmt.getUserObject());
393 if (allowMultiSelections)
395 srcs.addAll(sfetcher.getSourceProxy((String) dmt
401 .getSourceProxy((String) dmt.getUserObject()).get(0));
402 forcedFirstChild = true;
408 updateDbStatus(srcs, forcedFirstChild);
412 private void _revertSelectionState()
414 handleSelections = false;
415 if (selection == null || selection.size() == 0)
417 dbviews.clearSelection();
421 dbviews.setSelectionPaths(tsel);
423 handleSelections = true;
426 private void updateDbStatus(List<DbSourceProxy> srcs,
427 boolean forcedFirstChild)
430 String nm = "", qr = "";
431 for (DbSourceProxy dbs : srcs)
433 String tq = dbs.getTestQuery();
434 nm = dbs.getDbName();
435 if (tq != null && tq.trim().length() > 0 && dbs.isValidReference(tq))
442 dbstatex.setText(" ");
443 if (allowMultiSelections)
445 dbstatus.setText(MessageManager.formatMessage(
446 "label.selected_database_to_fetch_from", new String[] {
447 Integer.valueOf(srcs.size()).toString(),
448 (srcs.size() == 1 ? "" : "s"),
449 (srcs.size() > 0 ? " with " + x + " test quer"
450 + (x == 1 ? "y" : "ies") : ".") }));
456 dbstatus.setText(MessageManager.formatMessage(
457 "label.database_param", new String[] { nm }));
460 dbstatex.setText(MessageManager.formatMessage(
461 "label.example_param", new String[] { qr }));
466 dbstatus.setText(" ");
469 dbstatus.invalidate();
470 dbstatex.invalidate();
473 public String getSelectedItem()
477 return getSelectedSources().get(0).getDbName();
482 public String getExampleQueries()
488 StringBuffer sb = new StringBuffer();
489 HashSet<String> hs = new HashSet<String>();
490 for (DbSourceProxy dbs : getSelectedSources())
492 String tq = dbs.getTestQuery();
503 return sb.toString();
506 List<ActionListener> lstners = new Vector<ActionListener>();
508 public void addActionListener(ActionListener actionListener)
510 lstners.add(actionListener);
513 public void removeActionListener(ActionListener actionListener)
515 lstners.remove(actionListener);
518 public static void main(String args[])
520 Cache.getDasSourceRegistry();
521 JDatabaseTree jdt = new JDatabaseTree(new jalview.ws.SequenceFetcher());
522 JFrame foo = new JFrame();
523 foo.setLayout(new BorderLayout());
524 foo.add(jdt.getDatabaseSelectorButton(), BorderLayout.CENTER);
526 foo.setVisible(true);
528 final Thread us = Thread.currentThread();
529 jdt.addActionListener(new ActionListener()
533 public void actionPerformed(ActionEvent e)
543 } catch (InterruptedException x)
546 if (!jdt.hasSelection())
548 System.out.println("No Selection");
552 System.out.println("Selection: " + jdt.getSelectedItem());
554 for (DbSourceProxy pr : jdt.getSelectedSources())
556 System.out.println("Source " + s++ + ": " + pr.getDbName()
557 + " (" + pr.getDbSource() + ") Version "
558 + pr.getDbVersion() + ". Test:\t" + pr.getTestQuery());
560 System.out.println("Test queries: " + jdt.getExampleQueries());
563 } while (nultimes > 0 && foo.isVisible());
564 foo.setVisible(false);
568 public void keyPressed(KeyEvent arg0)
570 if (!arg0.isConsumed() && arg0.getKeyCode() == KeyEvent.VK_ENTER)
572 action = arg0.getKeyCode();
576 if (!arg0.isConsumed() && arg0.getKeyChar() == KeyEvent.VK_ESCAPE)
578 action = arg0.getKeyCode();
584 public void keyReleased(KeyEvent arg0)
586 // TODO Auto-generated method stub
591 public void keyTyped(KeyEvent arg0)
593 // TODO Auto-generated method stub