a738a2c760edda661d8a2ba1be3e7ed055783551
[jalview.git] / src / jalview / gui / AppVarnaBinding.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
3  * Copyright (C) 2014 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.util.MessageManager;
24
25 import java.awt.BorderLayout;
26 import java.awt.Color;
27 import java.awt.Component;
28 import java.awt.Dimension;
29 import java.awt.Font;
30 import java.awt.GridLayout;
31 import java.awt.datatransfer.DataFlavor;
32 import java.awt.datatransfer.Transferable;
33 import java.awt.dnd.DnDConstants;
34 import java.awt.dnd.DropTarget;
35 import java.awt.dnd.DropTargetDragEvent;
36 import java.awt.dnd.DropTargetDropEvent;
37 import java.awt.dnd.DropTargetEvent;
38 import java.awt.dnd.DropTargetListener;
39 import java.awt.event.ActionEvent;
40 import java.awt.event.ActionListener;
41 import java.awt.event.ComponentEvent;
42 import java.awt.event.MouseEvent;
43 import java.awt.event.MouseListener;
44 import java.io.File;
45 import java.util.ArrayList;
46 import java.util.Collection;
47 import java.util.List;
48
49 import javax.swing.DefaultListModel;
50 import javax.swing.DefaultListSelectionModel;
51 import javax.swing.JButton;
52 import javax.swing.JLabel;
53 import javax.swing.JList;
54 import javax.swing.JPanel;
55 import javax.swing.JScrollPane;
56 import javax.swing.JTextField;
57 import javax.swing.ListModel;
58 import javax.swing.ListSelectionModel;
59 import javax.swing.event.ListSelectionEvent;
60 import javax.swing.event.ListSelectionListener;
61
62 import fr.orsay.lri.varna.VARNAPanel;
63 import fr.orsay.lri.varna.components.ReorderableJList;
64 import fr.orsay.lri.varna.exceptions.ExceptionFileFormatOrSyntax;
65 import fr.orsay.lri.varna.exceptions.ExceptionLoadingFailed;
66 import fr.orsay.lri.varna.exceptions.ExceptionNonEqualLength;
67 import fr.orsay.lri.varna.exceptions.ExceptionUnmatchedClosingParentheses;
68 import fr.orsay.lri.varna.interfaces.InterfaceVARNAListener;
69 import fr.orsay.lri.varna.models.FullBackup;
70 import fr.orsay.lri.varna.models.VARNAConfig;
71 import fr.orsay.lri.varna.models.rna.Mapping;
72 import fr.orsay.lri.varna.models.rna.RNA;
73
74 public class AppVarnaBinding extends jalview.ext.varna.JalviewVarnaBinding
75         implements DropTargetListener, InterfaceVARNAListener,
76         MouseListener
77 {
78
79   /**
80          * 
81          */
82   // private static final long serialVersionUID = -790155708306987257L;
83
84   private String DEFAULT_SEQUENCE = "CAGCACGACACUAGCAGUCAGUGUCAGACUGCAIACAGCACGACACUAGCAGUCAGUGUCAGACUGCAIACAGCACGACACUAGCAGUCAGUGUCAGACUGCAIA";
85
86   private String DEFAULT_STRUCTURE1 = "..(((((...(((((...(((((...(((((.....)))))...))))).....(((((...(((((.....)))))...))))).....)))))...)))))..";
87
88   private String DEFAULT_STRUCTURE2 = "..(((((...(((((...(((((........(((((...(((((.....)))))...)))))..................))))).....)))))...)))))..";
89
90   public VARNAPanel vp;
91
92   protected JPanel _tools = new JPanel();
93
94   private JPanel _input = new JPanel();
95
96   private JPanel _seqPanel = new JPanel();
97
98   private JPanel _strPanel = new JPanel();
99
100   private JLabel _info = new JLabel();
101
102   private JTextField _str = new JTextField();
103
104   private JTextField _seq = new JTextField();
105
106   private JLabel _strLabel = new JLabel(
107           MessageManager.getString("label.str"));
108
109   private JLabel _seqLabel = new JLabel(
110           MessageManager.getString("label.seq"));
111
112   private JButton _createButton = new JButton(
113           MessageManager.getString("action.create"));
114
115   private JButton _updateButton = new JButton(
116           MessageManager.getString("action.update"));
117
118   private JButton _deleteButton = new JButton(
119           MessageManager.getString("action.delete"));
120
121   private JButton _duplicateButton = new JButton(
122           MessageManager.getString("action.snapshot"));
123
124   protected JPanel _listPanel = new JPanel();
125
126   private ReorderableJList _sideList = null;
127
128   private static String errorOpt = "error";
129
130   @SuppressWarnings("unused")
131   private boolean _error;
132
133   private Color _backgroundColor = Color.white;
134
135   private static int _nextID = 1;
136
137   @SuppressWarnings("unused")
138   private int _algoCode;
139
140   private BackupHolder _rnaList;
141
142   /*
143    * public AppVarnaBinding() { //super("VARNA in Jalview");
144    * //this.set_seq("ATGC"); //this.set_str(".()."); //RNAPanelDemoInit();
145    * 
146    * //initVarna("ATGCATGATATATATATAT","....((((...))))....");
147    * initVarna(this.DEFAULT_SEQUENCE,this.DEFAULT_STRUCTURE1); }
148    */
149
150   public AppVarnaBinding(String seq, String struc)
151   {
152     // super("VARNA in Jalview");
153     initVarna(seq, struc);
154
155   }
156
157   public AppVarnaBinding(ArrayList<RNA> rnaList)
158   {
159
160     // super("VARNA in Jalview");
161     initVarnaEdit(rnaList);
162   }
163
164   private void initVarna(String seq, String str)
165   {
166
167     DefaultListModel dlm = new DefaultListModel();
168
169     DefaultListSelectionModel m = new DefaultListSelectionModel();
170     m.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
171     m.setLeadAnchorNotificationEnabled(false);
172
173     _sideList = new ReorderableJList();
174     _sideList.setModel(dlm);
175     _sideList.addMouseListener(this);
176     _sideList.setSelectionModel(m);
177     _sideList.setPreferredSize(new Dimension(100, 0));
178     _sideList.addListSelectionListener(new ListSelectionListener()
179     {
180       public void valueChanged(ListSelectionEvent arg0)
181       {
182         if (!_sideList.isSelectionEmpty() && !arg0.getValueIsAdjusting())
183         {
184           FullBackup sel = (FullBackup) _sideList.getSelectedValue();
185           Mapping map = Mapping.DefaultOutermostMapping(vp.getRNA()
186                   .getSize(), sel.rna.getSize());
187           vp.showRNAInterpolated(sel.rna, sel.config, map);
188           _seq.setText(sel.rna.getSeq());
189           _str.setText(sel.rna.getStructDBN());
190         }
191       }
192     });
193
194     _rnaList = new BackupHolder(dlm, _sideList);
195     RNA _RNA1 = new RNA("User defined 1");
196
197     try
198     {
199
200       vp = new VARNAPanel("0", ".");
201       _RNA1.setRNA(seq, str);
202       _RNA1.drawRNARadiate(vp.getConfig());
203     } catch (ExceptionNonEqualLength e)
204     {
205       vp.errorDialog(e);
206     } catch (ExceptionUnmatchedClosingParentheses e2)
207     {
208       e2.printStackTrace();
209     } catch (ExceptionFileFormatOrSyntax e3)
210     {
211       e3.printStackTrace();
212     }
213     vp.setPreferredSize(new Dimension(400, 400));
214     _rnaList.add(vp.getConfig().clone(), _RNA1, generateDefaultName(), true);
215
216     // TODO setBackground(_backgroundColor);
217     vp.setBackground(_backgroundColor);
218
219     // TODO getContentPane().setLayout(new BorderLayout());
220     // TODO getContentPane().add(vp, BorderLayout.CENTER);
221
222     // setVisible(true);
223     vp.addVARNAListener(this);
224   }
225
226   private void initVarnaEdit(ArrayList<RNA> rnaInList)
227   {
228
229     DefaultListModel dlm = new DefaultListModel();
230
231     int marginTools = 40;
232
233     DefaultListSelectionModel m = new DefaultListSelectionModel();
234     m.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
235     m.setLeadAnchorNotificationEnabled(false);
236
237     _sideList = new ReorderableJList();
238     _sideList.setModel(dlm);
239     _sideList.addMouseListener(this);
240     _sideList.setSelectionModel(m);
241     _sideList.setPreferredSize(new Dimension(100, 0));
242     _sideList.addListSelectionListener(new ListSelectionListener()
243     {
244       public void valueChanged(ListSelectionEvent arg0)
245       {
246         // System.out.println(arg0);
247         if (!_sideList.isSelectionEmpty() && !arg0.getValueIsAdjusting())
248         {
249           FullBackup sel = (FullBackup) _sideList.getSelectedValue();
250           Mapping map = Mapping.DefaultOutermostMapping(vp.getRNA()
251                   .getSize(), sel.rna.getSize());
252           // vp.showRNAInterpolated(sel.rna, sel.config, map);
253           vp.showRNA(sel.rna, sel.config);
254           // _seq.setText(sel.rna.getSeq());
255           _str.setText(sel.rna.getStructDBN());
256         }
257       }
258     });
259     _rnaList = new BackupHolder(dlm, _sideList);
260
261     try
262     {
263
264       vp = new VARNAPanel("0", ".");
265       for (int i = 0; i < rnaInList.size(); i++)
266       {
267         rnaInList.get(i).drawRNARadiate(vp.getConfig());
268
269       }
270     } catch (ExceptionNonEqualLength e)
271     {
272       vp.errorDialog(e);
273     }
274     vp.setPreferredSize(new Dimension(400, 400));
275     for (int i = 0; i < rnaInList.size(); i++)
276     {
277       if (i < rnaInList.size() - 1)
278       {
279         _rnaList.add(vp.getConfig().clone(), rnaInList.get(i), rnaInList
280                 .get(i).getName());
281       }
282       else
283       {
284         _rnaList.add(vp.getConfig().clone(), rnaInList.get(i), rnaInList
285                 .get(i).getName(), true);
286       }
287     }
288
289     /*
290      * _rnaList.add(vp.getConfig().clone(),_RNA2,generateDefaultName());
291      * _rnaList.add(vp.getConfig().clone(),_RNA1,generateDefaultName(),true);
292      */
293
294     JScrollPane listScroller = new JScrollPane(_sideList);
295     listScroller.setPreferredSize(new Dimension(150, 0));
296
297     vp.setBackground(_backgroundColor);
298
299     Font textFieldsFont = Font.decode("MonoSpaced-PLAIN-12");
300
301     // _seqLabel.setHorizontalTextPosition(JLabel.LEFT);
302     // _seqLabel.setPreferredSize(new Dimension(marginTools, 15));
303     _seq.setFont(textFieldsFont);
304     _seq.setText(rnaInList.get(0).getSeq());
305
306     _updateButton.addActionListener(new ActionListener()
307     {
308       public void actionPerformed(ActionEvent e)
309       {
310         FullBackup sel = (FullBackup) _sideList.getSelectedValue();
311         sel.rna.setSequence("A");
312       }
313     });
314
315     // _seqPanel.setLayout(new BorderLayout());
316     // _seqPanel.add(_seqLabel, BorderLayout.WEST);
317     // _seqPanel.add(_seq, BorderLayout.CENTER);
318
319     _strLabel.setPreferredSize(new Dimension(marginTools, 15));
320     _strLabel.setHorizontalTextPosition(JLabel.LEFT);
321     _str.setFont(textFieldsFont);
322     _strPanel.setLayout(new BorderLayout());
323     _strPanel.add(_strLabel, BorderLayout.WEST);
324     _strPanel.add(_str, BorderLayout.CENTER);
325
326     _input.setLayout(new GridLayout(1, 0));
327     // _input.add(_seqPanel);
328     _input.add(_strPanel);
329
330     JPanel goPanel = new JPanel();
331     goPanel.setLayout(new BorderLayout());
332
333     _tools.setLayout(new BorderLayout());
334     _tools.add(_input, BorderLayout.CENTER);
335     // _tools.add(_info, BorderLayout.SOUTH);
336     _tools.add(goPanel, BorderLayout.EAST);
337
338     /*
339      * _deleteButton.addActionListener(new ActionListener() { public void
340      * actionPerformed(ActionEvent e) { _rnaList.removeSelected(); } });
341      * _duplicateButton.addActionListener(new ActionListener() { public void
342      * actionPerformed(ActionEvent e) {
343      * _rnaList.add((VARNAConfig)vp.getConfig().
344      * clone(),vp.getRNA().clone(),vp.getRNA
345      * ().getName()+"-"+DateFormat.getTimeInstance(DateFormat.LONG).format(new
346      * Date()),true); }});
347      */
348     goPanel.add(_updateButton, BorderLayout.CENTER);
349
350     JPanel ops = new JPanel();
351     ops.setLayout(new GridLayout(1, 2));
352     ops.add(_deleteButton);
353     ops.add(_duplicateButton);
354
355     JLabel j = new JLabel(
356             MessageManager.getString("label.structures_manager"),
357             JLabel.CENTER);
358     _listPanel.setLayout(new BorderLayout());
359
360     // _listPanel.add(ops, BorderLayout.SOUTH);
361     _listPanel.add(j, BorderLayout.NORTH);
362     _listPanel.add(listScroller, BorderLayout.CENTER);
363
364     // JSplitPane split = new
365     // JSplitPane(JSplitPane.HORIZONTAL_SPLIT,true,_listPanel,vp);
366     /**
367      * TODO getContentPane().setLayout(new BorderLayout());
368      * getContentPane().add(split, BorderLayout.CENTER);
369      * getContentPane().add(_tools, BorderLayout.NORTH);
370      */
371
372     // TODO setVisible(true);
373     DropTarget dt = new DropTarget(vp, this);
374
375     vp.addVARNAListener(this);
376   }
377
378   public JPanel getTools()
379   {
380     return _tools;
381   }
382
383   public JPanel getListPanel()
384   {
385     return _listPanel;
386   }
387
388   /**
389    * TODO: Is it effective to transfer the whole RNA?
390    * 
391    * @return Currently selected RNA
392    */
393   public RNA getSelectedRNA()
394   {
395     return _rnaList.getElementAt(_sideList.getSelectedIndex()).rna;
396   }
397
398   /**
399    * Substitute currently selected RNA with the edited one
400    * 
401    * @param rnaEdit
402    */
403   public void updateSelectedRNA(RNA rnaEdit)
404   {
405     vp.repaint();
406     vp.showRNA(rnaEdit);
407   }
408
409   /*
410    * private void RNAPanelDemoInit() { DefaultListModel dlm = new
411    * DefaultListModel();
412    * 
413    * 
414    * int marginTools = 40;
415    * 
416    * DefaultListSelectionModel m = new DefaultListSelectionModel();
417    * m.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
418    * m.setLeadAnchorNotificationEnabled(false);
419    * 
420    * 
421    * _sideList = new ReorderableJList(); _sideList.setModel(dlm);
422    * _sideList.addMouseListener(this); _sideList.setSelectionModel(m);
423    * _sideList.setPreferredSize(new Dimension(100, 0));
424    * _sideList.addListSelectionListener( new ListSelectionListener(){ public
425    * void valueChanged(ListSelectionEvent arg0) { //System.out.println(arg0); if
426    * (!_sideList.isSelectionEmpty() && !arg0.getValueIsAdjusting()) { FullBackup
427    * sel = (FullBackup) _sideList.getSelectedValue(); Mapping map =
428    * Mapping.DefaultOutermostMapping(vp.getRNA().getSize(), sel.rna.getSize());
429    * vp.showRNAInterpolated(sel.rna,sel.config,map);
430    * _seq.setText(sel.rna.getSeq()); _str.setText(sel.rna.getStructDBN()); } }
431    * });
432    * 
433    * _rnaList = new BackupHolder(dlm,_sideList); RNA _RNA1 = new
434    * RNA("User defined 1"); RNA _RNA2 = new RNA("User defined 2"); try { vp =
435    * new VARNAPanel("0","."); _RNA1.setRNA(DEFAULT_SEQUENCE,
436    * DEFAULT_STRUCTURE1); _RNA1.drawRNARadiate(vp.getConfig());
437    * _RNA2.setRNA(DEFAULT_SEQUENCE, DEFAULT_STRUCTURE2);
438    * _RNA2.drawRNARadiate(vp.getConfig()); } catch (ExceptionNonEqualLength e) {
439    * vp.errorDialog(e); } catch (ExceptionUnmatchedClosingParentheses e2) {
440    * e2.printStackTrace(); } catch (ExceptionFileFormatOrSyntax e3) {
441    * e3.printStackTrace(); } vp.setPreferredSize(new Dimension(400, 400));
442    * _rnaList.add(vp.getConfig().clone(),_RNA2,generateDefaultName());
443    * _rnaList.add(vp.getConfig().clone(),_RNA1,generateDefaultName(),true);
444    * 
445    * JScrollPane listScroller = new JScrollPane(_sideList);
446    * listScroller.setPreferredSize(new Dimension(150, 0));
447    * 
448    * setBackground(_backgroundColor); vp.setBackground(_backgroundColor);
449    * 
450    * 
451    * Font textFieldsFont = Font.decode("MonoSpaced-PLAIN-12");
452    * 
453    * _seqLabel.setHorizontalTextPosition(JLabel.LEFT);
454    * _seqLabel.setPreferredSize(new Dimension(marginTools, 15));
455    * _seq.setFont(textFieldsFont); _seq.setText(DEFAULT_SEQUENCE);
456    * 
457    * _createButton.addActionListener(new ActionListener() { public void
458    * actionPerformed(ActionEvent e) { try { RNA nRNA = new
459    * RNA(generateDefaultName()); nRNA.setRNA(_seq.getText(), _str.getText());
460    * nRNA.drawRNARadiate(vp.getConfig()); _rnaList.add(new
461    * VARNAConfig(),nRNA,true); } catch (ExceptionUnmatchedClosingParentheses e1)
462    * { JOptionPane.showMessageDialog(vp, e1.getMessage(),"Error",
463    * JOptionPane.ERROR_MESSAGE); } catch (ExceptionFileFormatOrSyntax e1) {
464    * JOptionPane.showMessageDialog(vp, e1.getMessage(),"Error",
465    * JOptionPane.ERROR_MESSAGE); } } });
466    * 
467    * 
468    * _seqPanel.setLayout(new BorderLayout()); _seqPanel.add(_seqLabel,
469    * BorderLayout.WEST); _seqPanel.add(_seq, BorderLayout.CENTER);
470    * 
471    * _strLabel.setPreferredSize(new Dimension(marginTools, 15));
472    * _strLabel.setHorizontalTextPosition(JLabel.LEFT);
473    * _str.setFont(textFieldsFont); _strPanel.setLayout(new BorderLayout());
474    * _strPanel.add(_strLabel, BorderLayout.WEST); _strPanel.add(_str,
475    * BorderLayout.CENTER);
476    * 
477    * _input.setLayout(new GridLayout(2, 0)); _input.add(_seqPanel);
478    * _input.add(_strPanel);
479    * 
480    * JPanel goPanel = new JPanel(); goPanel.setLayout(new BorderLayout());
481    * 
482    * _tools.setLayout(new BorderLayout()); _tools.add(_input,
483    * BorderLayout.CENTER); _tools.add(_info, BorderLayout.SOUTH);
484    * _tools.add(goPanel, BorderLayout.EAST);
485    * 
486    * _deleteButton.addActionListener(new ActionListener() { public void
487    * actionPerformed(ActionEvent e) { _rnaList.removeSelected(); } });
488    * _duplicateButton.addActionListener(new ActionListener() { public void
489    * actionPerformed(ActionEvent e) {
490    * _rnaList.add((VARNAConfig)vp.getConfig().clone
491    * (),vp.getRNA().clone(),vp.getRNA
492    * ().getName()+"-"+DateFormat.getTimeInstance(DateFormat.LONG).format(new
493    * Date()),true); }});
494    * 
495    * JPanel ops = new JPanel(); ops.setLayout(new GridLayout(1,2));
496    * ops.add(_deleteButton); ops.add(_duplicateButton);
497    * 
498    * JLabel j = new JLabel("Structures Manager",JLabel.CENTER);
499    * _listPanel.setLayout(new BorderLayout());
500    * 
501    * _listPanel.add(ops,BorderLayout.SOUTH);
502    * _listPanel.add(j,BorderLayout.NORTH);
503    * _listPanel.add(listScroller,BorderLayout.CENTER);
504    * 
505    * goPanel.add(_createButton, BorderLayout.CENTER);
506    * 
507    * JSplitPane split = new
508    * JSplitPane(JSplitPane.HORIZONTAL_SPLIT,true,_listPanel,vp);
509    * getContentPane().setLayout(new BorderLayout()); getContentPane().add(split,
510    * BorderLayout.CENTER); getContentPane().add(_tools, BorderLayout.NORTH);
511    * 
512    * setVisible(true); DropTarget dt = new DropTarget(vp, this);
513    * 
514    * vp.addVARNAListener(this); }
515    */
516   public static String generateDefaultName()
517   {
518     return "User file #" + _nextID++;
519   }
520
521   public RNA getRNA()
522   {
523     return (RNA) _sideList.getSelectedValue();
524   }
525
526   public String[][] getParameterInfo()
527   {
528     String[][] info =
529     {
530         // Parameter Name Kind of Value Description,
531         { "sequenceDBN", "String", "A raw RNA sequence" },
532         { "structureDBN", "String",
533             "An RNA structure in dot bracket notation (DBN)" },
534         { errorOpt, "boolean", "To show errors" }, };
535     return info;
536   }
537
538   public void init()
539   {
540     vp.setBackground(_backgroundColor);
541     _error = true;
542   }
543
544   @SuppressWarnings("unused")
545   private Color getSafeColor(String col, Color def)
546   {
547     Color result;
548     try
549     {
550       result = Color.decode(col);
551     } catch (Exception e)
552     {
553       try
554       {
555         result = Color.getColor(col, def);
556       } catch (Exception e2)
557       {
558         return def;
559       }
560     }
561     return result;
562   }
563
564   public VARNAPanel get_varnaPanel()
565   {
566     return vp;
567   }
568
569   public void set_varnaPanel(VARNAPanel surface)
570   {
571     vp = surface;
572   }
573
574   public String get_seq()
575   {
576     return _seq.getText();
577   }
578
579   public void set_seq(String _seq)
580   {
581     this._seq.setText(_seq);
582   }
583
584   public String get_str()
585   {
586     return _str.getText();
587   }
588
589   public void set_str(String _str)
590   {
591     this._str.setText(_str);
592   }
593
594   public JLabel get_info()
595   {
596     return _info;
597   }
598
599   public void set_info(JLabel _info)
600   {
601     this._info = _info;
602   }
603
604   /*
605    * public static void main(String[] args) { AppVarnaBinding d = new
606    * AppVarnaBinding(); d.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
607    * d.pack(); d.setVisible(true); }
608    */
609
610   public void dragEnter(DropTargetDragEvent arg0)
611   {
612     // TODO Auto-generated method stub
613
614   }
615
616   public void dragExit(DropTargetEvent arg0)
617   {
618     // TODO Auto-generated method stub
619
620   }
621
622   public void dragOver(DropTargetDragEvent arg0)
623   {
624     // TODO Auto-generated method stub
625
626   }
627
628   public void drop(DropTargetDropEvent dtde)
629   {
630     try
631     {
632       Transferable tr = dtde.getTransferable();
633       DataFlavor[] flavors = tr.getTransferDataFlavors();
634       for (int i = 0; i < flavors.length; i++)
635       {
636         if (flavors[i].isFlavorJavaFileListType())
637         {
638           dtde.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
639           Object ob = tr.getTransferData(flavors[i]);
640           if (ob instanceof List)
641           {
642             List list = (List) ob;
643             for (int j = 0; j < list.size(); j++)
644             {
645               Object o = list.get(j);
646
647               if (dtde.getSource() instanceof DropTarget)
648               {
649                 DropTarget dt = (DropTarget) dtde.getSource();
650                 Component c = dt.getComponent();
651                 if (c instanceof VARNAPanel)
652                 {
653                   String path = o.toString();
654                   VARNAPanel vp = (VARNAPanel) c;
655                   try
656                   {
657                     FullBackup bck = VARNAPanel.importSession(path);
658                     _rnaList.add(bck.config, bck.rna, bck.name, true);
659                   } catch (ExceptionLoadingFailed e3)
660                   {
661                     int mn = 1;
662                     Collection<RNA> mdls = fr.orsay.lri.varna.factories.RNAFactory
663                             .loadSecStr(path);
664                     for (RNA r : mdls)
665                     {
666                       r.drawRNA(vp.getConfig());
667                       String name = r.getName();
668                       if (name.equals(""))
669                       {
670                         name = path.substring(path
671                                 .lastIndexOf(File.separatorChar) + 1);
672                       }
673                       if (mdls.size() > 1)
674                       {
675                         name += " (Model " + mn++ + ")";
676                       }
677                       _rnaList.add(vp.getConfig().clone(), r, name, true);
678                     }
679                   }
680                 }
681               }
682             }
683           }
684           // If we made it this far, everything worked.
685           dtde.dropComplete(true);
686           return;
687         }
688       }
689       // Hmm, the user must not have dropped a file list
690       dtde.rejectDrop();
691     } catch (Exception e)
692     {
693       e.printStackTrace();
694       dtde.rejectDrop();
695     }
696
697   }
698
699   public void dropActionChanged(DropTargetDragEvent arg0)
700   {
701   }
702
703   private class BackupHolder
704   {
705     private DefaultListModel _rnaList;
706
707     private ArrayList<RNA> _rnas = new ArrayList<RNA>();
708
709     JList _l;
710
711     public BackupHolder(DefaultListModel rnaList, JList l)
712     {
713       _rnaList = rnaList;
714       _l = l;
715     }
716
717     public void add(VARNAConfig c, RNA r)
718     {
719       add(c, r, r.getName(), false);
720     }
721
722     public void add(VARNAConfig c, RNA r, boolean select)
723     {
724       add(c, r, r.getName(), select);
725     }
726
727     public void add(VARNAConfig c, RNA r, String name)
728     {
729       add(c, r, name, false);
730     }
731
732     public void add(VARNAConfig c, RNA r, String name, boolean select)
733     {
734       if (select)
735       {
736         _l.removeSelectionInterval(0, _rnaList.size());
737       }
738       if (name.equals(""))
739       {
740         name = generateDefaultName();
741       }
742       FullBackup bck = new FullBackup(c, r, name);
743       _rnas.add(0, r);
744       _rnaList.add(0, bck);
745       if (select)
746       {
747         _l.setSelectedIndex(0);
748       }
749     }
750
751     public void remove(int i)
752     {
753       _rnas.remove(i);
754       _rnaList.remove(i);
755
756     }
757
758     public DefaultListModel getModel()
759     {
760       return _rnaList;
761     }
762
763     public boolean contains(RNA r)
764     {
765       return _rnas.contains(r);
766     }
767
768     /*
769      * public int getSize() { return _rnaList.getSize(); }
770      */
771     public FullBackup getElementAt(int i)
772     {
773       return (FullBackup) _rnaList.getElementAt(i);
774     }
775
776     public void removeSelected()
777     {
778       int i = _l.getSelectedIndex();
779       if (i != -1)
780       {
781         if (_rnaList.getSize() == 1)
782         {
783           RNA r = new RNA();
784           try
785           {
786             r.setRNA(" ", ".");
787           } catch (ExceptionUnmatchedClosingParentheses e1)
788           {
789           } catch (ExceptionFileFormatOrSyntax e1)
790           {
791           }
792           vp.showRNA(r);
793           vp.repaint();
794         }
795         else
796         {
797           int newi = i + 1;
798           if (newi == _rnaList.getSize())
799           {
800             newi = _rnaList.getSize() - 2;
801           }
802           FullBackup bck = (FullBackup) _rnaList.getElementAt(newi);
803           _l.setSelectedValue(bck, true);
804         }
805         _rnaList.remove(i);
806       }
807
808     }
809   }
810
811   public void onLayoutChanged()
812   {
813     // TODO Auto-generated method stub
814
815   }
816
817   public void onUINewStructure(VARNAConfig v, RNA r)
818   {
819     // patch to fix infinite loop
820     // The problem is that onUINewStructure is called when user clicks
821     // check with Yann about whether Jalview should do anything with this event.
822     // e.g. if user has used VARNA's menu to import a structure .. Jalview may
823     // need to be told which structure is displayed.
824
825     // _rnaList.add(v, r, "", true);
826   }
827
828   public void onWarningEmitted(String s)
829   {
830     // TODO Auto-generated method stub
831
832   }
833
834   public void mouseClicked(MouseEvent e)
835   {
836     if (e.getClickCount() == 2)
837     {
838       int index = _sideList.locationToIndex(e.getPoint());
839       ListModel dlm = _sideList.getModel();
840       FullBackup item = (FullBackup) dlm.getElementAt(index);
841       ;
842       _sideList.ensureIndexIsVisible(index);
843       /*
844        * TODO Object newName = JOptionPane.showInputDialog( this,
845        * "Specify a new name for this RNA", "Rename RNA",
846        * JOptionPane.QUESTION_MESSAGE, (Icon)null, null, item.toString()); if
847        * (newName!=null) { item.name = newName.toString();
848        * this._sideList.repaint(); }
849        */
850     }
851   }
852
853   public void mouseEntered(MouseEvent arg0)
854   {
855     // TODO Auto-generated method stub
856
857   }
858
859   public void mouseExited(MouseEvent arg0)
860   {
861     // TODO Auto-generated method stub
862
863   }
864
865   public void mousePressed(MouseEvent arg0)
866   {
867     // TODO Auto-generated method stub
868
869   }
870
871   public void mouseReleased(MouseEvent arg0)
872   {
873     // TODO Auto-generated method stub
874
875   }
876
877   @Override
878   public Color getColour(int atomIndex, int pdbResNum, String chain,
879           String pdbId)
880   {
881     // TODO Auto-generated method stub
882     return null;
883   }
884
885   @Override
886   public String[] getPdbFile()
887   {
888     // TODO Auto-generated method stub
889     return null;
890   }
891
892   @Override
893   public void highlightAtom(int atomIndex, int pdbResNum, String chain,
894           String pdbId)
895   {
896     // TODO Auto-generated method stub
897
898   }
899
900   @Override
901   public void mouseOverStructure(int atomIndex, String strInfo)
902   {
903     // TODO Auto-generated method stub
904
905   }
906
907   @Override
908   public void releaseReferences(Object svl)
909   {
910     // TODO Auto-generated method stub
911
912   }
913
914   @Override
915   public void updateColours(Object source)
916   {
917     // TODO Auto-generated method stub
918
919   }
920
921   @Override
922   public void componentHidden(ComponentEvent e)
923   {
924     // TODO Auto-generated method stub
925
926   }
927
928   @Override
929   public void componentMoved(ComponentEvent e)
930   {
931     // TODO Auto-generated method stub
932
933   }
934
935   @Override
936   public void componentResized(ComponentEvent e)
937   {
938     // TODO Auto-generated method stub
939
940   }
941
942   @Override
943   public void componentShown(ComponentEvent e)
944   {
945     // TODO Auto-generated method stub
946
947   }
948
949   @Override
950   public void onStructureRedrawn()
951   {
952     // TODO Auto-generated method stub
953
954   }
955
956   @Override
957   public void onZoomLevelChanged()
958   {
959     // TODO Auto-generated method stub
960
961   }
962
963   @Override
964   public void onTranslationChanged()
965   {
966     // TODO Auto-generated method stub
967
968   }
969 }
970
971 /*
972  * public static void main(String[] args) { JTextField str = new
973  * JTextField("ATGC");
974  * 
975  * AppVarnaBinding vab = new AppVarnaBinding(); vab.varnagui.set_seq(str);
976  * vab.varnagui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
977  * vab.varnagui.pack(); vab.varnagui.setVisible(true); } }
978  */