JAL-3032 adds Java 8 functionality (2/2)
[jalview.git] / src2 / fr / orsay / lri / varna / controlers / ControleurDemoTextField.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.controlers;
19
20 import java.awt.Color;
21 import java.util.ArrayList;
22 import java.util.Stack;
23
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;
29
30 import fr.orsay.lri.varna.applications.VARNAOnlineDemo;
31 import fr.orsay.lri.varna.exceptions.ExceptionNonEqualLength;
32
33 /**
34  * It controls the sequence and structure text fields and changes their color if
35  * they have different lengths or unbalanced parentheses.
36  * 
37  * @author darty
38  * 
39  */
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;
47
48         public ControleurDemoTextField(VARNAOnlineDemo VOD) {
49                 _vod = 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);
55         }
56
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>();
69                         boolean pb = false;
70                         for (int i = 0; i < _vod.get_struct().getText().length(); i++) {
71                                 if (_vod.get_struct().getText().charAt(i) == '(') {
72                                         nbPO++;
73                                         p.push(i);
74                                 } else if (_vod.get_struct().getText().charAt(i) == ')') {
75                                         nbPF++;
76                                         if (p.size() == 0) {
77                                                 try {
78                                                         _hilit.addHighlight(i, i + 1, _painter);
79                                                 } catch (BadLocationException e1) {
80                                                         _vod.get_varnaPanel().errorDialog(e1);
81                                                 }
82                                                 pb = true;
83                                         } else
84                                                 p.pop();
85                                 }
86                         }
87
88                         // si le nombre de parentheses ouvrantes/fermantes est different
89                         if (pb || p.size() > 0) {
90                                 // colorie en rouge
91                                 if (pb) {
92                                         infos.add("too many closing parentheses");
93                                 }
94                                 if (p.size() > 0) {
95                                         int indice;
96                                         while (!p.isEmpty()) {
97                                                 indice = p.pop();
98                                                 try {
99                                                         _hilit.addHighlight(indice, indice + 1, _painter);
100                                                 } catch (BadLocationException e1) {
101                                                         _vod.get_varnaPanel().errorDialog(e1);
102                                                 }
103                                         }
104                                         infos.add("too many opening parentheses");
105                                 }
106                                 _vod.get_info().setForeground(COLORERROR);
107                                 _vod.get_seq().setForeground(COLORERROR);
108                                 _vod.get_struct().setForeground(COLORERROR);
109                         } else {
110                                 try {
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);
117                                 }
118                                 // verifie la longueur de la structure et de la sequence
119                                 if (_vod.get_seq().getText().length() != _vod.get_struct()
120                                                 .getText().length()) {
121                                         // colorie en orange
122                                         infos.add("different lenghts");
123                                         _vod.get_seq().setForeground(COLORWARNING);
124                                         _vod.get_struct().setForeground(COLORWARNING);
125                                 } else {
126                                         // sinon colorie en noir
127                                         _vod.get_seq().setForeground(Color.black);
128                                         _vod.get_struct().setForeground(Color.black);
129                                 }
130                         }
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);
137                                 }
138                                 info += ".";
139                         }
140                         _vod.get_info().setText(info);
141                 }
142         }
143 }