Further structure accessing feature
[jalview.git] / src / jalview / gui / AppVarnaBinding.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.6)
3  * Copyright (C) 2010 J Procter, AM Waterhouse, 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.MouseEvent;
37 import java.awt.event.MouseListener;
38 import java.io.File;
39 import java.text.DateFormat;
40 import java.util.ArrayList;
41 import java.util.Date;
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 JFrame implements DropTargetListener, InterfaceVARNAListener, MouseListener {
76
77         /**
78          * 
79          */
80         //private static final long serialVersionUID = -790155708306987257L;
81
82         private String DEFAULT_SEQUENCE = "CAGCACGACACUAGCAGUCAGUGUCAGACUGCAIACAGCACGACACUAGCAGUCAGUGUCAGACUGCAIACAGCACGACACUAGCAGUCAGUGUCAGACUGCAIA";
83
84         private String DEFAULT_STRUCTURE1 = "..(((((...(((((...(((((...(((((.....)))))...))))).....(((((...(((((.....)))))...))))).....)))))...)))))..";
85         private String DEFAULT_STRUCTURE2 = "..(((((...(((((...(((((........(((((...(((((.....)))))...)))))..................))))).....)))))...)))))..";
86         // private static final String DEFAULT_STRUCTURE1 = "((((....))))";
87         // private static final String DEFAULT_STRUCTURE2 =
88         // "((((..(((....)))..))))";
89
90         public VARNAPanel vp;
91
92         private JPanel _tools = new JPanel();
93         private JPanel _input = new JPanel();
94
95         private JPanel _seqPanel = new JPanel();
96         private JPanel _strPanel = new JPanel();
97         private JLabel _info = new JLabel();
98         private JTextField _str = new JTextField();
99         private JTextField _seq = new JTextField();
100         private JLabel _strLabel = new JLabel(" Str:");
101         private JLabel _seqLabel = new JLabel(" Seq:");
102         private JButton _createButton = new JButton("Create");
103         private JButton _deleteButton = new JButton("Delete");
104         private JButton _duplicateButton = new JButton("Snapshot");
105         
106         private JPanel _listPanel = new JPanel();
107         private ReorderableJList _sideList = null;
108
109
110         private static String errorOpt = "error";
111         @SuppressWarnings("unused")
112         private boolean _error;
113
114         private Color _backgroundColor = Color.white;
115
116         private static int _nextID = 1;
117         @SuppressWarnings("unused")
118         private int _algoCode;
119         
120         private BackupHolder _rnaList;
121
122
123         public AppVarnaBinding() {
124                 super("VARNA in Jalview");
125                 //this.set_seq("ATGC");
126                 //this.set_str(".().");
127                 //RNAPanelDemoInit();
128                 
129                 //initVarna("ATGCATGATATATATATAT","....((((...))))....");
130                 initVarna(this.DEFAULT_SEQUENCE,this.DEFAULT_STRUCTURE1);
131         }
132         
133         public AppVarnaBinding(String seq, String struc){
134                 super("VARNA in Jalview");
135                 initVarna(seq,struc);
136         }
137         
138         
139         
140         private void initVarna(String seq, String str){
141                 DefaultListModel dlm = new DefaultListModel(); 
142             
143                 DefaultListSelectionModel m = new DefaultListSelectionModel();
144             m.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
145             m.setLeadAnchorNotificationEnabled(false);
146             
147             _sideList = new ReorderableJList();
148                 _sideList.setModel(dlm);
149                 _sideList.addMouseListener(this);
150             _sideList.setSelectionModel(m);
151             _sideList.setPreferredSize(new Dimension(100, 0));
152             _sideList.addListSelectionListener( new ListSelectionListener(){
153                         public void valueChanged(ListSelectionEvent arg0) {
154                                 if (!_sideList.isSelectionEmpty() && !arg0.getValueIsAdjusting())
155                                 {
156                                         FullBackup  sel = (FullBackup) _sideList.getSelectedValue();
157                                         Mapping map = Mapping.DefaultOutermostMapping(vp.getRNA().getSize(), sel.rna.getSize());
158                                         vp.showRNAInterpolated(sel.rna,sel.config,map);
159                                         _seq.setText(sel.rna.getSeq());
160                                         _str.setText(sel.rna.getStructDBN());
161                                 }
162                         }
163             });
164
165             _rnaList = new BackupHolder(dlm,_sideList);
166                 RNA _RNA1 = new RNA("User defined 1");
167                 
168                 try {
169                         vp = new VARNAPanel("0",".");
170                         _RNA1.setRNA(seq, str);
171                         _RNA1.drawRNARadiate(vp.getConfig());
172                 } catch (ExceptionNonEqualLength e) {
173                         vp.errorDialog(e);
174                 } catch (ExceptionUnmatchedClosingParentheses e2) {
175                 e2.printStackTrace();
176                 } catch (ExceptionFileFormatOrSyntax e3) {
177                 e3.printStackTrace();
178                 }
179                 vp.setPreferredSize(new Dimension(400, 400));
180                 _rnaList.add(vp.getConfig().clone(),_RNA1,generateDefaultName(),true);
181
182                 setBackground(_backgroundColor);
183                 vp.setBackground(_backgroundColor);
184
185                 getContentPane().setLayout(new BorderLayout());
186                 getContentPane().add(vp, BorderLayout.CENTER);
187
188                 setVisible(true);
189                 vp.addVARNAListener(this);
190         }
191
192         private void RNAPanelDemoInit() 
193         {
194             DefaultListModel dlm = new DefaultListModel(); 
195             
196
197                 int marginTools = 40;
198
199             DefaultListSelectionModel m = new DefaultListSelectionModel();
200             m.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
201             m.setLeadAnchorNotificationEnabled(false);
202             
203             
204                 _sideList = new ReorderableJList();
205                 _sideList.setModel(dlm);
206                 _sideList.addMouseListener(this);
207             _sideList.setSelectionModel(m);
208             _sideList.setPreferredSize(new Dimension(100, 0));
209             _sideList.addListSelectionListener( new ListSelectionListener(){
210                         public void valueChanged(ListSelectionEvent arg0) {
211                                 //System.out.println(arg0);
212                                 if (!_sideList.isSelectionEmpty() && !arg0.getValueIsAdjusting())
213                                 {
214                                         FullBackup  sel = (FullBackup) _sideList.getSelectedValue();
215                                         Mapping map = Mapping.DefaultOutermostMapping(vp.getRNA().getSize(), sel.rna.getSize());
216                                         vp.showRNAInterpolated(sel.rna,sel.config,map);
217                                         _seq.setText(sel.rna.getSeq());
218                                         _str.setText(sel.rna.getStructDBN());
219                                 }
220                         }
221             });
222
223             _rnaList = new BackupHolder(dlm,_sideList);
224                 RNA _RNA1 = new RNA("User defined 1");
225                 RNA _RNA2 = new RNA("User defined 2");
226                 try {
227                         vp = new VARNAPanel("0",".");
228                         _RNA1.setRNA(DEFAULT_SEQUENCE, DEFAULT_STRUCTURE1);
229                         _RNA1.drawRNARadiate(vp.getConfig());
230                         _RNA2.setRNA(DEFAULT_SEQUENCE, DEFAULT_STRUCTURE2);
231                         _RNA2.drawRNARadiate(vp.getConfig());
232                 } catch (ExceptionNonEqualLength e) {
233                         vp.errorDialog(e);
234                 } catch (ExceptionUnmatchedClosingParentheses e2) {
235                 e2.printStackTrace();
236                 } catch (ExceptionFileFormatOrSyntax e3) {
237                 e3.printStackTrace();
238                 }
239                 vp.setPreferredSize(new Dimension(400, 400));
240             _rnaList.add(vp.getConfig().clone(),_RNA2,generateDefaultName());
241             _rnaList.add(vp.getConfig().clone(),_RNA1,generateDefaultName(),true);
242
243             JScrollPane listScroller = new JScrollPane(_sideList);
244             listScroller.setPreferredSize(new Dimension(150, 0));
245
246                 setBackground(_backgroundColor);
247                 vp.setBackground(_backgroundColor);
248
249
250                 Font textFieldsFont = Font.decode("MonoSpaced-PLAIN-12");
251
252                 _seqLabel.setHorizontalTextPosition(JLabel.LEFT);
253                 _seqLabel.setPreferredSize(new Dimension(marginTools, 15));
254                 _seq.setFont(textFieldsFont);
255                 _seq.setText(DEFAULT_SEQUENCE);
256
257                 _createButton.addActionListener(new ActionListener() {
258                         public void actionPerformed(ActionEvent e) {
259                                 try {
260                                 RNA nRNA = new RNA(generateDefaultName());
261                                 nRNA.setRNA(_seq.getText(), _str.getText());
262                                 nRNA.drawRNARadiate(vp.getConfig());
263                                 _rnaList.add(new VARNAConfig(),nRNA,true);
264                                 } catch (ExceptionUnmatchedClosingParentheses e1) {
265                                         JOptionPane.showMessageDialog(vp, e1.getMessage(),"Error", JOptionPane.ERROR_MESSAGE);
266                                 } catch (ExceptionFileFormatOrSyntax e1) {
267                                         JOptionPane.showMessageDialog(vp, e1.getMessage(),"Error", JOptionPane.ERROR_MESSAGE);
268                                 }
269                         }
270                 });
271
272
273                 _seqPanel.setLayout(new BorderLayout());
274                 _seqPanel.add(_seqLabel, BorderLayout.WEST);
275                 _seqPanel.add(_seq, BorderLayout.CENTER);
276
277                 _strLabel.setPreferredSize(new Dimension(marginTools, 15));
278                 _strLabel.setHorizontalTextPosition(JLabel.LEFT);
279                 _str.setFont(textFieldsFont);
280                 _strPanel.setLayout(new BorderLayout());
281                 _strPanel.add(_strLabel, BorderLayout.WEST);
282                 _strPanel.add(_str, BorderLayout.CENTER);
283
284                 _input.setLayout(new GridLayout(2, 0));
285                 _input.add(_seqPanel);
286                 _input.add(_strPanel);
287
288                 JPanel goPanel = new JPanel();
289                 goPanel.setLayout(new BorderLayout());
290
291                 _tools.setLayout(new BorderLayout());
292                 _tools.add(_input, BorderLayout.CENTER);
293                 _tools.add(_info, BorderLayout.SOUTH);
294                 _tools.add(goPanel, BorderLayout.EAST);
295
296                 _deleteButton.addActionListener(new ActionListener() {
297                         public void actionPerformed(ActionEvent e) {
298                                 _rnaList.removeSelected();
299                         }
300                 });
301                 _duplicateButton.addActionListener(new ActionListener() {
302                         public void actionPerformed(ActionEvent e) {
303                                         _rnaList.add((VARNAConfig)vp.getConfig().clone(),vp.getRNA().clone(),vp.getRNA().getName()+"-"+DateFormat.getTimeInstance(DateFormat.LONG).format(new Date()),true); 
304                         }});
305                 
306                 JPanel ops = new JPanel();
307                 ops.setLayout(new GridLayout(1,2));
308                 ops.add(_deleteButton);
309                 ops.add(_duplicateButton);
310
311                 JLabel j = new JLabel("Structures Manager",JLabel.CENTER);
312                 _listPanel.setLayout(new BorderLayout());
313                 
314                 _listPanel.add(ops,BorderLayout.SOUTH);
315                 _listPanel.add(j,BorderLayout.NORTH);
316                 _listPanel.add(listScroller,BorderLayout.CENTER);
317
318                 goPanel.add(_createButton, BorderLayout.CENTER);
319
320                 JSplitPane split = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,true,_listPanel,vp);
321                 getContentPane().setLayout(new BorderLayout());
322                 getContentPane().add(split, BorderLayout.CENTER);
323                 getContentPane().add(_tools, BorderLayout.NORTH);
324
325                 setVisible(true);
326                 DropTarget dt = new DropTarget(vp, this);
327                 
328                 vp.addVARNAListener(this);
329         }
330         
331         public static String generateDefaultName()
332         {
333                 return "User file #"+_nextID++;
334         }
335
336         public RNA getRNA() {
337                 return (RNA)_sideList.getSelectedValue();
338         }
339
340
341
342         public String[][] getParameterInfo() {
343                 String[][] info = {
344                                 // Parameter Name Kind of Value Description,
345                                 { "sequenceDBN", "String", "A raw RNA sequence" },
346                                 { "structureDBN", "String",
347                                                 "An RNA structure in dot bracket notation (DBN)" },
348                                 { errorOpt, "boolean", "To show errors" }, };
349                 return info;
350         }
351
352         public void init() {
353                 vp.setBackground(_backgroundColor);
354                 _error = true;
355         }
356
357         @SuppressWarnings("unused")
358         private Color getSafeColor(String col, Color def) {
359                 Color result;
360                 try {
361                         result = Color.decode(col);
362                 } catch (Exception e) {
363                         try {
364                                 result = Color.getColor(col, def);
365                         } catch (Exception e2) {
366                                 return def;
367                         }
368                 }
369                 return result;
370         }
371
372         public VARNAPanel get_varnaPanel() {
373                 return vp;
374         }
375
376         public void set_varnaPanel(VARNAPanel surface) {
377                 vp = surface;
378         }
379
380
381         public String get_seq() {
382                 return _seq.getText();
383         }
384
385         public void set_seq(String _seq) {
386                 this._seq.setText(_seq);
387         }
388         
389         public String get_str(){
390                 return _str.getText();
391         }
392         
393         public void set_str(String _str){
394                 this._str.setText(_str);
395         }
396
397         public JLabel get_info() {
398                 return _info;
399         }
400
401         public void set_info(JLabel _info) {
402                 this._info = _info;
403         }
404
405         public static void main(String[] args) {
406                 AppVarnaBinding d = new AppVarnaBinding();
407                 d.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
408                 d.pack();
409                 d.setVisible(true);
410         }
411         
412
413         public void dragEnter(DropTargetDragEvent arg0) {
414                 // TODO Auto-generated method stub
415                 
416         }
417
418         public void dragExit(DropTargetEvent arg0) {
419                 // TODO Auto-generated method stub
420                 
421         }
422
423         public void dragOver(DropTargetDragEvent arg0) {
424                 // TODO Auto-generated method stub
425                 
426         }
427
428         public void drop(DropTargetDropEvent dtde) {
429             try {
430                 Transferable tr = dtde.getTransferable();
431                 DataFlavor[] flavors = tr.getTransferDataFlavors();
432                 for (int i = 0; i < flavors.length; i++) {
433             if (flavors[i].isFlavorJavaFileListType()) {
434               dtde.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
435               Object ob = tr.getTransferData(flavors[i]);
436               if (ob instanceof List)
437               {
438                   List list = (List) ob;
439                   for (int j = 0; j < list.size(); j++) {
440                   Object o = list.get(j);
441                   
442                   if (dtde.getSource() instanceof DropTarget)
443                   {
444                           DropTarget dt = (DropTarget) dtde.getSource();
445                           Component c = dt.getComponent();
446                           if (c instanceof VARNAPanel)
447                           {
448                                           String path = o.toString();
449                                   VARNAPanel vp = (VARNAPanel) c;
450                                           try{
451                                   FullBackup bck =  VARNAPanel.importSession(path);
452                                   _rnaList.add(bck.config, bck.rna,bck.name,true);
453                                           }
454                                           catch (ExceptionLoadingFailed e3)
455                                           {
456                                                   RNA r = new RNA();
457                                                   r.loadSecStr(path);
458                                                   r.drawRNA(vp.getConfig());
459                                                   String name =r.getName();
460                                                   if (name.equals(""))
461                                                   { 
462                                                           name = path.substring(path.lastIndexOf(File.separatorChar)+1);
463                                                   }
464                                                   _rnaList.add(vp.getConfig().clone(),r,name,true);
465                                           }                                       
466                           }
467                   }
468                   }
469               }
470               // If we made it this far, everything worked.
471               dtde.dropComplete(true);
472               return;
473             }
474                 }
475                 // Hmm, the user must not have dropped a file list
476                 dtde.rejectDrop();
477               } catch (Exception e) {
478                 e.printStackTrace();
479                 dtde.rejectDrop();
480               }
481                 
482         }
483
484         public void dropActionChanged(DropTargetDragEvent arg0) {
485         }
486
487         private class BackupHolder{
488                 private DefaultListModel _rnaList;
489                 private ArrayList<RNA> _rnas = new ArrayList<RNA>();
490                 JList _l;
491                 
492                 public BackupHolder(DefaultListModel rnaList, JList l)
493                 {
494                         _rnaList = rnaList;
495                         _l = l;
496                 }
497                 
498                 public void add(VARNAConfig c, RNA r)
499                 {
500                         add(c, r, r.getName(),false);
501                 }
502
503                 public void add(VARNAConfig c, RNA r,boolean select)
504                 {
505                         add(c, r, r.getName(),select);
506                 }
507
508                 public void add(VARNAConfig c, RNA r, String name)
509                 {
510                         add(c, r, name,false);                  
511                 }
512                 public void add(VARNAConfig c, RNA r, String name, boolean select)
513                 {
514                         if (select){
515                                 _l.removeSelectionInterval(0, _rnaList.size());
516                         }
517                         if (name.equals(""))
518                         {
519                                 name = generateDefaultName();
520                         }
521                         FullBackup bck = new FullBackup(c,r,name);
522                         _rnas.add(0, r);
523                         _rnaList.add(0,bck);
524                         if (select){
525                           _l.setSelectedIndex(0);
526                         }
527                 }
528
529                 public void remove(int i)
530                 {
531                         _rnas.remove(i);
532                         _rnaList.remove(i);
533                         
534                 }
535                 public DefaultListModel getModel()
536                 {
537                         return _rnaList;
538                 }
539                 public boolean contains(RNA r)
540                 {
541                         return _rnas.contains(r);
542                 }
543                 /*public int getSize()
544                 {
545                         return _rnaList.getSize();
546                 }*/
547                 public FullBackup getElementAt(int i)
548                 {
549                         return (FullBackup) _rnaList.getElementAt(i);
550                 }
551                 
552                 public void removeSelected()
553                 {
554                         int i = _l.getSelectedIndex();
555                         if (i!=-1)
556                         {
557                           if (_rnaList.getSize()==1)
558                           {
559                                   RNA r = new RNA();
560                                   try {
561                                         r.setRNA(" ", ".");
562                                   } catch (ExceptionUnmatchedClosingParentheses e1) {
563                                   } catch (ExceptionFileFormatOrSyntax e1) {
564                                   }
565                                   vp.showRNA(r);
566                                   vp.repaint();
567                           }
568                           else
569                           {  
570                                  int newi = i+1;
571                                  if (newi==_rnaList.getSize())
572                                  {
573                                          newi = _rnaList.getSize()-2;
574                                  }
575                                  FullBackup bck = (FullBackup) _rnaList.getElementAt(newi);
576                                  _l.setSelectedValue(bck,true);
577                           }
578                           _rnaList.remove(i);
579                         }
580
581                 }
582         }
583
584         public void onLayoutChanged() {
585                 // TODO Auto-generated method stub
586                 
587         }
588
589         public void onUINewStructure(VARNAConfig v, RNA r) {
590                 _rnaList.add(v, r,"",true);
591         }
592
593         public void onWarningEmitted(String s) {
594                 // TODO Auto-generated method stub
595                 
596         }
597
598         public void mouseClicked(MouseEvent e) {
599                            if(e.getClickCount() == 2){
600                              int index = _sideList.locationToIndex(e.getPoint());
601                              ListModel dlm = _sideList.getModel();
602                              FullBackup item = (FullBackup) dlm.getElementAt(index);;
603                              _sideList.ensureIndexIsVisible(index);
604                              Object newName = JOptionPane.showInputDialog(
605                                             this,
606                                             "Specify a new name for this RNA",
607                                             "Rename RNA", 
608                                             JOptionPane.QUESTION_MESSAGE,
609                                             (Icon)null,
610                                             null,
611                                             item.toString());
612                              if (newName!=null)
613                              {
614                                  item.name = newName.toString();
615                                  this._sideList.repaint();
616                              }
617                              }
618         }
619
620         public void mouseEntered(MouseEvent arg0) {
621                 // TODO Auto-generated method stub
622                 
623         }
624
625         public void mouseExited(MouseEvent arg0) {
626                 // TODO Auto-generated method stub
627                 
628         }
629
630         public void mousePressed(MouseEvent arg0) {
631                 // TODO Auto-generated method stub
632                 
633         }
634
635         public void mouseReleased(MouseEvent arg0) {
636                 // TODO Auto-generated method stub
637                 
638         }
639 }
640
641
642 /*
643         public static void main(String[] args)
644         {
645                 JTextField str = new JTextField("ATGC");
646                 
647                 AppVarnaBinding vab = new AppVarnaBinding();
648                 vab.varnagui.set_seq(str);
649                 vab.varnagui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
650                 vab.varnagui.pack();
651                 vab.varnagui.setVisible(true);  
652         }
653 }
654 */