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