JAL-3026 srcjar files for VARNA and log4j
[jalview.git] / srcjar / fr / orsay / lri / varna / VARNA.java
1 package fr.orsay.lri.varna;
2 /*
3  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.
4  Copyright (C) 2008  Kevin Darty, Alain Denise and Yann Ponty.
5  electronic mail : Yann.Ponty@lri.fr
6  paper mail : LRI, bat 490 Université Paris-Sud 91405 Orsay Cedex France
7
8  This file is part of VARNA version 3.1.
9  VARNA version 3.1 is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License
10  as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
11
12  VARNA version 3.1 is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
13  without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14  See the GNU General Public License for more details.
15
16  You should have received a copy of the GNU General Public License along with VARNA version 3.1.
17  If not, see http://www.gnu.org/licenses.
18  */
19 import java.awt.Component;
20 import java.awt.GridLayout;
21 import java.awt.datatransfer.DataFlavor;
22 import java.awt.datatransfer.Transferable;
23 import java.awt.dnd.DnDConstants;
24 import java.awt.dnd.DropTarget;
25 import java.awt.dnd.DropTargetDragEvent;
26 import java.awt.dnd.DropTargetDropEvent;
27 import java.awt.dnd.DropTargetEvent;
28 import java.awt.dnd.DropTargetListener;
29 import java.io.File;
30 import java.io.FileReader;
31 import java.io.IOException;
32 import java.util.ArrayList;
33 import java.util.List;
34
35 import javax.swing.JApplet;
36 import javax.swing.JOptionPane;
37
38 import fr.orsay.lri.varna.controlers.ControleurScriptParser;
39 import fr.orsay.lri.varna.exceptions.ExceptionFileFormatOrSyntax;
40 import fr.orsay.lri.varna.exceptions.ExceptionLoadingFailed;
41 import fr.orsay.lri.varna.exceptions.ExceptionModeleStyleBaseSyntaxError;
42 import fr.orsay.lri.varna.exceptions.ExceptionNonEqualLength;
43 import fr.orsay.lri.varna.exceptions.ExceptionParameterError;
44 import fr.orsay.lri.varna.interfaces.InterfaceParameterLoader;
45 import fr.orsay.lri.varna.models.VARNAConfigLoader;
46 import fr.orsay.lri.varna.models.rna.RNA;
47
48
49 // @j2s issues -- see README_SWINGJS.txt
50
51 public class VARNA extends JApplet implements InterfaceParameterLoader,DropTargetListener {
52         ArrayList<VARNAPanel> _vpl = null;
53         static{
54                 /**
55                  * 
56                  * @j2sNative
57                  * 
58                  * 
59                  * if (!thisApplet.__Info.sequenceDBN) {
60                  *  thisApplet.__Info.sequenceDBN = "GGGGCCAAUAUGGCCAUCC";
61                  *  thisApplet.__Info.structureDBN = "((((((.....))))..))";
62                  *  thisApplet.__Info.title = "Hello RNA world, from SwingJS!";//prompt("Title?","Hello RNA world!");
63                  * } 
64                  * 
65                  * 
66                  * 
67                  */             
68         }
69
70         
71 //      private static final_long serialVersionUID = -2598221520127067670L;
72
73         public VARNA() {
74                 super();
75         }
76
77         public void init() {
78                 try {
79                         VARNAConfigLoader VARNAcfg = new VARNAConfigLoader(this);
80                         
81                         try {
82                                 _vpl = VARNAcfg.createVARNAPanels();
83                                 for (int i=0;i<_vpl.size();i++)
84                                 {
85                                     new DropTarget(_vpl.get(i), this);
86                                 }
87                         } catch (IOException e) {
88                                 JOptionPane.showMessageDialog(this, e.getMessage(),
89                                                 "VARNA Error", JOptionPane.ERROR_MESSAGE);
90                         } catch (ExceptionFileFormatOrSyntax e) {
91                                 JOptionPane.showMessageDialog(this, e.getMessage(),
92                                                 "VARNA Error", JOptionPane.ERROR_MESSAGE);
93                         } catch (ExceptionLoadingFailed e) {
94                                 JOptionPane.showMessageDialog(this, e.getMessage(),
95                                                 "VARNA Error", JOptionPane.ERROR_MESSAGE);
96                         }
97                         setLayout(new GridLayout(VARNAcfg.getNbColumns(), VARNAcfg
98                                         .getNbRows()));
99                         for (int i = 0; i < _vpl.size(); i++) {
100                                 getContentPane().add(_vpl.get(i));
101                         }
102                 } catch (ExceptionParameterError e) {
103                         VARNAPanel.errorDialogStatic(e, this);
104                 } catch (ExceptionModeleStyleBaseSyntaxError e) {
105                         VARNAPanel.errorDialogStatic(e, this);
106                 } catch (ExceptionNonEqualLength e) {
107                         VARNAPanel.errorDialogStatic(e, this);
108                 }
109
110         }
111
112         public void start() {
113                 //setVisible(true);
114                 //repaint();
115                 //getContentPane().setVisible(true);            
116                 //getContentPane().repaint();           
117         }
118         
119         public void update() {
120                 System.out.println("update");
121         }
122         
123         public String getParameterValue(String key, String def) {
124                 if (getParameter(key) == null) {
125                         return def;
126                 } else {
127                         return getParameter(key);
128                 }
129         }
130
131         public String[][] getParameterInfo() {
132                 return VARNAConfigLoader.getParameterInfo();
133         }
134         
135         public ArrayList<VARNAPanel> getPanels()
136         {
137                 return _vpl;
138         }
139         
140         public String getSelection()
141         {
142                 return getSelection(0);
143         }
144         
145         public String getSelection(int panel)
146         {
147                 String result = "[";
148                 VARNAPanel v = _vpl.get(panel);
149                 List<Integer> l = v.getSelectionIndices();
150                 for(int i=0;i<l.size();i++)
151                 {
152                         int n = l.get(i);
153                         if (i>0)
154                         {result += ",";}
155                         result += n;
156                         
157                 }
158                 result += "]";
159                 return result;
160         }
161
162         public void runScript(String script)
163         {
164                 if (_vpl.size()>0)
165                 { 
166                         VARNAPanel _vp = _vpl.get(0);
167                         try {
168                                 ControleurScriptParser.executeScript(_vp, script);
169                         } catch (Exception e) {
170                                 e.printStackTrace();
171                         }
172                 }
173         }
174         
175         
176         public void setRNA(String seq, String str) 
177         {
178                 if (_vpl.size()>0)
179                 { 
180                         try {
181                                 _vpl.get(0).drawRNA(seq, str);
182                         } catch (ExceptionNonEqualLength e) {
183                                 e.printStackTrace();
184                         } 
185                 }
186         }
187
188         public void setSmoothedRNA(String seq, String str) 
189         {
190                 if (_vpl.size()>0)
191                 { 
192                         try {
193                                   
194                                   _vpl.get(0).drawRNAInterpolated(seq, str);
195                                   _vpl.get(0).repaint();
196                         } catch (ExceptionNonEqualLength e) {
197                                 // TODO Auto-generated catch block
198                                 e.printStackTrace();
199                         } 
200                 }
201         }
202
203         public void dragEnter(DropTargetDragEvent arg0) {
204         }
205
206         public void dragExit(DropTargetEvent arg0) {
207         }
208
209         public void dragOver(DropTargetDragEvent arg0) {
210         }
211
212         public void drop(DropTargetDropEvent dtde) 
213         {
214           try 
215           {
216             Transferable tr = dtde.getTransferable();
217             DataFlavor[] flavors = tr.getTransferDataFlavors();
218             for (int i = 0; i < flavors.length; i++) 
219             {
220               if (flavors[i].isFlavorJavaFileListType()) 
221               {
222                   dtde.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
223                   List list = (List) tr.getTransferData(flavors[i]);
224                   for (int j = 0; j < list.size(); j++) 
225                   {
226                           Object o = list.get(j);
227                           if (dtde.getSource() instanceof DropTarget)
228                           {
229                                   DropTarget dt = (DropTarget) dtde.getSource();
230                                   Component c = dt.getComponent();
231                                   if (c instanceof VARNAPanel)
232                                   {
233                                           VARNAPanel vp = (VARNAPanel) c;
234                                           // BH -- in JavaScript, the File object has a .bytes 
235                                           // property that we need to maintain.
236                                           //String path = o.toString();
237                                           vp.loadFile((File) o,true);
238                                           //vp.repaint(); BH unnecessary
239                                   }
240                           }
241                   }
242                   dtde.dropComplete(true);
243                   return;
244               }
245             }
246         dtde.rejectDrop();
247          } 
248          catch (Exception e) 
249          {
250                  e.printStackTrace();
251              dtde.rejectDrop();
252           }
253         }
254
255         public void dropActionChanged(DropTargetDragEvent arg0) {
256         }
257
258         
259 }
260