e62503fd148e24572b32ffa1059e99efa05f60a6
[jalview.git] / src2 / fr / orsay / lri / varna / applications / VARNAGUI.java
1 /*
2  VARNA is a tool for the automated drawing, visualization and annotation of the secondary structure of RNA, designed as a companion software for web servers and databases.
3  Copyright (C) 2008  Kevin Darty, Alain Denise and Yann Ponty.
4  electronic mail : Yann.Ponty@lri.fr
5  paper mail : LRI, bat 490 Université Paris-Sud 91405 Orsay Cedex France
6
7  This file is part of VARNA version 3.1.
8  VARNA version 3.1 is free software: you can redistribute it and/or 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  VARNA version 3.1 is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
12  without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13  See the GNU General Public License for more details.
14
15  You should have received a copy of the GNU General Public License along with VARNA version 3.1.
16  If not, see http://www.gnu.org/licenses.
17  */
18 package fr.orsay.lri.varna.applications;
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.Image;
27 import java.awt.Point;
28 import java.awt.Rectangle;
29 import java.awt.Toolkit;
30 import java.awt.datatransfer.DataFlavor;
31 import java.awt.datatransfer.Transferable;
32 import java.awt.dnd.DnDConstants;
33 import java.awt.dnd.DropTarget;
34 import java.awt.dnd.DropTargetDragEvent;
35 import java.awt.dnd.DropTargetDropEvent;
36 import java.awt.dnd.DropTargetEvent;
37 import java.awt.dnd.DropTargetListener;
38 import java.awt.event.ActionEvent;
39 import java.awt.event.ActionListener;
40 import java.awt.event.AdjustmentEvent;
41 import java.awt.event.AdjustmentListener;
42 import java.awt.event.MouseEvent;
43 import java.awt.event.MouseListener;
44 import java.awt.geom.Point2D.Double;
45 import java.io.File;
46 import java.text.DateFormat;
47 import java.util.ArrayList;
48 import java.util.Collection;
49 import java.util.Date;
50 import java.util.Hashtable;
51 import java.util.List;
52 import java.util.Set;
53
54 import javax.swing.DefaultListModel;
55 import javax.swing.DefaultListSelectionModel;
56 import javax.swing.Icon;
57 import javax.swing.JButton;
58 import javax.swing.JFrame;
59 import javax.swing.JLabel;
60 import javax.swing.JList;
61 import javax.swing.JOptionPane;
62 import javax.swing.JPanel;
63 import javax.swing.JScrollBar;
64 import javax.swing.JScrollPane;
65 import javax.swing.JSplitPane;
66 import javax.swing.JTextField;
67 import javax.swing.ListModel;
68 import javax.swing.ListSelectionModel;
69 import javax.swing.UIManager;
70 import javax.swing.UnsupportedLookAndFeelException;
71 import javax.swing.event.ListSelectionEvent;
72 import javax.swing.event.ListSelectionListener;
73 import javax.swing.text.BadLocationException;
74 import javax.swing.text.DefaultHighlighter;
75
76 import fr.orsay.lri.varna.VARNAPanel;
77 import fr.orsay.lri.varna.components.ReorderableJList;
78 import fr.orsay.lri.varna.components.ZoomWindow;
79 import fr.orsay.lri.varna.exceptions.ExceptionFileFormatOrSyntax;
80 import fr.orsay.lri.varna.exceptions.ExceptionLoadingFailed;
81 import fr.orsay.lri.varna.exceptions.ExceptionNonEqualLength;
82 import fr.orsay.lri.varna.exceptions.ExceptionUnmatchedClosingParentheses;
83 import fr.orsay.lri.varna.factories.RNAFactory;
84 import fr.orsay.lri.varna.interfaces.InterfaceVARNAListener;
85 import fr.orsay.lri.varna.interfaces.InterfaceVARNARNAListener;
86 import fr.orsay.lri.varna.interfaces.InterfaceVARNASelectionListener;
87 import fr.orsay.lri.varna.models.BaseList;
88 import fr.orsay.lri.varna.models.FullBackup;
89 import fr.orsay.lri.varna.models.VARNAConfig;
90 import fr.orsay.lri.varna.models.rna.Mapping;
91 import fr.orsay.lri.varna.models.rna.ModeleBP;
92 import fr.orsay.lri.varna.models.rna.ModeleBase;
93 import fr.orsay.lri.varna.models.rna.RNA;
94
95 public class VARNAGUI extends JFrame implements DropTargetListener, InterfaceVARNAListener, MouseListener, AdjustmentListener {
96
97         /**
98          * 
99          */
100         private static final long serialVersionUID = -790155708306987257L;
101
102         private static final String DEFAULT_SEQUENCE = "CAGCACGACACUAGCAGUCAGUGUCAGACUGCAIACAGCACGACACUAGCAGUCAGUGUCAGACUGCAIACAGCACGACACUAGCAGUCAGUGUCAGACUGCAIA";
103
104         private static final String DEFAULT_STRUCTURE1 = "..(((((...(((((...(((((...(((((.....)))))...))))).....(((((...(((((.....)))))...))))).....)))))...)))))..";
105         private static final String DEFAULT_STRUCTURE2 = "..(((((...(((((...(((((........(((((...(((((.....)))))...)))))..................))))).....)))))...)))))..";
106         // private static final String DEFAULT_STRUCTURE1 = "((((....))))";
107         // private static final String DEFAULT_STRUCTURE2 =
108         // "((((..(((....)))..))))";
109
110         private VARNAPanel _vp;
111
112         private JPanel _tools = new JPanel();
113         private JPanel _input = new JPanel();
114
115         private JPanel _seqPanel = new JPanel();
116         private JPanel _strPanel = new JPanel();
117         private JLabel _info = new JLabel();
118         
119         private JTextField _str = new JTextField(DEFAULT_STRUCTURE1);
120         Object _hoverHighlightStr = null;
121         ArrayList<Object> _selectionHighlightStr = new ArrayList<Object>();
122         
123         private JTextField _seq = new JTextField(DEFAULT_SEQUENCE);
124         Object _hoverHighlightSeq = null;
125         ArrayList<Object> _selectionHighlightSeq = new ArrayList<Object>();
126         
127         
128         private ZoomWindow _zoomWindow;
129         private JLabel _strLabel = new JLabel(" Str:");
130         private JLabel _seqLabel = new JLabel(" Seq:");
131         private JButton _createButton = new JButton("Create");
132         private JButton _deleteButton = new JButton("Delete");
133         private JButton _duplicateButton = new JButton("Snapshot");
134         
135         private JPanel _listPanel = new JPanel();
136         private ReorderableJList _sideList = null;
137
138
139         private static String errorOpt = "error";
140         @SuppressWarnings("unused")
141         private boolean _error;
142
143         private Color _backgroundColor = Color.white;
144         
145         private JScrollBar _vert = new JScrollBar(JScrollBar.VERTICAL);
146         private JScrollBar _horiz = new JScrollBar(JScrollBar.HORIZONTAL);
147
148         private static int _nextID = 1;
149         @SuppressWarnings("unused")
150         private int _algoCode;
151         
152         private BackupHolder _rnaList;
153
154
155         public VARNAGUI() {
156                 super("VARNA GUI");
157                 RNAPanelDemoInit();
158         }
159
160         private void RNAPanelDemoInit() 
161         {
162             DefaultListModel dlm = new DefaultListModel(); 
163             
164
165                 int marginTools = 40;
166
167             DefaultListSelectionModel m = new DefaultListSelectionModel();
168             m.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
169             m.setLeadAnchorNotificationEnabled(false);
170             
171             
172                 _sideList = new ReorderableJList();
173                 _sideList.setModel(dlm);
174                 _sideList.addMouseListener(this);
175             _sideList.setSelectionModel(m);
176             _sideList.setPreferredSize(null);
177             
178             _sideList.addListSelectionListener( new ListSelectionListener(){
179                         public void valueChanged(ListSelectionEvent arg0) {
180                                 //System.out.println(arg0);
181                                 if (!_sideList.isSelectionEmpty() && !arg0.getValueIsAdjusting())
182                                 {
183                                         FullBackup  sel = (FullBackup) _sideList.getSelectedValue();
184                                         _vp.setConfig(sel.config);
185                                         showRNA(sel.rna);
186                                         _seq.setText(sel.rna.getSeq());
187                                         _str.setText(sel.rna.getStructDBN(true));
188                                 }
189                         }
190             });
191
192             _rnaList = new BackupHolder(dlm,_sideList);
193                 RNA _RNA1 = new RNA("User defined 1");
194                 RNA _RNA2 = new RNA("User defined 2");
195                 try {
196                         _vp = new VARNAPanel("0",".");
197                         _zoomWindow = new ZoomWindow(_vp);
198                         _RNA1.setRNA(DEFAULT_SEQUENCE, DEFAULT_STRUCTURE1);
199                         _RNA1.drawRNARadiate(_vp.getConfig());
200                         _RNA2.setRNA(DEFAULT_SEQUENCE, DEFAULT_STRUCTURE2);
201                         _RNA2.drawRNARadiate(_vp.getConfig());
202                 } catch (ExceptionNonEqualLength e) {
203                         _vp.errorDialog(e);
204                 } catch (ExceptionUnmatchedClosingParentheses e2) {
205                 e2.printStackTrace();
206                 } catch (ExceptionFileFormatOrSyntax e3) {
207                 e3.printStackTrace();
208                 }
209                 _vp.setPreferredSize(new Dimension(400, 400));
210             _rnaList.add(_vp.getConfig().clone(),_RNA2,generateDefaultName());
211             _rnaList.add(_vp.getConfig().clone(),_RNA1,generateDefaultName(),true);
212             
213
214             JScrollPane listScroller = new JScrollPane(_sideList);//,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
215             listScroller.setPreferredSize(new Dimension(150, 0));
216
217                 setBackground(_backgroundColor);
218                 _vp.setBackground(_backgroundColor);
219
220
221                 Font textFieldsFont = Font.decode("MonoSpaced-PLAIN-12");
222
223                 _seqLabel.setHorizontalTextPosition(JLabel.LEFT);
224                 _seqLabel.setPreferredSize(new Dimension(marginTools, 15));
225                 _seq.setFont(textFieldsFont);
226                 _seq.setText(DEFAULT_SEQUENCE);
227
228                 _createButton.addActionListener(new ActionListener() {
229                         public void actionPerformed(ActionEvent e) {
230                                 try {
231                                 RNA nRNA = new RNA(generateDefaultName());
232                                 nRNA.setRNA(_seq.getText(), _str.getText());
233                                 nRNA.drawRNARadiate(_vp.getConfig());
234                                 _rnaList.add(new VARNAConfig(),nRNA,true);
235                                 } catch (ExceptionUnmatchedClosingParentheses e1) {
236                                         JOptionPane.showMessageDialog(_vp, e1.getMessage(),"Error", JOptionPane.ERROR_MESSAGE);
237                                 } catch (ExceptionFileFormatOrSyntax e1) {
238                                         JOptionPane.showMessageDialog(_vp, e1.getMessage(),"Error", JOptionPane.ERROR_MESSAGE);
239                                 }
240                         }
241                 });
242
243
244                 _seqPanel.setLayout(new BorderLayout());
245                 _seqPanel.add(_seqLabel, BorderLayout.WEST);
246                 _seqPanel.add(_seq, BorderLayout.CENTER);
247
248                 _strLabel.setPreferredSize(new Dimension(marginTools, 15));
249                 _strLabel.setHorizontalTextPosition(JLabel.LEFT);
250                 _str.setFont(textFieldsFont);
251                 _strPanel.setLayout(new BorderLayout());
252                 _strPanel.add(_strLabel, BorderLayout.WEST);
253                 _strPanel.add(_str, BorderLayout.CENTER);
254
255                 _input.setLayout(new GridLayout(2, 0));
256                 _input.add(_seqPanel);
257                 _input.add(_strPanel);
258
259                 JPanel goPanel = new JPanel();
260                 goPanel.setLayout(new BorderLayout());
261
262                 _tools.setLayout(new BorderLayout());
263                 _tools.add(_input, BorderLayout.CENTER);
264                 _tools.add(_info, BorderLayout.SOUTH);
265                 _tools.add(goPanel, BorderLayout.EAST);
266
267                 _deleteButton.addActionListener(new ActionListener() {
268                         public void actionPerformed(ActionEvent e) {
269                                 _rnaList.removeSelected();
270                         }
271                 });
272                 // BH 2018 SwingJS can't clone, as it does not implement serialization 
273                 if (/** @j2sNative false && */ true)
274                   _duplicateButton.addActionListener(new ActionListener() {
275                         public void actionPerformed(ActionEvent e) {
276                                         _rnaList.add((VARNAConfig)_vp.getConfig().clone(),_vp.getRNA().clone(),_vp.getRNA().getName()+"-"+DateFormat.getTimeInstance(DateFormat.LONG).format(new Date()),true); 
277                         }});
278                 
279                 JPanel ops = new JPanel();
280                 ops.setLayout(new GridLayout(1,2));
281                 ops.add(_deleteButton);
282                 if (/** @j2sNative false && */ true)
283                   ops.add(_duplicateButton);
284                 
285                 JPanel opspanel = new JPanel(new BorderLayout());
286                 opspanel.add(ops,BorderLayout.NORTH);
287                 opspanel.add(_zoomWindow,BorderLayout.SOUTH);
288                 
289                 _zoomWindow.setPreferredSize(new Dimension(-1,200));
290                 
291
292                 JLabel j = new JLabel("Structure Manager",JLabel.CENTER);
293                 _listPanel.setLayout(new BorderLayout());
294                 
295                 _listPanel.add(opspanel,BorderLayout.SOUTH);
296                 _listPanel.add(j,BorderLayout.NORTH);
297                 _listPanel.add(listScroller,BorderLayout.CENTER);
298
299                 goPanel.add(_createButton, BorderLayout.CENTER);
300
301                 JPanel vpScroll = new JPanel();
302                 vpScroll.setLayout(new BorderLayout());
303                 _horiz.setVisible(false);
304                 _horiz.addAdjustmentListener(this);
305                 _vert.setVisible(false);
306                 _vert.addAdjustmentListener(this);
307                 vpScroll.add(_horiz,BorderLayout.SOUTH);
308                 vpScroll.add(_vert,BorderLayout.EAST);
309                 vpScroll.add(_vp,BorderLayout.CENTER);
310                 JSplitPane split = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,true,_listPanel,vpScroll);
311                 getContentPane().setLayout(new BorderLayout());
312                 getContentPane().add(split, BorderLayout.CENTER);
313                 getContentPane().add(_tools, BorderLayout.NORTH);
314                 
315
316                 setVisible(true);
317                 DropTarget dt = new DropTarget(_vp, this);
318                 
319                 _vp.addRNAListener(new InterfaceVARNARNAListener(){
320                         public void onSequenceModified(int index, String oldseq, String newseq) {
321                                 //System.out.println("Sequence changed: Index:"+index+" ["+oldseq+"]=>["+newseq+"]");
322                         }
323
324                         public void onStructureModified(Set<ModeleBP> current,
325                                         Set<ModeleBP> addedBasePairs, Set<ModeleBP> removedBasePairs) {
326                                 String result = "";
327                                 //System.out.println("Structure changed: ");
328                                 for (ModeleBP s:addedBasePairs)
329                                 {  result +=s;  }
330                                 //System.out.println("     Added: "+result);
331                                 result = "";
332                                 for (ModeleBP s:removedBasePairs)
333                                 {  result +=s;  }
334                                 //System.out.println("   Removed: "+result);
335                         }
336
337                         public void onRNALayoutChanged(Hashtable<Integer, Double> previousPositions) {
338                                 //System.out.print("Layout changed, bases#: ");
339                                 String result = "";
340                                 for (Integer s:previousPositions.keySet())
341                                 {  result +=s+" ";  }                           
342                                 //System.out.println(result);
343                         }
344                         
345                 });
346                 
347                 _vp.addSelectionListener(new InterfaceVARNASelectionListener(){
348
349                         public void onHoverChanged(ModeleBase oldbase, ModeleBase newBase) {
350                                 if (_hoverHighlightSeq!=null)
351                                 {
352                                         _seq.getHighlighter().removeHighlight(_hoverHighlightSeq);
353                                         _hoverHighlightSeq = null;
354                                 }
355                                 if (_hoverHighlightStr!=null)
356                                 {
357                                         _str.getHighlighter().removeHighlight(_hoverHighlightStr);
358                                         _hoverHighlightStr = null;
359                                 }
360                                 if (newBase!=null)
361                                 {
362                                         try {
363                                                 int i = newBase.getIndex();
364                                                 int[] shifts = _vp.getRNA().getStrandShifts();
365                                                 _hoverHighlightSeq = _seq.getHighlighter().addHighlight(i+shifts[i], i+shifts[i]+1, new DefaultHighlighter.DefaultHighlightPainter(Color.green) );
366                                                 _hoverHighlightStr = _str.getHighlighter().addHighlight(i+shifts[i], i+shifts[i]+1, new DefaultHighlighter.DefaultHighlightPainter(Color.green) );
367                                         } catch (BadLocationException e) {
368                                                 e.printStackTrace();
369                                         }
370                                 }
371                         }
372
373                         public void onSelectionChanged(BaseList selection,
374                                         BaseList addedBases, BaseList removedBases) {
375                                 for(Object tag: _selectionHighlightSeq)
376                                 {
377                                         _seq.getHighlighter().removeHighlight(tag);
378                                 }
379                                 _selectionHighlightSeq.clear();
380                                 for(Object tag: _selectionHighlightStr)
381                                 {
382                                         _str.getHighlighter().removeHighlight(tag);
383                                 }
384                                 _selectionHighlightStr.clear();
385                                 int[] shifts = _vp.getRNA().getStrandShifts();
386                                 for (ModeleBase m: selection.getBases())
387                                 {
388                                         try {
389                                                 int i = m.getIndex();
390                                                 _selectionHighlightSeq.add(_seq.getHighlighter().addHighlight(i+shifts[i], i+shifts[i]+1, new DefaultHighlighter.DefaultHighlightPainter(Color.orange) ));
391                                                 _selectionHighlightStr.add(_str.getHighlighter().addHighlight(i+shifts[i], i+shifts[i]+1, new DefaultHighlighter.DefaultHighlightPainter(Color.orange) ));
392                                         } catch (BadLocationException e) {
393                                                 e.printStackTrace();
394                                         }
395                                 }
396                         }
397                         
398                 });
399                 
400                 _vp.addVARNAListener(this);
401                 
402                 new Thread(_zoomWindow).start();   
403         }
404         
405         protected void showRNA(RNA rna) {
406                 _vp.showRNAInterpolated(rna);
407                 _zoomWindow.repaint();
408         }
409
410
411         public void addRNA(RNA r, VARNAConfig cfg)
412         {
413                 _rnaList.add(cfg,r); 
414         }
415         
416         public static String generateDefaultName()
417         {
418                 return "User file #"+_nextID++;
419         }
420
421         public RNA getRNA() {
422                 return (RNA)_sideList.getSelectedValue();
423         }
424
425
426
427         public String[][] getParameterInfo() {
428                 String[][] info = {
429                                 // Parameter Name Kind of Value Description,
430                                 { "sequenceDBN", "String", "A raw RNA sequence" },
431                                 { "structureDBN", "String",
432                                                 "An RNA structure in dot bracket notation (DBN)" },
433                                 { errorOpt, "boolean", "To show errors" }, };
434                 return info;
435         }
436
437         public void init() {
438                 _vp.setBackground(_backgroundColor);
439                 _error = true;
440         }
441
442         @SuppressWarnings("unused")
443         private Color getSafeColor(String col, Color def) {
444                 Color result;
445                 try {
446                         result = Color.decode(col);
447                 } catch (Exception e) {
448                         try {
449                                 result = Color.getColor(col, def);
450                         } catch (Exception e2) {
451                                 return def;
452                         }
453                 }
454                 return result;
455         }
456
457         public VARNAPanel get_varnaPanel() {
458                 return _vp;
459         }
460
461         public void set_varnaPanel(VARNAPanel surface) {
462                 _vp = surface;
463         }
464
465
466         public JTextField get_seq() {
467                 return _seq;
468         }
469
470         public void set_seq(JTextField _seq) {
471                 this._seq = _seq;
472         }
473
474         public JLabel get_info() {
475                 return _info;
476         }
477
478         public void set_info(JLabel _info) {
479                 this._info = _info;
480         }
481
482         public static void main(String[] args) {
483                 List<Image> icons = new ArrayList<Image>();
484                 //JOptionPane.showMessageDialog(null, ""+Toolkit.getDefaultToolkit().getImage("./VARNA16x16.png"), "Check", JOptionPane.INFORMATION_MESSAGE);
485                 icons.add(Toolkit.getDefaultToolkit().getImage("./VARNA16x16.png"));
486                 icons.add(Toolkit.getDefaultToolkit().getImage("./VARNA32x32.png"));
487                 icons.add(Toolkit.getDefaultToolkit().getImage("./VARNA64x64.png"));
488                 VARNAGUI d = new VARNAGUI();
489                 d.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
490                 d.pack();
491                 d.setIconImages(icons);
492                 d.setVisible(true);
493         }
494         
495
496         public void dragEnter(DropTargetDragEvent arg0) {
497                 // TODO Auto-generated method stub
498                 
499         }
500
501         public void dragExit(DropTargetEvent arg0) {
502                 // TODO Auto-generated method stub
503                 
504         }
505
506         public void dragOver(DropTargetDragEvent arg0) {
507                 // TODO Auto-generated method stub
508                 
509         }
510
511         public void drop(DropTargetDropEvent dtde) {
512             try {
513                 Transferable tr = dtde.getTransferable();
514                 DataFlavor[] flavors = tr.getTransferDataFlavors();
515                 for (int i = 0; i < flavors.length; i++) {
516             if (flavors[i].isFlavorJavaFileListType()) {
517               dtde.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
518               Object ob = tr.getTransferData(flavors[i]);
519               if (ob instanceof List)
520               {
521                   List list = (List) ob;
522                   for (int j = 0; j < list.size(); j++) {
523                   Object o = list.get(j);
524                   
525                   if (dtde.getSource() instanceof DropTarget)
526                   {
527                           DropTarget dt = (DropTarget) dtde.getSource();
528                           Component c = dt.getComponent();
529                           if (c instanceof VARNAPanel)
530                           {
531                                           String path = o.toString();
532                                   VARNAPanel vp = (VARNAPanel) c;
533                                           try{
534                                   FullBackup bck =  VARNAPanel.importSession((File) o);  // BH SwingJS
535                                   _rnaList.add(bck.config, bck.rna,bck.name,true);
536                                           }
537                                           catch (ExceptionLoadingFailed e3)
538                                           {
539                                                   ArrayList<RNA> rnas = RNAFactory.loadSecStr((File) o); // BH SwingJS
540                                                   if (rnas.isEmpty())
541                                                   {
542                                                           throw new ExceptionFileFormatOrSyntax("No RNA could be parsed from that source.");
543                                                   }
544                                                   
545                                               dtde.dropComplete(true);
546                                                   _vp.getVARNAUI().UIChooseRNAs(rnas);
547                                               return;
548                                                   /*
549                                                   for(RNA r: rnas)
550                                                   {
551                                                           r.drawRNA(vp.getConfig());
552                                                           String name = r.getName();
553                                                           if (name.equals(""))
554                                                           { 
555                                                                   name = path.substring(path.lastIndexOf(File.separatorChar)+1);
556                                                           }
557                                                           if (rnas.size()>1)
558                                                           {
559                                                                   name += " - Molecule# "+id++;
560                                                           }
561                                                           _rnaList.add(vp.getConfig().clone(),r,name,true);
562                                                   }*/
563                                           }                                       
564                           }
565                   }
566                   }
567               }
568               // If we made it this far, everything worked.
569               dtde.dropComplete(true);
570               return;
571             }
572                 }
573                 // Hmm, the user must not have dropped a file list
574                 dtde.rejectDrop();
575               } catch (Exception e) {
576                 e.printStackTrace();
577                 dtde.rejectDrop();
578               }
579                 
580         }
581
582         public void dropActionChanged(DropTargetDragEvent arg0) {
583         }
584
585         private class BackupHolder{
586                 private DefaultListModel _rnaList;
587                 private ArrayList<RNA> _rnas = new ArrayList<RNA>();
588                 JList _l;
589                 
590                 public BackupHolder(DefaultListModel rnaList, JList l)
591                 {
592                         _rnaList = rnaList;
593                         _l = l;
594                 }
595                 
596                 public void add(VARNAConfig c, RNA r)
597                 {
598                         add(c, r, r.getName(),false);
599                 }
600
601                 public void add(VARNAConfig c, RNA r,boolean select)
602                 {
603                         add(c, r, r.getName(),select);
604                 }
605
606                 public void add(VARNAConfig c, RNA r, String name)
607                 {
608                         add(c, r, name,false);                  
609                 }
610                 public void add(VARNAConfig c, RNA r, String name, boolean select)
611                 {
612                         if (!_rnas.contains(r))
613                         {
614                         if (select){
615                                 _l.removeSelectionInterval(0, _rnaList.size());
616                         }
617                         if (name.equals(""))
618                         {
619                                 name = generateDefaultName();
620                         }
621                         FullBackup bck = new FullBackup(c,r,name);
622                         _rnas.add(0, r);
623                         _rnaList.add(0,bck);
624                         _l.doLayout();
625                         if (select){
626                           _l.setSelectedIndex(0);
627                         }
628                         }
629                 }
630
631                 public void remove(int i)
632                 {
633                         _rnas.remove(i);
634                         _rnaList.remove(i);
635                         
636                 }
637                 public DefaultListModel getModel()
638                 {
639                         return _rnaList;
640                 }
641                 public boolean contains(RNA r)
642                 {
643                         return _rnas.contains(r);
644                 }
645                 /*public int getSize()
646                 {
647                         return _rnaList.getSize();
648                 }*/
649                 public FullBackup getElementAt(int i)
650                 {
651                         return (FullBackup) _rnaList.getElementAt(i);
652                 }
653                 
654                 public void removeSelected()
655                 {
656                         int i = _l.getSelectedIndex();
657                         if (i!=-1)
658                         {
659                           if (_rnaList.getSize()==1)
660                           {
661                                   RNA r = new RNA();
662                                   try {
663                                         r.setRNA(" ", ".");
664                                   } catch (ExceptionUnmatchedClosingParentheses e1) {
665                                   } catch (ExceptionFileFormatOrSyntax e1) {
666                                   }
667                                   showRNA(r);
668                           }
669                           else
670                           {  
671                                  int newi = i+1;
672                                  if (newi==_rnaList.getSize())
673                                  {
674                                          newi = _rnaList.getSize()-2;
675                                  }
676                                  FullBackup bck = (FullBackup) _rnaList.getElementAt(newi);
677                                  _l.setSelectedValue(bck,true);
678                           }
679                           _rnaList.remove(i);
680                         }
681
682                 }
683         }
684
685         public void onStructureRedrawn() {
686                 // TODO Auto-generated method stub
687                 
688         }
689
690         public void onUINewStructure(VARNAConfig v, RNA r) {
691                 _rnaList.add(v, r,r.getName(),true);
692                 onZoomLevelChanged();
693         }
694
695         public void onWarningEmitted(String s) {
696                 // TODO Auto-generated method stub
697                 
698         }
699
700         public void mouseClicked(MouseEvent e) {
701                            if(e.getClickCount() == 2){
702                              int index = _sideList.locationToIndex(e.getPoint());
703                              ListModel dlm = _sideList.getModel();
704                              FullBackup item = (FullBackup) dlm.getElementAt(index);;
705                              _sideList.ensureIndexIsVisible(index);
706                              Object newName = JOptionPane.showInputDialog(
707                                             this,
708                                             "Specify a new name for this RNA",
709                                             "Rename RNA", 
710                                             JOptionPane.QUESTION_MESSAGE,
711                                             (Icon)null,
712                                             null,
713                                             item.toString());
714                              if (newName!=null)
715                              {
716                                  item.name = newName.toString();
717                                  this._sideList.repaint();
718                              }
719                              }
720         }
721
722         public void mouseEntered(MouseEvent arg0) {
723                 // TODO Auto-generated method stub
724                 
725         }
726
727         public void mouseExited(MouseEvent arg0) {
728                 // TODO Auto-generated method stub
729                 
730         }
731
732         public void mousePressed(MouseEvent arg0) {
733                 // TODO Auto-generated method stub
734                 
735         }
736
737         public void mouseReleased(MouseEvent arg0) {
738                 // TODO Auto-generated method stub
739                 
740         }
741
742         public void onZoomLevelChanged() {
743                 if (_vp.getZoom()>1.02)
744                 {
745                   Rectangle r = _vp.getZoomedInTranslationBox();
746                   _horiz.setMinimum(r.x);
747                   _horiz.setMaximum(r.x+r.width+_vp.getWidth());
748                   _horiz.getModel().setExtent(_vp.getWidth());
749                   _horiz.getModel().setValue(_vp.getTranslation().x);             
750                   _horiz.doLayout();
751                   _horiz.setVisible(true);
752                   
753                   _vert.setMinimum(r.y);
754                   _vert.setMaximum(r.y+r.height+_vp.getHeight());
755                   _vert.getModel().setExtent(_vp.getHeight());
756                   _vert.getModel().setValue(_vp.getTranslation().y);              
757                   _vert.doLayout();
758                   _vert.setVisible(true);
759                 }
760                 else
761                 {
762                   _horiz.setVisible(false);
763                   _vert.setVisible(false);
764                 }
765         }
766
767         public void onTranslationChanged() {
768                 if (_vp.getZoom()>1.02)
769                 {
770                         int nx = _horiz.getMaximum()-(_vp.getTranslation().x-_horiz.getMinimum())-_vp.getWidth();
771                         int ny = _vert.getMaximum()-(_vp.getTranslation().y-_vert.getMinimum())-_vp.getHeight();
772                         _horiz.getModel().setValue(nx);
773                         _horiz.doLayout();
774                         _vert.getModel().setValue(ny);
775                         _vert.doLayout();
776                 }
777         }
778
779         public void adjustmentValueChanged(AdjustmentEvent arg0) {
780                 if (arg0.getSource()==_horiz)
781                 {
782                         _vp.setTranslation(new Point(_horiz.getMaximum()-(arg0.getValue()-_horiz.getMinimum())-_vp.getWidth(),_vp.getTranslation().y));
783                         _vp.repaint();
784                 }
785                 else if (arg0.getSource()==_vert)
786                 {
787                         _vp.setTranslation(new Point(_vp.getTranslation().x,_vert.getMaximum()-(arg0.getValue()-_vert.getMinimum())-_vp.getHeight()));
788                         _vp.repaint();
789                 }
790         }
791 }