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
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.
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.
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.
18 package fr.orsay.lri.varna.controlers;
20 import java.awt.Color;
21 import java.util.ArrayList;
22 import java.util.Stack;
24 import javax.swing.event.CaretEvent;
25 import javax.swing.event.CaretListener;
26 import javax.swing.text.BadLocationException;
27 import javax.swing.text.DefaultHighlighter;
28 import javax.swing.text.Highlighter;
30 import fr.orsay.lri.varna.applications.VARNAOnlineDemo;
31 import fr.orsay.lri.varna.exceptions.ExceptionNonEqualLength;
34 * It controls the sequence and structure text fields and changes their color if
35 * they have different lengths or unbalanced parentheses.
40 public class ControleurDemoTextField implements CaretListener {
41 private VARNAOnlineDemo _vod;
42 private String _oldSeq, _oldStruct;
43 private Highlighter _hilit;
44 private Highlighter.HighlightPainter _painter;
45 private final Color COLORERROR = Color.RED;
46 private final Color COLORWARNING = Color.ORANGE;
48 public ControleurDemoTextField(VARNAOnlineDemo VOD) {
50 _oldSeq = _vod.get_seq().getText();
51 _oldStruct = _vod.get_struct().getText();
52 _hilit = new DefaultHighlighter();
53 _painter = new DefaultHighlighter.DefaultHighlightPainter(Color.BLACK);
54 _vod.get_struct().setHighlighter(_hilit);
57 // if there is any change
58 public void caretUpdate(CaretEvent e) {
59 if (_oldStruct != _vod.get_struct().getText()
60 || _oldSeq != _vod.get_seq().getText()) {
61 ArrayList<String> infos = new ArrayList<String>();
62 _vod.get_info().removeAll();
63 _hilit.removeAllHighlights();
64 _oldStruct = _vod.get_struct().getText();
65 _oldSeq = _vod.get_seq().getText();
66 int nbPO = 0, nbPF = 0;
67 // compte les parentheses ouvrantes et fermantes
68 Stack<Integer> p = new Stack<Integer>();
70 for (int i = 0; i < _vod.get_struct().getText().length(); i++) {
71 if (_vod.get_struct().getText().charAt(i) == '(') {
74 } else if (_vod.get_struct().getText().charAt(i) == ')') {
78 _hilit.addHighlight(i, i + 1, _painter);
79 } catch (BadLocationException e1) {
80 _vod.get_varnaPanel().errorDialog(e1);
88 // si le nombre de parentheses ouvrantes/fermantes est different
89 if (pb || p.size() > 0) {
92 infos.add("too many closing parentheses");
96 while (!p.isEmpty()) {
99 _hilit.addHighlight(indice, indice + 1, _painter);
100 } catch (BadLocationException e1) {
101 _vod.get_varnaPanel().errorDialog(e1);
104 infos.add("too many opening parentheses");
106 _vod.get_info().setForeground(COLORERROR);
107 _vod.get_seq().setForeground(COLORERROR);
108 _vod.get_struct().setForeground(COLORERROR);
111 // redraw the new RNA
112 _vod.get_varnaPanel().drawRNA(_vod.get_seq().getText(),
113 _vod.get_struct().getText(),
114 _vod.get_varnaPanel().getRNA().get_drawMode());
115 } catch (ExceptionNonEqualLength e1) {
116 _vod.get_varnaPanel().errorDialog(e1);
118 // verifie la longueur de la structure et de la sequence
119 if (_vod.get_seq().getText().length() != _vod.get_struct()
120 .getText().length()) {
122 infos.add("different lenghts");
123 _vod.get_seq().setForeground(COLORWARNING);
124 _vod.get_struct().setForeground(COLORWARNING);
126 // sinon colorie en noir
127 _vod.get_seq().setForeground(Color.black);
128 _vod.get_struct().setForeground(Color.black);
131 _vod.get_varnaPanel().getVARNAUI().UIReset();
132 String info = new String();
133 if (infos.size() != 0) {
134 info += infos.get(0);
135 for (int i = 1; i < infos.size(); i++) {
136 info += ", " + infos.get(i);
140 _vod.get_info().setText(info);