Class |
Line # |
Actions |
|||
---|---|---|---|---|---|
JDatabaseTree | 61 | 173 | 62 | ||
JDatabaseTree.DbTreeRenderer | 255 | 18 | 9 |
1 | /* | |
2 | * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$) | |
3 | * Copyright (C) $$Year-Rel$$ The Jalview Authors | |
4 | * | |
5 | * This file is part of Jalview. | |
6 | * | |
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. | |
11 | * | |
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. | |
16 | * | |
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. | |
20 | */ | |
21 | package jalview.gui; | |
22 | ||
23 | import java.util.Locale; | |
24 | ||
25 | import jalview.util.MessageManager; | |
26 | import jalview.ws.seqfetcher.DbSourceProxy; | |
27 | ||
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; | |
44 | ||
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; | |
60 | ||
61 | public class JDatabaseTree extends JalviewDialog implements KeyListener | |
62 | { | |
63 | boolean allowMultiSelections = false; | |
64 | ||
65 | public int action; | |
66 | ||
67 | 0 | JButton getDatabaseSelectorButton() |
68 | { | |
69 | 0 | final JButton viewdbs = new JButton( |
70 | MessageManager.getString("action.select_ddbb")); | |
71 | 0 | viewdbs.addActionListener(new ActionListener() |
72 | { | |
73 | ||
74 | 0 | @Override |
75 | public void actionPerformed(ActionEvent arg0) | |
76 | { | |
77 | 0 | showDialog(); |
78 | } | |
79 | }); | |
80 | 0 | return viewdbs; |
81 | } | |
82 | ||
83 | JScrollPane svp; | |
84 | ||
85 | JTree dbviews; | |
86 | ||
87 | private jalview.ws.SequenceFetcher sfetcher; | |
88 | ||
89 | private JLabel dbstatus, dbstatex; | |
90 | ||
91 | private JPanel mainPanel = new JPanel(new BorderLayout()); | |
92 | ||
93 | 0 | public JDatabaseTree(jalview.ws.SequenceFetcher sfetch) |
94 | { | |
95 | 0 | mainPanel.add(this); |
96 | 0 | initDialogFrame(mainPanel, true, false, MessageManager |
97 | .getString("label.select_database_retrieval_source"), 650, 490); | |
98 | /* | |
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. | |
102 | */ | |
103 | 0 | DefaultMutableTreeNode tn = null, root = new DefaultMutableTreeNode(); |
104 | 0 | Hashtable<String, DefaultMutableTreeNode> source = new Hashtable<>(); |
105 | 0 | sfetcher = sfetch; |
106 | 0 | String dbs[] = sfetch.getSupportedDb(); |
107 | 0 | Hashtable<String, String> ht = new Hashtable<>(); |
108 | 0 | for (int i = 0; i < dbs.length; i++) |
109 | { | |
110 | 0 | tn = source.get(dbs[i]); |
111 | 0 | List<DbSourceProxy> srcs = sfetch.getSourceProxy(dbs[i]); |
112 | 0 | if (tn == null) |
113 | { | |
114 | 0 | source.put(dbs[i], tn = new DefaultMutableTreeNode(dbs[i], true)); |
115 | } | |
116 | 0 | for (DbSourceProxy dbp : srcs) |
117 | { | |
118 | 0 | if (ht.get(dbp.getDbName()) == null) |
119 | { | |
120 | 0 | tn.add(new DefaultMutableTreeNode(dbp, false)); |
121 | 0 | ht.put(dbp.getDbName(), dbp.getDbName()); |
122 | } | |
123 | else | |
124 | { | |
125 | 0 | jalview.bin.Console.errPrintln("dupe ig for : " + dbs[i] + " \t" |
126 | + dbp.getDbName() + " (" + dbp.getDbSource() + ")"); | |
127 | 0 | source.remove(tn); |
128 | } | |
129 | } | |
130 | } | |
131 | 0 | for (int i = 0; i < dbs.length; i++) |
132 | { | |
133 | 0 | tn = source.get(dbs[i]); |
134 | 0 | if (tn == null) |
135 | { | |
136 | 0 | continue; |
137 | } | |
138 | 0 | if (tn.getChildCount() == 1) |
139 | { | |
140 | 0 | DefaultMutableTreeNode ttn = (DefaultMutableTreeNode) tn |
141 | .getChildAt(0); | |
142 | // remove nodes with only one child | |
143 | 0 | tn.setUserObject(ttn.getUserObject()); |
144 | 0 | tn.removeAllChildren(); |
145 | 0 | source.put(dbs[i], tn); |
146 | 0 | tn.setAllowsChildren(false); |
147 | } | |
148 | 0 | root.add(tn); |
149 | } | |
150 | // and sort the tree | |
151 | 0 | sortTreeNodes(root); |
152 | 0 | dbviews = new JTree(new DefaultTreeModel(root, false)); |
153 | 0 | dbviews.setCellRenderer(new DbTreeRenderer(this)); |
154 | ||
155 | 0 | dbviews.getSelectionModel() |
156 | .setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION); | |
157 | 0 | svp = new JScrollPane(dbviews); |
158 | 0 | svp.setMinimumSize(new Dimension(100, 200)); |
159 | 0 | svp.setPreferredSize(new Dimension(200, 400)); |
160 | 0 | svp.setMaximumSize(new Dimension(300, 600)); |
161 | ||
162 | 0 | JPanel panel = new JPanel(new BorderLayout()); |
163 | 0 | panel.setSize(new Dimension(350, 220)); |
164 | 0 | panel.add(svp); |
165 | 0 | dbviews.addTreeSelectionListener(new TreeSelectionListener() |
166 | { | |
167 | ||
168 | 0 | @Override |
169 | public void valueChanged(TreeSelectionEvent arg0) | |
170 | { | |
171 | 0 | _setSelectionState(); |
172 | } | |
173 | }); | |
174 | 0 | dbviews.addMouseListener(new MouseAdapter() |
175 | { | |
176 | ||
177 | 0 | @Override |
178 | public void mousePressed(MouseEvent e) | |
179 | { | |
180 | 0 | if (e.getClickCount() == 2) |
181 | { | |
182 | 0 | okPressed(); |
183 | 0 | closeDialog(); |
184 | } | |
185 | } | |
186 | }); | |
187 | 0 | JPanel jc = new JPanel(new BorderLayout()), |
188 | j = new JPanel(new FlowLayout()); | |
189 | 0 | jc.add(svp, BorderLayout.CENTER); |
190 | ||
191 | 0 | java.awt.Font f; |
192 | // TODO: make the panel stay a fixed size for longest dbname+example set. | |
193 | 0 | JPanel dbstat = new JPanel(new GridLayout(2, 1)); |
194 | 0 | dbstatus = new JLabel(" "); // set the height correctly for layout |
195 | 0 | dbstatus.setFont(f = JvSwingUtils.getLabelFont(false, true)); |
196 | 0 | dbstatus.setSize(new Dimension(290, 50)); |
197 | 0 | dbstatex = new JLabel(" "); |
198 | 0 | dbstatex.setFont(f); |
199 | 0 | dbstatex.setSize(new Dimension(290, 50)); |
200 | 0 | dbstat.add(dbstatus); |
201 | 0 | dbstat.add(dbstatex); |
202 | 0 | jc.add(dbstat, BorderLayout.SOUTH); |
203 | 0 | jc.validate(); |
204 | 0 | add(jc, BorderLayout.CENTER); |
205 | 0 | ok.setEnabled(false); |
206 | 0 | j.add(ok); |
207 | 0 | j.add(cancel); |
208 | 0 | add(j, BorderLayout.SOUTH); |
209 | 0 | dbviews.addKeyListener(this); |
210 | 0 | validate(); |
211 | } | |
212 | ||
213 | 0 | private void sortTreeNodes(DefaultMutableTreeNode root) |
214 | { | |
215 | 0 | if (root.getChildCount() == 0) |
216 | { | |
217 | 0 | return; |
218 | } | |
219 | 0 | int count = root.getChildCount(); |
220 | 0 | String[] names = new String[count]; |
221 | 0 | DefaultMutableTreeNode[] nodes = new DefaultMutableTreeNode[count]; |
222 | 0 | for (int i = 0; i < count; i++) |
223 | { | |
224 | 0 | TreeNode node = root.getChildAt(i); |
225 | 0 | if (node instanceof DefaultMutableTreeNode) |
226 | { | |
227 | 0 | DefaultMutableTreeNode child = (DefaultMutableTreeNode) node; |
228 | 0 | nodes[i] = child; |
229 | 0 | if (child.getUserObject() instanceof DbSourceProxy) |
230 | { | |
231 | 0 | names[i] = ((DbSourceProxy) child.getUserObject()).getDbName() |
232 | .toLowerCase(Locale.ROOT); | |
233 | } | |
234 | else | |
235 | { | |
236 | 0 | names[i] = ((String) child.getUserObject()) |
237 | .toLowerCase(Locale.ROOT); | |
238 | 0 | sortTreeNodes(child); |
239 | } | |
240 | } | |
241 | else | |
242 | { | |
243 | 0 | throw new Error(MessageManager |
244 | .getString("error.implementation_error_cant_reorder_tree")); | |
245 | } | |
246 | } | |
247 | 0 | jalview.util.QuickSort.sort(names, nodes); |
248 | 0 | root.removeAllChildren(); |
249 | 0 | for (int i = count - 1; i >= 0; i--) |
250 | { | |
251 | 0 | root.add(nodes[i]); |
252 | } | |
253 | } | |
254 | ||
255 | private class DbTreeRenderer extends DefaultTreeCellRenderer | |
256 | implements TreeCellRenderer | |
257 | { | |
258 | JDatabaseTree us; | |
259 | ||
260 | 0 | public DbTreeRenderer(JDatabaseTree me) |
261 | { | |
262 | 0 | us = me; |
263 | 0 | ToolTipManager.sharedInstance().registerComponent(dbviews); |
264 | } | |
265 | ||
266 | 0 | private Component returnLabel(String txt) |
267 | { | |
268 | 0 | JLabel jl = new JLabel(txt); |
269 | 0 | jl.setFont(JvSwingUtils.getLabelFont()); |
270 | 0 | return jl; |
271 | } | |
272 | ||
273 | 0 | @Override |
274 | public Component getTreeCellRendererComponent(JTree tree, Object value, | |
275 | boolean selected, boolean expanded, boolean leaf, int row, | |
276 | boolean hasFocus) | |
277 | { | |
278 | 0 | String val = ""; |
279 | 0 | if (value != null && value instanceof DefaultMutableTreeNode) |
280 | { | |
281 | 0 | DefaultMutableTreeNode vl = (DefaultMutableTreeNode) value; |
282 | 0 | value = vl.getUserObject(); |
283 | 0 | if (value instanceof DbSourceProxy) |
284 | { | |
285 | 0 | val = ((DbSourceProxy) value).getDbName(); |
286 | 0 | if (((DbSourceProxy) value).getDescription() != null) |
287 | { // getName() | |
288 | 0 | this.setToolTipText(((DbSourceProxy) value).getDescription()); |
289 | } | |
290 | } | |
291 | else | |
292 | { | |
293 | 0 | if (value instanceof String) |
294 | { | |
295 | 0 | val = (String) value; |
296 | } | |
297 | } | |
298 | } | |
299 | 0 | if (value == null) |
300 | { | |
301 | 0 | val = ""; |
302 | } | |
303 | 0 | return super.getTreeCellRendererComponent(tree, val, selected, |
304 | expanded, leaf, row, hasFocus); | |
305 | ||
306 | } | |
307 | } | |
308 | ||
309 | List<DbSourceProxy> oldselection, selection = null; | |
310 | ||
311 | TreePath[] tsel = null, oldtsel = null; | |
312 | ||
313 | 0 | @Override |
314 | protected void raiseClosed() | |
315 | { | |
316 | 0 | for (ActionListener al : lstners) |
317 | { | |
318 | 0 | al.actionPerformed(null); |
319 | } | |
320 | } | |
321 | ||
322 | 0 | @Override |
323 | protected void okPressed() | |
324 | { | |
325 | 0 | _setSelectionState(); |
326 | } | |
327 | ||
328 | 0 | @Override |
329 | protected void cancelPressed() | |
330 | { | |
331 | 0 | selection = oldselection; |
332 | 0 | tsel = oldtsel; |
333 | 0 | _revertSelectionState(); |
334 | 0 | closeDialog(); |
335 | } | |
336 | ||
337 | 0 | void showDialog() |
338 | { | |
339 | 0 | oldselection = selection; |
340 | 0 | oldtsel = tsel; |
341 | 0 | validate(); |
342 | 0 | waitForInput(); |
343 | } | |
344 | ||
345 | 0 | public boolean hasSelection() |
346 | { | |
347 | 0 | return selection == null ? false : selection.size() == 0 ? false : true; |
348 | } | |
349 | ||
350 | 0 | public List<DbSourceProxy> getSelectedSources() |
351 | { | |
352 | 0 | return selection; |
353 | } | |
354 | ||
355 | /** | |
356 | * disable or enable selection handler | |
357 | */ | |
358 | boolean handleSelections = true; | |
359 | ||
360 | 0 | private void _setSelectionState() |
361 | { | |
362 | 0 | if (!handleSelections) |
363 | { | |
364 | 0 | return; |
365 | } | |
366 | 0 | ok.setEnabled(false); |
367 | 0 | if (dbviews.getSelectionCount() == 0) |
368 | { | |
369 | 0 | selection = null; |
370 | } | |
371 | ||
372 | 0 | tsel = dbviews.getSelectionPaths(); |
373 | 0 | boolean forcedFirstChild = false; |
374 | 0 | List<DbSourceProxy> srcs = new ArrayList<>(); |
375 | 0 | if (tsel != null) |
376 | { | |
377 | 0 | for (TreePath tp : tsel) |
378 | { | |
379 | 0 | DefaultMutableTreeNode admt, |
380 | dmt = (DefaultMutableTreeNode) tp.getLastPathComponent(); | |
381 | 0 | if (dmt.getUserObject() != null) |
382 | { | |
383 | /* | |
384 | * enable OK button once a selection has been made | |
385 | */ | |
386 | 0 | ok.setEnabled(true); |
387 | 0 | if (dmt.getUserObject() instanceof DbSourceProxy) |
388 | { | |
389 | 0 | srcs.add((DbSourceProxy) dmt.getUserObject()); |
390 | } | |
391 | else | |
392 | { | |
393 | 0 | if (allowMultiSelections) |
394 | { | |
395 | 0 | srcs.addAll(sfetcher |
396 | .getSourceProxy((String) dmt.getUserObject())); | |
397 | } | |
398 | else | |
399 | { | |
400 | 0 | srcs.add(sfetcher.getSourceProxy((String) dmt.getUserObject()) |
401 | .get(0)); | |
402 | 0 | forcedFirstChild = true; |
403 | } | |
404 | } | |
405 | } | |
406 | } | |
407 | } | |
408 | 0 | updateDbStatus(srcs, forcedFirstChild); |
409 | 0 | selection = srcs; |
410 | } | |
411 | ||
412 | 0 | private void _revertSelectionState() |
413 | { | |
414 | 0 | handleSelections = false; |
415 | 0 | if (selection == null || selection.size() == 0) |
416 | { | |
417 | 0 | dbviews.clearSelection(); |
418 | } | |
419 | else | |
420 | { | |
421 | 0 | dbviews.setSelectionPaths(tsel); |
422 | } | |
423 | 0 | handleSelections = true; |
424 | } | |
425 | ||
426 | 0 | private void updateDbStatus(List<DbSourceProxy> srcs, |
427 | boolean forcedFirstChild) | |
428 | { | |
429 | 0 | int x = 0; |
430 | 0 | String nm = "", qr = ""; |
431 | 0 | for (DbSourceProxy dbs : srcs) |
432 | { | |
433 | 0 | String tq = dbs.getTestQuery(); |
434 | 0 | nm = dbs.getDbName(); |
435 | 0 | if (tq != null && tq.trim().length() > 0 && dbs.isValidReference(tq)) |
436 | { | |
437 | 0 | qr = tq; |
438 | 0 | x++; |
439 | } | |
440 | } | |
441 | ||
442 | 0 | dbstatex.setText(" "); |
443 | 0 | if (allowMultiSelections) |
444 | { | |
445 | 0 | dbstatus.setText(MessageManager.formatMessage( |
446 | "label.selected_database_to_fetch_from", new String[] | |
447 | { Integer.valueOf(srcs.size()).toString(), | |
448 | 0 | (srcs.size() == 1 ? "" : "s"), |
449 | 0 | (srcs.size() > 0 |
450 | ? " with " + x + " test quer" | |
451 | 0 | + (x == 1 ? "y" : "ies") |
452 | : ".") })); | |
453 | } | |
454 | else | |
455 | { | |
456 | 0 | if (nm.length() > 0) |
457 | { | |
458 | 0 | dbstatus.setText(MessageManager |
459 | .formatMessage("label.database_param", new String[] | |
460 | { nm })); | |
461 | 0 | if (qr.length() > 0) |
462 | { | |
463 | 0 | dbstatex.setText(MessageManager |
464 | .formatMessage("label.example_param", new String[] | |
465 | { qr })); | |
466 | } | |
467 | } | |
468 | else | |
469 | { | |
470 | 0 | dbstatus.setText(" "); |
471 | } | |
472 | } | |
473 | 0 | dbstatus.invalidate(); |
474 | 0 | dbstatex.invalidate(); |
475 | } | |
476 | ||
477 | 0 | public String getSelectedItem() |
478 | { | |
479 | 0 | if (hasSelection()) |
480 | { | |
481 | 0 | return getSelectedSources().get(0).getDbName(); |
482 | } | |
483 | 0 | return null; |
484 | } | |
485 | ||
486 | 0 | public String getExampleQueries() |
487 | { | |
488 | 0 | if (!hasSelection()) |
489 | { | |
490 | 0 | return null; |
491 | } | |
492 | 0 | StringBuffer sb = new StringBuffer(); |
493 | 0 | HashSet<String> hs = new HashSet<>(); |
494 | 0 | for (DbSourceProxy dbs : getSelectedSources()) |
495 | { | |
496 | 0 | String tq = dbs.getTestQuery(); |
497 | 0 | ; |
498 | 0 | if (hs.add(tq)) |
499 | { | |
500 | 0 | if (sb.length() > 0) |
501 | { | |
502 | 0 | sb.append(";"); |
503 | } | |
504 | 0 | sb.append(tq); |
505 | } | |
506 | } | |
507 | 0 | return sb.toString(); |
508 | } | |
509 | ||
510 | List<ActionListener> lstners = new Vector<>(); | |
511 | ||
512 | 0 | public void addActionListener(ActionListener actionListener) |
513 | { | |
514 | 0 | lstners.add(actionListener); |
515 | } | |
516 | ||
517 | 0 | public void removeActionListener(ActionListener actionListener) |
518 | { | |
519 | 0 | lstners.remove(actionListener); |
520 | } | |
521 | ||
522 | 0 | @Override |
523 | public void keyPressed(KeyEvent arg0) | |
524 | { | |
525 | 0 | if (!arg0.isConsumed() && arg0.getKeyCode() == KeyEvent.VK_ENTER) |
526 | { | |
527 | 0 | action = arg0.getKeyCode(); |
528 | 0 | okPressed(); |
529 | 0 | closeDialog(); |
530 | } | |
531 | 0 | if (!arg0.isConsumed() && arg0.getKeyChar() == KeyEvent.VK_ESCAPE) |
532 | { | |
533 | 0 | action = arg0.getKeyCode(); |
534 | 0 | cancelPressed(); |
535 | } | |
536 | } | |
537 | ||
538 | 0 | @Override |
539 | public void keyReleased(KeyEvent arg0) | |
540 | { | |
541 | // TODO Auto-generated method stub | |
542 | ||
543 | } | |
544 | ||
545 | 0 | @Override |
546 | public void keyTyped(KeyEvent arg0) | |
547 | { | |
548 | // TODO Auto-generated method stub | |
549 | ||
550 | } | |
551 | ||
552 | 0 | @Override |
553 | public void setVisible(boolean arg0) | |
554 | { | |
555 | 0 | jalview.bin.Console.outPrintln("setVisible: " + arg0); |
556 | 0 | super.setVisible(arg0); |
557 | } | |
558 | } |