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 java.util.Locale;
25 import jalview.util.MessageManager;
26 import jalview.ws.seqfetcher.DbSourceProxy;
28 import java.awt.BorderLayout;
29 import java.awt.Component;
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.awt.event.MouseAdapter;
38 import java.awt.event.MouseEvent;
39 import java.util.ArrayList;
40 import java.util.HashSet;
41 import java.util.Hashtable;
42 import java.util.List;
43 import java.util.Vector;
45 import javax.swing.JButton;
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<>();
106 String dbs[] = sfetch.getSupportedDb();
107 Hashtable<String, String> ht = new Hashtable<>();
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()
232 .toLowerCase(Locale.ROOT);
236 names[i] = ((String) child.getUserObject())
237 .toLowerCase(Locale.ROOT);
238 sortTreeNodes(child);
243 throw new Error(MessageManager
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
256 implements TreeCellRenderer
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<>();
377 for (TreePath tp : tsel)
379 DefaultMutableTreeNode admt,
380 dmt = (DefaultMutableTreeNode) tp.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)
396 .getSourceProxy((String) dmt.getUserObject()));
400 srcs.add(sfetcher.getSourceProxy((String) dmt.getUserObject())
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"),
450 ? " with " + x + " test quer"
451 + (x == 1 ? "y" : "ies")
458 dbstatus.setText(MessageManager
459 .formatMessage("label.database_param", new String[]
463 dbstatex.setText(MessageManager
464 .formatMessage("label.example_param", new String[]
470 dbstatus.setText(" ");
473 dbstatus.invalidate();
474 dbstatex.invalidate();
477 public String getSelectedItem()
481 return getSelectedSources().get(0).getDbName();
486 public String getExampleQueries()
492 StringBuffer sb = new StringBuffer();
493 HashSet<String> hs = new HashSet<>();
494 for (DbSourceProxy dbs : getSelectedSources())
496 String tq = dbs.getTestQuery();
507 return sb.toString();
510 List<ActionListener> lstners = new Vector<>();
512 public void addActionListener(ActionListener actionListener)
514 lstners.add(actionListener);
517 public void removeActionListener(ActionListener actionListener)
519 lstners.remove(actionListener);
523 public void keyPressed(KeyEvent arg0)
525 if (!arg0.isConsumed() && arg0.getKeyCode() == KeyEvent.VK_ENTER)
527 action = arg0.getKeyCode();
531 if (!arg0.isConsumed() && arg0.getKeyChar() == KeyEvent.VK_ESCAPE)
533 action = arg0.getKeyCode();
539 public void keyReleased(KeyEvent arg0)
541 // TODO Auto-generated method stub
546 public void keyTyped(KeyEvent arg0)
548 // TODO Auto-generated method stub
553 public void setVisible(boolean arg0)
555 System.out.println("setVisible: " + arg0);
556 super.setVisible(arg0);