487fa963024f759c3400398e13b06447d21853fb
[jalview.git] / src2 / fr / orsay / lri / varna / applications / VARNAConsoleDemo.java
1 package fr.orsay.lri.varna.applications;
2
3 /*
4 VARNA is a Java library for quick automated drawings RNA secondary structure 
5 Copyright (C) 2007  Yann Ponty
6
7 This program is free software:you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program.  If not, see <http://www.gnu.org/licenses/>. 
19 */
20
21 import java.awt.BorderLayout;
22 import java.awt.Color;
23 import java.awt.Dimension;
24 import java.awt.Font;
25 import java.awt.GridLayout;
26 import java.awt.event.ActionEvent;
27 import java.awt.event.ActionListener;
28
29 import javax.swing.JApplet;
30 import javax.swing.JButton;
31 import javax.swing.JLabel;
32 import javax.swing.JPanel;
33 import javax.swing.JTextField;
34
35 import fr.orsay.lri.varna.VARNAPanel;
36 import fr.orsay.lri.varna.components.VARNAConsole;
37 import fr.orsay.lri.varna.controlers.ControleurDemoTextField;
38 import fr.orsay.lri.varna.exceptions.ExceptionNonEqualLength;
39 import fr.orsay.lri.varna.models.rna.RNA;
40
41 /**
42 * An RNA 2d Panel demo applet
43
44 * @author Yann Ponty & Darty Kévin
45
46 */
47
48 public class VARNAConsoleDemo extends JApplet implements ActionListener  {
49
50         /**
51          * 
52          */
53         private static final long serialVersionUID = -790155708306987257L;
54
55         private static final String DEFAULT_SEQUENCE = "CAGCACGACACUAGCAGUCAGUGUCAGACUGCAIACAGCACGACACUAGCAGUCAGUGUCAGACUGCAIACAGCACGACACUAGCAGUCAGUGUCAGACUGCAIA";
56
57         private static final String DEFAULT_STRUCTURE = "..(((((...(((((...(((((...(((((.....)))))...))))).....(((((...(((((.....)))))...))))).....)))))...)))))..";
58
59         private VARNAPanel _vp;
60
61         private JPanel _tools = new JPanel();
62         private JPanel _input = new JPanel();
63
64         private JPanel _seqPanel = new JPanel();
65         private JPanel _structPanel = new JPanel();
66         private JLabel _info = new JLabel();
67         private JButton _go = new JButton("Go");
68         private JTextField _struct = new JTextField();
69         private JTextField _seq = new JTextField();
70         private JLabel _structLabel = new JLabel(" Str:");
71         private JLabel _seqLabel = new JLabel(" Seq:");
72
73         private VARNAConsole _console;
74
75         private static String errorOpt = "error";
76         private boolean _error;
77
78         private Color _backgroundColor = Color.white;
79
80         private int _algoCode;
81
82         public VARNAConsoleDemo() {
83                 super();
84                 try {
85                         _vp = new VARNAPanel(_seq.getText(), _struct.getText());
86                         _vp.setErrorsOn(false);
87                 } catch (ExceptionNonEqualLength e) {
88                         _vp.errorDialog(e);
89                 }
90                 RNAPanelDemoInit();
91         }
92
93         private void RNAPanelDemoInit() {
94                 
95                 
96                 int marginTools = 40;
97
98                 setBackground(_backgroundColor);
99                 _vp.setBackground(_backgroundColor);
100
101                 try {
102                         _vp.getRNA().setRNA(_seq.getText(), _struct.getText());
103                         _vp.setErrorsOn(false);
104                 } catch (Exception e1) {
105                         _vp.errorDialog(e1);
106                 }
107
108                 Font textFieldsFont = Font.decode("MonoSpaced-PLAIN-12");
109
110                 _console = new VARNAConsole(_vp); 
111
112                 
113                 _go.addActionListener(this);
114                 
115                 _seqLabel.setHorizontalTextPosition(JLabel.LEFT);
116                 _seqLabel.setPreferredSize(new Dimension(marginTools, 15));
117                 _seq.setFont(textFieldsFont);
118                 _seq.setText(_vp.getRNA().getSeq());
119
120                 _seqPanel.setLayout(new BorderLayout());
121                 _seqPanel.add(_seqLabel, BorderLayout.WEST);
122                 _seqPanel.add(_seq, BorderLayout.CENTER);
123
124                 _structLabel.setPreferredSize(new Dimension(marginTools, 15));
125                 _structLabel.setHorizontalTextPosition(JLabel.LEFT);
126                 _struct.setFont(textFieldsFont);
127                 _struct.setText(_vp.getRNA().getStructDBN());
128                 _structPanel.setLayout(new BorderLayout());
129                 _structPanel.add(_structLabel, BorderLayout.WEST);
130                 _structPanel.add(_struct, BorderLayout.CENTER);
131
132
133                 _input.setLayout(new GridLayout(3, 0));
134                 _input.add(_seqPanel);
135                 _input.add(_structPanel);
136
137                 _tools.setLayout(new BorderLayout());
138                 _tools.add(_input, BorderLayout.CENTER);
139                 _tools.add(_info, BorderLayout.SOUTH);
140                 _tools.add(_go, BorderLayout.EAST);
141
142                 getContentPane().setLayout(new BorderLayout());
143                 getContentPane().add(_vp, BorderLayout.CENTER);
144                 getContentPane().add(_tools, BorderLayout.SOUTH);
145
146                 _vp.getVARNAUI().UIRadiate();
147                 setPreferredSize(new Dimension(400,400));
148                 
149                 setVisible(true);
150                 
151                 _console.setVisible(true);
152                 
153         }
154
155         public String[][] getParameterInfo() {
156                 String[][] info = {
157                                 // Parameter Name Kind of Value Description,
158                                 { "sequenceDBN", "String", "A raw RNA sequence" },
159                                 { "structureDBN", "String",
160                                                 "An RNA structure in dot bracket notation (DBN)" },
161                                 { errorOpt, "boolean", "To show errors" }, };
162                 return info;
163         }
164
165         public void init() {
166                 retrieveParametersValues();
167                 _vp.setBackground(_backgroundColor);
168                 _error = true;
169         }
170
171         private Color getSafeColor(String col, Color def) {
172                 Color result;
173                 try {
174                         result = Color.decode(col);
175                 } catch (Exception e) {
176                         try {
177                                 result = Color.getColor(col, def);
178                         } catch (Exception e2) {
179                                 return def;
180                         }
181                 }
182                 return result;
183         }
184
185         private String getParameterValue(String key, String def) {
186                 String tmp;
187                 tmp = getParameter(key);
188                 if (tmp == null) {
189                         return def;
190                 } else {
191                         return tmp;
192                 }
193         }
194
195         private void retrieveParametersValues() {
196                 _error = Boolean.parseBoolean(getParameterValue(errorOpt, "false"));
197                 _vp.setErrorsOn(_error);
198                 _backgroundColor = getSafeColor(getParameterValue("background",
199                                 _backgroundColor.toString()), _backgroundColor);
200                 _vp.setBackground(_backgroundColor);
201                 _seq.setText(getParameterValue("sequenceDBN", ""));
202                 _struct.setText(getParameterValue("structureDBN", ""));
203                 String _algo = getParameterValue("algorithm", "radiate");
204                 if (_algo.equals("circular"))
205                         _algoCode = RNA.DRAW_MODE_CIRCULAR;
206                 else if (_algo.equals("naview"))
207                         _algoCode = RNA.DRAW_MODE_NAVIEW;
208                 else if (_algo.equals("line"))
209                         _algoCode = RNA.DRAW_MODE_LINEAR;
210                 else
211                         _algoCode = RNA.DRAW_MODE_RADIATE;
212                 if (_seq.getText().equals("") && _struct.getText().equals("")) {
213                         _seq.setText(DEFAULT_SEQUENCE);
214                         _struct.setText(DEFAULT_STRUCTURE);
215                 }
216                 try {
217                         _vp.drawRNA(_seq.getText(), _struct.getText(), _algoCode);
218                 } catch (ExceptionNonEqualLength e) {
219                         e.printStackTrace();
220                 }
221
222         }
223
224         public VARNAPanel get_varnaPanel() {
225                 return _vp;
226         }
227
228         public void set_varnaPanel(VARNAPanel surface) {
229                 _vp = surface;
230         }
231
232         public JTextField get_struct() {
233                 return _struct;
234         }
235
236         public void set_struct(JTextField _struct) {
237                 this._struct = _struct;
238         }
239
240         public JTextField get_seq() {
241                 return _seq;
242         }
243
244         public void set_seq(JTextField _seq) {
245                 this._seq = _seq;
246         }
247
248         public JLabel get_info() {
249                 return _info;
250         }
251
252         public void set_info(JLabel _info) {
253                 this._info = _info;
254         }
255
256         public void actionPerformed(ActionEvent arg0) {
257                   RNA r = new RNA();
258                   try {
259                         _vp.drawRNAInterpolated(_seq.getText(), _struct.getText());
260                 } catch (ExceptionNonEqualLength e) {
261                         // TODO Auto-generated catch block
262                         e.printStackTrace();
263                 }
264                   _vp.repaint();
265         }
266 }