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