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