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