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.util.MessageManager;
24 import jalview.ws.seqfetcher.DbSourceProxy;
26 import java.awt.BorderLayout;
27 import java.awt.Component;
28 import java.awt.Dimension;
29 import java.awt.FlowLayout;
30 import java.awt.GridLayout;
31 import java.awt.event.ActionEvent;
32 import java.awt.event.ActionListener;
33 import java.awt.event.KeyEvent;
34 import java.awt.event.KeyListener;
35 import java.awt.event.MouseAdapter;
36 import java.awt.event.MouseEvent;
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.JLabel;
45 import javax.swing.JPanel;
46 import javax.swing.JScrollPane;
47 import javax.swing.JTree;
48 import javax.swing.ToolTipManager;
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 private JPanel mainPanel = new JPanel(new BorderLayout());
91 public JDatabaseTree(jalview.ws.SequenceFetcher sfetch)
94 initDialogFrame(mainPanel, true, false, MessageManager
95 .getString("label.select_database_retrieval_source"), 650, 490);
97 * Dynamically generated database list will need a translation function from
98 * internal source to externally distinct names. UNIPROT and UP_NAME are
99 * identical DB sources, and should be collapsed.
101 DefaultMutableTreeNode tn = null, root = new DefaultMutableTreeNode();
102 Hashtable<String, DefaultMutableTreeNode> source = new Hashtable<>();
104 String dbs[] = sfetch.getSupportedDb();
105 Hashtable<String, String> ht = new Hashtable<>();
106 for (int i = 0; i < dbs.length; i++)
108 tn = source.get(dbs[i]);
109 List<DbSourceProxy> srcs = sfetch.getSourceProxy(dbs[i]);
112 source.put(dbs[i], tn = new DefaultMutableTreeNode(dbs[i], true));
114 for (DbSourceProxy dbp : srcs)
116 if (ht.get(dbp.getDbName()) == null)
118 tn.add(new DefaultMutableTreeNode(dbp, false));
119 ht.put(dbp.getDbName(), dbp.getDbName());
123 System.err.println("dupe ig for : " + dbs[i] + " \t"
124 + dbp.getDbName() + " (" + dbp.getDbSource() + ")");
129 for (int i = 0; i < dbs.length; i++)
131 tn = source.get(dbs[i]);
136 if (tn.getChildCount() == 1)
138 DefaultMutableTreeNode ttn = (DefaultMutableTreeNode) tn
140 // remove nodes with only one child
141 tn.setUserObject(ttn.getUserObject());
142 tn.removeAllChildren();
143 source.put(dbs[i], tn);
144 tn.setAllowsChildren(false);
150 dbviews = new JTree(new DefaultTreeModel(root, false));
151 dbviews.setCellRenderer(new DbTreeRenderer(this));
153 dbviews.getSelectionModel()
154 .setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
155 svp = new JScrollPane(dbviews);
156 svp.setMinimumSize(new Dimension(100, 200));
157 svp.setPreferredSize(new Dimension(200, 400));
158 svp.setMaximumSize(new Dimension(300, 600));
160 JPanel panel = new JPanel(new BorderLayout());
161 panel.setSize(new Dimension(350, 220));
163 dbviews.addTreeSelectionListener(new TreeSelectionListener()
167 public void valueChanged(TreeSelectionEvent arg0)
169 _setSelectionState();
172 dbviews.addMouseListener(new MouseAdapter()
176 public void mousePressed(MouseEvent e)
178 if (e.getClickCount() == 2)
185 JPanel jc = new JPanel(new BorderLayout()),
186 j = new JPanel(new FlowLayout());
187 jc.add(svp, BorderLayout.CENTER);
190 // TODO: make the panel stay a fixed size for longest dbname+example set.
191 JPanel dbstat = new JPanel(new GridLayout(2, 1));
192 dbstatus = new JLabel(" "); // set the height correctly for layout
193 dbstatus.setFont(f = JvSwingUtils.getLabelFont(false, true));
194 dbstatus.setSize(new Dimension(290, 50));
195 dbstatex = new JLabel(" ");
197 dbstatex.setSize(new Dimension(290, 50));
198 dbstat.add(dbstatus);
199 dbstat.add(dbstatex);
200 jc.add(dbstat, BorderLayout.SOUTH);
202 add(jc, BorderLayout.CENTER);
203 ok.setEnabled(false);
206 add(j, BorderLayout.SOUTH);
207 dbviews.addKeyListener(this);
211 private void sortTreeNodes(DefaultMutableTreeNode root)
213 if (root.getChildCount() == 0)
217 int count = root.getChildCount();
218 String[] names = new String[count];
219 DefaultMutableTreeNode[] nodes = new DefaultMutableTreeNode[count];
220 for (int i = 0; i < count; i++)
222 TreeNode node = root.getChildAt(i);
223 if (node instanceof DefaultMutableTreeNode)
225 DefaultMutableTreeNode child = (DefaultMutableTreeNode) node;
227 if (child.getUserObject() instanceof DbSourceProxy)
229 names[i] = ((DbSourceProxy) child.getUserObject()).getDbName()
234 names[i] = ((String) child.getUserObject()).toLowerCase();
235 sortTreeNodes(child);
240 throw new Error(MessageManager
241 .getString("error.implementation_error_cant_reorder_tree"));
244 jalview.util.QuickSort.sort(names, nodes);
245 root.removeAllChildren();
246 for (int i = count - 1; i >= 0; i--)
252 private class DbTreeRenderer extends DefaultTreeCellRenderer
253 implements TreeCellRenderer
257 public DbTreeRenderer(JDatabaseTree me)
260 ToolTipManager.sharedInstance().registerComponent(dbviews);
263 private Component returnLabel(String txt)
265 JLabel jl = new JLabel(txt);
266 jl.setFont(JvSwingUtils.getLabelFont());
271 public Component getTreeCellRendererComponent(JTree tree, Object value,
272 boolean selected, boolean expanded, boolean leaf, int row,
276 if (value != null && value instanceof DefaultMutableTreeNode)
278 DefaultMutableTreeNode vl = (DefaultMutableTreeNode) value;
279 value = vl.getUserObject();
280 if (value instanceof DbSourceProxy)
282 val = ((DbSourceProxy) value).getDbName();
283 if (((DbSourceProxy) value).getDescription() != null)
285 this.setToolTipText(((DbSourceProxy) value).getDescription());
290 if (value instanceof String)
292 val = (String) value;
300 return super.getTreeCellRendererComponent(tree, val, selected,
301 expanded, leaf, row, hasFocus);
306 List<DbSourceProxy> oldselection, selection = null;
308 TreePath[] tsel = null, oldtsel = null;
311 protected void raiseClosed()
313 for (ActionListener al : lstners)
315 al.actionPerformed(null);
320 protected void okPressed()
322 _setSelectionState();
326 protected void cancelPressed()
328 selection = oldselection;
330 _revertSelectionState();
336 oldselection = selection;
342 public boolean hasSelection()
344 return selection == null ? false : selection.size() == 0 ? false : true;
347 public List<DbSourceProxy> getSelectedSources()
353 * disable or enable selection handler
355 boolean handleSelections = true;
357 private void _setSelectionState()
359 if (!handleSelections)
363 ok.setEnabled(false);
364 if (dbviews.getSelectionCount() == 0)
369 tsel = dbviews.getSelectionPaths();
370 boolean forcedFirstChild = false;
371 List<DbSourceProxy> srcs = new ArrayList<>();
374 for (TreePath tp : tsel)
376 DefaultMutableTreeNode admt,
377 dmt = (DefaultMutableTreeNode) tp.getLastPathComponent();
378 if (dmt.getUserObject() != null)
381 * enable OK button once a selection has been made
384 if (dmt.getUserObject() instanceof DbSourceProxy)
386 srcs.add((DbSourceProxy) dmt.getUserObject());
390 if (allowMultiSelections)
393 .getSourceProxy((String) dmt.getUserObject()));
397 srcs.add(sfetcher.getSourceProxy((String) dmt.getUserObject())
399 forcedFirstChild = true;
405 updateDbStatus(srcs, forcedFirstChild);
409 private void _revertSelectionState()
411 handleSelections = false;
412 if (selection == null || selection.size() == 0)
414 dbviews.clearSelection();
418 dbviews.setSelectionPaths(tsel);
420 handleSelections = true;
423 private void updateDbStatus(List<DbSourceProxy> srcs,
424 boolean forcedFirstChild)
427 String nm = "", qr = "";
428 for (DbSourceProxy dbs : srcs)
430 String tq = dbs.getTestQuery();
431 nm = dbs.getDbName();
432 if (tq != null && tq.trim().length() > 0 && dbs.isValidReference(tq))
439 dbstatex.setText(" ");
440 if (allowMultiSelections)
442 dbstatus.setText(MessageManager.formatMessage(
443 "label.selected_database_to_fetch_from", new String[]
444 { Integer.valueOf(srcs.size()).toString(),
445 (srcs.size() == 1 ? "" : "s"),
447 ? " with " + x + " test quer"
448 + (x == 1 ? "y" : "ies")
455 dbstatus.setText(MessageManager
456 .formatMessage("label.database_param", new String[]
460 dbstatex.setText(MessageManager
461 .formatMessage("label.example_param", new String[]
467 dbstatus.setText(" ");
470 dbstatus.invalidate();
471 dbstatex.invalidate();
474 public String getSelectedItem()
478 return getSelectedSources().get(0).getDbName();
483 public String getExampleQueries()
489 StringBuffer sb = new StringBuffer();
490 HashSet<String> hs = new HashSet<>();
491 for (DbSourceProxy dbs : getSelectedSources())
493 String tq = dbs.getTestQuery();
504 return sb.toString();
507 List<ActionListener> lstners = new Vector<>();
509 public void addActionListener(ActionListener actionListener)
511 lstners.add(actionListener);
514 public void removeActionListener(ActionListener actionListener)
516 lstners.remove(actionListener);
521 public void keyPressed(KeyEvent arg0)
523 if (!arg0.isConsumed() && arg0.getKeyCode() == KeyEvent.VK_ENTER)
525 action = arg0.getKeyCode();
529 if (!arg0.isConsumed() && arg0.getKeyChar() == KeyEvent.VK_ESCAPE)
531 action = arg0.getKeyCode();
537 public void keyReleased(KeyEvent arg0)
539 // TODO Auto-generated method stub
544 public void keyTyped(KeyEvent arg0)
546 // TODO Auto-generated method stub
551 public void setVisible(boolean arg0)
553 System.out.println("setVisible: " + arg0);
554 super.setVisible(arg0);