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