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