JAL-3032 adds Java 8 functionality (2/2)
[jalview.git] / src2 / fr / orsay / lri / varna / applications / VARNAOnlineDemo.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 /*
21  VARNA is a Java library for quick automated drawings RNA secondary structure 
22  Copyright (C) 2007  Yann Ponty
23
24  This program is free software:you can redistribute it and/or modify
25  it under the terms of the GNU General Public License as published by
26  the Free Software Foundation, either version 3 of the License, or
27  (at your option) any later version.
28
29  This program is distributed in the hope that it will be useful,
30  but WITHOUT ANY WARRANTY; without even the implied warranty of
31  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
32  GNU General Public License for more details.
33
34  You should have received a copy of the GNU General Public License
35  along with this program.  If not, see <http://www.gnu.org/licenses/>. 
36  */
37
38 import java.awt.BorderLayout;
39 import java.awt.Color;
40 import java.awt.Dimension;
41 import java.awt.Font;
42 import java.awt.GridLayout;
43
44 import javax.swing.JApplet;
45 import javax.swing.JLabel;
46 import javax.swing.JPanel;
47 import javax.swing.JTextField;
48
49 import fr.orsay.lri.varna.VARNAPanel;
50 import fr.orsay.lri.varna.controlers.ControleurDemoTextField;
51 import fr.orsay.lri.varna.exceptions.ExceptionNonEqualLength;
52 import fr.orsay.lri.varna.models.rna.RNA;
53
54 /**
55  * An RNA 2d Panel demo applet
56  * 
57  * @author Yann Ponty & Darty Kévin
58  * 
59  */
60
61 public class VARNAOnlineDemo extends JApplet {
62
63         /**
64          * 
65          */
66         private static final long serialVersionUID = -790155708306987257L;
67
68         private static final String DEFAULT_SEQUENCE = "CAGCACGACACUAGCAGUCAGUGUCAGACUGCAIACAGCACGACACUAGCAGUCAGUGUCAGACUGCAIACAGCACGACACUAGCAGUCAGUGUCAGACUGCAIA";
69
70         private static final String DEFAULT_STRUCTURE = "..(((((...(((((...(((((...(((((.....)))))...))))).....(((((...(((((.....)))))...))))).....)))))...)))))..";
71
72         private VARNAPanel _vp;
73
74         private JPanel _tools = new JPanel();
75         private JPanel _input = new JPanel();
76
77         private JPanel _seqPanel = new JPanel();
78         private JPanel _structPanel = new JPanel();
79         private JLabel _info = new JLabel();
80         private JTextField _struct = new JTextField();
81         private JTextField _seq = new JTextField();
82         private JLabel _structLabel = new JLabel(" Str:");
83         private JLabel _seqLabel = new JLabel(" Seq:");
84
85         private static String errorOpt = "error";
86         private boolean _error;
87
88         private Color _backgroundColor = Color.white;
89
90         private int _algoCode;
91
92         public VARNAOnlineDemo() {
93                 super();
94                 try {
95                         _vp = new VARNAPanel(_seq.getText(), _struct.getText());
96                         _vp.setErrorsOn(false);
97                 } catch (ExceptionNonEqualLength e) {
98                         _vp.errorDialog(e);
99                 }
100                 RNAPanelDemoInit();
101         }
102
103         private void RNAPanelDemoInit() {
104                 int marginTools = 40;
105
106                 setBackground(_backgroundColor);
107                 _vp.setBackground(_backgroundColor);
108
109                 try {
110                         _vp.getRNA().setRNA(_seq.getText(), _struct.getText());
111                         _vp.setErrorsOn(false);
112                 } catch (Exception e1) {
113                         _vp.errorDialog(e1);
114                 }
115
116                 Font textFieldsFont = Font.decode("MonoSpaced-PLAIN-12");
117
118                 _seqLabel.setHorizontalTextPosition(JLabel.LEFT);
119                 _seqLabel.setPreferredSize(new Dimension(marginTools, 15));
120                 _seq.setFont(textFieldsFont);
121                 _seq.setText(_vp.getRNA().getSeq());
122
123                 _seqPanel.setLayout(new BorderLayout());
124                 _seqPanel.add(_seqLabel, BorderLayout.WEST);
125                 _seqPanel.add(_seq, BorderLayout.CENTER);
126
127                 _structLabel.setPreferredSize(new Dimension(marginTools, 15));
128                 _structLabel.setHorizontalTextPosition(JLabel.LEFT);
129                 _struct.setFont(textFieldsFont);
130                 _struct.setText(_vp.getRNA().getStructDBN());
131                 _structPanel.setLayout(new BorderLayout());
132                 _structPanel.add(_structLabel, BorderLayout.WEST);
133                 _structPanel.add(_struct, BorderLayout.CENTER);
134
135                 ControleurDemoTextField controleurTextField = new ControleurDemoTextField(this);
136                 _seq.addCaretListener(controleurTextField);
137                 _struct.addCaretListener(controleurTextField);
138
139                 _input.setLayout(new GridLayout(3, 0));
140                 _input.add(_seqPanel);
141                 _input.add(_structPanel);
142
143                 _tools.setLayout(new BorderLayout());
144                 _tools.add(_input, BorderLayout.CENTER);
145                 _tools.add(_info, BorderLayout.SOUTH);
146
147                 getContentPane().setLayout(new BorderLayout());
148                 getContentPane().add(_vp, BorderLayout.CENTER);
149                 getContentPane().add(_tools, BorderLayout.SOUTH);
150
151                 setVisible(true);
152                 _vp.getVARNAUI().UIRadiate();
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 }