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
5 * This file is part of Jalview.
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.
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.
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
20 import java.awt.BorderLayout;
21 import java.awt.Color;
22 import java.awt.Component;
23 import java.awt.Dimension;
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;
39 import java.text.DateFormat;
40 import java.util.ArrayList;
41 import java.util.Date;
42 import java.util.List;
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;
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;
75 public class AppVarnaBinding extends JFrame implements DropTargetListener, InterfaceVARNAListener, MouseListener {
80 //private static final long serialVersionUID = -790155708306987257L;
82 private String DEFAULT_SEQUENCE = "CAGCACGACACUAGCAGUCAGUGUCAGACUGCAIACAGCACGACACUAGCAGUCAGUGUCAGACUGCAIACAGCACGACACUAGCAGUCAGUGUCAGACUGCAIA";
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 // "((((..(((....)))..))))";
92 private JPanel _tools = new JPanel();
93 private JPanel _input = new JPanel();
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");
106 private JPanel _listPanel = new JPanel();
107 private ReorderableJList _sideList = null;
110 private static String errorOpt = "error";
111 @SuppressWarnings("unused")
112 private boolean _error;
114 private Color _backgroundColor = Color.white;
116 private static int _nextID = 1;
117 @SuppressWarnings("unused")
118 private int _algoCode;
120 private BackupHolder _rnaList;
123 public AppVarnaBinding() {
124 super("VARNA in Jalview");
125 //this.set_seq("ATGC");
126 //this.set_str(".().");
127 //RNAPanelDemoInit();
129 //initVarna("ATGCATGATATATATATAT","....((((...))))....");
130 initVarna(this.DEFAULT_SEQUENCE,this.DEFAULT_STRUCTURE1);
133 public AppVarnaBinding(String seq, String struc){
134 super("VARNA in Jalview");
135 initVarna(seq,struc);
140 private void initVarna(String seq, String str){
141 DefaultListModel dlm = new DefaultListModel();
143 DefaultListSelectionModel m = new DefaultListSelectionModel();
144 m.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
145 m.setLeadAnchorNotificationEnabled(false);
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())
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());
165 _rnaList = new BackupHolder(dlm,_sideList);
166 RNA _RNA1 = new RNA("User defined 1");
169 vp = new VARNAPanel("0",".");
170 _RNA1.setRNA(seq, str);
171 _RNA1.drawRNARadiate(vp.getConfig());
172 } catch (ExceptionNonEqualLength e) {
174 } catch (ExceptionUnmatchedClosingParentheses e2) {
175 e2.printStackTrace();
176 } catch (ExceptionFileFormatOrSyntax e3) {
177 e3.printStackTrace();
179 vp.setPreferredSize(new Dimension(400, 400));
180 _rnaList.add(vp.getConfig().clone(),_RNA1,generateDefaultName(),true);
182 setBackground(_backgroundColor);
183 vp.setBackground(_backgroundColor);
185 getContentPane().setLayout(new BorderLayout());
186 getContentPane().add(vp, BorderLayout.CENTER);
189 vp.addVARNAListener(this);
192 private void RNAPanelDemoInit()
194 DefaultListModel dlm = new DefaultListModel();
197 int marginTools = 40;
199 DefaultListSelectionModel m = new DefaultListSelectionModel();
200 m.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
201 m.setLeadAnchorNotificationEnabled(false);
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())
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());
223 _rnaList = new BackupHolder(dlm,_sideList);
224 RNA _RNA1 = new RNA("User defined 1");
225 RNA _RNA2 = new RNA("User defined 2");
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) {
234 } catch (ExceptionUnmatchedClosingParentheses e2) {
235 e2.printStackTrace();
236 } catch (ExceptionFileFormatOrSyntax e3) {
237 e3.printStackTrace();
239 vp.setPreferredSize(new Dimension(400, 400));
240 _rnaList.add(vp.getConfig().clone(),_RNA2,generateDefaultName());
241 _rnaList.add(vp.getConfig().clone(),_RNA1,generateDefaultName(),true);
243 JScrollPane listScroller = new JScrollPane(_sideList);
244 listScroller.setPreferredSize(new Dimension(150, 0));
246 setBackground(_backgroundColor);
247 vp.setBackground(_backgroundColor);
250 Font textFieldsFont = Font.decode("MonoSpaced-PLAIN-12");
252 _seqLabel.setHorizontalTextPosition(JLabel.LEFT);
253 _seqLabel.setPreferredSize(new Dimension(marginTools, 15));
254 _seq.setFont(textFieldsFont);
255 _seq.setText(DEFAULT_SEQUENCE);
257 _createButton.addActionListener(new ActionListener() {
258 public void actionPerformed(ActionEvent e) {
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);
273 _seqPanel.setLayout(new BorderLayout());
274 _seqPanel.add(_seqLabel, BorderLayout.WEST);
275 _seqPanel.add(_seq, BorderLayout.CENTER);
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);
284 _input.setLayout(new GridLayout(2, 0));
285 _input.add(_seqPanel);
286 _input.add(_strPanel);
288 JPanel goPanel = new JPanel();
289 goPanel.setLayout(new BorderLayout());
291 _tools.setLayout(new BorderLayout());
292 _tools.add(_input, BorderLayout.CENTER);
293 _tools.add(_info, BorderLayout.SOUTH);
294 _tools.add(goPanel, BorderLayout.EAST);
296 _deleteButton.addActionListener(new ActionListener() {
297 public void actionPerformed(ActionEvent e) {
298 _rnaList.removeSelected();
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);
306 JPanel ops = new JPanel();
307 ops.setLayout(new GridLayout(1,2));
308 ops.add(_deleteButton);
309 ops.add(_duplicateButton);
311 JLabel j = new JLabel("Structures Manager",JLabel.CENTER);
312 _listPanel.setLayout(new BorderLayout());
314 _listPanel.add(ops,BorderLayout.SOUTH);
315 _listPanel.add(j,BorderLayout.NORTH);
316 _listPanel.add(listScroller,BorderLayout.CENTER);
318 goPanel.add(_createButton, BorderLayout.CENTER);
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);
326 DropTarget dt = new DropTarget(vp, this);
328 vp.addVARNAListener(this);
331 public static String generateDefaultName()
333 return "User file #"+_nextID++;
336 public RNA getRNA() {
337 return (RNA)_sideList.getSelectedValue();
342 public String[][] getParameterInfo() {
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" }, };
353 vp.setBackground(_backgroundColor);
357 @SuppressWarnings("unused")
358 private Color getSafeColor(String col, Color def) {
361 result = Color.decode(col);
362 } catch (Exception e) {
364 result = Color.getColor(col, def);
365 } catch (Exception e2) {
372 public VARNAPanel get_varnaPanel() {
376 public void set_varnaPanel(VARNAPanel surface) {
381 public String get_seq() {
382 return _seq.getText();
385 public void set_seq(String _seq) {
386 this._seq.setText(_seq);
389 public String get_str(){
390 return _str.getText();
393 public void set_str(String _str){
394 this._str.setText(_str);
397 public JLabel get_info() {
401 public void set_info(JLabel _info) {
405 public static void main(String[] args) {
406 AppVarnaBinding d = new AppVarnaBinding();
407 d.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
413 public void dragEnter(DropTargetDragEvent arg0) {
414 // TODO Auto-generated method stub
418 public void dragExit(DropTargetEvent arg0) {
419 // TODO Auto-generated method stub
423 public void dragOver(DropTargetDragEvent arg0) {
424 // TODO Auto-generated method stub
428 public void drop(DropTargetDropEvent dtde) {
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)
438 List list = (List) ob;
439 for (int j = 0; j < list.size(); j++) {
440 Object o = list.get(j);
442 if (dtde.getSource() instanceof DropTarget)
444 DropTarget dt = (DropTarget) dtde.getSource();
445 Component c = dt.getComponent();
446 if (c instanceof VARNAPanel)
448 String path = o.toString();
449 VARNAPanel vp = (VARNAPanel) c;
451 FullBackup bck = VARNAPanel.importSession(path);
452 _rnaList.add(bck.config, bck.rna,bck.name,true);
454 catch (ExceptionLoadingFailed e3)
458 r.drawRNA(vp.getConfig());
459 String name =r.getName();
462 name = path.substring(path.lastIndexOf(File.separatorChar)+1);
464 _rnaList.add(vp.getConfig().clone(),r,name,true);
470 // If we made it this far, everything worked.
471 dtde.dropComplete(true);
475 // Hmm, the user must not have dropped a file list
477 } catch (Exception e) {
484 public void dropActionChanged(DropTargetDragEvent arg0) {
487 private class BackupHolder{
488 private DefaultListModel _rnaList;
489 private ArrayList<RNA> _rnas = new ArrayList<RNA>();
492 public BackupHolder(DefaultListModel rnaList, JList l)
498 public void add(VARNAConfig c, RNA r)
500 add(c, r, r.getName(),false);
503 public void add(VARNAConfig c, RNA r,boolean select)
505 add(c, r, r.getName(),select);
508 public void add(VARNAConfig c, RNA r, String name)
510 add(c, r, name,false);
512 public void add(VARNAConfig c, RNA r, String name, boolean select)
515 _l.removeSelectionInterval(0, _rnaList.size());
519 name = generateDefaultName();
521 FullBackup bck = new FullBackup(c,r,name);
525 _l.setSelectedIndex(0);
529 public void remove(int i)
535 public DefaultListModel getModel()
539 public boolean contains(RNA r)
541 return _rnas.contains(r);
543 /*public int getSize()
545 return _rnaList.getSize();
547 public FullBackup getElementAt(int i)
549 return (FullBackup) _rnaList.getElementAt(i);
552 public void removeSelected()
554 int i = _l.getSelectedIndex();
557 if (_rnaList.getSize()==1)
562 } catch (ExceptionUnmatchedClosingParentheses e1) {
563 } catch (ExceptionFileFormatOrSyntax e1) {
571 if (newi==_rnaList.getSize())
573 newi = _rnaList.getSize()-2;
575 FullBackup bck = (FullBackup) _rnaList.getElementAt(newi);
576 _l.setSelectedValue(bck,true);
584 public void onLayoutChanged() {
585 // TODO Auto-generated method stub
589 public void onUINewStructure(VARNAConfig v, RNA r) {
590 _rnaList.add(v, r,"",true);
593 public void onWarningEmitted(String s) {
594 // TODO Auto-generated method stub
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(
606 "Specify a new name for this RNA",
608 JOptionPane.QUESTION_MESSAGE,
614 item.name = newName.toString();
615 this._sideList.repaint();
620 public void mouseEntered(MouseEvent arg0) {
621 // TODO Auto-generated method stub
625 public void mouseExited(MouseEvent arg0) {
626 // TODO Auto-generated method stub
630 public void mousePressed(MouseEvent arg0) {
631 // TODO Auto-generated method stub
635 public void mouseReleased(MouseEvent arg0) {
636 // TODO Auto-generated method stub
643 public static void main(String[] args)
645 JTextField str = new JTextField("ATGC");
647 AppVarnaBinding vab = new AppVarnaBinding();
648 vab.varnagui.set_seq(str);
649 vab.varnagui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
651 vab.varnagui.setVisible(true);