JAL-3026 srcjar files for VARNA and log4j
[jalview.git] / srcjar / fr / orsay / lri / varna / views / VueListeAnnotations.java
1 /*
2  * VARNA is a tool for the automated drawing, visualization and annotation
3  * of the secondary structure of RNA, designed as a companion software for
4  * web servers and databases. Copyright (C) 2008 Kevin Darty, Alain Denise
5  * and Yann Ponty. electronic mail : Yann.Ponty@lri.fr paper mail : LRI, bat
6  * 490 Université Paris-Sud 91405 Orsay Cedex France
7  * 
8  * This file is part of VARNA version 3.1. VARNA version 3.1 is free
9  * software: you can redistribute it and/or modify it under the terms of the
10  * GNU General Public License as published by the Free Software Foundation,
11  * either version 3 of the License, or (at your option) any later version.
12  * 
13  * VARNA version 3.1 is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
16  * Public License for more details.
17  * 
18  * You should have received a copy of the GNU General Public License along
19  * with VARNA version 3.1. If not, see http://www.gnu.org/licenses.
20  */
21 package fr.orsay.lri.varna.views;
22
23 import java.awt.BorderLayout;
24 import java.awt.Dimension;
25 import java.awt.GridLayout;
26 import java.awt.event.ActionEvent;
27 import java.awt.event.ActionListener;
28 import java.io.BufferedInputStream;
29 import java.io.BufferedReader;
30 import java.io.File;
31 import java.io.FileInputStream;
32 import java.io.FileNotFoundException;
33 import java.io.FileReader;
34 import java.io.IOException;
35 import java.io.PrintWriter;
36 import java.util.ArrayList;
37
38 import javax.swing.Box;
39 import javax.swing.BoxLayout;
40 import javax.swing.JButton;
41 import javax.swing.JComponent;
42 import javax.swing.JFileChooser;
43 import javax.swing.JOptionPane;
44 import javax.swing.JPanel;
45 import javax.swing.JScrollPane;
46 import javax.swing.JTable;
47 import javax.swing.filechooser.FileFilter;
48
49 import fr.orsay.lri.varna.VARNAPanel;
50 import fr.orsay.lri.varna.components.AnnotationTableModel;
51 import fr.orsay.lri.varna.controlers.ControleurTableAnnotations;
52 import fr.orsay.lri.varna.models.annotations.TextAnnotation;
53 import fr.orsay.lri.varna.models.rna.ModeleColorMap;
54
55 /**
56  * a view for all annoted texts on the VARNAPanel
57  * 
58  * @author Darty@lri.fr
59  * 
60  */
61 public class VueListeAnnotations extends JPanel {
62         /**
63          * 
64          */
65         private static final long serialVersionUID = 1L;
66
67         /**
68          * if this view is for removing annoted texts
69          */
70         public static final int REMOVE = 0;
71         /**
72          * if this view is for editing annoted texts
73          */
74         public static final int EDIT = 1;
75
76         private VARNAPanel _vp;
77         private ArrayList<Object> data;
78         private JTable table;
79         private int type;
80         private AnnotationTableModel specialTableModel;
81         // BH SwingJS - this is never used in JavaScript
82         private static JFileChooser fc = new JFileChooser(){
83             public void approveSelection(){
84                 File f = getSelectedFile();
85                 if(f.exists() && getDialogType() == SAVE_DIALOG){
86                     int result = JOptionPane.showConfirmDialog(this,"The file exists, overwrite?","Existing file",JOptionPane.YES_NO_OPTION);
87                     switch(result){
88                         case JOptionPane.YES_OPTION:
89                             super.approveSelection();
90                             return;
91                         case JOptionPane.NO_OPTION:
92                             return;
93                         case JOptionPane.CLOSED_OPTION:
94                             return;
95                         case JOptionPane.CANCEL_OPTION:
96                             cancelSelection();
97                             return;
98                     }
99                 }
100                 super.approveSelection();
101             }        
102         };
103
104
105         /**
106          * creates the view
107          * 
108          * @param vp
109          * @param type
110          *            (REMOVE or EDIT)
111          */
112         public VueListeAnnotations(VARNAPanel vp, int type) {
113                 super(new BorderLayout());
114                 this.type = type;
115                 _vp = vp;
116                 data = new ArrayList<Object>();
117                 data.addAll(_vp.getListeAnnotations());
118                 data.addAll(_vp.getRNA().getHighlightRegion());
119                 data.addAll(_vp.getRNA().getChemProbAnnotations());
120                 createView();
121         }
122
123         private void createView() {
124                 specialTableModel = new AnnotationTableModel(data);
125                 table = new JTable(specialTableModel);
126                 ControleurTableAnnotations ctrl = new ControleurTableAnnotations(table,
127                                 _vp, type);
128                 table.addMouseListener(ctrl);
129                 table.addMouseMotionListener(ctrl);
130                 // table.setPreferredScrollableViewportSize(new Dimension(500, 100));
131                 // TODO: Find equivalent in JRE 1.5
132                 // table.setFillsViewportHeight(true);
133                 // Create the scroll pane and add the table to it.
134                 JScrollPane scrollPane = new JScrollPane(table);
135
136                 add(scrollPane, BorderLayout.CENTER);
137                 
138                 FileFilter CPAFiles = new FileFilter(){
139                         public boolean accept(File f) {
140                                 return f.getName().toLowerCase().endsWith(".cpa") || f.isDirectory();
141                         }
142
143                         public String getDescription() {
144                                 return "Chemical Probing Annotations (*.cpa) Files";
145                         }
146                         
147                 };
148                 fc.addChoosableFileFilter(CPAFiles);
149                 fc.setFileFilter(CPAFiles);
150
151                 
152                 JButton loadStyleButton = new JButton("Load");
153                 loadStyleButton.addActionListener(new ActionListener(){
154                         public void actionPerformed(ActionEvent e) {
155                                 if (fc.showOpenDialog(VueListeAnnotations.this)==JFileChooser.APPROVE_OPTION)
156                                 {
157                                         File file = fc.getSelectedFile();
158                                         try {
159                                                 BufferedReader br = new BufferedReader(new FileReader(file));
160                                                 String s = br.readLine();
161                                                 while(s != null)
162                                                 {
163                                                         if (s.startsWith(TextAnnotation.HEADER_TEXT))
164                                                         s = br.readLine();
165                                                 }
166                                                 // TODO
167                                         } catch (FileNotFoundException e1) {
168                                                 e1.printStackTrace();
169                                         } catch (IOException e1) {
170                                                 e1.printStackTrace();
171                                         }
172                                 }
173                         }
174                         
175                 });
176                 JButton saveStyleButton = new JButton("Save");
177                 saveStyleButton.addActionListener(new ActionListener(){
178                         public void actionPerformed(ActionEvent e) {
179                                 if (fc.showSaveDialog(VueListeAnnotations.this)==JFileChooser.APPROVE_OPTION)
180                                 {
181                                         try {
182                                                 PrintWriter out = new PrintWriter(fc.getSelectedFile());
183                                                 // TODO out.println(_gp.getColorMap().getParamEncoding());
184                                                 out.close();
185                                         } catch (FileNotFoundException e1) {
186                                                 e1.printStackTrace();
187                                         } catch (IOException e1) {
188                                                 e1.printStackTrace();
189                                         }
190                                 }
191                         }
192                         
193                 });
194                 saveStyleButton.setAlignmentX(CENTER_ALIGNMENT);
195                 loadStyleButton.setAlignmentX(CENTER_ALIGNMENT);
196
197                 JPanel jp2 = new JPanel();
198                 BoxLayout bl = new BoxLayout(jp2, BoxLayout.X_AXIS);
199                 jp2.setLayout(bl);
200                 jp2.setAlignmentX(CENTER_ALIGNMENT);
201                 jp2.add(loadStyleButton);
202                 jp2.add(Box.createRigidArea(new Dimension(5,0)));
203                 jp2.add(saveStyleButton);
204                 this.add(jp2,BorderLayout.SOUTH);
205
206                 
207
208                 UIvueListeAnnotations();
209         }
210
211         /**
212          * Create the GUI and show it. For thread safety, this method should be
213          * invoked from the event-dispatching thread.
214          */
215         public void UIvueListeAnnotations() {
216                 JComponent newContentPane = this;
217                 newContentPane.setOpaque(true); 
218                 JOptionPane.showMessageDialog(_vp, newContentPane,
219                                 "Annotation edition", JOptionPane.PLAIN_MESSAGE);
220         }
221
222         public ArrayList<Object> getData() {
223                 return data;
224         }
225
226         public void setData(ArrayList<Object> data) {
227                 this.data = data;
228         }
229
230         public VARNAPanel get_vp() {
231                 return _vp;
232         }
233
234         public JTable getTable() {
235                 return table;
236         }
237
238         public void setTable(JTable table) {
239                 this.table = table;
240         }
241
242         public AnnotationTableModel getSpecialTableModel() {
243                 return specialTableModel;
244         }
245
246         public void setSpecialTableModel(AnnotationTableModel specialTableModel) {
247                 this.specialTableModel = specialTableModel;
248         }
249 }