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,
98 .getString("label.select_database_retrieval_source"),
101 * Dynamically generated database list will need a translation function from
102 * internal source to externally distinct names. UNIPROT and UP_NAME are
103 * identical DB sources, and should be collapsed.
105 DefaultMutableTreeNode tn = null, root = new DefaultMutableTreeNode();
106 Hashtable<String, DefaultMutableTreeNode> source = new Hashtable<String, DefaultMutableTreeNode>();
108 String dbs[] = sfetch.getSupportedDb();
109 Hashtable<String, String> ht = new Hashtable<String, String>();
110 for (int i = 0; i < dbs.length; i++)
112 tn = source.get(dbs[i]);
113 List<DbSourceProxy> srcs = sfetch.getSourceProxy(dbs[i]);
116 source.put(dbs[i], tn = new DefaultMutableTreeNode(dbs[i], true));
118 for (DbSourceProxy dbp : srcs)
120 if (ht.get(dbp.getDbName()) == null)
122 tn.add(new DefaultMutableTreeNode(dbp, false));
123 ht.put(dbp.getDbName(), dbp.getDbName());
127 System.err.println("dupe ig for : " + dbs[i] + " \t"
128 + dbp.getDbName() + " (" + dbp.getDbSource() + ")");
133 for (int i = 0; i < dbs.length; i++)
135 tn = source.get(dbs[i]);
140 if (tn.getChildCount() == 1)
142 DefaultMutableTreeNode ttn = (DefaultMutableTreeNode) tn
144 // remove nodes with only one child
145 tn.setUserObject(ttn.getUserObject());
146 tn.removeAllChildren();
147 source.put(dbs[i], tn);
148 tn.setAllowsChildren(false);
154 dbviews = new JTree(new DefaultTreeModel(root, false));
155 dbviews.setCellRenderer(new DbTreeRenderer(this));
157 dbviews.getSelectionModel().setSelectionMode(
158 TreeSelectionModel.SINGLE_TREE_SELECTION);
159 svp = new JScrollPane(dbviews);
160 svp.setMinimumSize(new Dimension(100, 200));
161 svp.setPreferredSize(new Dimension(200, 400));
162 svp.setMaximumSize(new Dimension(300, 600));
164 JPanel panel = new JPanel(new BorderLayout());
165 panel.setSize(new Dimension(350, 220));
167 dbviews.addTreeSelectionListener(new TreeSelectionListener()
171 public void valueChanged(TreeSelectionEvent arg0)
173 _setSelectionState();
176 dbviews.addMouseListener(new MouseAdapter()
180 public void mousePressed(MouseEvent e)
182 if (e.getClickCount() == 2)
189 JPanel jc = new JPanel(new BorderLayout()), j = new JPanel(
191 jc.add(svp, BorderLayout.CENTER);
194 // TODO: make the panel stay a fixed size for longest dbname+example set.
195 JPanel dbstat = new JPanel(new GridLayout(2, 1));
196 dbstatus = new JLabel(" "); // set the height correctly for layout
197 dbstatus.setFont(f = JvSwingUtils.getLabelFont(false, true));
198 dbstatus.setSize(new Dimension(290, 50));
199 dbstatex = new JLabel(" ");
201 dbstatex.setSize(new Dimension(290, 50));
202 dbstat.add(dbstatus);
203 dbstat.add(dbstatex);
204 jc.add(dbstat, BorderLayout.SOUTH);
206 add(jc, BorderLayout.CENTER);
207 ok.setEnabled(false);
210 add(j, BorderLayout.SOUTH);
211 dbviews.addKeyListener(this);
215 private void sortTreeNodes(DefaultMutableTreeNode root)
217 if (root.getChildCount() == 0)
221 int count = root.getChildCount();
222 String[] names = new String[count];
223 DefaultMutableTreeNode[] nodes = new DefaultMutableTreeNode[count];
224 for (int i = 0; i < count; i++)
226 TreeNode node = root.getChildAt(i);
227 if (node instanceof DefaultMutableTreeNode)
229 DefaultMutableTreeNode child = (DefaultMutableTreeNode) node;
231 if (child.getUserObject() instanceof DbSourceProxy)
233 names[i] = ((DbSourceProxy) child.getUserObject()).getDbName()
238 names[i] = ((String) child.getUserObject()).toLowerCase();
239 sortTreeNodes(child);
246 .getString("error.implementation_error_cant_reorder_tree"));
249 jalview.util.QuickSort.sort(names, nodes);
250 root.removeAllChildren();
251 for (int i = count - 1; i >= 0; i--)
257 private class DbTreeRenderer extends DefaultTreeCellRenderer implements
262 public DbTreeRenderer(JDatabaseTree me)
265 ToolTipManager.sharedInstance().registerComponent(dbviews);
268 private Component returnLabel(String txt)
270 JLabel jl = new JLabel(txt);
271 jl.setFont(JvSwingUtils.getLabelFont());
276 public Component getTreeCellRendererComponent(JTree tree, Object value,
277 boolean selected, boolean expanded, boolean leaf, int row,
281 if (value != null && value instanceof DefaultMutableTreeNode)
283 DefaultMutableTreeNode vl = (DefaultMutableTreeNode) value;
284 value = vl.getUserObject();
285 if (value instanceof DbSourceProxy)
287 val = ((DbSourceProxy) value).getDbName();
288 if (((DbSourceProxy) value).getDescription() != null)
290 this.setToolTipText(((DbSourceProxy) value).getDescription());
295 if (value instanceof String)
297 val = (String) value;
305 return super.getTreeCellRendererComponent(tree, val, selected,
306 expanded, leaf, row, hasFocus);
311 List<DbSourceProxy> oldselection, selection = null;
313 TreePath[] tsel = null, oldtsel = null;
316 protected void raiseClosed()
318 for (ActionListener al : lstners)
320 al.actionPerformed(null);
325 protected void okPressed()
327 _setSelectionState();
331 protected void cancelPressed()
333 selection = oldselection;
335 _revertSelectionState();
341 oldselection = selection;
347 public boolean hasSelection()
349 return selection == null ? false : selection.size() == 0 ? false : true;
352 public List<DbSourceProxy> getSelectedSources()
358 * disable or enable selection handler
360 boolean handleSelections = true;
362 private void _setSelectionState()
364 if (!handleSelections)
368 ok.setEnabled(false);
369 if (dbviews.getSelectionCount() == 0)
374 tsel = dbviews.getSelectionPaths();
375 boolean forcedFirstChild = false;
376 List<DbSourceProxy> srcs = new ArrayList<DbSourceProxy>();
379 for (TreePath tp : tsel)
381 DefaultMutableTreeNode admt, dmt = (DefaultMutableTreeNode) tp
382 .getLastPathComponent();
383 if (dmt.getUserObject() != null)
386 * enable OK button once a selection has been made
389 if (dmt.getUserObject() instanceof DbSourceProxy)
391 srcs.add((DbSourceProxy) dmt.getUserObject());
395 if (allowMultiSelections)
397 srcs.addAll(sfetcher.getSourceProxy((String) dmt
403 .getSourceProxy((String) dmt.getUserObject()).get(0));
404 forcedFirstChild = true;
410 updateDbStatus(srcs, forcedFirstChild);
414 private void _revertSelectionState()
416 handleSelections = false;
417 if (selection == null || selection.size() == 0)
419 dbviews.clearSelection();
423 dbviews.setSelectionPaths(tsel);
425 handleSelections = true;
428 private void updateDbStatus(List<DbSourceProxy> srcs,
429 boolean forcedFirstChild)
432 String nm = "", qr = "";
433 for (DbSourceProxy dbs : srcs)
435 String tq = dbs.getTestQuery();
436 nm = dbs.getDbName();
437 if (tq != null && tq.trim().length() > 0 && dbs.isValidReference(tq))
444 dbstatex.setText(" ");
445 if (allowMultiSelections)
447 dbstatus.setText(MessageManager.formatMessage(
448 "label.selected_database_to_fetch_from", new String[] {
449 Integer.valueOf(srcs.size()).toString(),
450 (srcs.size() == 1 ? "" : "s"),
451 (srcs.size() > 0 ? " with " + x + " test quer"
452 + (x == 1 ? "y" : "ies") : ".") }));
458 dbstatus.setText(MessageManager.formatMessage(
459 "label.database_param", new String[] { nm }));
462 dbstatex.setText(MessageManager.formatMessage(
463 "label.example_param", new String[] { qr }));
468 dbstatus.setText(" ");
471 dbstatus.invalidate();
472 dbstatex.invalidate();
475 public String getSelectedItem()
479 return getSelectedSources().get(0).getDbName();
484 public String getExampleQueries()
490 StringBuffer sb = new StringBuffer();
491 HashSet<String> hs = new HashSet<String>();
492 for (DbSourceProxy dbs : getSelectedSources())
494 String tq = dbs.getTestQuery();
505 return sb.toString();
508 List<ActionListener> lstners = new Vector<ActionListener>();
510 public void addActionListener(ActionListener actionListener)
512 lstners.add(actionListener);
515 public void removeActionListener(ActionListener actionListener)
517 lstners.remove(actionListener);
520 public static void main(String args[])
522 Cache.getDasSourceRegistry();
523 JDatabaseTree jdt = new JDatabaseTree(new jalview.ws.SequenceFetcher());
524 JFrame foo = new JFrame();
525 foo.setLayout(new BorderLayout());
526 foo.add(jdt.getDatabaseSelectorButton(), BorderLayout.CENTER);
528 foo.setVisible(true);
530 final Thread us = Thread.currentThread();
531 jdt.addActionListener(new ActionListener()
535 public void actionPerformed(ActionEvent e)
545 } catch (InterruptedException x)
548 if (!jdt.hasSelection())
550 System.out.println("No Selection");
554 System.out.println("Selection: " + jdt.getSelectedItem());
556 for (DbSourceProxy pr : jdt.getSelectedSources())
558 System.out.println("Source " + s++ + ": " + pr.getDbName()
559 + " (" + pr.getDbSource() + ") Version "
560 + pr.getDbVersion() + ". Test:\t" + pr.getTestQuery());
562 System.out.println("Test queries: " + jdt.getExampleQueries());
565 } while (nultimes > 0 && foo.isVisible());
566 foo.setVisible(false);
570 public void keyPressed(KeyEvent arg0)
572 if (!arg0.isConsumed() && arg0.getKeyCode() == KeyEvent.VK_ENTER)
574 action = arg0.getKeyCode();
578 if (!arg0.isConsumed() && arg0.getKeyChar() == KeyEvent.VK_ESCAPE)
580 action = arg0.getKeyCode();
586 public void keyReleased(KeyEvent arg0)
588 // TODO Auto-generated method stub
593 public void keyTyped(KeyEvent arg0)
595 // TODO Auto-generated method stub