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 private JPanel mainPanel = new JPanel(new BorderLayout());
93 public JDatabaseTree(jalview.ws.SequenceFetcher sfetch)
96 initDialogFrame(mainPanel, true, false, MessageManager
97 .getString("label.select_database_retrieval_source"), 650, 490);
99 * Dynamically generated database list will need a translation function from
100 * internal source to externally distinct names. UNIPROT and UP_NAME are
101 * identical DB sources, and should be collapsed.
103 DefaultMutableTreeNode tn = null, root = new DefaultMutableTreeNode();
104 Hashtable<String, DefaultMutableTreeNode> source = new Hashtable<String, DefaultMutableTreeNode>();
106 String dbs[] = sfetch.getSupportedDb();
107 Hashtable<String, String> ht = new Hashtable<String, String>();
108 for (int i = 0; i < dbs.length; i++)
110 tn = source.get(dbs[i]);
111 List<DbSourceProxy> srcs = sfetch.getSourceProxy(dbs[i]);
114 source.put(dbs[i], tn = new DefaultMutableTreeNode(dbs[i], true));
116 for (DbSourceProxy dbp : srcs)
118 if (ht.get(dbp.getDbName()) == null)
120 tn.add(new DefaultMutableTreeNode(dbp, false));
121 ht.put(dbp.getDbName(), dbp.getDbName());
125 System.err.println("dupe ig for : " + dbs[i] + " \t"
126 + dbp.getDbName() + " (" + dbp.getDbSource() + ")");
131 for (int i = 0; i < dbs.length; i++)
133 tn = source.get(dbs[i]);
138 if (tn.getChildCount() == 1)
140 DefaultMutableTreeNode ttn = (DefaultMutableTreeNode) tn
142 // remove nodes with only one child
143 tn.setUserObject(ttn.getUserObject());
144 tn.removeAllChildren();
145 source.put(dbs[i], tn);
146 tn.setAllowsChildren(false);
152 dbviews = new JTree(new DefaultTreeModel(root, false));
153 dbviews.setCellRenderer(new DbTreeRenderer(this));
155 dbviews.getSelectionModel()
156 .setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
157 svp = new JScrollPane(dbviews);
158 svp.setMinimumSize(new Dimension(100, 200));
159 svp.setPreferredSize(new Dimension(200, 400));
160 svp.setMaximumSize(new Dimension(300, 600));
162 JPanel panel = new JPanel(new BorderLayout());
163 panel.setSize(new Dimension(350, 220));
165 dbviews.addTreeSelectionListener(new TreeSelectionListener()
169 public void valueChanged(TreeSelectionEvent arg0)
171 _setSelectionState();
174 dbviews.addMouseListener(new MouseAdapter()
178 public void mousePressed(MouseEvent e)
180 if (e.getClickCount() == 2)
187 JPanel jc = new JPanel(new BorderLayout()),
188 j = new JPanel(new FlowLayout());
189 jc.add(svp, BorderLayout.CENTER);
192 // TODO: make the panel stay a fixed size for longest dbname+example set.
193 JPanel dbstat = new JPanel(new GridLayout(2, 1));
194 dbstatus = new JLabel(" "); // set the height correctly for layout
195 dbstatus.setFont(f = JvSwingUtils.getLabelFont(false, true));
196 dbstatus.setSize(new Dimension(290, 50));
197 dbstatex = new JLabel(" ");
199 dbstatex.setSize(new Dimension(290, 50));
200 dbstat.add(dbstatus);
201 dbstat.add(dbstatex);
202 jc.add(dbstat, BorderLayout.SOUTH);
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);
242 throw new Error(MessageManager
243 .getString("error.implementation_error_cant_reorder_tree"));
246 jalview.util.QuickSort.sort(names, nodes);
247 root.removeAllChildren();
248 for (int i = count - 1; i >= 0; i--)
254 private class DbTreeRenderer extends DefaultTreeCellRenderer
255 implements TreeCellRenderer
259 public DbTreeRenderer(JDatabaseTree me)
262 ToolTipManager.sharedInstance().registerComponent(dbviews);
265 private Component returnLabel(String txt)
267 JLabel jl = new JLabel(txt);
268 jl.setFont(JvSwingUtils.getLabelFont());
273 public Component getTreeCellRendererComponent(JTree tree, Object value,
274 boolean selected, boolean expanded, boolean leaf, int row,
278 if (value != null && value instanceof DefaultMutableTreeNode)
280 DefaultMutableTreeNode vl = (DefaultMutableTreeNode) value;
281 value = vl.getUserObject();
282 if (value instanceof DbSourceProxy)
284 val = ((DbSourceProxy) value).getDbName();
285 if (((DbSourceProxy) value).getDescription() != null)
287 this.setToolTipText(((DbSourceProxy) value).getDescription());
292 if (value instanceof String)
294 val = (String) value;
302 return super.getTreeCellRendererComponent(tree, val, selected,
303 expanded, leaf, row, hasFocus);
308 List<DbSourceProxy> oldselection, selection = null;
310 TreePath[] tsel = null, oldtsel = null;
313 protected void raiseClosed()
315 for (ActionListener al : lstners)
317 al.actionPerformed(null);
322 protected void okPressed()
324 _setSelectionState();
328 protected void cancelPressed()
330 selection = oldselection;
332 _revertSelectionState();
338 oldselection = selection;
344 public boolean hasSelection()
346 return selection == null ? false : selection.size() == 0 ? false : true;
349 public List<DbSourceProxy> getSelectedSources()
355 * disable or enable selection handler
357 boolean handleSelections = true;
359 private void _setSelectionState()
361 if (!handleSelections)
365 ok.setEnabled(false);
366 if (dbviews.getSelectionCount() == 0)
371 tsel = dbviews.getSelectionPaths();
372 boolean forcedFirstChild = false;
373 List<DbSourceProxy> srcs = new ArrayList<DbSourceProxy>();
376 for (TreePath tp : tsel)
378 DefaultMutableTreeNode admt,
379 dmt = (DefaultMutableTreeNode) tp.getLastPathComponent();
380 if (dmt.getUserObject() != null)
383 * enable OK button once a selection has been made
386 if (dmt.getUserObject() instanceof DbSourceProxy)
388 srcs.add((DbSourceProxy) dmt.getUserObject());
392 if (allowMultiSelections)
395 .getSourceProxy((String) dmt.getUserObject()));
399 srcs.add(sfetcher.getSourceProxy((String) dmt.getUserObject())
401 forcedFirstChild = true;
407 updateDbStatus(srcs, forcedFirstChild);
411 private void _revertSelectionState()
413 handleSelections = false;
414 if (selection == null || selection.size() == 0)
416 dbviews.clearSelection();
420 dbviews.setSelectionPaths(tsel);
422 handleSelections = true;
425 private void updateDbStatus(List<DbSourceProxy> srcs,
426 boolean forcedFirstChild)
429 String nm = "", qr = "";
430 for (DbSourceProxy dbs : srcs)
432 String tq = dbs.getTestQuery();
433 nm = dbs.getDbName();
434 if (tq != null && tq.trim().length() > 0 && dbs.isValidReference(tq))
441 dbstatex.setText(" ");
442 if (allowMultiSelections)
444 dbstatus.setText(MessageManager.formatMessage(
445 "label.selected_database_to_fetch_from", new String[]
446 { Integer.valueOf(srcs.size()).toString(),
447 (srcs.size() == 1 ? "" : "s"),
449 ? " with " + x + " test quer"
450 + (x == 1 ? "y" : "ies")
457 dbstatus.setText(MessageManager
458 .formatMessage("label.database_param", new String[]
462 dbstatex.setText(MessageManager
463 .formatMessage("label.example_param", new String[]
469 dbstatus.setText(" ");
472 dbstatus.invalidate();
473 dbstatex.invalidate();
476 public String getSelectedItem()
480 return getSelectedSources().get(0).getDbName();
485 public String getExampleQueries()
491 StringBuffer sb = new StringBuffer();
492 HashSet<String> hs = new HashSet<String>();
493 for (DbSourceProxy dbs : getSelectedSources())
495 String tq = dbs.getTestQuery();
506 return sb.toString();
509 List<ActionListener> lstners = new Vector<ActionListener>();
511 public void addActionListener(ActionListener actionListener)
513 lstners.add(actionListener);
516 public void removeActionListener(ActionListener actionListener)
518 lstners.remove(actionListener);
521 public static void main(String args[])
523 Cache.getDasSourceRegistry();
524 JDatabaseTree jdt = new JDatabaseTree(new jalview.ws.SequenceFetcher());
525 JFrame foo = new JFrame();
526 foo.setLayout(new BorderLayout());
527 foo.add(jdt.getDatabaseSelectorButton(), BorderLayout.CENTER);
529 foo.setVisible(true);
531 final Thread us = Thread.currentThread();
532 jdt.addActionListener(new ActionListener()
536 public void actionPerformed(ActionEvent e)
546 } catch (InterruptedException x)
549 if (!jdt.hasSelection())
551 System.out.println("No Selection");
555 System.out.println("Selection: " + jdt.getSelectedItem());
557 for (DbSourceProxy pr : jdt.getSelectedSources())
559 System.out.println("Source " + s++ + ": " + pr.getDbName()
560 + " (" + pr.getDbSource() + ") Version "
561 + pr.getDbVersion() + ". Test:\t" + pr.getTestQuery());
563 System.out.println("Test queries: " + jdt.getExampleQueries());
566 } while (nultimes > 0 && foo.isVisible());
567 foo.setVisible(false);
571 public void keyPressed(KeyEvent arg0)
573 if (!arg0.isConsumed() && arg0.getKeyCode() == KeyEvent.VK_ENTER)
575 action = arg0.getKeyCode();
579 if (!arg0.isConsumed() && arg0.getKeyChar() == KeyEvent.VK_ESCAPE)
581 action = arg0.getKeyCode();
587 public void keyReleased(KeyEvent arg0)
589 // TODO Auto-generated method stub
594 public void keyTyped(KeyEvent arg0)
596 // TODO Auto-generated method stub