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