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.Container;
30 import java.awt.Dimension;
31 import java.awt.FlowLayout;
32 import java.awt.GridLayout;
33 import java.awt.event.ActionEvent;
34 import java.awt.event.ActionListener;
35 import java.awt.event.KeyEvent;
36 import java.awt.event.KeyListener;
37 import java.util.ArrayList;
38 import java.util.HashSet;
39 import java.util.Hashtable;
40 import java.util.List;
41 import java.util.Vector;
43 import javax.swing.JButton;
44 import javax.swing.JFrame;
45 import javax.swing.JLabel;
46 import javax.swing.JPanel;
47 import javax.swing.JScrollPane;
48 import javax.swing.JTree;
49 import javax.swing.event.TreeSelectionEvent;
50 import javax.swing.event.TreeSelectionListener;
51 import javax.swing.tree.DefaultMutableTreeNode;
52 import javax.swing.tree.DefaultTreeCellRenderer;
53 import javax.swing.tree.DefaultTreeModel;
54 import javax.swing.tree.TreeCellRenderer;
55 import javax.swing.tree.TreeNode;
56 import javax.swing.tree.TreePath;
57 import javax.swing.tree.TreeSelectionModel;
59 public class JDatabaseTree extends JalviewDialog implements KeyListener
61 boolean allowMultiSelections = false;
65 JButton getDatabaseSelectorButton()
67 final JButton viewdbs = new JButton(
68 MessageManager.getString("action.select_ddbb"));
69 viewdbs.addActionListener(new ActionListener()
73 public void actionPerformed(ActionEvent arg0)
85 private jalview.ws.SequenceFetcher sfetcher;
87 private JLabel dbstatus, dbstatex;
89 public JDatabaseTree(jalview.ws.SequenceFetcher sfetch)
91 initDialogFrame(this, true, false, MessageManager.getString("label.select_database_retrieval_source"),
94 * Dynamically generated database list will need a translation function from
95 * internal source to externally distinct names. UNIPROT and UP_NAME are
96 * identical DB sources, and should be collapsed.
98 DefaultMutableTreeNode tn = null, root = new DefaultMutableTreeNode();
99 Hashtable<String, DefaultMutableTreeNode> source = new Hashtable<String, DefaultMutableTreeNode>();
101 String dbs[] = sfetch.getSupportedDb();
102 Hashtable<String, String> ht = new Hashtable<String, String>();
103 for (int i = 0; i < dbs.length; i++)
105 tn = source.get(dbs[i]);
106 List<DbSourceProxy> srcs = sfetch.getSourceProxy(dbs[i]);
109 source.put(dbs[i], tn = new DefaultMutableTreeNode(dbs[i], true));
111 for (DbSourceProxy dbp : srcs)
113 if (ht.get(dbp.getDbName()) == null)
115 tn.add(new DefaultMutableTreeNode(dbp, false));
116 ht.put(dbp.getDbName(), dbp.getDbName());
120 System.err.println("dupe ig for : " + dbs[i] + " \t"
121 + dbp.getDbName() + " (" + dbp.getDbSource() + ")");
126 for (int i = 0; i < dbs.length; i++)
128 tn = source.get(dbs[i]);
133 if (tn.getChildCount() == 1)
135 DefaultMutableTreeNode ttn = (DefaultMutableTreeNode) tn
137 // remove nodes with only one child
138 tn.setUserObject(ttn.getUserObject());
139 tn.removeAllChildren();
140 source.put(dbs[i], tn);
141 tn.setAllowsChildren(false);
147 svp = new JScrollPane();
148 // svp.setAutoscrolls(true);
149 dbviews = new JTree(new DefaultTreeModel(root, false));
150 dbviews.setCellRenderer(new DbTreeRenderer(this));
152 dbviews.getSelectionModel().setSelectionMode(
153 TreeSelectionModel.SINGLE_TREE_SELECTION);
154 svp.getViewport().setView(dbviews);
155 // svp.getViewport().setMinimumSize(new Dimension(300,200));
156 // svp.setSize(300,250);
157 // JPanel panel=new JPanel();
158 // panel.setSize(new Dimension(350,220));
160 dbviews.addTreeSelectionListener(new TreeSelectionListener()
164 public void valueChanged(TreeSelectionEvent arg0)
166 _setSelectionState();
169 JPanel jc = new JPanel(new BorderLayout()), j = new JPanel(
171 jc.add(svp, BorderLayout.CENTER);
174 // TODO: make the panel stay a fixed size for longest dbname+example set.
175 JPanel dbstat = new JPanel(new GridLayout(2, 1));
176 dbstatus = new JLabel(" "); // set the height correctly for layout
177 dbstatus.setFont(f = JvSwingUtils.getLabelFont(false, true));
178 dbstatus.setSize(new Dimension(290, 50));
179 dbstatex = new JLabel(" ");
181 dbstatex.setSize(new Dimension(290, 50));
182 dbstat.add(dbstatus);
183 dbstat.add(dbstatex);
184 jc.add(dbstat, BorderLayout.SOUTH);
186 // j.setPreferredSize(new Dimension(300,50));
187 add(jc, BorderLayout.CENTER);
190 add(j, BorderLayout.SOUTH);
191 dbviews.addKeyListener(this);
195 private void sortTreeNodes(DefaultMutableTreeNode root)
197 if (root.getChildCount() == 0)
201 int count = root.getChildCount();
202 String[] names = new String[count];
203 DefaultMutableTreeNode[] nodes = new DefaultMutableTreeNode[count];
204 for (int i = 0; i < count; i++)
206 TreeNode node = root.getChildAt(i);
207 if (node instanceof DefaultMutableTreeNode)
209 DefaultMutableTreeNode child = (DefaultMutableTreeNode) node;
211 if (child.getUserObject() instanceof DbSourceProxy)
213 names[i] = ((DbSourceProxy) child.getUserObject()).getDbName()
218 names[i] = ((String) child.getUserObject()).toLowerCase();
219 sortTreeNodes(child);
224 throw new Error(MessageManager.getString("error.implementation_error_cant_reorder_tree"));
227 jalview.util.QuickSort.sort(names, nodes);
228 root.removeAllChildren();
229 for (int i = count - 1; i >= 0; i--)
235 private class DbTreeRenderer extends DefaultTreeCellRenderer implements
240 public DbTreeRenderer(JDatabaseTree me)
245 private Component returnLabel(String txt)
247 JLabel jl = new JLabel(txt);
248 jl.setFont(JvSwingUtils.getLabelFont());
253 public Component getTreeCellRendererComponent(JTree tree, Object value,
254 boolean selected, boolean expanded, boolean leaf, int row,
258 if (value != null && value instanceof DefaultMutableTreeNode)
260 DefaultMutableTreeNode vl = (DefaultMutableTreeNode) value;
261 value = vl.getUserObject();
262 if (value instanceof DbSourceProxy)
264 val = (((DbSourceProxy) value).getDbName());
268 if (value instanceof String)
270 val = ((String) value);
278 return super.getTreeCellRendererComponent(tree, val, selected,
279 expanded, leaf, row, hasFocus);
284 List<DbSourceProxy> oldselection, selection = null;
286 TreePath[] tsel = null, oldtsel = null;
289 protected void raiseClosed()
291 for (ActionListener al : lstners)
293 al.actionPerformed(null);
298 protected void okPressed()
300 _setSelectionState();
305 protected void cancelPressed()
307 selection = oldselection;
309 _revertSelectionState();
313 private void showDialog(Container parent)
315 oldselection = selection;
321 public boolean hasSelection()
323 return selection == null ? false : selection.size() == 0 ? false : true;
326 public List<DbSourceProxy> getSelectedSources()
332 * disable or enable selection handler
334 boolean handleSelections = true;
336 private void _setSelectionState()
338 if (!handleSelections)
342 if (dbviews.getSelectionCount() == 0)
346 tsel = dbviews.getSelectionPaths();
347 boolean forcedFirstChild = false;
348 List<DbSourceProxy> srcs = new ArrayList<DbSourceProxy>();
351 for (TreePath tp : tsel)
353 DefaultMutableTreeNode admt, dmt = (DefaultMutableTreeNode) tp
354 .getLastPathComponent();
355 if (dmt.getUserObject() != null)
357 if (dmt.getUserObject() instanceof DbSourceProxy)
359 srcs.add((DbSourceProxy) dmt.getUserObject());
363 if (allowMultiSelections)
365 srcs.addAll(sfetcher.getSourceProxy((String) dmt
371 .getSourceProxy((String) dmt.getUserObject()).get(0));
372 forcedFirstChild = true;
378 updateDbStatus(srcs, forcedFirstChild);
382 private void _revertSelectionState()
384 handleSelections = false;
385 if (selection == null || selection.size() == 0)
387 dbviews.clearSelection();
391 dbviews.setSelectionPaths(tsel);
393 handleSelections = true;
396 private void updateDbStatus(List<DbSourceProxy> srcs,
397 boolean forcedFirstChild)
400 String nm = "", qr = "";
401 for (DbSourceProxy dbs : srcs)
403 String tq = dbs.getTestQuery();
404 nm = dbs.getDbName();
405 if (tq != null && tq.trim().length() > 0 && dbs.isValidReference(tq))
412 if (allowMultiSelections)
414 dbstatus.setText(MessageManager.formatMessage(
415 "label.selected_database_to_fetch_from", new String[]
417 Integer.valueOf(srcs.size()).toString(),
418 (srcs.size() == 1 ? "" : "s"),
419 (srcs.size() > 0 ? " with " + x + " test quer"
420 + (x == 1 ? "y" : "ies") : ".") }));
421 dbstatex.setText(" ");
427 dbstatus.setText(MessageManager.formatMessage(
428 "label.database_param", new String[]
432 dbstatex.setText(MessageManager.formatMessage(
433 "label.example_param", new String[]
438 dbstatex.setText(" ");
443 dbstatus.setText(" ");
446 dbstatus.invalidate();
447 dbstatex.invalidate();
450 public String getSelectedItem()
454 return getSelectedSources().get(0).getDbName();
459 public String getExampleQueries()
465 StringBuffer sb = new StringBuffer();
466 HashSet<String> hs = new HashSet<String>();
467 for (DbSourceProxy dbs : getSelectedSources())
469 String tq = dbs.getTestQuery();
480 return sb.toString();
483 List<ActionListener> lstners = new Vector<ActionListener>();
485 public void addActionListener(ActionListener actionListener)
487 lstners.add(actionListener);
490 public void removeActionListener(ActionListener actionListener)
492 lstners.remove(actionListener);
495 public static void main(String args[])
497 Cache.getDasSourceRegistry();
498 JDatabaseTree jdt = new JDatabaseTree(new jalview.ws.SequenceFetcher());
499 JFrame foo = new JFrame();
500 foo.setLayout(new BorderLayout());
501 foo.add(jdt.getDatabaseSelectorButton(), BorderLayout.CENTER);
503 foo.setVisible(true);
505 final Thread us = Thread.currentThread();
506 jdt.addActionListener(new ActionListener()
510 public void actionPerformed(ActionEvent e)
520 } catch (InterruptedException x)
523 if (!jdt.hasSelection())
525 System.out.println("No Selection");
529 System.out.println("Selection: " + jdt.getSelectedItem());
531 for (DbSourceProxy pr : jdt.getSelectedSources())
533 System.out.println("Source " + s++ + ": " + pr.getDbName()
534 + " (" + pr.getDbSource() + ") Version "
535 + pr.getDbVersion() + ". Test:\t" + pr.getTestQuery());
537 System.out.println("Test queries: " + jdt.getExampleQueries());
540 } while (nultimes > 0 && foo.isVisible());
541 foo.setVisible(false);
545 public void keyPressed(KeyEvent arg0)
547 if (!arg0.isConsumed() && arg0.getKeyCode() == KeyEvent.VK_ENTER)
549 action = arg0.getKeyCode();
552 if (!arg0.isConsumed() && arg0.getKeyChar() == KeyEvent.VK_ESCAPE)
554 action = arg0.getKeyCode();
560 public void keyReleased(KeyEvent arg0)
562 // TODO Auto-generated method stub
567 public void keyTyped(KeyEvent arg0)
569 // TODO Auto-generated method stub