Clover icon

jalviewX

  1. Project Clover database Wed Oct 31 2018 15:13:58 GMT
  2. Package jalview.gui

File AppVarnaBinding.java

 

Coverage histogram

../../img/srcFileCovDistChart0.png
56% of files have more coverage

Code metrics

38
137
36
2
578
402
62
0.45
3.81
18
1.72

Classes

Class Line # Actions
AppVarnaBinding 67 124 55 188
0.00%
AppVarnaBinding.BackupHolder 315 13 7 23
0.00%
 

Contributing tests

No tests hitting this source file were found.

Source view

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 jalview.datamodel.SequenceI;
24    import jalview.ext.varna.JalviewVarnaBinding;
25    import jalview.structure.AtomSpec;
26    import jalview.util.MessageManager;
27   
28    import java.awt.BorderLayout;
29    import java.awt.Color;
30    import java.awt.Component;
31    import java.awt.Dimension;
32    import java.awt.datatransfer.DataFlavor;
33    import java.awt.datatransfer.Transferable;
34    import java.awt.dnd.DnDConstants;
35    import java.awt.dnd.DropTarget;
36    import java.awt.dnd.DropTargetAdapter;
37    import java.awt.dnd.DropTargetDropEvent;
38    import java.awt.event.ComponentEvent;
39    import java.awt.event.MouseAdapter;
40    import java.awt.event.MouseEvent;
41    import java.io.File;
42    import java.io.IOException;
43    import java.util.ArrayList;
44    import java.util.Collection;
45    import java.util.List;
46   
47    import javax.swing.DefaultListModel;
48    import javax.swing.DefaultListSelectionModel;
49    import javax.swing.JLabel;
50    import javax.swing.JList;
51    import javax.swing.JPanel;
52    import javax.swing.JScrollPane;
53    import javax.swing.ListModel;
54    import javax.swing.ListSelectionModel;
55    import javax.swing.event.ListSelectionEvent;
56    import javax.swing.event.ListSelectionListener;
57   
58    import fr.orsay.lri.varna.VARNAPanel;
59    import fr.orsay.lri.varna.components.ReorderableJList;
60    import fr.orsay.lri.varna.exceptions.ExceptionLoadingFailed;
61    import fr.orsay.lri.varna.exceptions.ExceptionNAViewAlgorithm;
62    import fr.orsay.lri.varna.exceptions.ExceptionNonEqualLength;
63    import fr.orsay.lri.varna.models.FullBackup;
64    import fr.orsay.lri.varna.models.VARNAConfig;
65    import fr.orsay.lri.varna.models.rna.RNA;
66   
 
67    public class AppVarnaBinding extends JalviewVarnaBinding
68    {
69    public VARNAPanel vp;
70   
71    protected JPanel _listPanel = new JPanel();
72   
73    private ReorderableJList _sideList = null;
74   
75    private static String errorOpt = "error";
76   
77    @SuppressWarnings("unused")
78    private boolean _error;
79   
80    private Color _backgroundColor = Color.white;
81   
82    private static int _nextID = 1;
83   
84    @SuppressWarnings("unused")
85    private int _algoCode;
86   
87    private BackupHolder _rnaList;
88   
89    /**
90    * Constructor
91    */
 
92  0 toggle public AppVarnaBinding()
93    {
94  0 init();
95    }
96   
97    /**
98    * Constructs the VARNAPanel and an (empty) selection list of structures to
99    * show in it
100    */
 
101  0 toggle private void init()
102    {
103  0 DefaultListModel<FullBackup> dlm = new DefaultListModel<FullBackup>();
104   
105  0 int marginTools = 40;
106   
107  0 DefaultListSelectionModel m = new DefaultListSelectionModel();
108  0 m.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
109  0 m.setLeadAnchorNotificationEnabled(false);
110   
111  0 _sideList = new ReorderableJList();
112  0 _sideList.setModel(dlm);
113  0 _sideList.addMouseListener(new MouseAdapter()
114    {
 
115  0 toggle @Override
116    public void mouseClicked(MouseEvent e)
117    {
118  0 AppVarnaBinding.this.mouseClicked(e);
119    }
120    });
121  0 _sideList.setSelectionModel(m);
122  0 _sideList.setPreferredSize(new Dimension(100, 0));
123  0 _sideList.addListSelectionListener(new ListSelectionListener()
124    {
 
125  0 toggle public void valueChanged(ListSelectionEvent evt)
126    {
127  0 changeSelectedStructure_actionPerformed(evt);
128    }
129    });
130  0 _rnaList = new BackupHolder(dlm, _sideList);
131   
132  0 try
133    {
134  0 vp = new VARNAPanel("0", ".");
135    } catch (ExceptionNonEqualLength e)
136    {
137  0 vp.errorDialog(e);
138    }
139  0 vp.setPreferredSize(new Dimension(400, 400));
140   
141  0 JScrollPane listScroller = new JScrollPane(_sideList);
142  0 listScroller.setPreferredSize(new Dimension(150, 0));
143   
144  0 vp.setBackground(_backgroundColor);
145   
146  0 JLabel j = new JLabel(
147    MessageManager.getString("label.structures_manager"),
148    JLabel.CENTER);
149  0 _listPanel.setLayout(new BorderLayout());
150   
151  0 _listPanel.add(j, BorderLayout.NORTH);
152  0 _listPanel.add(listScroller, BorderLayout.CENTER);
153   
154  0 new DropTarget(vp, new DropTargetAdapter()
155    {
 
156  0 toggle @Override
157    public void drop(DropTargetDropEvent dtde)
158    {
159  0 AppVarnaBinding.this.drop(dtde);
160    }
161    });
162    }
163   
 
164  0 toggle public JPanel getListPanel()
165    {
166  0 return _listPanel;
167    }
168   
169    /**
170    * Returns the currently selected RNA, or null if none selected
171    *
172    * @return
173    */
 
174  0 toggle public RNA getSelectedRNA()
175    {
176  0 int selectedIndex = _sideList.getSelectedIndex();
177  0 if (selectedIndex < 0)
178    {
179  0 return null;
180    }
181  0 FullBackup selected = _rnaList.getElementAt(selectedIndex);
182  0 return selected.rna;
183    }
184   
185    /**
186    * Substitute currently selected RNA with the edited one
187    *
188    * @param rnaEdit
189    */
 
190  0 toggle public void updateSelectedRNA(RNA rnaEdit)
191    {
192  0 vp.repaint();
193  0 vp.showRNA(rnaEdit);
194    }
195   
 
196  0 toggle public static String generateDefaultName()
197    {
198  0 return "User file #" + _nextID++;
199    }
200   
 
201  0 toggle public String[][] getParameterInfo()
202    {
203  0 String[][] info = {
204    // Parameter Name Kind of Value Description,
205    { "sequenceDBN", "String", "A raw RNA sequence" },
206    { "structureDBN", "String",
207    "An RNA structure in dot bracket notation (DBN)" },
208    { errorOpt, "boolean", "To show errors" }, };
209  0 return info;
210    }
211   
 
212  0 toggle @SuppressWarnings("unused")
213    private Color getSafeColor(String col, Color def)
214    {
215  0 Color result;
216  0 try
217    {
218  0 result = Color.decode(col);
219    } catch (Exception e)
220    {
221  0 try
222    {
223  0 result = Color.getColor(col, def);
224    } catch (Exception e2)
225    {
226  0 return def;
227    }
228    }
229  0 return result;
230    }
231   
 
232  0 toggle public VARNAPanel get_varnaPanel()
233    {
234  0 return vp;
235    }
236   
 
237  0 toggle public void set_varnaPanel(VARNAPanel surface)
238    {
239  0 vp = surface;
240    }
241   
 
242  0 toggle public void drop(DropTargetDropEvent dtde)
243    {
244  0 try
245    {
246  0 Transferable tr = dtde.getTransferable();
247  0 DataFlavor[] flavors = tr.getTransferDataFlavors();
248  0 for (int i = 0; i < flavors.length; i++)
249    {
250  0 if (flavors[i].isFlavorJavaFileListType())
251    {
252  0 dtde.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
253  0 Object ob = tr.getTransferData(flavors[i]);
254  0 if (ob instanceof List)
255    {
256  0 List list = (List) ob;
257  0 for (int j = 0; j < list.size(); j++)
258    {
259  0 Object o = list.get(j);
260   
261  0 if (dtde.getSource() instanceof DropTarget)
262    {
263  0 DropTarget dt = (DropTarget) dtde.getSource();
264  0 Component c = dt.getComponent();
265  0 if (c instanceof VARNAPanel)
266    {
267  0 String path = o.toString();
268  0 VARNAPanel varnaPanel = (VARNAPanel) c;
269  0 try
270    {
271  0 FullBackup bck = VARNAPanel.importSession(path);
272  0 _rnaList.add(bck.config, bck.rna, bck.name, true);
273    } catch (ExceptionLoadingFailed e3)
274    {
275  0 int mn = 1;
276  0 Collection<RNA> mdls = fr.orsay.lri.varna.factories.RNAFactory
277    .loadSecStr(path);
278  0 for (RNA r : mdls)
279    {
280  0 r.drawRNA(varnaPanel.getConfig());
281  0 String name = r.getName();
282  0 if (name.equals(""))
283    {
284  0 name = path.substring(
285    path.lastIndexOf(File.separatorChar) + 1);
286    }
287  0 if (mdls.size() > 1)
288    {
289  0 name += " (Model " + mn++ + ")";
290    }
291  0 _rnaList.add(varnaPanel.getConfig().clone(), r, name,
292    true);
293    // BH 2018 SwingJS clone of varnaPanel or its config will be the object itself, not a clone
294    }
295    }
296    }
297    }
298    }
299    }
300    // If we made it this far, everything worked.
301  0 dtde.dropComplete(true);
302  0 return;
303    }
304    }
305    // Hmm, the user must not have dropped a file list
306  0 dtde.rejectDrop();
307    } catch (Exception e)
308    {
309  0 e.printStackTrace();
310  0 dtde.rejectDrop();
311    }
312   
313    }
314   
 
315    private class BackupHolder
316    {
317    private DefaultListModel<FullBackup> _rnalist;
318   
319    private List<RNA> _rnas = new ArrayList<RNA>();
320   
321    JList _l;
322   
 
323  0 toggle public BackupHolder(DefaultListModel<FullBackup> rnaList, JList l)
324    {
325  0 _rnalist = rnaList;
326  0 _l = l;
327    }
328   
 
329  0 toggle public void add(VARNAConfig c, RNA r, String name)
330    {
331  0 add(c, r, name, false);
332    }
333   
334    /**
335    * Adds an entry to the end of the selection list and (optionally) sets it
336    * as selected
337    *
338    * @param c
339    * @param r
340    * @param name
341    * @param select
342    */
 
343  0 toggle public void add(VARNAConfig c, RNA r, String name, boolean select)
344    {
345  0 if (select)
346    {
347  0 _l.removeSelectionInterval(0, _rnalist.size());
348    }
349  0 if (name.equals(""))
350    {
351  0 name = generateDefaultName();
352    }
353  0 FullBackup bck = new FullBackup(c, r, name);
354  0 _rnas.add(r);
355  0 _rnalist.addElement(bck);
356  0 if (select)
357    {
358  0 _l.setSelectedIndex(0);
359    }
360    }
361   
 
362  0 toggle public FullBackup getElementAt(int i)
363    {
364  0 return _rnalist.getElementAt(i);
365    }
366    }
367   
 
368  0 toggle public void mouseClicked(MouseEvent e)
369    {
370  0 if (e.getClickCount() == 2)
371    {
372  0 int index = _sideList.locationToIndex(e.getPoint());
373  0 ListModel<FullBackup> dlm = _sideList.getModel();
374    // FullBackup item = dlm.getElementAt(index);
375   
376  0 _sideList.ensureIndexIsVisible(index);
377    /*
378    * TODO Object newName = JvOptionPane.showInputDialog( this,
379    * "Specify a new name for this RNA", "Rename RNA",
380    * JvOptionPane.QUESTION_MESSAGE, (Icon)null, null, item.toString()); if
381    * (newName!=null) { item.name = newName.toString();
382    * this._sideList.repaint(); }
383    */
384    }
385    }
386   
 
387  0 toggle @Override
388    public String[] getStructureFiles()
389    {
390  0 return null;
391    }
392   
 
393  0 toggle @Override
394    public void releaseReferences(Object svl)
395    {
396    }
397   
 
398  0 toggle @Override
399    public void updateColours(Object source)
400    {
401    }
402   
 
403  0 toggle @Override
404    public void componentHidden(ComponentEvent e)
405    {
406    }
407   
 
408  0 toggle @Override
409    public void componentMoved(ComponentEvent e)
410    {
411    }
412   
 
413  0 toggle @Override
414    public void componentResized(ComponentEvent e)
415    {
416    }
417   
 
418  0 toggle @Override
419    public void componentShown(ComponentEvent e)
420    {
421    }
422   
 
423  0 toggle @Override
424    public void highlightAtoms(List<AtomSpec> atoms)
425    {
426    }
427   
 
428  0 toggle @Override
429    public boolean isListeningFor(SequenceI seq)
430    {
431  0 return true;
432    }
433   
434    /**
435    * Returns the path to a temporary file containing a representation of the
436    * state of the Varna display, or null on any error
437    *
438    * @param rna
439    * @param jds
440    *
441    * @return
442    */
 
443  0 toggle public String getStateInfo(RNA rna)
444    {
445  0 if (vp == null)
446    {
447  0 return null;
448    }
449   
450    /*
451    * we have to show the RNA we want to save in the viewer; get the currently
452    * displayed model first so we can restore it
453    */
454  0 FullBackup sel = (FullBackup) _sideList.getSelectedValue();
455   
456  0 FullBackup model = null;
457  0 ListModel models = _sideList.getModel();
458  0 for (int i = 0; i < models.getSize(); i++)
459    {
460  0 model = (FullBackup) models.getElementAt(i);
461  0 if (model.rna == rna)
462    {
463  0 break;
464    }
465    }
466  0 if (model == null)
467    {
468  0 return null;
469    }
470   
471    /*
472    * switch display
473    */
474  0 vp.showRNA(model.rna, model.config);
475   
476  0 try
477    {
478  0 File temp;
479  0 temp = File.createTempFile("varna", null);
480  0 temp.deleteOnExit();
481  0 String filePath = temp.getAbsolutePath();
482  0 vp.toXML(filePath);
483   
484    /*
485    * restore the previous display
486    */
487  0 vp.showRNA(sel.rna, sel.config);
488   
489  0 return filePath;
490    } catch (IOException e)
491    {
492  0 return null;
493    }
494    }
495   
 
496  0 toggle public int getSelectedIndex()
497    {
498  0 return _sideList.getSelectedIndex();
499    }
500   
501    /**
502    * Switch the Varna display to the structure selected in the left hand panel
503    *
504    * @param evt
505    */
 
506  0 toggle protected void changeSelectedStructure_actionPerformed(
507    ListSelectionEvent evt)
508    {
509  0 if (!evt.getValueIsAdjusting())
510    {
511  0 showSelectedStructure();
512    }
513    }
514   
515    /**
516    *
517    */
 
518  0 toggle protected void showSelectedStructure()
519    {
520  0 FullBackup sel = (FullBackup) _sideList.getSelectedValue();
521  0 if (sel != null)
522    {
523  0 vp.showRNA(sel.rna, sel.config);
524    }
525    }
526   
527    /**
528    * Set and display the selected item in the list of structures
529    *
530    * @param selectedRna
531    */
 
532  0 toggle public void setSelectedIndex(final int selectedRna)
533    {
534    /*
535    * note this does nothing if, say, selecting item 3 when only 1 has been
536    * added on load
537    */
538  0 _sideList.setSelectedIndex(selectedRna);
539    // TODO ? need a worker thread to get this to happen properly
540    }
541   
542    /**
543    * Add an RNA structure to the selection list
544    *
545    * @param rna
546    */
 
547  0 toggle public void addStructure(RNA rna)
548    {
549  0 VARNAConfig config = vp.getConfig().clone(); // BH 2018 this will NOT be a clone in SwingJS
550  0 addStructure(rna, config);
551    }
552   
553    /**
554    * @param rna
555    * @param config
556    */
 
557  0 toggle protected void addStructure(final RNA rna, final VARNAConfig config)
558    {
559  0 drawRna(rna, config);
560  0 _rnaList.add(config, rna, rna.getName());
561    }
562   
563    /**
564    * @param rna
565    * @param config
566    */
 
567  0 toggle protected void drawRna(final RNA rna, final VARNAConfig config)
568    {
569  0 try
570    {
571  0 rna.drawRNA(rna.getDrawMode(), config);
572    } catch (ExceptionNAViewAlgorithm e)
573    {
574    // only throwable for draw mode = 3 NAView
575  0 System.err.println("Error drawing RNA: " + e.getMessage());
576    }
577    }
578    }