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