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