Java 11 integration;
[jalview.git] / src2 / fr / orsay / lri / varna / views / VueBases.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.views;
19
20 import java.awt.Color;
21 import java.awt.Dimension;
22 import java.awt.GridLayout;
23 import java.util.ArrayList;
24 import java.util.Hashtable;
25
26 import javax.swing.JComponent;
27 import javax.swing.JOptionPane;
28 import javax.swing.JPanel;
29 import javax.swing.JScrollPane;
30 import javax.swing.JTable;
31 import javax.swing.event.TableModelEvent;
32 import javax.swing.event.TableModelListener;
33
34
35 import fr.orsay.lri.varna.VARNAPanel;
36 import fr.orsay.lri.varna.components.BaseSpecialColorEditor;
37 import fr.orsay.lri.varna.components.BaseTableModel;
38 import fr.orsay.lri.varna.components.ColorRenderer;
39 import fr.orsay.lri.varna.models.BaseList;
40 import fr.orsay.lri.varna.models.rna.ModeleBase;
41 import fr.orsay.lri.varna.models.rna.ModeleBaseNucleotide;
42
43 public class VueBases extends JPanel {
44         /**
45          * 
46          */
47         private static final long serialVersionUID = 1L;
48
49         /**
50          * for bases by kind
51          */
52         public final static int KIND_MODE = 1;
53         /**
54          * for all bases 
55          */
56         public final static int ALL_MODE = 2;
57         /**
58          * for base pairs by king
59          */
60         public final static int COUPLE_MODE = 3;
61
62         private int _mode;
63
64         private VARNAPanel _vp;
65
66         private ArrayList<BaseList> data = new ArrayList<BaseList>();
67         
68         private Hashtable<String,BaseList> revdata = new Hashtable<String,BaseList>();
69         
70         private JTable table;
71
72         private BaseTableModel specialTableModel;
73
74         public VueBases(VARNAPanel vp, int mode) {
75                 super(new GridLayout(1, 0));
76                 _vp = vp;
77                 switch (mode) {
78                 case (KIND_MODE):
79                         _mode = KIND_MODE;
80                         kindMode();
81                         break;
82                 case (ALL_MODE):
83                         _mode = ALL_MODE;
84                         allMode();
85                         break;
86                 case (COUPLE_MODE):
87                         _mode = COUPLE_MODE;
88                         coupleMode();
89                         break;
90                 default:
91                         break;
92                 }
93         }
94         
95         private BaseList locateOrAddList(String caption)
96         {
97           if (!revdata.containsKey(caption))
98           {
99                   BaseList mbl = new BaseList(caption);
100                   revdata.put(caption,mbl);
101                   data.add(mbl);
102           }
103           return revdata.get(caption);
104         }
105
106         private void coupleMode() {
107                 String pairString;
108                 for (int i = 0; i < _vp.getRNA().get_listeBases().size(); i++) {
109                         
110                         int j = _vp.getRNA().get_listeBases().get(i).getElementStructure();
111                         if (j > i) {
112                                 String tmp1 = (_vp.getRNA().get_listeBases().get(i).getContent()); 
113                                 String tmp2 = (_vp.getRNA().get_listeBases().get(j).getContent()); 
114                                 pairString = tmp1 +"-"+ tmp2;
115                                 BaseList bl = locateOrAddList(pairString);
116                                 bl.addBase(_vp.getRNA().get_listeBases().get(i));
117                                 bl.addBase(_vp.getRNA().get_listeBases().get(j));
118                         }
119                 }
120                 createView();
121         }
122
123         private void allMode() {
124                 for (int i = 0; i < _vp.getRNA().get_listeBases().size(); i++) {
125                         ModeleBase mb = _vp.getRNA().get_listeBases().get(i);
126                         BaseList bl = locateOrAddList(""+i);
127                         bl.addBase(mb);
128                 }
129                 createView();
130         }
131
132         private void kindMode() {
133                 for (int i = 0; i < _vp.getRNA().get_listeBases().size(); i++) {
134                         ModeleBase mb = _vp.getRNA().get_listeBases().get(i);
135                         String tmp1 = (mb.getContent()); 
136                         BaseList bl = locateOrAddList(tmp1);
137                         bl.addBase(mb);
138                 }
139                 createView();
140         }
141
142         private void createView() {
143                 specialTableModel = new BaseTableModel(data);
144                 table = new JTable(specialTableModel);
145                 table.setPreferredScrollableViewportSize(new Dimension(500, 300));
146                 // TODO: Find equivalent in JRE 1.5
147                 //table.setFillsViewportHeight(true);
148                 // Create the scroll pane and add the table to it.
149                 JScrollPane scrollPane = new JScrollPane(table);
150
151                 // Set up renderer and editor for the Favorite Color column.
152                 table.setDefaultRenderer(Color.class, new ColorRenderer(true));
153                 table.setDefaultEditor(Color.class, new BaseSpecialColorEditor(this));
154                 specialTableModel.addTableModelListener(new TableModelListener(){
155
156                         public void tableChanged(TableModelEvent e) {
157                                 _vp.repaint();
158                         }
159                         
160                 });
161
162                 // Add the scroll pane to this panel.
163                 add(scrollPane);
164
165                 UIvueBases();
166         }
167
168         /**
169          * Create the GUI and show it. For thread safety, this method should be
170          * invoked from the event-dispatching thread.
171          */
172         public void UIvueBases() {
173                 // Create and set up the content pane.
174                 JComponent newContentPane = this;
175                 newContentPane.setOpaque(true); // content panes must be opaque
176
177                 JOptionPane.showMessageDialog(_vp, newContentPane,
178                                 "Base Colors Edition", JOptionPane.PLAIN_MESSAGE);
179
180         }
181
182         public int getMode() {
183                 return _mode;
184         }
185
186         public BaseList getDataAt(int i) {
187                 return data.get(i);
188         }
189
190         
191         public ArrayList<BaseList> getData() {
192                 return data;
193         }
194
195         public VARNAPanel get_vp() {
196                 return _vp;
197         }
198
199         public JTable getTable() {
200                 return table;
201         }
202
203         public void setTable(JTable table) {
204                 this.table = table;
205         }
206
207         public BaseTableModel getSpecialTableModel() {
208                 return specialTableModel;
209         }
210
211         public void setSpecialTableModel(BaseTableModel specialTableModel) {
212                 this.specialTableModel = specialTableModel;
213         }
214         
215 }