JAL-3032 adds Java 8 functionality (2/2)
[jalview.git] / src2 / fr / orsay / lri / varna / controlers / ControleurTableAnnotations.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.event.MouseEvent;
21 import java.awt.event.MouseListener;
22 import java.awt.event.MouseMotionListener;
23
24 import javax.swing.JTable;
25
26 import fr.orsay.lri.varna.VARNAPanel;
27 import fr.orsay.lri.varna.models.annotations.ChemProbAnnotation;
28 import fr.orsay.lri.varna.models.annotations.HighlightRegionAnnotation;
29 import fr.orsay.lri.varna.models.annotations.TextAnnotation;
30 import fr.orsay.lri.varna.views.VueAnnotation;
31 import fr.orsay.lri.varna.views.VueChemProbAnnotation;
32 import fr.orsay.lri.varna.views.VueHighlightRegionEdit;
33
34
35 /**
36  * Mouse action and motion listener for AnnotationTableModel
37  * 
38  * @author Darty@lri.fr
39  * 
40  */
41 public class ControleurTableAnnotations implements MouseListener,
42                 MouseMotionListener {
43         public static final int REMOVE = 0, EDIT = 1;
44         private JTable _table;
45         private VARNAPanel _vp;
46         private int _type;
47
48         /**
49          * 
50          * @param table
51          * @param vp
52          * @param type
53          *            : REMOVE = 0, EDIT = 1
54          */
55         public ControleurTableAnnotations(JTable table, VARNAPanel vp, int type) {
56                 _table = table;
57                 _vp = vp;
58                 _type = type;
59         }
60
61         public void mouseClicked(MouseEvent arg0) {
62                 switch (_type) {
63                 case EDIT:
64                         edit();
65                         break;
66                 case REMOVE:
67                         remove();
68                         break;
69                 default:
70                         break;
71                 }
72         }
73
74         /**
75          * if remove case
76          */
77         private void remove() {
78                 _vp.set_selectedAnnotation(null);
79                 Object o = _table.getValueAt(_table.getSelectedRow(), 0);
80                 if (o instanceof TextAnnotation) {
81                         if (!_vp.removeAnnotation((TextAnnotation) o))
82                                 _vp.errorDialog(new Exception("Impossible de supprimer"));
83                         _table.setValueAt("Deleted!", _table.getSelectedRow(), 0);
84                 }
85                 else if (o instanceof ChemProbAnnotation) {
86                         _vp.getRNA().removeChemProbAnnotation((ChemProbAnnotation) o);
87                         _table.setValueAt("Deleted!", _table.getSelectedRow(), 0);
88                 }
89                 else if (o instanceof HighlightRegionAnnotation) {
90                         _vp.getRNA().removeHighlightRegion((HighlightRegionAnnotation) o);
91                         _table.setValueAt("Deleted!", _table.getSelectedRow(), 0);
92                 }
93                 _vp.repaint();
94         }
95
96         /**
97          * if edit case
98          */
99         private void edit() {
100                 Object o = _table.getValueAt(_table
101                                 .getSelectedRow(), 0);
102                 if (o instanceof TextAnnotation)
103                 {
104                 TextAnnotation textAnnot = (TextAnnotation) o; 
105                 VueAnnotation vueAnnotation;
106                 vueAnnotation = new VueAnnotation(_vp, textAnnot, false);
107                 vueAnnotation.show();
108                 }else if (o instanceof HighlightRegionAnnotation)
109                 {
110                         HighlightRegionAnnotation annot = (HighlightRegionAnnotation) o; 
111                         HighlightRegionAnnotation an = annot.clone(); 
112                         VueHighlightRegionEdit vueAnnotation = new VueHighlightRegionEdit(_vp,annot);
113                         if (!vueAnnotation.show())
114                         {
115                                 annot.setBases(an.getBases());
116                                 annot.setFillColor(an.getFillColor());
117                                 annot.setOutlineColor(an.getOutlineColor());
118                                 annot.setRadius(an.getRadius());
119                         }
120                 }else if (o instanceof ChemProbAnnotation)
121                 {
122                         ChemProbAnnotation annot = (ChemProbAnnotation) o; 
123                         ChemProbAnnotation an = annot.clone(); 
124                         VueChemProbAnnotation vueAnnotation = new VueChemProbAnnotation(_vp,annot);
125                         if (!vueAnnotation.show())
126                         {
127                                 annot.setColor(an.getColor());
128                                 annot.setIntensity(an.getIntensity());
129                                 annot.setType(an.getType());
130                                 annot.setOut(an.isOut());
131                         }
132                 }
133
134         }
135
136         public void mouseEntered(MouseEvent arg0) {
137         }
138
139         public void mouseExited(MouseEvent arg0) {
140                 _vp.set_selectedAnnotation(null);
141                 _vp.repaint();
142         }
143
144         public void mousePressed(MouseEvent arg0) {
145         }
146
147         public void mouseReleased(MouseEvent arg0) {
148         }
149
150         public void mouseDragged(MouseEvent arg0) {
151         }
152
153         /**
154          * update selected annotation
155          */
156         public void mouseMoved(MouseEvent arg0) {
157                 if (_table.rowAtPoint(arg0.getPoint()) < 0)
158                         return;
159                 Object o = _table.getValueAt(_table.rowAtPoint(arg0.getPoint()), 0);
160                 if (o.getClass().equals(TextAnnotation.class)
161                                 && o != _vp.get_selectedAnnotation()) {
162                         _vp.set_selectedAnnotation((TextAnnotation) o);
163                         _vp.repaint();
164                 }
165         }
166
167 }