JAL-1503 update version in GPL header
[jalview.git] / src / jalview / javascript / MouseOverStructureListener.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.1)
3  * Copyright (C) 2014 The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * 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  * Jalview is distributed in the hope that it will be useful, but 
12  * WITHOUT ANY WARRANTY; without even the implied warranty 
13  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
14  * PURPOSE.  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 Jalview.  If not, see <http://www.gnu.org/licenses/>.
17  * The Jalview Authors are detailed in the 'AUTHORS' file.
18  */
19 package jalview.javascript;
20
21 import java.awt.Color;
22 import java.util.ArrayList;
23
24 import jalview.api.AlignmentViewPanel;
25 import jalview.api.FeatureRenderer;
26 import jalview.api.SequenceRenderer;
27 import jalview.appletgui.AlignFrame;
28 import jalview.bin.JalviewLite;
29 import jalview.datamodel.SequenceI;
30 import jalview.ext.jmol.JmolCommands;
31 import jalview.structure.StructureListener;
32 import jalview.structure.StructureMapping;
33 import jalview.structure.StructureMappingcommandSet;
34 import jalview.structure.StructureSelectionManager;
35
36 /**
37  * Propagate events involving PDB structures associated with sequences to a
38  * javascript function. Generally, the javascript handler is called with a
39  * series of arguments like (eventname, ... ). As of Jalview 2.7, the following
40  * different types of events are supported:
41  * <ul>
42  * <li>mouseover: javascript function called with arguments
43  * 
44  * <pre>
45  * ['mouseover',String(pdb file URI), String(pdb file chain ID), String(residue
46  * number moused over), String(atom index corresponding to residue)]
47  * </pre>
48  * 
49  * </li>
50  * <li>colourstruct: javascript function called with arguments
51  * 
52  * <pre>
53  * ['colourstruct',String(alignment view id),String(number of javascript message
54  * chunks to collect),String(length of first chunk in set of messages - or zero
55  * for null message)]
56  * </pre>
57  * 
58  * <br>
59  * The message contains a series of Jmol script commands that will colour
60  * structures according to their associated sequences in the current view. Use
61  * jalview
62  * .javascript.JalviewLiteJsApi.getJsMessage('colourstruct',String(alignment
63  * view id)) to retrieve successive chunks of the message.</li>
64  * </ul>
65  * 
66  * @author Jim Procter (jprocter)
67  * 
68  */
69 public class MouseOverStructureListener extends JSFunctionExec implements
70         JsCallBack, StructureListener
71 {
72
73   String _listenerfn;
74
75   String[] modelSet;
76
77   public MouseOverStructureListener(JalviewLite jalviewLite,
78           String listener, String[] modelList)
79   {
80     super(jalviewLite);
81     _listenerfn = listener;
82     modelSet = modelList;
83     if (modelSet != null)
84     {
85       for (int i = 0; i < modelSet.length; i++)
86       {
87         // resolve a real filename
88         try
89         {
90           if (new java.net.URL(modelSet[i]).openConnection() != null)
91           {
92             continue;
93           }
94         } catch (Exception x)
95         {
96         }
97         ;
98         try
99         {
100           String db = jvlite.getDocumentBase().toString();
101           db = db.substring(0, db.lastIndexOf("/"));
102           if (new java.net.URL(db + "/" + modelSet[i]).openConnection() != null)
103           {
104             modelSet[i] = db + "/" + modelSet[i];
105             continue;
106           }
107         } catch (Exception x)
108         {
109         }
110         ;
111         try
112         {
113           if (new java.net.URL(jvlite.getCodeBase() + modelSet[i])
114                   .openConnection() != null)
115           {
116             modelSet[i] = jvlite.getCodeBase() + modelSet[i];
117             continue;
118           }
119         } catch (Exception x)
120         {
121         }
122         ;
123
124       }
125     }
126   }
127
128   @Override
129   public String[] getPdbFile()
130   {
131     return modelSet;
132   }
133
134   @Override
135   public void mouseOverStructure(int atomIndex, String strInfo)
136   {
137
138     // StructureSelectionManager.getStructureSelectionManager().mouseOverStructure(atomIndex,
139     // chain, pdbfile)
140     // TODO Auto-generated method stub
141
142   }
143
144   @Override
145   public void highlightAtom(int atomIndex, int pdbResNum, String chain,
146           String pdbId)
147   {
148     String[] st = new String[0];
149     try
150     {
151       executeJavascriptFunction(_listenerfn, st = new String[]
152       { "mouseover", "" + pdbId, "" + chain, "" + (pdbResNum),
153           "" + atomIndex });
154     } catch (Exception ex)
155     {
156       System.err.println("Couldn't execute callback with " + _listenerfn
157               + " using args { " + st[0] + ", " + st[1] + ", " + st[2]
158               + "," + st[3] + "\n");
159       ex.printStackTrace();
160
161     }
162
163   }
164
165   @Override
166   public synchronized void updateColours(Object srce)
167   {
168     final Object source = srce;
169     StructureSelectionManager ssm = StructureSelectionManager
170             .getStructureSelectionManager(jvlite);
171     // if (jvlite.debug)
172     // {
173     // ssm.reportMapping();
174     // }
175     if (source instanceof jalview.api.AlignmentViewPanel)
176     {
177       SequenceI[][] sequence = new SequenceI[modelSet.length][];
178       for (int m = 0; m < modelSet.length; m++)
179       {
180         StructureMapping[] sm = ssm.getMapping(modelSet[m]);
181         if (sm != null && sm.length > 0)
182         {
183           sequence[m] = new SequenceI[sm.length];
184           for (int i = 0; i < sm.length; i++)
185           {
186             sequence[m][i] = sm[i].getSequence();
187           }
188         }
189         else
190         {
191           sequence[m] = new SequenceI[0];
192         }
193         // if (jvlite.debug)
194         // {
195         // System.err.println("Mapped '" + modelSet[m] + "' to "
196         // + sequence[m].length + " sequences.");
197         // }
198       }
199
200       SequenceRenderer sr = ((jalview.appletgui.AlignmentPanel) source)
201               .getSequenceRenderer();
202       FeatureRenderer fr = ((jalview.appletgui.AlignmentPanel) source).av
203               .getShowSequenceFeatures() ? new jalview.appletgui.FeatureRenderer(
204               ((jalview.appletgui.AlignmentPanel) source).av) : null;
205       if (fr != null)
206       {
207         ((jalview.appletgui.FeatureRenderer) fr)
208                 .transferSettings(((jalview.appletgui.AlignmentPanel) source)
209                         .getFeatureRenderer());
210       }
211       ;
212
213       // Form a colour command from the given alignment panel for each distinct
214       // structure
215       ArrayList<String[]> ccomands = new ArrayList<String[]>();
216       ArrayList<String> pdbfn = new ArrayList<String>();
217       StructureMappingcommandSet[] colcommands = JmolCommands
218               .getColourBySequenceCommand(ssm, modelSet, sequence, sr, fr,
219                       ((AlignmentViewPanel) source).getAlignment());
220       if (colcommands == null)
221       {
222         return;
223       }
224       int sz = 0;
225       for (jalview.structure.StructureMappingcommandSet ccset : colcommands)
226       {
227         sz += ccset.commands.length;
228         ccomands.add(ccset.commands);
229         pdbfn.add(ccset.mapping);
230       }
231
232       String mclass, mhandle;
233       String ccomandset[] = new String[sz];
234       sz = 0;
235       for (String[] ccset : ccomands)
236       {
237         System.arraycopy(ccset, 0, ccomandset, sz, ccset.length);
238         sz += ccset.length;
239       }
240       if (jvlite.isJsMessageSetChanged(
241               mclass = "colourstruct",
242               mhandle = ((jalview.appletgui.AlignmentPanel) source).av
243                       .getViewId(), ccomandset))
244       {
245         jvlite.setJsMessageSet(mclass, mhandle, ccomandset);
246         // and notify javascript handler
247         String st[] = new String[]
248         {
249             "colourstruct",
250             "" + ((jalview.appletgui.AlignmentPanel) source).av.getViewId(),
251             "" + ccomandset.length,
252             jvlite.arrayToSeparatorList(pdbfn.toArray(new String[pdbfn
253                     .size()])) };
254         try
255         {
256           executeJavascriptFunction(true, _listenerfn, st);
257         } catch (Exception ex)
258         {
259           System.err.println("Couldn't execute callback with "
260                   + _listenerfn + " using args { " + st[0] + ", " + st[1]
261                   + ", " + st[2] + "," + st[3] + "}"); // + ","+st[4]+"\n");
262           ex.printStackTrace();
263
264         }
265       }
266       /*
267        * new Thread(new Runnable() { public void run() { // and send to
268        * javascript handler String st[] = new String[0]; int i = 0; for (String
269        * colcommand : colcommands) { // do sync execution for each chunk try {
270        * executeJavascriptFunction( false, _listenerfn, st = new String[] {
271        * "colourstruct", "" + ((jalview.appletgui.AlignmentPanel) source).av
272        * .getViewId(), handle, "" }); } catch (Exception ex) {
273        * System.err.println("Couldn't execute callback with " + _listenerfn +
274        * " using args { " + st[0] + ", " + st[1] + ", " + st[2] + "," + st[3] +
275        * "\n"); ex.printStackTrace();
276        * 
277        * } } } }).start();
278        */
279     }
280
281   }
282
283   @Override
284   public Color getColour(int atomIndex, int pdbResNum, String chain,
285           String pdbId)
286   {
287     return null;
288   }
289
290   @Override
291   public AlignFrame getAlignFrame()
292   {
293     // associated with all alignframes, always.
294     return null;
295   }
296
297   @Override
298   public String getListenerFunction()
299   {
300     return _listenerfn;
301   }
302
303   public void finalise()
304   {
305     jvlite = null;
306     super.finalize();
307   }
308
309   @Override
310   public void releaseReferences(Object svl)
311   {
312
313     // TODO Auto-generated method stub
314
315   }
316
317 }